<commit_before>require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

module QBFC::Test
  class BaseKlass < QBFC::Element
    is_base_class
  end
  
  class NormalKlass < QBFC::Element
  end
end

# An Element is a Transaction or a List; that is any QuickBooks objects that can
# be created, edited (possibly), deleted and read. Contrast to a Report or Info
# which are read-only.
describe QBFC::Element do

  before(:each) do 
    @sess = mock(QBFC::Session)
    @ole_wrapper = mock(QBFC::OLEWrapper)
    @ole_object = mock(WIN32OLE)
    @ole_methods = ["FullName", "DataExtRetList"]
    @element = QBFC::Test::NormalKlass.new(@sess, @ole_wrapper)
  end

  describe "#initialize" do
    it "should set up add request if ole_object is nil" do
      @request = mock(QBFC::Request)
      @request.should_receive(:ole_object).and_return(@ole_object)
      QBFC::Request.should_receive(:new).with(@sess, "NormalKlassAdd").and_return(@request)
      QBFC::Test::NormalKlass.new(@sess)
    end
    
    it "should assign the Add request as the @setter" do
      @request = mock(QBFC::Request)
      @request.stub!(:ole_object).and_return(@ole_object)
      QBFC::Request.should_receive(:new).with(@sess, "NormalKlassAdd").and_return(@request)
      QBFC::Test::NormalKlass.new(@sess).
          instance_variable_get(:@setter).should == @request
    end
    
    it "should error if class is_base_class?" do
      lambda {
        QBFC::Test::BaseKlass.new(@sess)
      }.should raise_error(QBFC::BaseClassNewError)

      lambda {
        QBFC::Test::BaseKlass.new(@sess, @ole_wrapper)
      }.should raise_error(QBFC::BaseClassNewError)
    end
  end
  
  describe "#new_record?" do
    before(:each) do
      @request = mock(QBFC::Request)
      @request.stub!(:ole_object).and_return(@ole_object)
      QBFC::Request.stub!(:new).with(@sess, "NormalKlassAdd").and_return(@request)
    end
    
    it "should return true if ole_object is an AddRq" do
      QBFC::Test::NormalKlass.new(@sess).new_record?.should be_true
    end

    it "should return false if ole_object is from a QueryRq" do
      @element.new_record?.should be_false
    end
  end
  
  describe ".is_base_class? (and is_base_class macro)" do
    it "should return true if Class is_base_class has been called" do
      QBFC::Test::BaseKlass.is_base_class?.should be_true
      QBFC::Test::NormalKlass.is_base_class?.should be_false
    end
  end
  
  describe "#custom" do
    before(:each) do
      @data_ext = mock("QBFC::OLEWrapper#DataExtRet")
      @data_ext_list = [@data_ext, @data_ext]
      @ole_wrapper.stub!(:DataExtRetList).and_return(@data_ext_list)
      @ole_wrapper.stub!(:data_ext).and_return(@data_ext_list)
    end
  
    it "should get custom fields" do
      @data_ext.should_receive(:data_ext_name).and_return("Custom Field")
      @data_ext.should_receive(:owner_id).and_return(0)
      @data_ext.should_receive(:data_ext_value).and_return("Hello")
      
      @element.custom("Custom Field").should == "Hello"
    end
    
    it "should return nil if there are no custom fields" do
      @ole_wrapper.should_receive(:DataExtRetList).and_return(nil)
      
      @element.custom("Custom Field").should be_nil
    end
    
    it "should return nil if the custom field is not found" do<commit_msg>Update Element#custom spec to reflect that owner_id is a string<commit_after>require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

module QBFC::Test
  class BaseKlass < QBFC::Element
    is_base_class
  end
  
  class NormalKlass < QBFC::Element
  end
end

# An Element is a Transaction or a List; that is any QuickBooks objects that can
# be created, edited (possibly), deleted and read. Contrast to a Report or Info
# which are read-only.
describe QBFC::Element do

  before(:each) do 
    @sess = mock(QBFC::Session)
    @ole_wrapper = mock(QBFC::OLEWrapper)
    @ole_object = mock(WIN32OLE)
    @ole_methods = ["FullName", "DataExtRetList"]
    @element = QBFC::Test::NormalKlass.new(@sess, @ole_wrapper)
  end

  describe "#initialize" do
    it "should set up add request if ole_object is nil" do
      @request = mock(QBFC::Request)
      @request.should_receive(:ole_object).and_return(@ole_object)
      QBFC::Request.should_receive(:new).with(@sess, "NormalKlassAdd").and_return(@request)
      QBFC::Test::NormalKlass.new(@sess)
    end
    
    it "should assign the Add request as the @setter" do
      @request = mock(QBFC::Request)
      @request.stub!(:ole_object).and_return(@ole_object)
      QBFC::Request.should_receive(:new).with(@sess, "NormalKlassAdd").and_return(@request)
      QBFC::Test::NormalKlass.new(@sess).
          instance_variable_get(:@setter).should == @request
    end
    
    it "should error if class is_base_class?" do
      lambda {
        QBFC::Test::BaseKlass.new(@sess)
      }.should raise_error(QBFC::BaseClassNewError)

      lambda {
        QBFC::Test::BaseKlass.new(@sess, @ole_wrapper)
      }.should raise_error(QBFC::BaseClassNewError)
    end
  end
  
  describe "#new_record?" do
    before(:each) do
      @request = mock(QBFC::Request)
      @request.stub!(:ole_object).and_return(@ole_object)
      QBFC::Request.stub!(:new).with(@sess, "NormalKlassAdd").and_return(@request)
    end
    
    it "should return true if ole_object is an AddRq" do
      QBFC::Test::NormalKlass.new(@sess).new_record?.should be_true
    end

    it "should return false if ole_object is from a QueryRq" do
      @element.new_record?.should be_false
    end
  end
  
  describe ".is_base_class? (and is_base_class macro)" do
    it "should return true if Class is_base_class has been called" do
      QBFC::Test::BaseKlass.is_base_class?.should be_true
      QBFC::Test::NormalKlass.is_base_class?.should be_false
    end
  end
  
  describe "#custom" do
    before(:each) do
      @data_ext = mock("QBFC::OLEWrapper#DataExtRet")
      @data_ext_list = [@data_ext, @data_ext]
      @ole_wrapper.stub!(:DataExtRetList).and_return(@data_ext_list)
      @ole_wrapper.stub!(:data_ext).and_return(@data_ext_list)
    end
  
    it "should get custom fields" do
      @data_ext.should_receive(:data_ext_name).and_return("Custom Field")
      @data_ext.should_receive(:owner_id).and_return('0')
      @data_ext.should_receive(:data_ext_value).and_return("Hello")
      
      @element.custom("Custom Field").should == "Hello"
    end
    
    it "should return nil if there are no custom fields" do
      @ole_wrapper.should_receive(:DataExtRetList).and_return(nil)
      
      @element.custom("Custom Field").should be_nil
    end
    
    it "should return nil if the custom field is not found" do
{'fraction_non_alphanumeric': 0.11894736842105263, 'fraction_numerical': 0.0009022556390977444, 'mean_word_length': 3.6380753138075312, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '631639', 'n_tokens_mistral': 2272, 'n_tokens_neox': 2225, 'n_words': 486}