<commit_before>require "spec_helper"

RSpec.describe ExternalFields, :temporal do
  describe ".external_fields" do
    describe "the association" do
      Temping.create :test_class do
        with_columns do |t|
          t.string :name
        end

        include ExternalFields

        has_one :assoc,
                class_name: "AssociationTestClass"

        external_field :ext_field_1,
                       :assoc,
                       class_name: "AssociationTestClass"

        external_field :ext_field_2,
                       :assoc,
                       class_name: "AssociationTestClass",
                       underscore: true
      end

      Temping.create :association_test_class do
        with_columns do |t|
          t.integer :test_class_id
          t.string :ext_field_1
          t.string :ext_field_2
        end

        belongs_to :test_class
      end

      # Clean up after each test. This is a lot lighter for these few tests than
      # trying to wrangle with RSpec-Rails to get transactional tests to work.
      after :each do
        TestClass.delete_all
        AssociationTestClass.delete_all
      end

      it "should be built on first access" do
        e = TestClass.create!(name: "Hello")

        expect(AssociationTestClass.count).to eq(0)
        expect(e.assoc.class).to eq(AssociationTestClass)
      end

      it "should be saved when employee is saved" do
        e = Fabricate(:employee)
        expect(SIS::EmployeeData.count).to eq(0)
        expect(e.extra_data.class).to eq(SIS::EmployeeData)
        e.save!
        expect(SIS::EmployeeData.count).to eq(1)
      end

      it "should not be created or saved if unused" do
        e = Fabricate(:employee)
        e.first_name = "TEST"
        e.save!
        expect(SIS::EmployeeData.count).to eq(0)
      end

      # This test is checking that student_friendly_name goes through
      # extra_data.
      it "should be created if used" do
        e = Fabricate(:employee, student_friendly_name: "TEST")
        expect(SIS::EmployeeData.count).to eq(1)
        expect(e.student_friendly_name).to eq "TEST"
      end

      # This test also checks that student_friendly_name goes through
      # extra_data.
      it "should provide underscored methods" do
        e = Fabricate(:employee, _student_friendly_name: "TEST")
        expect(SIS::EmployeeData.count).to eq(1)
        expect(e._student_friendly_name).to eq "TEST"
      end

      it "should provide an accessor that does not build a new object" do
        e = Fabricate.build(:employee)
        e.extra_data(use_original: true) # Access without building.
        e.save!
        expect(SIS::EmployeeData.count).to eq 0
      end
    end
  end
end<commit_msg>All tests passing<commit_after>require "spec_helper"

RSpec.describe ExternalFields, :temporal do
  describe ".external_fields" do
    describe "the association" do
      Temping.create :test_class do
        with_columns do |t|
          t.string :name
        end

        include ExternalFields

        has_one :assoc,
                class_name: "AssociationTestClass"

        external_field :ext_field_1,
                       :assoc,
                       class_name: "AssociationTestClass"

        external_field :ext_field_2,
                       :assoc,
                       class_name: "AssociationTestClass",
                       underscore: true
      end

      Temping.create :association_test_class do
        with_columns do |t|
          t.integer :test_class_id
          t.string :ext_field_1
          t.string :ext_field_2
        end

        belongs_to :test_class
      end

      # Clean up after each test. This is a lot lighter for these few tests than
      # trying to wrangle with RSpec-Rails to get transactional tests to work.
      after :each do
        TestClass.delete_all
        AssociationTestClass.delete_all
      end

      it "should be built on first access" do
        e = TestClass.create!(name: "Hello")

        expect(AssociationTestClass.count).to eq(0)
        expect(e.assoc.class).to eq(AssociationTestClass)
      end

      it "should be saved when the model is saved" do
        e = TestClass.create!(name: "Hello")
        expect(AssociationTestClass.count).to eq(0)
        expect(e.assoc.class).to eq(AssociationTestClass)
        e.save!
        expect(AssociationTestClass.count).to eq(1)
      end

      it "should not be created or saved if unused" do
        e = TestClass.create!
        e.name = "TEST"
        e.save!
        expect(AssociationTestClass.count).to eq(0)
      end

      it "should be created if used" do
        e = TestClass.create!(name: "Hello", ext_field_1: "Field1")

        expect(AssociationTestClass.count).to eq(1)
        expect(e.ext_field_1).to eq "Field1"
      end

      context "when underscore flag is true" do
        it "should provide underscored methods" do
          e = TestClass.create!(_ext_field_2: "_Field2")

          expect(AssociationTestClass.count).to eq(1)
          expect(e._ext_field_2).to eq "_Field2"
        end
      end

      it "should provide an accessor that does not build a new object" do
        e = TestClass.new(name: "Hello")

        e.assoc(use_original: true) # Access without creating
        e.save!
        expect(AssociationTestClass.count).to eq 0
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07629326386304429, 'fraction_numerical': 0.005582433941198363, 'mean_word_length': 2.040158371040724, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2240587', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1515, 'n_words': 436}