Skip to content

Commit

Permalink
Add coverage for StatusTrend and PreviewCardTrend models, add `lo…
Browse files Browse the repository at this point in the history
…cales` class method to `RankedTrend` (mastodon#32688)
  • Loading branch information
mjankowski authored Oct 29, 2024
1 parent df3b954 commit babee06
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/trends/links_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/trends/statuses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/models/concerns/ranked_trend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/models/trends/links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def refresh(at_time = Time.now.utc)
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)

Expand Down
2 changes: 1 addition & 1 deletion app/models/trends/statuses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def refresh(at_time = Time.now.utc)
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)

Expand Down
5 changes: 5 additions & 0 deletions spec/fabricators/preview_card_trend_fabricator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

Fabricator(:preview_card_trend) do
preview_card
end
6 changes: 6 additions & 0 deletions spec/fabricators/status_trend_fabricator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

Fabricator(:status_trend) do
status
account
end
22 changes: 22 additions & 0 deletions spec/models/preview_card_trend_spec.rb
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions spec/models/status_trend_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit babee06

Please sign in to comment.