@@ -46,19 +46,124 @@ class MultiWordParent < Formatted
4646
4747class MultiWordChild < Formatted
4848 belongs_to :multi_word_parent
49+ self . read_only_attributes = read_only_attributes + [ :multi_word_parent_id ]
50+
51+ def self . key_formatter
52+ JsonApiClient ::DasherizedKeyFormatter
53+ end
54+
55+ def self . route_formatter
56+ JsonApiClient ::UnderscoredKeyFormatter
57+ end
58+ end
59+
60+ class Account < TestResource
61+ property :name
62+ property :is_active , default : true
63+ property :balance
64+ end
65+
66+ class UserAccount < TestResource
67+ self . add_defaults_to_changes = true
68+ property :name
69+ property :is_active , default : true
70+ property :balance
4971end
5072
5173class AssociationTest < MiniTest ::Test
5274
75+ def test_default_properties_no_changes
76+ stub_request ( :post , 'http://example.com/accounts' ) .
77+ with ( headers : { content_type : 'application/vnd.api+json' , accept : 'application/vnd.api+json' } , body : {
78+ data : {
79+ type : 'accounts' ,
80+ attributes : {
81+ name : 'foo'
82+ }
83+ }
84+ } . to_json )
85+ . to_return ( headers : { content_type : 'application/vnd.api+json' } , body : {
86+ data : {
87+ id : '1' ,
88+ type : 'accounts' ,
89+ attributes : {
90+ name : 'foo' ,
91+ is_active : false ,
92+ balance : '0.0'
93+ }
94+ }
95+ } . to_json )
96+ record = Account . new ( name : 'foo' )
97+ assert record . save
98+ assert_equal ( false , record . is_active )
99+ assert_equal ( '0.0' , record . balance )
100+ end
101+
102+ def test_default_properties_changes
103+ stub_request ( :post , 'http://example.com/user_accounts' ) .
104+ with ( headers : { content_type : 'application/vnd.api+json' , accept : 'application/vnd.api+json' } , body : {
105+ data : {
106+ type : 'user_accounts' ,
107+ attributes : {
108+ name : 'foo' ,
109+ is_active : true
110+ }
111+ }
112+ } . to_json )
113+ . to_return ( headers : { content_type : 'application/vnd.api+json' } , body : {
114+ data : {
115+ id : '1' ,
116+ type : 'user_accounts' ,
117+ attributes : {
118+ name : 'foo' ,
119+ is_active : true ,
120+ balance : '0.0'
121+ }
122+ }
123+ } . to_json )
124+ record = UserAccount . new ( name : 'foo' )
125+ assert record . save
126+ assert_equal ( true , record . is_active )
127+ assert_equal ( '0.0' , record . balance )
128+ end
129+
53130 def test_belongs_to_urls_are_formatted
54- request = stub_request ( :get , "http://example.com/multi-word-parents /1/multi-word-children " )
131+ request = stub_request ( :get , "http://example.com/multi_word_parents /1/multi_word_children " )
55132 . to_return ( headers : { content_type : "application/vnd.api+json" } , body : { data : [ ] } . to_json )
56133
57134 MultiWordChild . where ( multi_word_parent_id : 1 ) . to_a
58135
59136 assert_requested ( request )
60137 end
61138
139+ def test_belongs_to_urls_create_record
140+ stub_request ( :post , 'http://example.com/multi_word_parents/1/multi_word_children' ) .
141+ with ( headers : { content_type : 'application/vnd.api+json' , accept : 'application/vnd.api+json' } , body : {
142+ data : {
143+ type : 'multi_word_children' ,
144+ attributes : {
145+ foo : 'bar' ,
146+ 'multi-word-field' : true
147+ }
148+ }
149+ } . to_json )
150+ . to_return ( headers : { content_type : 'application/vnd.api+json' } , body : {
151+ data : {
152+ id : '2' ,
153+ type : 'multi_word_children' ,
154+ attributes : {
155+ foo : 'bar' ,
156+ 'multi-word-field' : true
157+ }
158+ }
159+ } . to_json )
160+
161+ record = MultiWordChild . new ( multi_word_parent_id : 1 , foo : 'bar' , multi_word_field : true )
162+ result = record . save
163+ assert result
164+ assert_equal ( '2' , record . id )
165+ end
166+
62167 def test_load_has_one
63168 stub_request ( :get , "http://example.com/properties/1" )
64169 . to_return ( headers : { content_type : "application/vnd.api+json" } , body : {
0 commit comments