<commit_before>require 'spec_helper'

require "rexml/document"

module LibMbus
  describe Frame do
    describe "package from abb meter" do
      before :each do
        str =  "\x68\x82\x82\x68\x08\x1f\x72\x31\x83\x82\x00\x2d\x2c\x01\x04"
        str << "\x02\x00\x00\x00\x0c\x0f\x78\x55\x03\x00\x0c\x14\x92\x40\x37"
        str << "\x00\x0c\x22\x81\x84\x10\x00\x0c\x59\x60\x20\x00\x00\x0c\x5d"
        str << "\x86\x20\x00\x00\x0c\x61\x00\x00\x00\x00\x0c\x2d\x00\x00\x00"
        str << "\x00\x0c\x3b\x00\x00\x00\x00\x4c\x0f\x78\x55\x03\x00\x4c\x14"
        str << "\x92\x40\x37\x00\x42\x6c\x61\x16\x0f\x31\x83\x82\x00\x00\x00"
        str << "\x00\x00\x00\x00\x00\x00\x01\x00\x24\x20\x00\x00\x00\x00\x00"
        str << "\x00\x30\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        str << "\x00\x00\x19\x21\x02\x00\x00\x00\x76\x03\x17\x07\x11\x00\xde"
        str << "\x16"
        @frame = Frame.parse(str)
      end

      it "parses mbus frame to xml" do
        xml = @frame.to_xml
        doc = REXML::Document.new(xml)
        doc.elements["MBusData/SlaveInformation/Id"].text.should == "828331"
        result = []
        doc.elements.each("MBusData/DataRecord") {|e| result << e}
        result.should have(12).elements
      end

      it "has a secondary id" do
        @frame.serial.should == "8283312D2C0104"
      end

      it "runs block over each data field" do
        data = @frame.map_data_fields do |h|
          h
        end
        data.should have(12).elements
        data[0][:quantity].should == "Energy"
        data[0][:function].should == "Instantaneous value"
        data[0][:unit].should == "J"
        data[0][:value].should == 355780000000
        data[1][:quantity].should == "Volume"
        data[1][:function].should == "Instantaneous value"
        data[1][:unit].should == "m^3"
        data[1][:value].should == 3740.92
      end
    end

    describe "with invalid mbus frame" do
      it "returns no frame" do<commit_msg>Split spec refactor<commit_after>require 'spec_helper'

require "rexml/document"

module LibMbus
  describe Frame do
    describe "package from abb meter" do
      before :each do
        str =  "\x68\x82\x82\x68\x08\x1f\x72\x31\x83\x82\x00\x2d\x2c\x01\x04"
        str << "\x02\x00\x00\x00\x0c\x0f\x78\x55\x03\x00\x0c\x14\x92\x40\x37"
        str << "\x00\x0c\x22\x81\x84\x10\x00\x0c\x59\x60\x20\x00\x00\x0c\x5d"
        str << "\x86\x20\x00\x00\x0c\x61\x00\x00\x00\x00\x0c\x2d\x00\x00\x00"
        str << "\x00\x0c\x3b\x00\x00\x00\x00\x4c\x0f\x78\x55\x03\x00\x4c\x14"
        str << "\x92\x40\x37\x00\x42\x6c\x61\x16\x0f\x31\x83\x82\x00\x00\x00"
        str << "\x00\x00\x00\x00\x00\x00\x01\x00\x24\x20\x00\x00\x00\x00\x00"
        str << "\x00\x30\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
        str << "\x00\x00\x19\x21\x02\x00\x00\x00\x76\x03\x17\x07\x11\x00\xde"
        str << "\x16"
        @frame = Frame.parse(str)
      end

      it "parses mbus frame to xml" do
        xml = @frame.to_xml
        doc = REXML::Document.new(xml)
        doc.elements["MBusData/SlaveInformation/Id"].text.should == "828331"
        result = []
        doc.elements.each("MBusData/DataRecord") {|e| result << e}
        result.should have(12).elements
      end

      it "has a secondary id" do
        @frame.serial.should == "8283312D2C0104"
      end

      describe "mapping data fields" do
        before :each do
          @data = @frame.map_data_fields do |h|
            h
          end
        end

        it "finds 12 data fields" do
          @data.should have(12).elements
        end

        it "finds first data field" do
          @data[0][:quantity].should == "Energy"
          @data[0][:function].should == "Instantaneous value"
          @data[0][:unit].should == "J"
          @data[0][:value].should == 355780000000
        end

        it "finds second data field" do
          @data[1][:quantity].should == "Volume"
          @data[1][:function].should == "Instantaneous value"
          @data[1][:unit].should == "m^3"
          @data[1][:value].should == 3740.92
        end
      end
    end

    describe "with invalid mbus frame" do
      it "returns no frame" do
{'fraction_non_alphanumeric': 0.1654297342590376, 'fraction_numerical': 0.14364376346660282, 'mean_word_length': 2.0585651537335283, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '782403', 'n_tokens_mistral': 2102, 'n_tokens_neox': 1804, 'n_words': 259}