Skip to content

Commit

Permalink
Improves test structure + Refactors api.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
empty-codes committed Nov 15, 2024
1 parent b7fec4c commit 67d1554
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 39 deletions.
10 changes: 5 additions & 5 deletions lib/wikidata/diff/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Api

def self.get_revision_contents(revision_ids)
revision_ids = revision_ids.uniq if revision_ids
response = fetch_revision_data(revision_ids)
response = fetch_all_revisions(revision_ids)

return {} if response.nil? || response['pages'].nil?

Expand All @@ -21,18 +21,18 @@ def self.get_revision_contents(revision_ids)
raise e
end

def self.fetch_revision_data(revision_ids)
query_parameters = {
def self.get_query_parameters(revision_ids)
{
prop: 'revisions',
revids: revision_ids&.join('|'),
rvslots: 'main',
rvprop: 'content|ids|comment',
format: 'json'
}
fetch_all_revisions(query_parameters)
end

def self.fetch_all_revisions(query)
def self.fetch_all_revisions(revision_ids)
query = get_query_parameters(revision_ids)
client = api_client
data = {}
continue_param = nil
Expand Down
63 changes: 29 additions & 34 deletions spec/wikidata/diff/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,37 @@
require 'rspec'

RSpec.describe Api do
describe '.get_revision_contents' do
let(:revision_ids) do
[
2266123021, 2266341034, 2266123060, 2266123123, 2266123148,
2266123175, 2266123210, 2266123270, 2266123325, 2266123373,
2266123418, 2266341148, 2266123442, 2266123459, 2266123479,
2266123502, 2266123529, 2266123536, 2266123548, 2266123562,
2266123568, 2266341782, 2266123581, 2266123596, 2266123602
]
end

it 'returns a truncation warning since query exceeds response size limit' do
API_URL = 'https://www.wikidata.org/w/api.php'
client = MediawikiApi::Client.new(API_URL)
let(:revision_ids) do
[
2266123021, 2266341034, 2266123060, 2266123123, 2266123148,
2266123175, 2266123210, 2266123270, 2266123325, 2266123373,
2266123418, 2266341148, 2266123442, 2266123459, 2266123479,
2266123502, 2266123529, 2266123536, 2266123548, 2266123562,
2266123568, 2266341782, 2266123581, 2266123596, 2266123602
]
end

query_parameters = {
prop: 'revisions',
revids: revision_ids&.join('|'),
rvslots: 'main',
rvprop: 'content|ids|comment',
format: 'json'
}
response = client.send('query', query_parameters)
describe '.mediawiki_request' do
it 'returns a truncation warning since query exceeds response size limit' do
client = Api.api_client
query = Api.get_query_parameters(revision_ids)

response = Api.mediawiki_request(client, 'query', query)

expect(response['warnings']).not_to be_nil
expect(response['warnings']['result']['*']).to include(
"This result was truncated because it would otherwise be larger than the limit of 12,582,912 bytes."
)
end
expect(response['warnings']).not_to be_nil
expect(response['warnings']['result']['*']).to include(
"This result was truncated because it would otherwise be larger than the limit of 12,582,912 bytes."
)
end
end

it 'returns the correct result and handles the warning' do
@result = Api.get_revision_contents(revision_ids)

expect {@result}.not_to raise_error
expect(@result).to be_a(Hash)
expect(@result.size).to eq(25)
end
describe '.get_revision_contents' do
it 'returns the correct result and handles the warning' do
result = Api.get_revision_contents(revision_ids)

expect { result }.not_to raise_error
expect(result).to be_a(Hash)
expect(result.size).to eq(25)
end
end
end

0 comments on commit 67d1554

Please sign in to comment.