diff --git a/Gemfile b/Gemfile index b43281018..8b48e6c30 100644 --- a/Gemfile +++ b/Gemfile @@ -89,4 +89,5 @@ group :test do gem "timecop" gem "webmock" gem "minitest-ci" + gem "mock_redis" end diff --git a/Gemfile.lock b/Gemfile.lock index 2518ea9c9..428aee33b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -232,6 +232,7 @@ GEM minitest (>= 5.0.6) mocha (1.5.0) metaclass (~> 0.0.1) + mock_redis (0.19.0) msgpack (1.2.4) msgpack (1.2.4-x64-mingw32) multi_json (1.13.1) @@ -479,6 +480,7 @@ DEPENDENCIES memoist minitest-ci mocha + mock_redis net-sftp newrelic_rpm oauth2 diff --git a/Procfile b/Procfile index 54767a5b0..f9ade14dc 100644 --- a/Procfile +++ b/Procfile @@ -3,4 +3,3 @@ jobs: bin/rake jobs:work recommender: bundle exec ruby script/mock_services/recommender.rb iqdbs: bundle exec ruby script/mock_services/iqdbs.rb reportbooru: bundle exec ruby script/mock_services/reportbooru.rb -listbooru: bundle exec ruby script/mock_services/listbooru.rb \ No newline at end of file diff --git a/README.md b/README.md index f0341f343..02bdb1945 100644 --- a/README.md +++ b/README.md @@ -68,11 +68,6 @@ The following features requires a Google API account: IQDB integration is delegated to the [IQDBS service](https://github.com/r888888888/iqdbs). -### Listbooru Service - -In order to access saved search functionality you will need to install and -configure the [Listbooru service](https://github.com/r888888888/listbooru). - ### Archive Service In order to access versioned data for pools and posts you will diff --git a/app/controllers/saved_searches_controller.rb b/app/controllers/saved_searches_controller.rb index 6f67bf7ea..b291010e5 100644 --- a/app/controllers/saved_searches_controller.rb +++ b/app/controllers/saved_searches_controller.rb @@ -50,7 +50,7 @@ class SavedSearchesController < ApplicationController def check_availability if !SavedSearch.enabled? - raise NotImplementedError.new("Listbooru service is not configured. Saved searches are not available.") + raise NotImplementedError.new("Saved searches are not available.") end end diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index 02219ac87..148da88ff 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -71,9 +71,9 @@ class PostQueryBuilder if SavedSearch.enabled? saved_searches.each do |saved_search| if saved_search == "all" - post_ids = SavedSearch.post_ids(CurrentUser.id) + post_ids = SavedSearch.post_ids_for(CurrentUser.id) else - post_ids = SavedSearch.post_ids(CurrentUser.id, saved_search) + post_ids = SavedSearch.post_ids_for(CurrentUser.id, label: saved_search) end post_ids = [0] if post_ids.empty? diff --git a/app/models/saved_search.rb b/app/models/saved_search.rb index 349253a65..0d17002a3 100644 --- a/app/models/saved_search.rb +++ b/app/models/saved_search.rb @@ -1,30 +1,35 @@ class SavedSearch < ApplicationRecord REDIS_EXPIRY = 3600 - QUERY_LIMIT = 500 + QUERY_LIMIT = 1000 + + def self.enabled? + Danbooru.config.redis_url.present? + end concerning :Redis do class_methods do - def enabled? - Danbooru.config.redis_url.present? + extend Memoist + + def redis + ::Redis.new(url: Danbooru.config.redis_url) end + memoize :redis def post_ids_for(user_id, label: nil) - redis = Redis.new(url: Danbooru.config.redis_url) label = normalize_label(label) if label queries = queries_for(user_id, label: label) post_ids = Set.new queries.each do |query| - query_hash = Cache.hash(query) - redis_key = "search:#{query_hash}" + redis_key = "search:#{query}" if redis.exists(redis_key) - sub_ids = redis.smembers(redis_key) + sub_ids = redis.smembers(redis_key).map(&:to_i) post_ids.merge(sub_ids) redis.expire(redis_key, REDIS_EXPIRY) else SavedSearch.delay(queue: "default").populate(query) end end - post_ids.to_a.sort + post_ids.to_a.sort.last(QUERY_LIMIT) end end end @@ -52,24 +57,39 @@ class SavedSearch < ApplicationRecord labels end - def self.labels_for(user_id) - Cache.get(cache_key(user_id)) do - SavedSearch. - where(user_id: user_id). - order("label"). - pluck(Arel.sql("distinct unnest(labels) as label")) - end + def labels_for(user_id) + SavedSearch. + where(user_id: user_id). + order("label"). + pluck(Arel.sql("distinct unnest(labels) as label")) end end + + def normalize_labels + self.labels = labels. + map {|x| SavedSearch.normalize_label(x)}. + reject {|x| x.blank?} + end + + def label_string + labels.join(" ") + end + + def label_string=(val) + self.labels = val.to_s.split(/[[:space:]]+/) + end + + def labels=(labels) + labels = labels.map { |label| SavedSearch.normalize_label(label) } + super(labels) + end end concerning :Search do class_methods do def populate(query) CurrentUser.as_system do - query_hash = Cache.hash(query) - redis_key = "search:#{query_hash}" - redis = Redis.new(url: Danbooru.config.redis_url) + redis_key = "search:#{query}" return if redis.exists(redis_key) post_ids = Post.tag_match(query, true).limit(QUERY_LIMIT).pluck(:id) redis.sadd(redis_key, post_ids) @@ -81,35 +101,37 @@ class SavedSearch < ApplicationRecord end end + concerning :Queries do + class_methods do + def queries_for(user_id, label: nil, options: {}) + SavedSearch. + where(user_id: user_id). + labeled(label). + pluck(:query). + map {|x| Tag.normalize_query(x, sort: true)}. + sort. + uniq + end + end + + def normalized_query + Tag.normalize_query(query, sort: true) + end + + def normalize_query + self.query = Tag.normalize_query(query, sort: false) + end + end + attr_accessor :disable_labels belongs_to :user validates :query, presence: true validate :validate_count before_create :update_user_on_create after_destroy :update_user_on_destroy - before_validation :normalize - scope :labeled, ->(label) { where("labels @> string_to_array(?, '~~~~')", label)} - - def self.queries_for(user_id, label: nil, options: {}) - SavedSearch. - where(user_id: user_id). - tap {|arel| label ? arel.labeled(label) : arel}. - pluck(:query). - map {|x| Tag.normalize_query(x, sort: true)}. - sort. - uniq - end - - def normalized_query - Tag.normalize_query(query, sort: true) - end - - def normalize - self.query = Tag.normalize_query(query, sort: false) - self.labels = labels. - map {|x| SavedSearch.normalize_label(x)}. - reject {|x| x.blank?} - end + before_validation :normalize_query + before_validation :normalize_labels + scope :labeled, ->(label) { label.present? ? where("labels @> string_to_array(?, '~~~~')", label) : where("true") } def validate_count if user.saved_searches.count + 1 > user.max_saved_searches @@ -129,19 +151,6 @@ class SavedSearch < ApplicationRecord end end - def label_string - labels.join(" ") - end - - def label_string=(val) - self.labels = val.to_s.split(/[[:space:]]+/) - end - - def labels=(labels) - labels = labels.map { |label| SavedSearch.normalize_label(label) } - super(labels) - end - def disable_labels=(value) CurrentUser.update(disable_categorized_saved_searches: true) if value.to_s.truthy? end diff --git a/app/presenters/post_set_presenters/post.rb b/app/presenters/post_set_presenters/post.rb index 06bcc9564..3d001ec5b 100644 --- a/app/presenters/post_set_presenters/post.rb +++ b/app/presenters/post_set_presenters/post.rb @@ -19,7 +19,7 @@ module PostSetPresenters if post_set.is_pattern_search? pattern_tags elsif post_set.is_saved_search? - SavedSearch.labels_for(CurrentUser.user.id).map {|x| "search:#{x}"} + ["search:all"] + SavedSearch.labels_for(CurrentUser.user.id).map {|x| "search:#{x}"} elsif post_set.is_empty_tag? || post_set.tag_string == "order:rank" popular_tags elsif post_set.is_single_tag? diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index 411ee47ce..86c540ea2 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -673,13 +673,6 @@ module Danbooru def reportbooru_key end - # listbooru options - see https://github.com/r888888888/listbooru - def listbooru_server - end - - def listbooru_auth_key - end - # iqdbs options - see https://github.com/r888888888/iqdbs def iqdbs_auth_key end diff --git a/db/structure.sql b/db/structure.sql index 7cdd77c0c..3c436be3b 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -443,7 +443,6 @@ CREATE TABLE public.advertisement_hits ( -- CREATE SEQUENCE public.advertisement_hits_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -482,7 +481,6 @@ CREATE TABLE public.advertisements ( -- CREATE SEQUENCE public.advertisements_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -514,7 +512,6 @@ CREATE TABLE public.amazon_backups ( -- CREATE SEQUENCE public.amazon_backups_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -546,7 +543,6 @@ CREATE TABLE public.anti_voters ( -- CREATE SEQUENCE public.anti_voters_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -579,7 +575,6 @@ CREATE TABLE public.api_keys ( -- CREATE SEQUENCE public.api_keys_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -627,7 +622,6 @@ CREATE TABLE public.artist_commentaries ( -- CREATE SEQUENCE public.artist_commentaries_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -665,7 +659,6 @@ CREATE TABLE public.artist_commentary_versions ( -- CREATE SEQUENCE public.artist_commentary_versions_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -700,7 +693,6 @@ CREATE TABLE public.artist_urls ( -- CREATE SEQUENCE public.artist_urls_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -740,7 +732,6 @@ CREATE TABLE public.artist_versions ( -- CREATE SEQUENCE public.artist_versions_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -778,7 +769,6 @@ CREATE TABLE public.artists ( -- CREATE SEQUENCE public.artists_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -813,7 +803,6 @@ CREATE TABLE public.bans ( -- CREATE SEQUENCE public.bans_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -851,7 +840,6 @@ CREATE TABLE public.bulk_update_requests ( -- CREATE SEQUENCE public.bulk_update_requests_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -885,7 +873,6 @@ CREATE TABLE public.comment_votes ( -- CREATE SEQUENCE public.comment_votes_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -927,7 +914,6 @@ CREATE TABLE public.comments ( -- CREATE SEQUENCE public.comments_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -967,7 +953,6 @@ CREATE TABLE public.delayed_jobs ( -- CREATE SEQUENCE public.delayed_jobs_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -1000,7 +985,6 @@ CREATE TABLE public.dmail_filters ( -- CREATE SEQUENCE public.dmail_filters_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -1041,7 +1025,6 @@ CREATE TABLE public.dmails ( -- CREATE SEQUENCE public.dmails_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -1077,7 +1060,6 @@ CREATE TABLE public.favorite_groups ( -- CREATE SEQUENCE public.favorite_groups_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2108,7 +2090,6 @@ INHERITS (public.favorites); -- CREATE SEQUENCE public.favorites_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2178,7 +2159,6 @@ CREATE TABLE public.forum_posts ( -- CREATE SEQUENCE public.forum_posts_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2211,7 +2191,6 @@ CREATE TABLE public.forum_subscriptions ( -- CREATE SEQUENCE public.forum_subscriptions_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2245,7 +2224,6 @@ CREATE TABLE public.forum_topic_visits ( -- CREATE SEQUENCE public.forum_topic_visits_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2286,7 +2264,6 @@ CREATE TABLE public.forum_topics ( -- CREATE SEQUENCE public.forum_topics_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2320,7 +2297,6 @@ CREATE TABLE public.ip_bans ( -- CREATE SEQUENCE public.ip_bans_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2355,7 +2331,6 @@ CREATE TABLE public.janitor_trials ( -- CREATE SEQUENCE public.janitor_trials_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2389,7 +2364,6 @@ CREATE TABLE public.mod_actions ( -- CREATE SEQUENCE public.mod_actions_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2423,7 +2397,6 @@ CREATE TABLE public.news_updates ( -- CREATE SEQUENCE public.news_updates_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2465,7 +2438,6 @@ CREATE TABLE public.note_versions ( -- CREATE SEQUENCE public.note_versions_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2506,7 +2478,6 @@ CREATE TABLE public.notes ( -- CREATE SEQUENCE public.notes_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2538,7 +2509,6 @@ CREATE TABLE public.pixiv_ugoira_frame_data ( -- CREATE SEQUENCE public.pixiv_ugoira_frame_data_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2576,7 +2546,6 @@ CREATE TABLE public.pools ( -- CREATE SEQUENCE public.pools_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2611,7 +2580,6 @@ CREATE TABLE public.post_appeals ( -- CREATE SEQUENCE public.post_appeals_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2644,7 +2612,6 @@ CREATE TABLE public.post_approvals ( -- CREATE SEQUENCE public.post_approvals_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2679,7 +2646,6 @@ CREATE TABLE public.post_disapprovals ( -- CREATE SEQUENCE public.post_disapprovals_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2715,7 +2681,6 @@ CREATE TABLE public.post_flags ( -- CREATE SEQUENCE public.post_flags_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2760,7 +2725,6 @@ CREATE TABLE public.post_replacements ( -- CREATE SEQUENCE public.post_replacements_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2803,7 +2767,6 @@ CREATE TABLE public.post_votes ( -- CREATE SEQUENCE public.post_votes_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2874,7 +2837,6 @@ CREATE TABLE public.posts ( -- CREATE SEQUENCE public.posts_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2908,7 +2870,6 @@ CREATE TABLE public.saved_searches ( -- CREATE SEQUENCE public.saved_searches_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2949,7 +2910,6 @@ CREATE TABLE public.super_voters ( -- CREATE SEQUENCE public.super_voters_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -2989,7 +2949,6 @@ CREATE TABLE public.tag_aliases ( -- CREATE SEQUENCE public.tag_aliases_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3029,7 +2988,6 @@ CREATE TABLE public.tag_implications ( -- CREATE SEQUENCE public.tag_implications_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3067,7 +3025,6 @@ CREATE TABLE public.tag_subscriptions ( -- CREATE SEQUENCE public.tag_subscriptions_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3104,7 +3061,6 @@ CREATE TABLE public.tags ( -- CREATE SEQUENCE public.tags_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3169,7 +3125,6 @@ CREATE TABLE public.uploads ( -- CREATE SEQUENCE public.uploads_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3204,7 +3159,6 @@ CREATE TABLE public.user_feedback ( -- CREATE SEQUENCE public.user_feedback_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3242,7 +3196,6 @@ CREATE TABLE public.user_name_change_requests ( -- CREATE SEQUENCE public.user_name_change_requests_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3275,7 +3228,6 @@ CREATE TABLE public.user_password_reset_nonces ( -- CREATE SEQUENCE public.user_password_reset_nonces_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3334,7 +3286,6 @@ furry -rating:s'::text, -- CREATE SEQUENCE public.users_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3373,7 +3324,6 @@ CREATE TABLE public.wiki_page_versions ( -- CREATE SEQUENCE public.wiki_page_versions_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -3413,7 +3363,6 @@ CREATE TABLE public.wiki_pages ( -- CREATE SEQUENCE public.wiki_pages_id_seq - AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE @@ -7413,7 +7362,7 @@ CREATE TRIGGER trigger_wiki_pages_on_update_for_other_names BEFORE INSERT OR UPD -- PostgreSQL database dump complete -- -SET search_path TO public; +SET search_path TO "$user", public; INSERT INTO "schema_migrations" (version) VALUES ('20100204211522'), diff --git a/script/mock_services/README.md b/script/mock_services/README.md index 6f2e10259..6f8f84e8a 100644 --- a/script/mock_services/README.md +++ b/script/mock_services/README.md @@ -4,4 +4,4 @@ These are mocked services to be used for development purposes. - recommender: port 3001 - iqdbs: port 3002 - reportbooru: port 3003 -- listbooru: port 3004 + diff --git a/script/mock_services/listbooru.rb b/script/mock_services/listbooru.rb deleted file mode 100644 index b25cffc7f..000000000 --- a/script/mock_services/listbooru.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'sinatra' -require 'json' - -set :port, 3004 - -post '/v2/search' do - # todo -end diff --git a/test/functional/saved_searches_controller_test.rb b/test/functional/saved_searches_controller_test.rb index 52c5fe30c..acd9c6b21 100644 --- a/test/functional/saved_searches_controller_test.rb +++ b/test/functional/saved_searches_controller_test.rb @@ -3,6 +3,7 @@ require 'test_helper' class SavedSearchesControllerTest < ActionDispatch::IntegrationTest context "The saved searches controller" do setup do + SavedSearch.stubs(:enabled?).returns(true) @user = create(:user) as_user do @saved_search = create(:saved_search, user: @user) diff --git a/test/test_helpers/saved_search_test_helper.rb b/test/test_helpers/saved_search_test_helper.rb index 2b2a9071b..f36543852 100644 --- a/test/test_helpers/saved_search_test_helper.rb +++ b/test/test_helpers/saved_search_test_helper.rb @@ -17,7 +17,5 @@ module SavedSearchTestHelper service = mock_sqs_service.new SavedSearch.stubs(:sqs_service).returns(service) Danbooru.config.stubs(:aws_sqs_saved_search_url).returns("http://localhost:3002") - Danbooru.config.stubs(:listbooru_auth_key).returns("blahblahblah") - Danbooru.config.stubs(:listbooru_server).returns("http://localhost:3001") end end diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index 21b8e55ab..92b99ff00 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -2265,25 +2265,35 @@ class PostTest < ActiveSupport::TestCase assert_tag_match([post], "pixiv_id:none") end - # should "return posts for a pixiv novel id search" do - # url = "http://www.pixiv.net/novel/show.php?id=2156088" - # post = FactoryBot.create(:post, :source => url) - # assert_equal(1, Post.tag_match("pixiv_novel_id:2156088").count) - # end + context "saved searches" do + setup do + SavedSearch.stubs(:enabled?).returns(true) + @post1 = FactoryBot.create(:post, tag_string: "aaa") + @post2 = FactoryBot.create(:post, tag_string: "bbb") + FactoryBot.create(:saved_search, query: "aaa", labels: ["zzz"], user: CurrentUser.user) + FactoryBot.create(:saved_search, query: "bbb", user: CurrentUser.user) + end - should "return posts for a search: metatag" do - post1 = FactoryBot.create(:post, tag_string: "aaa") - post2 = FactoryBot.create(:post, tag_string: "bbb") - FactoryBot.create(:saved_search, query: "aaa", labels: ["zzz"], user: CurrentUser.user) - FactoryBot.create(:saved_search, query: "bbb", user: CurrentUser.user) + context "labeled" do + should "work" do + SavedSearch.expects(:post_ids_for).with(CurrentUser.id, label: "zzz").returns([@post1.id]) + assert_tag_match([@post1], "search:zzz") + end + end - SavedSearch.expects(:post_ids).with(CurrentUser.id, "zzz").returns([post1.id]) - SavedSearch.expects(:post_ids).with(CurrentUser.id, "uncategorized").returns([post2.id]) - SavedSearch.expects(:post_ids).with(CurrentUser.id).returns([post1.id, post2.id]) + context "missing" do + should "work" do + SavedSearch.expects(:post_ids_for).with(CurrentUser.id, label: "uncategorized").returns([@post2.id]) + assert_tag_match([@post2], "search:uncategorized") + end + end - assert_tag_match([post1], "search:zzz") - assert_tag_match([post2], "search:uncategorized") - assert_tag_match([post2, post1], "search:all") + context "all" do + should "work" do + SavedSearch.expects(:post_ids_for).with(CurrentUser.id).returns([@post1.id, @post2.id]) + assert_tag_match([@post2, @post1], "search:all") + end + end end should "return posts for a rating: metatag" do diff --git a/test/unit/saved_search_test.rb b/test/unit/saved_search_test.rb index 535b3c49d..7d2513186 100644 --- a/test/unit/saved_search_test.rb +++ b/test/unit/saved_search_test.rb @@ -6,7 +6,8 @@ class SavedSearchTest < ActiveSupport::TestCase @user = FactoryBot.create(:user) CurrentUser.user = @user CurrentUser.ip_addr = "127.0.0.1" - mock_saved_search_service! + @mock_redis = MockRedis.new + SavedSearch.stubs(:redis).returns(@mock_redis) end def teardown @@ -24,11 +25,6 @@ class SavedSearchTest < ActiveSupport::TestCase should "fetch the labels used by a user" do assert_equal(%w(blah zah), SavedSearch.labels_for(@user.id)) end - - should "expire when a search is updated" do - Cache.expects(:delete).once - FactoryBot.create(:saved_search, user: @user, query: "blah") - end end context ".queries_for" do @@ -40,47 +36,81 @@ class SavedSearchTest < ActiveSupport::TestCase end 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, label: "blah")) end - should "return fully normalized queries" do + should "fetch the queries used by a user without a label" do assert_equal(["aaa", "aaa ccc"], SavedSearch.queries_for(@user.id)) end end - context "Fetching the post ids for a search" do + context ".search_labels" do + setup do + FactoryBot.create(:tag_alias, antecedent_name: "bbb", consequent_name: "ccc", creator: @user) + FactoryBot.create(:saved_search, user: @user, label_string: "blah", query: "aaa") + FactoryBot.create(:saved_search, user: @user, label_string: "blahbling", query: "CCC BBB AAA") + FactoryBot.create(:saved_search, user: @user, label_string: "qux", query: " aaa bbb ccc ") + end + + should "fetch the queries used by a user for a label" do + assert_equal(%w(blah blahbling), SavedSearch.search_labels(@user.id, label: "blah")) + end + end + + context ".post_ids_for" do context "with a label" do setup do - SavedSearch.expects(:queries_for).with(1, "blah").returns(%w(a b c)) - stub_request(:post, "http://localhost:3001/v2/search").to_return(:body => "1 2 3 4") + SavedSearch.expects(:queries_for).with(1, label: "blah").returns(%w(a b c)) end - should "return a list of ids" do - post_ids = SavedSearch.post_ids(1, "blah") - assert_equal([1,2,3,4], post_ids) + context "without a primed cache" do + should "delay processing three times" do + SavedSearch.expects(:populate).times(3) + post_ids = SavedSearch.post_ids_for(1, label: "blah") + assert_equal([], post_ids) + end + end + + context "with a primed cached" do + setup do + @mock_redis.sadd("search:a", 1) + @mock_redis.sadd("search:b", 2) + @mock_redis.sadd("search:c", 3) + end + + should "fetch the post ids" do + SavedSearch.expects(:delay).never + post_ids = SavedSearch.post_ids_for(1, label: "blah") + assert_equal([1,2,3], post_ids) + end end end context "without a label" do setup do - SavedSearch.expects(:queries_for).with(1, nil).returns(%w(a b c)) - stub_request(:post, "http://localhost:3001/v2/search").to_return(:body => "1 2 3 4") + SavedSearch.expects(:queries_for).with(1, label: nil).returns(%w(a b c)) end - should "return a list of ids" do - post_ids = SavedSearch.post_ids(1) - assert_equal([1,2,3,4], post_ids) - end - end - - context "with a nonexistent label" do - setup do - SavedSearch.expects(:queries_for).with(1, "empty").returns([]) + context "without a primed cache" do + should "delay processing three times" do + SavedSearch.expects(:populate).times(3) + post_ids = SavedSearch.post_ids_for(1) + assert_equal([], post_ids) + end end - should "return an empty list of ids" do - post_ids = SavedSearch.post_ids(1, "empty") - assert_equal([], post_ids) + context "with a primed cache" do + setup do + @mock_redis.sadd("search:a", 1) + @mock_redis.sadd("search:b", 2) + @mock_redis.sadd("search:c", 3) + end + + should "fetch the post ids" do + SavedSearch.expects(:delay).never + post_ids = SavedSearch.post_ids_for(1) + assert_equal([1,2,3], post_ids) + end end end end @@ -88,7 +118,7 @@ class SavedSearchTest < ActiveSupport::TestCase context "Creating a saved search" do setup do FactoryBot.create(:tag_alias, antecedent_name: "zzz", consequent_name: "yyy", creator: @user) - @saved_search = @user.saved_searches.create(:query => " ZZZ xxx ") + @saved_search = @user.saved_searches.create(query: " ZZZ xxx ") end should "update the bitpref on the user" do diff --git a/test/unit/tag_alias_test.rb b/test/unit/tag_alias_test.rb index f8d769db6..e9eba20ab 100644 --- a/test/unit/tag_alias_test.rb +++ b/test/unit/tag_alias_test.rb @@ -68,13 +68,19 @@ class TagAliasTest < ActiveSupport::TestCase assert_nil(Cache.get("ta:#{Cache.hash("aaa")}")) end - should "move saved searches" do - tag1 = FactoryBot.create(:tag, :name => "...") - tag2 = FactoryBot.create(:tag, :name => "bbb") - ss = FactoryBot.create(:saved_search, :query => "123 ... 456", :user => CurrentUser.user) - ta = FactoryBot.create(:tag_alias, :antecedent_name => "...", :consequent_name => "bbb") - ss.reload - assert_equal(%w(123 456 bbb), ss.query.split.sort) + context "saved searches" do + setup do + SavedSearch.stubs(:enabled?).returns(true) + end + + should "move saved searches" do + tag1 = FactoryBot.create(:tag, :name => "...") + tag2 = FactoryBot.create(:tag, :name => "bbb") + ss = FactoryBot.create(:saved_search, :query => "123 ... 456", :user => CurrentUser.user) + ta = FactoryBot.create(:tag_alias, :antecedent_name => "...", :consequent_name => "bbb") + ss.reload + assert_equal(%w(123 456 bbb), ss.query.split.sort) + end end should "update any affected posts when saved" do