Skip to content

Commit 8e5dff3

Browse files
committed
added helpers for testing fetch_all
1 parent 90348e8 commit 8e5dff3

File tree

2 files changed

+57
-41
lines changed

2 files changed

+57
-41
lines changed

spec/shared/ex_fetch_all.rb

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
describe '#fetch_all' do
33
context 'when one page of items is returned' do
44
before do
5-
stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll")
6-
.to_return_json(body: { "items": ["one", "two", "three"] })
5+
stub_fetch_all("https://#{host}/xrpc/com.example.service.fetchAll", [
6+
{ "items": ["one", "two", "three"] }
7+
])
78
end
89

910
it 'should make one request to the given endpoint' do
1011
subject.fetch_all('com.example.service.fetchAll', field: 'items')
11-
12-
WebMock.should have_requested(:get, "https://#{host}/xrpc/com.example.service.fetchAll").once
12+
verify_fetch_all
1313
end
1414

1515
it 'should return the parsed items' do
@@ -20,18 +20,15 @@
2020

2121
context 'when more than one page of items is returned' do
2222
before do
23-
stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll")
24-
.to_return_json(body: { "items": ["one", "two", "three"], "cursor": "ccc111" })
25-
26-
stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll?cursor=ccc111")
27-
.to_return_json(body: { "items": ["four", "five"] })
23+
stub_fetch_all("https://#{host}/xrpc/com.example.service.fetchAll", [
24+
{ "items": ["one", "two", "three"] },
25+
{ "items": ["four", "five"] },
26+
])
2827
end
2928

3029
it 'should make multiple requests, passing the last cursor' do
3130
subject.fetch_all('com.example.service.fetchAll', field: 'items')
32-
33-
WebMock.should have_requested(:get, "https://#{host}/xrpc/com.example.service.fetchAll").once
34-
WebMock.should have_requested(:get, "https://#{host}/xrpc/com.example.service.fetchAll?cursor=ccc111").once
31+
verify_fetch_all
3532
end
3633

3734
it 'should return all the parsed items collected from the responses' do
@@ -42,37 +39,29 @@
4239

4340
context 'when params are passed' do
4441
before do
45-
stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll?type=post")
46-
.to_return_json(body: { "items": ["one", "two", "three"], "cursor": "ccc222" })
47-
48-
stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll?type=post&cursor=ccc222")
49-
.to_return_json(body: { "items": ["four", "five"] })
42+
stub_fetch_all("https://#{host}/xrpc/com.example.service.fetchAll?type=post", [
43+
{ "items": ["one", "two", "three"] },
44+
{ "items": ["four", "five"] },
45+
])
5046
end
5147

5248
it 'should add the params to the url' do
5349
subject.fetch_all('com.example.service.fetchAll', { type: 'post' }, field: 'items')
54-
55-
WebMock.should have_requested(:get,
56-
"https://#{host}/xrpc/com.example.service.fetchAll?type=post").once
57-
WebMock.should have_requested(:get,
58-
"https://#{host}/xrpc/com.example.service.fetchAll?type=post&cursor=ccc222").once
50+
verify_fetch_all
5951
end
6052
end
6153

6254
context 'when params are an explicit nil' do
6355
before do
64-
stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll")
65-
.to_return_json(body: { "items": ["one", "two", "three"], "cursor": "ccc222" })
66-
67-
stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll?cursor=ccc222")
68-
.to_return_json(body: { "items": ["four", "five"] })
56+
stub_fetch_all("https://#{host}/xrpc/com.example.service.fetchAll", [
57+
{ "items": ["one", "two", "three"] },
58+
{ "items": ["four", "five"] },
59+
])
6960
end
7061

7162
it 'should not add anything to the url' do
7263
subject.fetch_all('com.example.service.fetchAll', nil, field: 'items')
73-
74-
WebMock.should have_requested(:get, "https://#{host}/xrpc/com.example.service.fetchAll").once
75-
WebMock.should have_requested(:get, "https://#{host}/xrpc/com.example.service.fetchAll?cursor=ccc222").once
64+
verify_fetch_all
7665
end
7766
end
7867

@@ -97,22 +86,19 @@
9786

9887
context 'when break condition is passed' do
9988
before do
100-
stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll")
101-
.to_return_json(body: { "items": ["one", "two", "three"], "cursor": "page1" })
102-
103-
stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll?cursor=page1")
104-
.to_return_json(body: { "items": ["four", "five"], "cursor": "page2" })
105-
106-
stub_request(:get, "https://#{host}/xrpc/com.example.service.fetchAll?cursor=page2")
107-
.to_return_json(body: { "items": ["six"] })
89+
stub_fetch_all("https://#{host}/xrpc/com.example.service.fetchAll", [
90+
{ "items": ["one", "two", "three"] },
91+
{ "items": ["four", "five"] },
92+
{ "items": ["six"] },
93+
])
10894
end
10995

11096
it 'should stop when a matching item is found' do
11197
subject.fetch_all('com.example.service.fetchAll', field: 'items', break_when: ->(x) { x =~ /u/ })
11298

113-
WebMock.should have_requested(:get, "https://#{host}/xrpc/com.example.service.fetchAll").once
114-
WebMock.should have_requested(:get, "https://#{host}/xrpc/com.example.service.fetchAll?cursor=page1").once
115-
WebMock.should_not have_requested(:get, "https://#{host}/xrpc/com.example.service.fetchAll?cursor=page2")
99+
WebMock.should have_requested(:get, @stubbed_urls[0]).once
100+
WebMock.should have_requested(:get, @stubbed_urls[1]).once
101+
WebMock.should_not have_requested(:get, @stubbed_urls[2])
116102
end
117103

118104
it 'should filter out matching items from the response' do

spec/spec_helper.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,33 @@ def format(result)
4545
'/xrpc/com.atproto.repo.getRecords',
4646
'app.bsky.feed.under_score'
4747
]
48+
49+
def stub_fetch_all(base_url, responses)
50+
cursor = nil
51+
urls = []
52+
53+
responses.each_with_index do |r, i|
54+
url = base_url
55+
body = r
56+
57+
if cursor
58+
url += (url.include?('?') ? '&' : '?') + "cursor=#{cursor}"
59+
end
60+
61+
if i < responses.length - 1
62+
cursor = rand.to_s
63+
body = body.merge("cursor" => cursor)
64+
end
65+
66+
stub_request(:get, url).to_return_json(body: body)
67+
urls << url
68+
end
69+
70+
@stubbed_urls = urls
71+
end
72+
73+
def verify_fetch_all
74+
@stubbed_urls.each do |url|
75+
WebMock.should have_requested(:get, url).once
76+
end
77+
end

0 commit comments

Comments
 (0)