saved searches: add query normalization tests.

This commit is contained in:
evazion
2017-04-28 16:22:37 -05:00
parent 0b63dd32d1
commit 8e5491a536
3 changed files with 14 additions and 7 deletions

View File

@@ -186,8 +186,8 @@ class TagAlias < ActiveRecord::Base
end end
def initialize_creator def initialize_creator
self.creator_id = CurrentUser.user.id self.creator_id ||= CurrentUser.user.id
self.creator_ip_addr = CurrentUser.ip_addr self.creator_ip_addr ||= CurrentUser.ip_addr
end end
def antecedent_tag def antecedent_tag

View File

@@ -4,6 +4,7 @@ FactoryGirl.define do
consequent_name "bbb" consequent_name "bbb"
status "active" status "active"
skip_secondary_validations true skip_secondary_validations true
creator_ip_addr { FFaker::Internet.ip_v4_address }
after(:create) do |tag_alias| after(:create) do |tag_alias|
unless tag_alias.status == "pending" unless tag_alias.status == "pending"

View File

@@ -30,13 +30,18 @@ class SavedSearchTest < ActiveSupport::TestCase
context ".queries_for" do context ".queries_for" do
setup do setup do
@user = FactoryGirl.create(:user) @user = FactoryGirl.create(:user)
FactoryGirl.create(:tag_alias, antecedent_name: "bbb", consequent_name: "ccc", creator: @user)
FactoryGirl.create(:saved_search, user: @user, label_string: "blah", query: "aaa") FactoryGirl.create(:saved_search, user: @user, label_string: "blah", query: "aaa")
FactoryGirl.create(:saved_search, user: @user, label_string: "zah", query: "bbb") FactoryGirl.create(:saved_search, user: @user, label_string: "zah", query: "CCC BBB AAA")
FactoryGirl.create(:saved_search, user: @user, label_string: "qux", query: " aaa bbb ccc ")
end end
should "fetch the queries used by a user for a label" do should "fetch the queries used by a user for a label" do
assert_equal(%w(aaa), SavedSearch.queries_for(@user.id, "blah")) assert_equal(%w(aaa), SavedSearch.queries_for(@user.id, "blah"))
assert_equal(%w(aaa bbb), SavedSearch.queries_for(@user.id)) end
should "return fully normalized queries" do
assert_equal(["aaa", "aaa ccc"], SavedSearch.queries_for(@user.id))
end end
end end
@@ -84,7 +89,8 @@ class SavedSearchTest < ActiveSupport::TestCase
context "Creating a saved search" do context "Creating a saved search" do
setup do setup do
@user = FactoryGirl.create(:gold_user) @user = FactoryGirl.create(:gold_user)
@saved_search = @user.saved_searches.create(:query => " xxx ") FactoryGirl.create(:tag_alias, antecedent_name: "zzz", consequent_name: "yyy", creator: @user)
@saved_search = @user.saved_searches.create(:query => " ZZZ xxx ")
end end
should "update the bitpref on the user" do should "update the bitpref on the user" do
@@ -92,8 +98,8 @@ class SavedSearchTest < ActiveSupport::TestCase
assert(@user.has_saved_searches?, "should have saved_searches bitpref set") assert(@user.has_saved_searches?, "should have saved_searches bitpref set")
end end
should "normalize whitespace" do should "normalize the query aside from the order" do
assert_equal("xxx", @saved_search.query) assert_equal("yyy xxx", @saved_search.query)
end end
should "normalize the label string" do should "normalize the label string" do