From ffd0716ba7d2a82073c83d370397238744865742 Mon Sep 17 00:00:00 2001 From: weeklies <80141759+weeklies@users.noreply.github.com> Date: Thu, 28 Sep 2023 18:06:14 +0800 Subject: [PATCH] AO3-6328 Feedback --- features/step_definitions/tag_steps.rb | 2 +- spec/models/tag_spec.rb | 21 +++++++++------------ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/features/step_definitions/tag_steps.rb b/features/step_definitions/tag_steps.rb index a12e0457f3a..2973da919ba 100644 --- a/features/step_definitions/tag_steps.rb +++ b/features/step_definitions/tag_steps.rb @@ -71,7 +71,7 @@ # Create tags with specified number of uses hash.each do |freeform, uses| - tag = Freeform.create(name: freeform) + tag = Freeform.find_or_create_by_name(freeform) tag.taggings_count = uses end diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb index 567e2ea6761..16daf57b9bb 100644 --- a/spec/models/tag_spec.rb +++ b/spec/models/tag_spec.rb @@ -78,7 +78,7 @@ end end - context "write_redis_to_database" do + describe "write_redis_to_database" do let(:tag) { create(:fandom) } let!(:work) { create(:work, fandom_string: tag.name) } @@ -87,22 +87,23 @@ tag.reload end + def expect_tag_update_flag_in_redis_to_be(flag) + expect(REDIS_GENERAL.sismember("tag_update", tag.id)).to eq flag + end + it "does not write to the database when reading the count" do tag.taggings_count - # Check if redis has flagged this tag for an update to the database. - expect(REDIS_GENERAL.sismember("tag_update", tag.id)).to eq false + expect_tag_update_flag_in_redis_to_be(false) end it "does not write to the database when assigning the same count" do tag.taggings_count = 1 - # Check if redis has flagged this tag for an update to the database. - expect(REDIS_GENERAL.sismember("tag_update", tag.id)).to eq false + expect_tag_update_flag_in_redis_to_be(false) end it "writes to the database when assigning a new count" do tag.taggings_count = 2 - # Check if redis has flagged this tag for an update to the database. - expect(REDIS_GENERAL.sismember("tag_update", tag.id)).to eq true + expect_tag_update_flag_in_redis_to_be(true) RedisJobSpawner.perform_now("TagCountUpdateJob") tag.reload @@ -113,12 +114,8 @@ end it "writes to the database when adding a new work with the same tag" do - expect(tag.taggings_count_cache).to eq 1 - expect(tag.taggings_count).to eq 1 - create(:work, fandom_string: tag.name) - # Check if redis has flagged this tag for an update to the database. - expect(REDIS_GENERAL.sismember("tag_update", tag.id)).to eq true + expect_tag_update_flag_in_redis_to_be(true) RedisJobSpawner.perform_now("TagCountUpdateJob") tag.reload