Skip to content

Commit

Permalink
Merge pull request rails-api#1902 from bf4/fix_intermittent_relations…
Browse files Browse the repository at this point in the history
…hip_test_failures

Fix intermittent test failures by not mutating global state
  • Loading branch information
bf4 authored Sep 1, 2016
2 parents d34069b + 1d4cd6f commit 1dc2b74
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions test/adapter/json_api/relationships_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,29 @@ def test_relationship_simple_link
self: '//example.com/link_author/relationships/bio'
}
}
assert_relationship(:bio, expected)

author = @author.dup
assert_author_relationship_serialized(expected, author, :bio)
end

def test_relationship_block_link
expected = {
data: { id: '1337', type: 'profiles' },
links: { related: '//example.com/profiles/1337' }
}
assert_relationship(:profile, expected)

author = @author.dup
assert_author_relationship_serialized(expected, author, :profile)
end

def test_relationship_nil_link
@author.profile.id = 123
expected = {
data: { id: '123', type: 'profiles' }
}
assert_relationship(:profile, expected)

author = @author.dup
author.profile.id = 123
assert_author_relationship_serialized(expected, author, :profile)
end

def test_relationship_block_link_href
Expand All @@ -129,7 +135,9 @@ def test_relationship_block_link_href
related: { href: '//example.com/locations/1337' }
}
}
assert_relationship(:locations, expected)

author = @author.dup
assert_author_relationship_serialized(expected, author, :locations)
end

def test_relationship_block_link_href_and_meta
Expand All @@ -142,7 +150,9 @@ def test_relationship_block_link_href_and_meta
}
}
}
assert_relationship(:posts, expected)

author = @author.dup
assert_author_relationship_serialized(expected, author, :posts)
end

def test_relationship_block_link_meta
Expand All @@ -154,27 +164,33 @@ def test_relationship_block_link_meta
}
}
}
assert_relationship(:comments, expected)

author = @author.dup
assert_author_relationship_serialized(expected, author, :comments)
end

def test_relationship_meta
expected = {
data: [{ id: 'from-serializer-method', type: 'roles' }],
meta: { count: 1 }
}
assert_relationship(:roles, expected)

author = @author.dup
assert_author_relationship_serialized(expected, author, :roles)
end

def test_relationship_not_including_data
@author.define_singleton_method(:read_attribute_for_serialization) do |attr|
fail 'should not be called' if attr == :blog
super(attr)
end
expected = {
links: { self: '//example.com/link_author/relationships/blog' }
}

author = @author.dup
author.define_singleton_method(:read_attribute_for_serialization) do |attr|
fail 'should not be called' if attr == :blog
super(attr)
end
assert_nothing_raised do
assert_relationship(:blog, expected)
assert_author_relationship_serialized(expected, author, :blog)
end
end

Expand All @@ -183,7 +199,9 @@ def test_relationship_including_data_explicit
data: { id: '1337', type: 'authors' },
meta: { name: 'Dan Brown' }
}
assert_relationship(:reviewer, expected)

author = @author.dup
assert_author_relationship_serialized(expected, author, :reviewer)
end

def test_relationship_with_everything
Expand All @@ -197,14 +215,17 @@ def test_relationship_with_everything
},
meta: { liked: true }
}
assert_relationship(:likes, expected)

author = @author.dup
assert_author_relationship_serialized(expected, author, :likes)
end

private

def assert_relationship(relationship_name, expected)
hash = serializable(@author, adapter: :json_api).serializable_hash
assert_equal(expected, hash[:data][:relationships][relationship_name])
def assert_author_relationship_serialized(expected, author, relationship_name)
hash = serializable(author, adapter: :json_api).serializable_hash
actual_relationship = hash[:data][:relationships][relationship_name]
assert_equal(expected, actual_relationship)
end
end
end
Expand Down

0 comments on commit 1dc2b74

Please sign in to comment.