forked from justinweiss/reactive_resource
/
base_test.rb
135 lines (110 loc) · 6.14 KB
/
base_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
require 'test_helper'
class ReactiveResource::BaseTest < Test::Unit::TestCase
context "A resource that inherits from ReactiveResource::Base" do
setup do
@object = ReactiveResource::Lawyer.new
end
should "hit the Avvo API with the correct URL when saved" do
stub_request(:post, "https://api.avvo.com/api/1/lawyers.json")
@object.save
assert_requested(:post, "https://api.avvo.com/api/1/lawyers.json")
end
should "hit the Avvo API with the correct URL when retrieved" do
stub_request(:get, "https://api.avvo.com/api/1/lawyers/1.json").to_return(:body => {:id => '1'}.to_json)
ReactiveResource::Lawyer.find(1)
assert_requested(:get, "https://api.avvo.com/api/1/lawyers/1.json")
end
context "with a has_many relationship to another object" do
should "hit the associated object's URL with the correct parameters when requested" do
stub_request(:get, "https://api.avvo.com/api/1/lawyers/1/addresses.json")
@object.id = 1
@object.addresses
assert_requested(:get, "https://api.avvo.com/api/1/lawyers/1/addresses.json")
end
end
context "with a has_one relationship to another object" do
should "hit the associated object's URL with the correct parameters when requested" do
stub_request(:get, "https://api.avvo.com/api/1/lawyers/1/headshot.json").to_return(:body => {:headshot_url => "blah"}.to_json)
@object.id = 1
@object.headshot
assert_requested(:get, "https://api.avvo.com/api/1/lawyers/1/headshot.json")
end
end
context "with a belongs_to association and correct parameters" do
setup do
@object = ReactiveResource::Address.new(:lawyer_id => 2)
end
should "hit the Avvo API with the correct URL when saved" do
stub_request(:post, "https://api.avvo.com/api/1/lawyers/2/addresses.json")
@object.save
assert_requested(:post, "https://api.avvo.com/api/1/lawyers/2/addresses.json")
end
should "hit the Avvo API with the correct URL when retrieved" do
stub_request(:get, "https://api.avvo.com/api/1/lawyers/2/addresses/3.json").to_return(:body => {:id => '3'}.to_json)
ReactiveResource::Address.find(3, :params => {:lawyer_id => 2})
assert_requested(:get, "https://api.avvo.com/api/1/lawyers/2/addresses/3.json")
end
should "hit the Avvo API with the correct URL when updated" do
stub_request(:get, "https://api.avvo.com/api/1/lawyers/2/addresses/3.json").to_return(:body => {:id => '3'}.to_json)
license = ReactiveResource::Address.find(3, :params => {:lawyer_id => 2})
assert_requested(:get, "https://api.avvo.com/api/1/lawyers/2/addresses/3.json")
stub_request(:put, "https://api.avvo.com/api/1/lawyers/2/addresses/3.json")
license.save
assert_requested(:put, "https://api.avvo.com/api/1/lawyers/2/addresses/3.json")
end
should "set the prefix parameters correctly when saved" do
stub_request(:post, "https://api.avvo.com/api/1/lawyers/2/addresses.json").to_return(:body => {:id => '2'}.to_json)
@object.save
assert_requested(:post, "https://api.avvo.com/api/1/lawyers/2/addresses.json")
assert_equal "/api/1/lawyers/2/addresses/2.json", @object.send(:element_path)
end
end
context "with a belongs_to hierarchy and correct parameters" do
setup do
@object = ReactiveResource::Phone.new(:doctor_id => 2, :address_id => 3)
end
should "allow setting the prefix options after creation" do
@object = ReactiveResource::Phone.new
@object.doctor_id = 2
@object.address_id = 3
stub_request(:post, "https://api.avvo.com/api/1/doctors/2/addresses/3/phones.json")
@object.save
assert_requested(:post, "https://api.avvo.com/api/1/doctors/2/addresses/3/phones.json")
end
should "allow following +belongs_to+ associations" do
@object = ReactiveResource::Phone.new
@object.doctor_id = 2
@object.address_id = 3
assert_equal 3, @object.address_id
stub_request(:get, "https://api.avvo.com/api/1/doctors/2/addresses/3.json").to_return(:body => {:id => '3'}.to_json)
@object.address
@object.address
assert_requested(:get, "https://api.avvo.com/api/1/doctors/2/addresses/3.json", :times => 1)
end
should "hit the Avvo API with the correct URL when saved" do
stub_request(:post, "https://api.avvo.com/api/1/doctors/2/addresses/3/phones.json")
@object.save
assert_requested(:post, "https://api.avvo.com/api/1/doctors/2/addresses/3/phones.json")
end
should "hit the Avvo API with the correct URL when updated" do
stub_request(:get, "https://api.avvo.com/api/1/doctors/2/addresses/3/phones/4.json").to_return(:body => {:id => '4'}.to_json)
phone = ReactiveResource::Phone.find(4, :params => {:doctor_id => 2, :address_id => 3})
assert_requested(:get, "https://api.avvo.com/api/1/doctors/2/addresses/3/phones/4.json")
stub_request(:put, "https://api.avvo.com/api/1/doctors/2/addresses/3/phones/4.json")
phone.save
assert_requested(:put, "https://api.avvo.com/api/1/doctors/2/addresses/3/phones/4.json")
end
should "set the prefix parameters correctly when saved" do
stub_request(:post, "https://api.avvo.com/api/1/doctors/2/addresses/3/phones.json").to_return(:body => {:phone_type_id => 2, :id => 4, :phone_number=>"206-728-0588"}.to_json)
@object.save
assert_requested(:post, "https://api.avvo.com/api/1/doctors/2/addresses/3/phones.json")
assert_equal "/api/1/doctors/2/addresses/3/phones/4.json", @object.send(:element_path)
end
should "hit the Avvo API with the correct URL when retrieved" do
stub_request(:get, "https://api.avvo.com/api/1/doctors/2/addresses/3/phones/4.json").to_return(:body => {:id => '4'}.to_json)
ReactiveResource::Phone.find(4, :params => {:doctor_id => 2, :address_id => 3})
assert_requested(:get, "https://api.avvo.com/api/1/doctors/2/addresses/3/phones/4.json")
end
end
end
end