Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Abishekcs committed Oct 18, 2023
1 parent 3f04768 commit f1987c5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
20 changes: 20 additions & 0 deletions spec/controllers/discouraged_articles_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe DiscouragedArticlesController, type: :controller do
describe 'GET #category_member?' do
it 'returns true when a category member exists' do
FactoryBot.create(:wikipedia_category_member, category_member: 'Example Article')
get :category_member?, params: { article_title: 'Example Article' }
expect(response).to have_http_status(:success)
expect(JSON.parse(response.body)['is_category_member']).to be_truthy
end

it 'returns false when a category member does not exist' do
get :category_member?, params: { article_title: 'Nonexistent Article' }
expect(response).to have_http_status(:success)
expect(JSON.parse(response.body)['is_category_member']).to be_falsey
end
end
end
4 changes: 3 additions & 1 deletion spec/factories/wikipedia_category_members.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

FactoryBot.define do
factory :wikipedia_category_member do
category_members { "MyText" }
category_member { 'Example Category Member' }
end
end
32 changes: 31 additions & 1 deletion spec/models/wikipedia_category_member_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe WikipediaCategoryMember, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
describe '#fetch_category_members' do
it 'fetches and saves category members' do
response = instance_double('response', status: 200,
data: { 'categorymembers' => [{ 'title' => 'Member 1' }, { 'title' => 'Member 2' }] })

wiki_api = instance_double('wiki_api', query: response)
allow(WikiApi).to receive(:new).and_return(wiki_api)

described_class.create(category_member: 'Member 2')
described_class.create(category_member: 'Member 3')

subject.fetch_category_members

expect(described_class.pluck(:category_member)).to contain_exactly('Member 1', 'Member 2')

expect(described_class.find_by(category_member: 'Member 3')).to be_nil
end

it 'logs a warning on API failure' do
response = instance_double('response', status: 500)

wiki_api = instance_double('wiki_api', query: response)
allow(WikiApi).to receive(:new).and_return(wiki_api)

expect(Rails.logger).to receive(:warn).with('Failed to fetch categorymembers data')

subject.fetch_category_members
end
end
end
18 changes: 18 additions & 0 deletions spec/workers/update_wikipedia_category_worker_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require 'rails_helper'
require 'sidekiq/testing'

Sidekiq::Testing.fake!

RSpec.describe UpdateWikipediaCategoryWorker, type: :worker do
it 'fetches and saves Wikipedia category members' do
allow_any_instance_of(WikipediaCategoryMember).to receive(:fetch_category_members)

expect do
described_class.perform_async
end.to change(described_class.jobs, :size).by(1)

described_class.drain
end
end

0 comments on commit f1987c5

Please sign in to comment.