Skip to content

Commit a09a8d4

Browse files
committed
Generate API tests
1 parent f3225b8 commit a09a8d4

File tree

1 file changed

+89
-47
lines changed

1 file changed

+89
-47
lines changed

spec/api_spec.rb

+89-47
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,143 @@
1+
# This file is auto-generated from the same code that generates
2+
# https://docs.patreon.com. Community pull requests against this
3+
# file may not be accepted.
4+
15
require 'json'
26

37
describe Patreon::API do
48
before do
59
@api = Patreon::API.new("some token")
610
end
711

8-
describe "Patreon::API#fetch_user" do
12+
describe "Patreon::API" do
913
before(:all) do
1014
@response = File.read(File.expand_path("fixtures/current_user.json", __dir__))
1115
end
1216

13-
it "should get current_user" do
14-
@api.expects(:get_json).with("current_user").returns(@response)
15-
response = @api.fetch_user
17+
it "should parse data" do
18+
@api.expects(:get_json).with("identity").returns(@response)
19+
response = @api.get_identity
1620
assert_equal response.data.pledges.count, 5
1721
assert_equal response.data.vanity, "corgi"
1822
assert_equal response.data.first_name, "Corgi"
1923
end
2024

21-
it "should get current_user and the user should be mutable" do
22-
@api.expects(:get_json).with("current_user").returns(@response)
23-
response = @api.fetch_user
24-
assert_equal response.data.pledges.count, 5
25-
assert_equal response.data.vanity, "corgi"
25+
it "should allow mutating data" do
26+
@api.expects(:get_json).with("identity").returns(@response)
27+
response = @api.get_identity
2628
assert_equal response.data.first_name, "Corgi"
2729
response.data.first_name = "Jack"
2830
assert_equal response.data.first_name, "Jack"
2931
end
3032

31-
it "should get current_user correctly if field is nil" do
33+
it "should support nill value" do
3234
munged_response = JSON.parse(@response)
3335
munged_response['data']['attributes']['first_name'] = nil
34-
@api.expects(:get_json).with("current_user").returns(munged_response.to_json)
35-
response = @api.fetch_user
36-
assert_equal response.data.pledges.count, 5
37-
assert_equal response.data.vanity, "corgi"
36+
@api.expects(:get_json).with("identity").returns(munged_response.to_json)
37+
response = @api.get_identity
3838
assert_nil response.data.first_name
3939
end
40+
end
4041

41-
it "should get current_user with includes" do
42-
@api.expects(:get_json).with("current_user?include=hello").returns(@response)
43-
@api.fetch_user(includes: "hello")
42+
describe "Patreon::API#get_campaigns" do
43+
before(:all) do
44+
@response = File.read(File.expand_path("fixtures/current_user.json", __dir__))
4445
end
4546

46-
it "should get current_user with fields" do
47-
@api.expects(:get_json).with("current_user?fields%5Bhello%5D=there").returns(@response)
48-
@api.fetch_user(fields: {hello: "there"})
47+
it "should return data" do
48+
resource_id = "32187"
49+
expected_url = "campaigns"
50+
expected_url += "?page%5Bcount%5D=10"
51+
@api.expects(:get_json).with(expected_url).returns(@response)
52+
response = @api.get_campaigns()
53+
assert_equal response.data.id, resource_id
4954
end
5055
end
5156

52-
describe "Patreon::API#fetch_campaign" do
57+
describe "Patreon::API#get_identity" do
5358
before(:all) do
54-
@response = File.read(File.expand_path("fixtures/fetch_campaign.json", __dir__))
59+
@response = File.read(File.expand_path("fixtures/current_user.json", __dir__))
5560
end
5661

57-
it "should get fetch_campaign" do
58-
@api.expects(:get_json).with("current_user/campaigns").returns(@response)
59-
response = @api.fetch_campaign
60-
assert_equal response.data.count, 1
61-
assert_equal response.data[0].creation_name, "an unforgettable high school experience"
62+
it "should return data" do
63+
resource_id = "32187"
64+
expected_url = "identity"
65+
@api.expects(:get_json).with(expected_url).returns(@response)
66+
response = @api.get_identity()
67+
assert_equal response.data.id, resource_id
6268
end
69+
end
6370

