<commit_before>require "spec_helper"

describe Winnow::Model do
  # N.B. See spec/dummy/app/model.rb for info

  describe ".searchable" do
    it "should accept field names" do
      User.searchable(:name)
      User.searchables.should eq [:name]
    end

    it "should accept field names contains" do
      User.searchable(:name_contains)
      User.searchables.should eq [:name_contains]
    end

    it "should accept scopes" do
      User.searchable(:name_starts_with)
      User.searchables.should eq [:name_starts_with]
    end

    it "should accept class methods" do
      User.searchable(:email_from)
      User.searchables.should eq [:email_from]
    end

    it "should accept arrays of names" do
      User.searchable(:name, :email_from)
      User.searchables.should eq [:name, :email_from]
    end

    it "should raise an error if an unknown name is passed in" do
      expect { User.searchable(:some_other_method) }.to raise_error(RuntimeError, "Unknown searchable: :some_other_method")
    end

    it "should include all unknown names in error" do
      expect { User.searchable(:name, :some_other_method, :another_method) }.to raise_error(RuntimeError, "Unknown searchable: :some_other_method, :another_method")
    end
  end

  describe ".search" do
    it "should call any class methods defined as searchable" do
      User.searchable(:name_starts_with)
      User.should_receive(:name_starts_with).with("Joelle").and_call_original
      User.search(name_starts_with: "Joelle")
    end

    it "should call any scopes defined as searchable" do
      User.searchable(:email_from)
      User.should_receive(:email_from).with("gmail.com").and_call_original
      User.search(email_from: "gmail.com")
    end

    it "should set up equality conditions on any fields defined as searchable" do
      User.searchable(:name)
      ActiveRecord::Relation.any_instance.should_receive(:where).with(name: "<NAME>")
      User.search(name: "<NAME>")
    end

    it "should set up contains conditions on any fields defined as contains searchable" do
      User.searchable(:name_contains)
      ActiveRecord::Relation.any_instance.should_receive(:where).with("users.name like ?", "%ate%")
      User.search(name_contains: "ate")
    end

    it "should call all searchables if multiple params passed in" do
      User.searchable(:email_from, :email)
      User.should_receive(:email_from).with("Incand").and_return(User) # fake return otherwise our expectations get confused
      User.should_receive(:where).with(email: "<EMAIL>").and_call_original
      User.search(email_from: "Incand", email: "<EMAIL>")
    end

    context 'with boolean columns' do
      before { User.searchable(:awesome) }

      it "should convert 'true' to true" do
        ActiveRecord::Relation.any_instance.should_receive(:where).with(awesome: true).and_call_original
        User.search(awesome: 'true')
      end

      it "should convert 'false' to false" do
        ActiveRecord::Relation.any_instance.should_receive(:where).with(awesome: false).and_call_original
        User.search(awesome: 'false')
      end

      it "should handle true as you'd expect" do
        ActiveRecord::Relation.any_instance.should_receive(:where).with(awesome: true).and_call_original
        User.search(awesome: true)
      end

      it "should handle false as you'd expect" do
        ActiveRecord::Relation.any_instance.should_receive(:where).with(awesome: false).and_call_original
        User.search(awesome: false)
      end

      it "should ignore nil" do
        User.should_not_receive(:where)
        User.search(awesome: nil)
      end

      it "should convert truthy to true" do
        ActiveRecord::Relation.any_instance.should_receive(:where).with(awesome: true).and_call_original<commit_msg>Also check relation instances<commit_after>require "spec_helper"

describe Winnow::Model do
  # N.B. See spec/dummy/app/model.rb for info

  describe ".searchable" do
    it "should accept field names" do
      User.searchable(:name)
      User.searchables.should eq [:name]
    end

    it "should accept field names contains" do
      User.searchable(:name_contains)
      User.searchables.should eq [:name_contains]
    end

    it "should accept scopes" do
      User.searchable(:name_starts_with)
      User.searchables.should eq [:name_starts_with]
    end

    it "should accept class methods" do
      User.searchable(:email_from)
      User.searchables.should eq [:email_from]
    end

    it "should accept arrays of names" do
      User.searchable(:name, :email_from)
      User.searchables.should eq [:name, :email_from]
    end

    it "should raise an error if an unknown name is passed in" do
      expect { User.searchable(:some_other_method) }.to raise_error(RuntimeError, "Unknown searchable: :some_other_method")
    end

    it "should include all unknown names in error" do
      expect { User.searchable(:name, :some_other_method, :another_method) }.to raise_error(RuntimeError, "Unknown searchable: :some_other_method, :another_method")
    end
  end

  describe ".search" do
    it "should call any class methods defined as searchable" do
      User.searchable(:name_starts_with)
      User.should_receive(:name_starts_with).with("Joelle").and_call_original
      User.search(name_starts_with: "Joelle")
    end

    it "should call any scopes defined as searchable" do
      User.searchable(:email_from)
      User.should_receive(:email_from).with("gmail.com").and_call_original
      User.search(email_from: "gmail.com")
    end

    it "should set up equality conditions on any fields defined as searchable" do
      User.searchable(:name)
      ActiveRecord::Relation.any_instance.should_receive(:where).with(name: "<NAME>")
      User.search(name: "<NAME>")
    end

    it "should set up contains conditions on any fields defined as contains searchable" do
      User.searchable(:name_contains)
      ActiveRecord::Relation.any_instance.should_receive(:where).with("users.name like ?", "%ate%")
      User.search(name_contains: "ate")
    end

    it "should call all searchables if multiple params passed in" do
      User.searchable(:email_from, :email)
      User.should_receive(:email_from).with("Incand").and_return(User) # fake return otherwise our expectations get confused
      User.should_receive(:where).with(email: "<EMAIL>").and_call_original
      User.search(email_from: "Incand", email: "<EMAIL>")
    end

    context 'with boolean columns' do
      before { User.searchable(:awesome) }

      it "should convert 'true' to true" do
        ActiveRecord::Relation.any_instance.should_receive(:where).with(awesome: true).and_call_original
        User.search(awesome: 'true')
      end

      it "should convert 'false' to false" do
        ActiveRecord::Relation.any_instance.should_receive(:where).with(awesome: false).and_call_original
        User.search(awesome: 'false')
      end

      it "should handle true as you'd expect" do
        ActiveRecord::Relation.any_instance.should_receive(:where).with(awesome: true).and_call_original
        User.search(awesome: true)
      end

      it "should handle false as you'd expect" do
        ActiveRecord::Relation.any_instance.should_receive(:where).with(awesome: false).and_call_original
        User.search(awesome: false)
      end

      it "should ignore nil" do
        User.should_not_receive(:where)
        ActiveRecord::Relation.any_instance.should_not_receive(:where)
        User.search(awesome: nil)
      end

      it "should convert truthy to true" do
        ActiveRecord::Relation.any_instance.should_receive(:where).with(awesome: true).and_call_original
{'fraction_non_alphanumeric': 0.10095337599582081, 'fraction_numerical': 0.0, 'mean_word_length': 3.544807121661721, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '6927873', 'n_tokens_mistral': 2318, 'n_tokens_neox': 2245, 'n_words': 579}