<commit_before>require 'spec_helper'

describe CsvRowModel::Import::Mapper::Attributes do
  describe "instance" do
    let(:klass) { DependentImportMapper }
    let(:instance) { klass.new source_row }
    let (:source_row) { ["no_errors", "no_errors"]  }

    describe "attribute_methods" do
      subject { instance.attribute1 }

      it "should execute with memoization" do
        expect(subject).to_not eql nil
        expect(subject).to eql instance.attribute1
      end

      it "should work when calling next" do
        instance.attribute2
        instance.attribute2
        expect(instance.attribute2).to eql nil
        expect(instance.attribute3).to eql nil
      end

      it "should work when calling return" do
        instance.attribute4
        instance.attribute4
        expect(instance.attribute4).to eql nil
        expect(instance.attribute5).to eql nil
      end

      context "with row_model errors" do
        let(:source_row) {["", ""] }

        it "should just return nil" do
          expect(Random).to_not receive(:rand)
          expect(subject).to eql nil
        end
      end
    end

    describe "#filter_errors" do
      subject { instance.send :filter_errors }
      before do
        instance.errors.add(:attribute1)
        instance.errors.add(:string2)
      end

      it "only has mapper errors" do
        subject
        expect(instance.errors.keys).to eql [:attribute1, :string2]
      end

      context "with row_model errors" do
        let(:source_row) {["", ""] }

        it "only has row_model errors and the non-dependent attribute" do
          subject
          expect(instance.errors.keys).to eql [:string2, :string1]
        end
      end
    end

    describe "#valid_dependencies?" do
      subject { instance.send :valid_dependencies?, :attribute1 }
      before { instance.errors.add(:attribute1) }

      it "returns true" do
        expect(subject).to eql true
      end

      context "returns false" do
        let(:source_row) {["", ""] }

        it "only has row_model errors" do
          expect(subject).to eql false
        end
      end
    end

    describe "#memoize" do
      let(:klass) do
        Class.new do
          include CsvRowModel::Import::Mapper::Attributes
        end
      end

      let(:instance) { klass.new }
      subject { instance.send(:memoize, "test") { Random.rand } }

      it "memoizes the result" do
        expect(subject).to_not eql nil
        expect(subject).to eql instance.send(:memoize, "test")
      end
    end
  end
end<commit_msg>add more tests<commit_after>require 'spec_helper'

describe CsvRowModel::Import::Mapper::Attributes do
  describe "instance" do
    let(:klass) { DependentImportMapper }
    let(:instance) { klass.new source_row }
    let (:source_row) { ["no_errors", "no_errors"]  }

    describe "attribute_methods" do
      subject { instance.attribute1 }

      it "should execute with memoization" do
        expect(subject).to_not eql nil
        expect(subject).to eql instance.attribute1
      end

      it "should work when calling next" do
        instance.attribute2
        instance.attribute2
        expect(instance.attribute2).to eql nil
        expect(instance.attribute3).to eql nil
      end

      it "should work when calling return" do
        instance.attribute4
        instance.attribute4
        expect(instance.attribute4).to eql nil
        expect(instance.attribute5).to eql nil
      end

      context "with row_model errors" do
        let(:source_row) {["", ""] }

        it "should just return nil" do
          expect(Random).to_not receive(:rand)
          expect(subject).to eql nil
        end
      end
    end

    describe "#filter_errors" do
      subject { instance.send :filter_errors }
      before do
        instance.errors.add(:attribute1)
        instance.errors.add(:string2)
      end

      it "only has mapper errors" do
        subject
        expect(instance.errors.keys).to eql [:attribute1, :string2]
      end

      context "with row_model errors" do
        let(:source_row) {["", ""] }

        it "only has row_model errors and the non-dependent attribute" do
          subject
          expect(instance.errors.keys).to eql [:string2, :string1]
        end
      end
    end

    describe "#valid_dependencies?" do
      subject { instance.send :valid_dependencies?, :attribute1 }
      before { instance.errors.add(:attribute1) }

      it "returns true" do
        expect(subject).to eql true
      end

      context "returns false" do
        let(:source_row) {["", ""] }

        it "only has row_model errors" do
          expect(subject).to eql false
        end
      end
    end

    describe "#memoize" do
      let(:klass) do
        Class.new do
          include CsvRowModel::Import::Mapper::Attributes
        end
      end

      let(:instance) { klass.new }
      subject { instance.send(:memoize, "test") { Random.rand } }

      it "memoizes the result" do
        expect(subject).to_not eql nil
        expect(subject).to eql instance.send(:memoize, "test")
      end
    end

    describe "class" do
      let(:klass) do
        Class.new do
          include CsvRowModel::Import::Mapper::Attributes
        end
      end

      describe "::attribute" do
        subject { klass.send(:attribute, :attribute_name, options) { true } }
        let(:options) { { dependencies: [], memoize: false } }

        it "works" do
          subject
        end

        context "when giving an invalid option" do
          let(:options) { { testing: "" } }

          it "raises exception" do
            expect { subject }.to raise_error(ArgumentError)
          end
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09558823529411764, 'fraction_numerical': 0.0063025210084033615, 'mean_word_length': 2.1476584022038567, 'pattern_counts': {'":': 1, '<': 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': '3828384', 'n_tokens_mistral': 1640, 'n_tokens_neox': 1602, 'n_words': 452}