From ce8963ced680eb55131057189c5bcc2e70f8c2d9 Mon Sep 17 00:00:00 2001 From: Denis Talakevich Date: Thu, 8 Nov 2018 17:59:04 +0200 Subject: [PATCH] 290 fix passing relationships on create also fix false positive tests --- .../relationships/relations.rb | 1 - lib/json_api_client/resource.rb | 1 + test/unit/creation_test.rb | 20 +++++++++++-------- test/unit/updating_test.rb | 15 ++++++++++++-- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/json_api_client/relationships/relations.rb b/lib/json_api_client/relationships/relations.rb index 9d14a937..a51033d2 100644 --- a/lib/json_api_client/relationships/relations.rb +++ b/lib/json_api_client/relationships/relations.rb @@ -11,7 +11,6 @@ class Relations def initialize(record_class, relations) @record_class = record_class self.attributes = relations - clear_changes_information end def present? diff --git a/lib/json_api_client/resource.rb b/lib/json_api_client/resource.rb index ea5b0d8a..4ec14576 100644 --- a/lib/json_api_client/resource.rb +++ b/lib/json_api_client/resource.rb @@ -98,6 +98,7 @@ def load(params) new(params).tap do |resource| resource.mark_as_persisted! resource.clear_changes_information + resource.relationships.clear_changes_information end end diff --git a/test/unit/creation_test.rb b/test/unit/creation_test.rb index abcf78cd..9932469f 100644 --- a/test/unit/creation_test.rb +++ b/test/unit/creation_test.rb @@ -18,8 +18,7 @@ def after_create_method class Author < TestResource end - def setup - super + def stub_simple_creation stub_request(:post, "http://example.com/articles") .with(headers: {content_type: "application/vnd.api+json", accept: "application/vnd.api+json"}, body: { data: { @@ -47,6 +46,7 @@ def setup end def test_can_create_with_class_method + stub_simple_creation article = Article.create({ title: "Rails is Omakase" }) @@ -57,6 +57,7 @@ def test_can_create_with_class_method end def test_changed_attributes_empty_after_create_with_class_method + stub_simple_creation article = Article.create({ title: "Rails is Omakase" }) @@ -65,6 +66,7 @@ def test_changed_attributes_empty_after_create_with_class_method end def test_can_create_with_new_record_and_save + stub_simple_creation article = Article.new({ title: "Rails is Omakase" }) @@ -139,6 +141,7 @@ def test_can_create_with_includes_and_fields end def test_can_create_with_links + stub_simple_creation article = Article.new({ title: "Rails is Omakase" }) @@ -234,6 +237,7 @@ def test_correct_create_with_nil_attribute_value end def test_changed_attributes_empty_after_create_with_new_record_and_save + stub_simple_creation article = Article.new({title: "Rails is Omakase"}) article.save @@ -278,18 +282,18 @@ def test_create_with_relationships_in_payload .with(headers: {content_type: 'application/vnd.api+json', accept: 'application/vnd.api+json'}, body: { data: { type: 'articles', - attributes: { - title: 'Rails is Omakase' - }, relationships: { comments: { data: [ { - id: '2', - type: 'comments' + type: 'comments', + id: '2' } ] } + }, + attributes: { + title: 'Rails is Omakase' } } }.to_json) @@ -303,7 +307,7 @@ def test_create_with_relationships_in_payload } }.to_json) - article = Article.new(title: 'Rails is Omakase', relationships: {comments: [Comment.new(id: 2)]}) + article = Article.new(title: 'Rails is Omakase', relationships: {comments: [Comment.new(id: '2')]}) assert article.save assert article.persisted? diff --git a/test/unit/updating_test.rb b/test/unit/updating_test.rb index 08a4e78f..4a96f32f 100644 --- a/test/unit/updating_test.rb +++ b/test/unit/updating_test.rb @@ -23,8 +23,7 @@ def after_save_method end end - def setup - super + def stub_simple_fetch stub_request(:get, "http://example.com/articles/1") .to_return(headers: {content_type: "application/vnd.api+json"}, body: { data: { @@ -38,6 +37,7 @@ def setup end def test_can_update_found_record + stub_simple_fetch articles = Article.find(1) article = articles.first @@ -69,6 +69,7 @@ def test_can_update_found_record end def test_changed_attributes_blank_after_update + stub_simple_fetch articles = Article.find(1) article = articles.first @@ -109,6 +110,7 @@ def test_changed_attributes_blank_after_update end def test_can_update_found_record_in_bulk + stub_simple_fetch articles = Article.find(1) article = articles.first @@ -141,6 +143,7 @@ def test_can_update_found_record_in_bulk end def test_can_update_found_record_in_builk_using_update_method + stub_simple_fetch articles = Article.find(1) article = articles.first @@ -173,6 +176,7 @@ def test_can_update_found_record_in_builk_using_update_method end def test_can_update_single_relationship + stub_simple_fetch articles = Article.find(1) article = articles.first @@ -216,6 +220,7 @@ def test_can_update_single_relationship end def test_can_update_single_relationship_via_setter + stub_simple_fetch articles = Article.find(1) article = articles.first @@ -259,6 +264,7 @@ def test_can_update_single_relationship_via_setter end def test_can_update_single_relationship_with_all_attributes_dirty + stub_simple_fetch articles = Article.find(1) article = articles.first @@ -311,6 +317,7 @@ def test_can_update_single_relationship_with_all_attributes_dirty end def test_can_update_has_many_relationships + stub_simple_fetch articles = Article.find(1) article = articles.first @@ -360,6 +367,7 @@ def test_can_update_has_many_relationships end def test_can_update_has_many_relationships_via_setter + stub_simple_fetch articles = Article.find(1) article = articles.first @@ -409,6 +417,7 @@ def test_can_update_has_many_relationships_via_setter end def test_can_update_has_many_relationships_with_all_attributes_dirty + stub_simple_fetch articles = Article.find(1) article = articles.first @@ -757,6 +766,7 @@ def test_callbacks_on_update end def test_can_update_with_includes_and_fields + stub_simple_fetch stub_request(:patch, "http://example.com/articles/1") .with( headers: {content_type: "application/vnd.api+json", accept: "application/vnd.api+json"}, @@ -844,6 +854,7 @@ def test_can_update_with_includes_and_fields end def test_can_update_with_includes_and_fields_with_keep_request_params + stub_simple_fetch stub_request(:patch, "http://example.com/articles/1") .with( headers: {content_type: "application/vnd.api+json", accept: "application/vnd.api+json"},