Skip to content

Commit ce8963c

Browse files
committed
290 fix passing relationships on create
also fix false positive tests
1 parent 36d750b commit ce8963c

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

lib/json_api_client/relationships/relations.rb

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class Relations
1111
def initialize(record_class, relations)
1212
@record_class = record_class
1313
self.attributes = relations
14-
clear_changes_information
1514
end
1615

1716
def present?

lib/json_api_client/resource.rb

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def load(params)
9898
new(params).tap do |resource|
9999
resource.mark_as_persisted!
100100
resource.clear_changes_information
101+
resource.relationships.clear_changes_information
101102
end
102103
end
103104

test/unit/creation_test.rb

+12-8
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ def after_create_method
1818
class Author < TestResource
1919
end
2020

21-
def setup
22-
super
21+
def stub_simple_creation
2322
stub_request(:post, "http://example.com/articles")
2423
.with(headers: {content_type: "application/vnd.api+json", accept: "application/vnd.api+json"}, body: {
2524
data: {
@@ -47,6 +46,7 @@ def setup
4746
end
4847

4948
def test_can_create_with_class_method
49+
stub_simple_creation
5050
article = Article.create({
5151
title: "Rails is Omakase"
5252
})
@@ -57,6 +57,7 @@ def test_can_create_with_class_method
5757
end
5858

5959
def test_changed_attributes_empty_after_create_with_class_method
60+
stub_simple_creation
6061
article = Article.create({
6162
title: "Rails is Omakase"
6263
})
@@ -65,6 +66,7 @@ def test_changed_attributes_empty_after_create_with_class_method
6566
end
6667

6768
def test_can_create_with_new_record_and_save
69+
stub_simple_creation
6870
article = Article.new({
6971
title: "Rails is Omakase"
7072
})
@@ -139,6 +141,7 @@ def test_can_create_with_includes_and_fields
139141
end
140142

141143
def test_can_create_with_links
144+
stub_simple_creation
142145
article = Article.new({
143146
title: "Rails is Omakase"
144147
})
@@ -234,6 +237,7 @@ def test_correct_create_with_nil_attribute_value
234237
end
235238

236239
def test_changed_attributes_empty_after_create_with_new_record_and_save
240+
stub_simple_creation
237241
article = Article.new({title: "Rails is Omakase"})
238242

239243
article.save
@@ -278,18 +282,18 @@ def test_create_with_relationships_in_payload
278282
.with(headers: {content_type: 'application/vnd.api+json', accept: 'application/vnd.api+json'}, body: {
279283
data: {
280284
type: 'articles',
281-
attributes: {
282-
title: 'Rails is Omakase'
283-
},
284285
relationships: {
285286
comments: {
286287
data: [
287288
{
288-
id: '2',
289-
type: 'comments'
289+
type: 'comments',
290+
id: '2'
290291
}
291292
]
292293
}
294+
},
295+
attributes: {
296+
title: 'Rails is Omakase'
293297
}
294298
}
295299
}.to_json)
@@ -303,7 +307,7 @@ def test_create_with_relationships_in_payload
303307
}
304308
}.to_json)
305309

306-
article = Article.new(title: 'Rails is Omakase', relationships: {comments: [Comment.new(id: 2)]})
310+
article = Article.new(title: 'Rails is Omakase', relationships: {comments: [Comment.new(id: '2')]})
307311

308312
assert article.save
309313
assert article.persisted?

test/unit/updating_test.rb

+13-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ def after_save_method
2323
end
2424
end
2525

26-
def setup
27-
super
26+
def stub_simple_fetch
2827
stub_request(:get, "http://example.com/articles/1")
2928
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
3029
data: {
@@ -38,6 +37,7 @@ def setup
3837
end
3938

4039
def test_can_update_found_record
40+
stub_simple_fetch
4141
articles = Article.find(1)
4242
article = articles.first
4343

@@ -69,6 +69,7 @@ def test_can_update_found_record
6969
end
7070

7171
def test_changed_attributes_blank_after_update
72+
stub_simple_fetch
7273
articles = Article.find(1)
7374
article = articles.first
7475

@@ -109,6 +110,7 @@ def test_changed_attributes_blank_after_update
109110
end
110111

111112
def test_can_update_found_record_in_bulk
113+
stub_simple_fetch
112114
articles = Article.find(1)
113115
article = articles.first
114116

@@ -141,6 +143,7 @@ def test_can_update_found_record_in_bulk
141143
end
142144

143145
def test_can_update_found_record_in_builk_using_update_method
146+
stub_simple_fetch
144147
articles = Article.find(1)
145148
article = articles.first
146149

@@ -173,6 +176,7 @@ def test_can_update_found_record_in_builk_using_update_method
173176
end
174177

175178
def test_can_update_single_relationship
179+
stub_simple_fetch
176180
articles = Article.find(1)
177181
article = articles.first
178182

@@ -216,6 +220,7 @@ def test_can_update_single_relationship
216220
end
217221

218222
def test_can_update_single_relationship_via_setter
223+
stub_simple_fetch
219224
articles = Article.find(1)
220225
article = articles.first
221226

@@ -259,6 +264,7 @@ def test_can_update_single_relationship_via_setter
259264
end
260265

261266
def test_can_update_single_relationship_with_all_attributes_dirty
267+
stub_simple_fetch
262268
articles = Article.find(1)
263269
article = articles.first
264270

@@ -311,6 +317,7 @@ def test_can_update_single_relationship_with_all_attributes_dirty
311317
end
312318

313319
def test_can_update_has_many_relationships
320+
stub_simple_fetch
314321
articles = Article.find(1)
315322
article = articles.first
316323

@@ -360,6 +367,7 @@ def test_can_update_has_many_relationships
360367
end
361368

362369
def test_can_update_has_many_relationships_via_setter
370+
stub_simple_fetch
363371
articles = Article.find(1)
364372
article = articles.first
365373

@@ -409,6 +417,7 @@ def test_can_update_has_many_relationships_via_setter
409417
end
410418

411419
def test_can_update_has_many_relationships_with_all_attributes_dirty
420+
stub_simple_fetch
412421
articles = Article.find(1)
413422
article = articles.first
414423

@@ -757,6 +766,7 @@ def test_callbacks_on_update
757766
end
758767

759768
def test_can_update_with_includes_and_fields
769+
stub_simple_fetch
760770
stub_request(:patch, "http://example.com/articles/1")
761771
.with(
762772
headers: {content_type: "application/vnd.api+json", accept: "application/vnd.api+json"},
@@ -844,6 +854,7 @@ def test_can_update_with_includes_and_fields
844854
end
845855

846856
def test_can_update_with_includes_and_fields_with_keep_request_params
857+
stub_simple_fetch
847858
stub_request(:patch, "http://example.com/articles/1")
848859
.with(
849860
headers: {content_type: "application/vnd.api+json", accept: "application/vnd.api+json"},

0 commit comments

Comments
 (0)