55require "fixtures/person"
66require "fixtures/beast"
77require "fixtures/customer"
8+ require "fixtures/weather"
89
910
1011class AssociationTest < ActiveSupport ::TestCase
1112 def setup
1213 @klass = ActiveResource ::Associations ::Builder ::Association
1314 @reflection = ActiveResource ::Reflection ::AssociationReflection . new :belongs_to , :customer , { }
15+ @previous_reflections , External ::Person . reflections = External ::Person . reflections , { }
1416 end
1517
18+ def teardown
19+ External ::Person . reflections = @previous_reflections
20+ end
1621
1722 def test_validations_for_instance
1823 object = @klass . new ( Person , :customers , { } )
@@ -48,6 +53,28 @@ def test_has_many
4853 assert_equal [ "Related" ] , people . map ( &:name )
4954 end
5055
56+ def test_has_many_with_primary_key
57+ External ::Person . has_many ( :people , primary_key : :parent_id )
58+
59+ ActiveResource ::HttpMock . respond_to . get "/people.json?person_id=1" , { } , { people : [ { id : 2 , name : "Related" } ] } . to_json
60+ person = External ::Person . new ( { parent_id : 1 } , true )
61+
62+ people = person . people
63+
64+ assert_equal [ "Related" ] , people . map ( &:name )
65+ end
66+
67+ def test_has_many_with_foreign_key
68+ External ::Person . has_many ( :people , foreign_key : :parent_id )
69+
70+ ActiveResource ::HttpMock . respond_to . get "/people.json?parent_id=1" , { } , { people : [ { id : 2 , name : "Related" } ] } . to_json
71+ person = External ::Person . new ( { id : 1 } , true )
72+
73+ people = person . people
74+
75+ assert_equal [ "Related" ] , people . map ( &:name )
76+ end
77+
5178 def test_has_many_chain
5279 External ::Person . send ( :has_many , :people )
5380
@@ -68,6 +95,62 @@ def test_has_many_on_new_record
6895 def test_has_one
6996 External ::Person . send ( :has_one , :customer )
7097 assert_equal 1 , External ::Person . reflections . select { |name , reflection | reflection . macro . eql? ( :has_one ) } . count
98+
99+ ActiveResource ::HttpMock . respond_to . get "/people/1/customer.json" , { } , { person : { id : 2 , name : "Customer" } } . to_json
100+ person = External ::Person . new ( { id : 1 } , true )
101+
102+ customer = person . customer
103+
104+ assert_equal "Customer" , customer . name
105+ end
106+
107+ def test_has_one_singleton
108+ External ::Person . send ( :has_one , :weather )
109+
110+ ActiveResource ::HttpMock . respond_to . get "/weather.json?person_id=1" , { } , { weather : { id : 1 , status : "Sunshine" } } . to_json
111+ person = External ::Person . new ( { id : 1 } , true )
112+
113+ weather = person . weather
114+
115+ assert_equal "Sunshine" , weather . status
116+ end
117+
118+ def test_has_one_with_primary_key
119+ External ::Person . send ( :has_one , :customer , primary_key : :customer_id )
120+
121+ ActiveResource ::HttpMock . respond_to . get "/people/1/customer.json" , { } , { person : { id : 2 , name : "Customer" } } . to_json
122+ person = External ::Person . new ( { customer_id : 1 } , true )
123+
124+ customer = person . customer
125+
126+ assert_equal "Customer" , customer . name
127+ end
128+
129+ def test_has_one_singleton_with_primary_key
130+ External ::Person . send ( :has_one , :weather , primary_key : :person_id )
131+
132+ ActiveResource ::HttpMock . respond_to . get "/weather.json?person_id=1" , { } , { weather : { id : 1 , status : "Sunshine" } } . to_json
133+ person = External ::Person . new ( { person_id : 1 } , true )
134+
135+ weather = person . weather
136+
137+ assert_equal "Sunshine" , weather . status
138+ end
139+
140+ def test_has_one_singleton_with_foreign_key
141+ previous_prefix = Weather . prefix
142+ Weather . prefix = "/people/:owner_id/"
143+
144+ External ::Person . send ( :has_one , :weather , foreign_key : :owner_id )
145+
146+ ActiveResource ::HttpMock . respond_to . get "/people/1/weather.json" , { } , { weather : { id : 1 , status : "Sunshine" } } . to_json
147+ person = External ::Person . new ( { id : 1 } , true )
148+
149+ weather = person . weather
150+
151+ assert_equal "Sunshine" , weather . status
152+ ensure
153+ Weather . prefix = previous_prefix
71154 end
72155
73156 def test_belongs_to
0 commit comments