<commit_before>RSpec.describe MiqAeField do
  describe "#to_export_xml" do
    let(:default_value) { nil }
    let(:miq_ae_field) do
      described_class.new(
        :created_on    => Time.zone.now,
        :default_value => default_value,
        :display_name  => "display_name",
        :id            => 123,
        :method_id     => 321,
        :updated_by    => "me",
        :updated_on    => Time.zone.now
      )
    end

    context "when default_value is blank" do
      let(:expected_xml) do
        <<-XML
<MiqAeField name="" substitute="true" display_name="display_name"></MiqAeField>
        XML
      end

      it "does not include the default_value" do
        expect(miq_ae_field.to_export_xml).to eql(expected_xml.chomp)
      end
    end

    context "when default_value is not blank" do
      let(:default_value) { "default_value" }
      let(:expected_xml) do
        <<-XML
<MiqAeField name="" substitute="true" display_name="display_name">default_value</MiqAeField>
        XML
      end

      it "includes the default_value" do
        expect(miq_ae_field.to_export_xml).to eql(expected_xml.chomp)
      end
    end
  end

  context "legacy tests" do
    before do
      @ns = FactoryBot.create(:miq_ae_namespace, :name => "TEST", :parent => FactoryBot.create(:miq_ae_domain))
      @c1 = MiqAeClass.create(:namespace_id => @ns.id, :name => "fields_test")
      @user = FactoryBot.create(:user_with_group)
    end

    it "looks up by name" do
      field_name = "TEST"
      field = @c1.ae_fields.create(:name => field_name)

      expect(MiqAeField.lookup_by_name(field_name)).to eq(field)
    end

    it "should enforce necessary parameters upon create" do
      expect { @c1.ae_fields.new.save! }.to raise_error(ActiveRecord::RecordInvalid)
      # remove the invalid unsaved record in the association by clearing it
      @c1.ae_fields.clear
      f1 = @c1.ae_fields.build(:name => "TEST")
      expect(f1).not_to be_nil
      expect(f1.save!).to be_truthy
    end

    it "should not create fields with invalid names" do
      ["fie ld1", "fie-ld1", "fie:ld1"].each do |name|
        f1 = @c1.ae_fields.build(:name => name)
        expect(f1).not_to be_nil
        expect { f1.save! }.to raise_error(ActiveRecord::RecordInvalid)
      end
    end

    it "should process boolean fields properly" do
      fname1 = "TEST_EVALUATE"<commit_msg>add spec showing current query count (4) on save<commit_after>RSpec.describe MiqAeField do
  describe "#to_export_xml" do
    let(:default_value) { nil }
    let(:miq_ae_field) do
      described_class.new(
        :created_on    => Time.zone.now,
        :default_value => default_value,
        :display_name  => "display_name",
        :id            => 123,
        :method_id     => 321,
        :updated_by    => "me",
        :updated_on    => Time.zone.now
      )
    end

    context "when default_value is blank" do
      let(:expected_xml) do
        <<-XML
<MiqAeField name="" substitute="true" display_name="display_name"></MiqAeField>
        XML
      end

      it "does not include the default_value" do
        expect(miq_ae_field.to_export_xml).to eql(expected_xml.chomp)
      end
    end

    context "when default_value is not blank" do
      let(:default_value) { "default_value" }
      let(:expected_xml) do
        <<-XML
<MiqAeField name="" substitute="true" display_name="display_name">default_value</MiqAeField>
        XML
      end

      it "includes the default_value" do
        expect(miq_ae_field.to_export_xml).to eql(expected_xml.chomp)
      end
    end
  end

  context "legacy tests" do
    before do
      @ns = FactoryBot.create(:miq_ae_namespace, :name => "TEST", :parent => FactoryBot.create(:miq_ae_domain))
      @c1 = MiqAeClass.create(:namespace_id => @ns.id, :name => "fields_test")
      @user = FactoryBot.create(:user_with_group)
    end

    it "looks up by name" do
      field_name = "TEST"
      field = @c1.ae_fields.create(:name => field_name)

      expect(MiqAeField.lookup_by_name(field_name)).to eq(field)
    end

    it "should enforce necessary parameters upon create" do
      expect { @c1.ae_fields.new.save! }.to raise_error(ActiveRecord::RecordInvalid)
      # remove the invalid unsaved record in the association by clearing it
      @c1.ae_fields.clear
      f1 = @c1.ae_fields.build(:name => "TEST")
      expect(f1).not_to be_nil
      expect(f1.save!).to be_truthy
    end

    it "should not create fields with invalid names" do
      ["fie ld1", "fie-ld1", "fie:ld1"].each do |name|
        f1 = @c1.ae_fields.build(:name => name)
        expect(f1).not_to be_nil
        expect { f1.save! }.to raise_error(ActiveRecord::RecordInvalid)
      end
    end

    it "doesn't access database when unchanged model is saved" do
      m = described_class.create
      expect { m.save }.to make_database_queries(:count => 4)
    end

    it "should process boolean fields properly" do
      fname1 = "TEST_EVALUATE"
{'fraction_non_alphanumeric': 0.11122305057424944, 'fraction_numerical': 0.00926858754785412, 'mean_word_length': 2.710014947683109, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '1664416', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1640, 'n_words': 387}