64-
it "should get fetch_campaign with includes" do
65-
@api.expects(:get_json).with("current_user/campaigns?include=hello").returns(@response)
66-
@api.fetch_campaign(includes: "hello")
71+
describe "Patreon::API#get_webhooks" do
72+
before(:all) do
73+
@response = File.read(File.expand_path("fixtures/current_user.json", __dir__))
6774
end
6875

69-
it "should get fetch_campaign with fields" do
70-
@api.expects(:get_json).with("current_user/campaigns?fields%5Bhello%5D=there").returns(@response)
71-
@api.fetch_campaign(fields: {hello: "there"})
76+
it "should return data" do
77+
resource_id = "32187"
78+
expected_url = "webhooks"
79+
expected_url += "?page%5Bcount%5D=10"
80+
@api.expects(:get_json).with(expected_url).returns(@response)
81+
response = @api.get_webhooks()
82+
assert_equal response.data.id, resource_id
7283
end
7384
end
7485

75-
describe "Patreon::API#fetch_campaign_and_patrons" do
86+
describe "Patreon::API#get_campaigns_by_id_members" do
7687
before(:all) do
77-
@response = File.read(File.expand_path("fixtures/fetch_campaign.json", __dir__))
88+
@response = File.read(File.expand_path("fixtures/current_user.json", __dir__))
7889
end
7990

80-
it "should get fetch_campaign_and_patrons" do
81-
@api.expects(:get_json).with("current_user/campaigns?include=rewards%2Ccreator%2Cgoals%2Cpledges").returns(@response)
82-
@api.fetch_campaign_and_patrons
91+
it "should return data" do
92+
resource_id = "32187"
93+
expected_url = "campaigns/{}/members".gsub('{}', resource_id)
94+
expected_url += "?page%5Bcount%5D=10"
95+
@api.expects(:get_json).with(expected_url).returns(@response)
96+
response = @api.get_campaigns_by_id_members(resource_id)
97+
assert_equal response.data.id, resource_id
8398
end
99+
end
84100

85-
it "should get fetch_campaign_and_patrons with more includes" do
86-
@api.expects(:get_json).with("current_user/campaigns?include=doohickey%2Crewards%2Ccreator%2Cgoals%2Cpledges").returns(@response)
87-
@api.fetch_campaign_and_patrons(includes: "doohickey")
101+
describe "Patreon::API#get_campaigns_by_id" do
102+
before(:all) do
103+
@response = File.read(File.expand_path("fixtures/current_user.json", __dir__))
104+
end
105+
106+
it "should return data" do
107+
resource_id = "32187"
108+
expected_url = "campaigns/{}".gsub('{}', resource_id)
109+
@api.expects(:get_json).with(expected_url).returns(@response)
110+
response = @api.get_campaigns_by_id(resource_id)
111+
assert_equal response.data.id, resource_id
88112
end
89113
end
90114

91-
describe "Patreon::API#fetch_page_of_pledges" do
115+
describe "Patreon::API#get_webhooks_by_id" do
92116
before(:all) do
93-
@response = File.read(File.expand_path("fixtures/fetch_campaign.json", __dir__))
117+
@response = File.read(File.expand_path("fixtures/current_user.json", __dir__))
94118
end
95119

96-
it "should get fetch_page_of_pledges" do
97-
@api.expects(:get_json).with("campaigns/123/pledges?page%5Bcount%5D=10").returns(@response)
98-
@api.fetch_page_of_pledges(123)
120+
it "should return data" do
121+
resource_id = "32187"
122+
expected_url = "webhooks/{}".gsub('{}', resource_id)
123+
@api.expects(:get_json).with(expected_url).returns(@response)
124+
response = @api.get_webhooks_by_id(resource_id)
125+
assert_equal response.data.id, resource_id
99126
end
100127
end
101-
end
128+
129+
describe "Patreon::API#get_members_by_id" do
130+
before(:all) do
131+
@response = File.read(File.expand_path("fixtures/current_user.json", __dir__))
132+
end
133+
134+
it "should return data" do
135+
resource_id = "32187"
136+
expected_url = "members/{}".gsub('{}', resource_id)
137+
@api.expects(:get_json).with(expected_url).returns(@response)
138+
response = @api.get_members_by_id(resource_id)
139+
assert_equal response.data.id, resource_id
140+
end
141+
end
142+
143+
end

0 commit comments

Comments
 (0)