<commit_before>require 'rails_helper'

RSpec.describe EventFilterQuery do
  let(:filter) {{ "src_ip" => '192.0.2.1' }}
  let!(:event1) { FactoryGirl.create(:event, src_ip: "192.0.2.1", sensor: 'abc001') }
  let!(:event2) { FactoryGirl.create(:event, src_ip: "198.51.100.1", sensor: 'abc002') }
  let!(:event3) { FactoryGirl.create(:event, src_ip: "192.0.2.7", sensor: 'def001') }
  let!(:event4) { FactoryGirl.create(:event, src_ip: "198.51.100.8", sensor: 'def002') }


  # check for class methods
  it { expect(EventFilterQuery.respond_to? :new).to be_truthy}

  it "raise an ArgumentError" do
  expect {
    EventFilterQuery.new
  }.to raise_error(KeyError)
  end

  # check for instance methods
  describe "instance methods" do
    subject { EventFilterQuery.new(filter: filter) }
    it { expect(subject.respond_to? :all).to be_truthy}
    it { expect(subject.respond_to? :find_each).to be_truthy}
    it { expect(subject.respond_to? :include?).to be_truthy }
  end

  context "with filter src_ip: 192.0.2.1" do
    subject { EventFilterQuery.new(filter: filter) }
    describe "#all" do
      it { expect(subject.all).to contain_exactly(event1) }
    end

    describe "#find_each" do
      it "executes only event1" do
        a = []
        subject.find_each do |e|
          a << e.id
        end
        expect(a).to contain_exactly(event1.id)
      end
    end

    describe "#include?" do
      it { expect(subject.include?(event1)).to be_truthy }
    end
  end

  context "with filter src_ip: '192.0.2.1;192.0.2.7'" do
    subject { EventFilterQuery.new(filter: {src_ip: "192.0.2.1;192.0.2.7"}) }
    describe "#all" do
      it { expect(subject.all).to contain_exactly(event1,event3) }
    end

    describe "#find_each" do
      it "executes event1 and event3" do
        a = []
        subject.find_each do |e|
          a << e.id
        end
        expect(a).to contain_exactly(event1.id, event3.id)
      end
    end

    describe "#include?" do
      it { expect(subject.include?(event1)).to be_truthy }
      it { expect(subject.include?(event2)).to be_falsey }
      it { expect(subject.include?(event3)).to be_truthy }
      it { expect(subject.include?(event4)).to be_falsey }
    end
  end

  context "with filter src_ip: '192.0.2.1/29'" do
    subject { EventFilterQuery.new(filter: {src_ip: "192.0.2.0/29"}) }
    describe "#all" do
      it { expect(subject.all).to contain_exactly(event1,event3) }
    end

    describe "#find_each" do
      it "executes event1 and event3" do
        a = []
        subject.find_each do |e|
          a << e.id
        end
        expect(a).to contain_exactly(event1.id, event3.id)
      end
    end

    describe "#include?" do
      it { expect(subject.include?(event1)).to be_truthy }
      it { expect(subject.include?(event2)).to be_falsey }
      it { expect(subject.include?(event3)).to be_truthy }
      it { expect(subject.include?(event4)).to be_falsey }
    end
  end

  context "with filter sensor: '*001'" do<commit_msg>add list check with strings<commit_after>require 'rails_helper'

RSpec.describe EventFilterQuery do
  let(:filter) {{ "src_ip" => '192.0.2.1' }}
  let!(:event1) { FactoryGirl.create(:event, src_ip: "192.0.2.1", sensor: 'abc001') }
  let!(:event2) { FactoryGirl.create(:event, src_ip: "198.51.100.1", sensor: 'abc002') }
  let!(:event3) { FactoryGirl.create(:event, src_ip: "192.0.2.7", sensor: 'def001') }
  let!(:event4) { FactoryGirl.create(:event, src_ip: "198.51.100.8", sensor: 'def002') }


  # check for class methods
  it { expect(EventFilterQuery.respond_to? :new).to be_truthy}

  it "raise an ArgumentError" do
  expect {
    EventFilterQuery.new
  }.to raise_error(KeyError)
  end

  # check for instance methods
  describe "instance methods" do
    subject { EventFilterQuery.new(filter: filter) }
    it { expect(subject.respond_to? :all).to be_truthy}
    it { expect(subject.respond_to? :find_each).to be_truthy}
    it { expect(subject.respond_to? :include?).to be_truthy }
  end

  context "with filter src_ip: 192.0.2.1" do
    subject { EventFilterQuery.new(filter: filter) }
    describe "#all" do
      it { expect(subject.all).to contain_exactly(event1) }
    end

    describe "#find_each" do
      it "executes only event1" do
        a = []
        subject.find_each do |e|
          a << e.id
        end
        expect(a).to contain_exactly(event1.id)
      end
    end

    describe "#include?" do
      it { expect(subject.include?(event1)).to be_truthy }
    end
  end

  context "with filter src_ip: '192.0.2.1;192.0.2.7'" do
    subject { EventFilterQuery.new(filter: {src_ip: "192.0.2.1;192.0.2.7"}) }
    describe "#all" do
      it { expect(subject.all).to contain_exactly(event1,event3) }
    end

    describe "#find_each" do
      it "executes event1 and event3" do
        a = []
        subject.find_each do |e|
          a << e.id
        end
        expect(a).to contain_exactly(event1.id, event3.id)
      end
    end

    describe "#include?" do
      it { expect(subject.include?(event1)).to be_truthy }
      it { expect(subject.include?(event2)).to be_falsey }
      it { expect(subject.include?(event3)).to be_truthy }
      it { expect(subject.include?(event4)).to be_falsey }
    end
  end

  context "with filter sensor: 'abc001;def002'" do
    subject { EventFilterQuery.new(filter: {sensor: "abc001;def002"}) }
    describe "#all" do
      it { expect(subject.all).to contain_exactly(event1,event4) }
    end

    describe "#find_each" do
      it "executes event1 and event4" do
        a = []
        subject.find_each do |e|
          a << e.id
        end
        expect(a).to contain_exactly(event1.id, event4.id)
      end
    end

    describe "#include?" do
      it { expect(subject.include?(event1)).to be_truthy }
      it { expect(subject.include?(event2)).to be_falsey }
      it { expect(subject.include?(event3)).to be_falsey }
      it { expect(subject.include?(event4)).to be_truthy }
    end
  end

  context "with filter src_ip: '192.0.2.1/29'" do
    subject { EventFilterQuery.new(filter: {src_ip: "192.0.2.0/29"}) }
    describe "#all" do
      it { expect(subject.all).to contain_exactly(event1,event3) }
    end

    describe "#find_each" do
      it "executes event1 and event3" do
        a = []
        subject.find_each do |e|
          a << e.id
        end
        expect(a).to contain_exactly(event1.id, event3.id)
      end
    end

    describe "#include?" do
      it { expect(subject.include?(event1)).to be_truthy }
      it { expect(subject.include?(event2)).to be_falsey }
      it { expect(subject.include?(event3)).to be_truthy }
      it { expect(subject.include?(event4)).to be_falsey }
    end
  end

  context "with filter sensor: '*001'" do
{'fraction_non_alphanumeric': 0.13867854491462508, 'fraction_numerical': 0.040386043058648846, 'mean_word_length': 2.7970687711386697, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 24, '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': '3819430', 'n_tokens_mistral': 2492, 'n_tokens_neox': 2365, 'n_words': 524}