From babee06794e188dffb9f82787cc0f0ab784dc99e Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 29 Oct 2024 11:46:32 -0400 Subject: [PATCH] Add coverage for `StatusTrend` and `PreviewCardTrend` models, add `locales` class method to `RankedTrend` (#32688) --- .../admin/trends/links_controller.rb | 2 +- .../admin/trends/statuses_controller.rb | 2 +- app/models/concerns/ranked_trend.rb | 4 ++++ app/models/trends/links.rb | 2 +- app/models/trends/statuses.rb | 2 +- .../preview_card_trend_fabricator.rb | 5 ++++ spec/fabricators/status_trend_fabricator.rb | 6 +++++ spec/models/preview_card_trend_spec.rb | 22 ++++++++++++++++++ spec/models/status_trend_spec.rb | 23 +++++++++++++++++++ 9 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 spec/fabricators/preview_card_trend_fabricator.rb create mode 100644 spec/fabricators/status_trend_fabricator.rb create mode 100644 spec/models/preview_card_trend_spec.rb create mode 100644 spec/models/status_trend_spec.rb diff --git a/app/controllers/admin/trends/links_controller.rb b/app/controllers/admin/trends/links_controller.rb index 83d68eba63..65eca11c7f 100644 --- a/app/controllers/admin/trends/links_controller.rb +++ b/app/controllers/admin/trends/links_controller.rb @@ -4,7 +4,7 @@ class Admin::Trends::LinksController < Admin::BaseController def index authorize :preview_card, :review? - @locales = PreviewCardTrend.pluck('distinct language') + @locales = PreviewCardTrend.locales @preview_cards = filtered_preview_cards.page(params[:page]) @form = Trends::PreviewCardBatch.new end diff --git a/app/controllers/admin/trends/statuses_controller.rb b/app/controllers/admin/trends/statuses_controller.rb index 3d8b53ea8a..682fe70bb5 100644 --- a/app/controllers/admin/trends/statuses_controller.rb +++ b/app/controllers/admin/trends/statuses_controller.rb @@ -4,7 +4,7 @@ class Admin::Trends::StatusesController < Admin::BaseController def index authorize [:admin, :status], :review? - @locales = StatusTrend.pluck('distinct language') + @locales = StatusTrend.locales @statuses = filtered_statuses.page(params[:page]) @form = Trends::StatusBatch.new end diff --git a/app/models/concerns/ranked_trend.rb b/app/models/concerns/ranked_trend.rb index add36afb0c..e707fe0bad 100644 --- a/app/models/concerns/ranked_trend.rb +++ b/app/models/concerns/ranked_trend.rb @@ -9,6 +9,10 @@ module RankedTrend end class_methods do + def locales + distinct.pluck(:language) + end + def recalculate_ordered_rank connection .exec_update(<<~SQL.squish) diff --git a/app/models/trends/links.rb b/app/models/trends/links.rb index 0650c4109d..9d721d25f8 100644 --- a/app/models/trends/links.rb +++ b/app/models/trends/links.rb @@ -85,7 +85,7 @@ class Trends::Links < Trends::Base end def request_review - PreviewCardTrend.pluck('distinct language').flat_map do |language| + PreviewCardTrend.locales.flat_map do |language| score_at_threshold = PreviewCardTrend.where(language: language).allowed.by_rank.ranked_below(options[:review_threshold]).first&.score || 0 preview_card_trends = PreviewCardTrend.where(language: language).not_allowed.joins(:preview_card) diff --git a/app/models/trends/statuses.rb b/app/models/trends/statuses.rb index 9be6eb13a5..8757e5aa9c 100644 --- a/app/models/trends/statuses.rb +++ b/app/models/trends/statuses.rb @@ -78,7 +78,7 @@ class Trends::Statuses < Trends::Base end def request_review - StatusTrend.pluck('distinct language').flat_map do |language| + StatusTrend.locales.flat_map do |language| score_at_threshold = StatusTrend.where(language: language, allowed: true).by_rank.ranked_below(options[:review_threshold]).first&.score || 0 status_trends = StatusTrend.where(language: language, allowed: false).joins(:status).includes(status: :account) diff --git a/spec/fabricators/preview_card_trend_fabricator.rb b/spec/fabricators/preview_card_trend_fabricator.rb new file mode 100644 index 0000000000..14c126cc89 --- /dev/null +++ b/spec/fabricators/preview_card_trend_fabricator.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +Fabricator(:preview_card_trend) do + preview_card +end diff --git a/spec/fabricators/status_trend_fabricator.rb b/spec/fabricators/status_trend_fabricator.rb new file mode 100644 index 0000000000..c775892b1f --- /dev/null +++ b/spec/fabricators/status_trend_fabricator.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +Fabricator(:status_trend) do + status + account +end diff --git a/spec/models/preview_card_trend_spec.rb b/spec/models/preview_card_trend_spec.rb new file mode 100644 index 0000000000..6edd24b693 --- /dev/null +++ b/spec/models/preview_card_trend_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe PreviewCardTrend do + describe 'Associations' do + it { is_expected.to belong_to(:preview_card).required } + end + + describe '.locales' do + before do + Fabricate :preview_card_trend, language: 'en' + Fabricate :preview_card_trend, language: 'en' + Fabricate :preview_card_trend, language: 'es' + end + + it 'returns unique set of languages' do + expect(described_class.locales) + .to eq(['en', 'es']) + end + end +end diff --git a/spec/models/status_trend_spec.rb b/spec/models/status_trend_spec.rb new file mode 100644 index 0000000000..34522493cd --- /dev/null +++ b/spec/models/status_trend_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe StatusTrend do + describe 'Associations' do + it { is_expected.to belong_to(:account).required } + it { is_expected.to belong_to(:status).required } + end + + describe '.locales' do + before do + Fabricate :status_trend, language: 'en' + Fabricate :status_trend, language: 'en' + Fabricate :status_trend, language: 'es' + end + + it 'returns unique set of languages' do + expect(described_class.locales) + .to eq(['en', 'es']) + end + end +end