Skip to content

Commit

Permalink
Resolve duplicate tests from diverged tests files
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Sep 26, 2016
1 parent baa4716 commit 35f38dc
Showing 1 changed file with 50 additions and 96 deletions.
146 changes: 50 additions & 96 deletions test/adapter/json_api/relationship_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,44 @@ module Adapter
class JsonApi
class RelationshipTest < ActiveSupport::TestCase
setup do
@blog = Blog.new(id: 1)
@author = Author.new(id: 1, name: 'Steve K.', blog: @blog)
@serializer = BlogSerializer.new(@blog)
ActionController::Base.cache_store.clear
end

def test_relationship_with_data
blog = Blog.new(id: 1)
author = Author.new(id: 1, name: 'Steve K.', blog: blog)
serializer = BlogSerializer.new(blog)
expected = {
data: {
id: '1',
type: 'blogs'
}
}
assert_relationship(expected, options: { include_data: true })
test_options = { options: { include_data: true }, author: author, serializer: serializer }
assert_relationship(expected, test_options)
end

def test_relationship_with_nil_model
@serializer = BlogSerializer.new(nil)
serializer = BlogSerializer.new(nil)
expected = { data: nil }
assert_relationship(expected, options: { include_data: true })
test_options = { options: { include_data: true }, serializer: serializer }
assert_relationship(expected, test_options)
end

def test_relationship_with_nil_serializer
@serializer = nil
serializer = nil
expected = { data: nil }
assert_relationship(expected, options: { include_data: true })
test_options = { options: { include_data: true }, serializer: serializer }
assert_relationship(expected, test_options)
end

def test_relationship_with_data_array
posts = [Post.new(id: 1), Post.new(id: 2)]
@serializer = ActiveModel::Serializer::CollectionSerializer.new(posts)
@author.posts = posts
@author.blog = nil
blog = Blog.new(id: 1)
author = Author.new(id: 1, name: 'Steve K.', blog: blog)
serializer = ActiveModel::Serializer::CollectionSerializer.new(posts)
author.posts = posts
author.blog = nil
expected = {
data: [
{
Expand All @@ -50,20 +55,17 @@ def test_relationship_with_data_array
}
]
}
assert_relationship(expected, options: { include_data: true })
test_options = { options: { include_data: true }, author: author, serializer: serializer }
assert_relationship(expected, test_options)
end

def test_relationship_data_not_included
expected = {meta: {}}
assert_relationship(expected, options: { include_data: false })
expected = { meta: {} }
test_options = { options: { include_data: false } }
assert_relationship(expected, test_options)
end

def test_relationship_simple_link
links = { self: 'a link' }
assert_relationship({ links: { self: 'a link' } }, links: links)
end

def test_take2_relationship_simple_link
expected = {
data: {
id: '1337',
Expand Down Expand Up @@ -96,16 +98,11 @@ def test_relationship_many_links
related: 'another link'
}
}
assert_relationship(expected, links: links)
test_options = { links: links }
assert_relationship(expected, test_options)
end

def test_relationship_block_link
links = { self: proc { object.id.to_s } }
expected = { links: { self: @blog.id.to_s } }
assert_relationship(expected, links: links)
end

def test_take2_relationship_block_link
expected = {
data: { id: '1337', type: 'profiles' },
links: { related: '//example.com/profiles/1337' }
Expand All @@ -132,66 +129,45 @@ def test_relationship_block_link_with_meta
meta(id: object.id)
end
}
blog = Blog.new(id: 1)
author = Author.new(id: 1, name: 'Steve K.', blog: blog)
expected = {
links: {
self: {
href: @blog.id.to_s,
meta: { id: @blog.id }
href: blog.id.to_s,
meta: { id: blog.id }
}
}
}
assert_relationship(expected, links: links)
serializer = BlogSerializer.new(blog)
test_options = { links: links, author: author, serializer: serializer }
assert_relationship(expected, test_options)
end

def test_relationship_simple_meta
meta = { id: '1' }
expected = { meta: meta }
assert_relationship(expected, meta: meta)
test_options = { meta: meta }
assert_relationship(expected, test_options)
end

def test_relationship_block_meta
meta = proc do
{ id: object.id }
end
blog = Blog.new(id: 1)
author = Author.new(id: 1, name: 'Steve K.', blog: blog)
serializer = BlogSerializer.new(blog)
expected = {
meta: {
id: @blog.id
id: blog.id
}
}
assert_relationship(expected, meta: meta)
test_options = { meta: meta, author: author, serializer: serializer }
assert_relationship(expected, test_options)
end

def test_relationship_with_everything
links = {
self: 'a link',
related: proc do
href object.id.to_s
meta object.id
end

}
meta = proc do
{ id: object.id }
end
expected = {
data: {
id: '1',
type: 'blogs'
},
links: {
self: 'a link',
related: {
href: '1', meta: 1
}
},
meta: {
id: @blog.id
}
}
assert_relationship(expected, meta: meta, options: { include_data: true }, links: links)
end

def test_take2_relationship_with_everything
expected = {
data: [{ id: '1337', type: 'likes' }],
links: {
Expand Down Expand Up @@ -219,8 +195,7 @@ def test_take2_relationship_with_everything
assert_equal(expected, actual)
end

### BEGIN NEW TESTS TO DISAMBIGUATE
def test_take2_relationship_nil_link
def test_relationship_nil_link
expected = {
data: { id: '123', type: 'profiles' }
}
Expand All @@ -239,7 +214,7 @@ def test_take2_relationship_nil_link
assert_equal(expected, actual)
end

def test_take2_relationship_block_link_href
def test_relationship_block_link_href
expected = {
data: [{ id: '1337', type: 'locations' }],
links: {
Expand All @@ -261,7 +236,7 @@ def test_take2_relationship_block_link_href
assert_equal(expected, actual)
end

def test_take2_relationship_block_link_href_and_meta
def test_relationship_block_link_href_and_meta
expected = {
data: [{ id: '1337', type: 'posts' }],
links: {
Expand All @@ -287,7 +262,7 @@ def test_take2_relationship_block_link_href_and_meta
assert_equal(expected, actual)
end

def test_take2_relationship_block_link_meta
def test_relationship_block_link_meta
expected = {
data: [{ id: '1337', type: 'comments' }],
links: {
Expand All @@ -310,7 +285,7 @@ def test_take2_relationship_block_link_meta
assert_equal(expected, actual)
end

def test_take2_relationship_meta
def test_relationship_meta
expected = {
data: [{ id: 'from-serializer-method', type: 'roles' }],
meta: { count: 1 }
Expand All @@ -333,7 +308,7 @@ def cached_roles
assert_equal(expected, actual)
end

def test_take2_relationship_not_including_data
def test_relationship_not_including_data
expected = {
links: { self: '//example.com/link_author/relationships/blog' }
}
Expand All @@ -356,7 +331,7 @@ def test_take2_relationship_not_including_data
end
end

def test_take2_relationship_including_data_explicit
def test_relationship_including_data_explicit
expected = {
data: { id: '1337', type: 'authors' },
meta: { name: 'Dan Brown' }
Expand All @@ -373,19 +348,16 @@ def test_take2_relationship_including_data_explicit
end
assert_equal(expected, actual)
end
### END NEW TESTS TO DISAMBIGUATE

private

def assert_relationship(expected, test_options = {})
parent_serializer = AuthorSerializer.new(@author)

serializable_resource_options = {} # adapter.instance_options

options = test_options.delete(:options) || {}
options[:links] = test_options.delete(:links)
options[:meta] = test_options.delete(:meta)
association_serializer = @serializer
author = test_options.delete(:author)
association_serializer = test_options.delete(:serializer)

if association_serializer && association_serializer.object
association_name = association_serializer.json_key.to_sym
options[:serializer] = association_serializer
Expand All @@ -395,6 +367,8 @@ def assert_relationship(expected, test_options = {})
association = ::ActiveModel::Serializer::Association.new(:association_name_not_used, options, nil)
end

serializable_resource_options = {} # adapter.instance_options
parent_serializer = AuthorSerializer.new(author)
relationship = Relationship.new(parent_serializer, serializable_resource_options, association)
assert_equal(expected, relationship.as_json)
end
Expand All @@ -406,26 +380,6 @@ def build_serializer_and_serialize_relationship(model, relationship_name, &block
end

def new_model(model_attributes)
post = Post.new(id: 1337, comments: [], author: nil)
bio = Bio.new(id: 1337)
like = Like.new(id: 1337)
role = Role.new(id: 'from-record')
profile = Profile.new(id: 1337)
location = Location.new(id: 1337)
reviewer = Author.new(id: 1337)
comment = Comment.new(id: 1337)
default_model_attributes = {
id: 1337,
posts: [post],
reviewer: reviewer,
bio: bio,
likes: [like],
roles: [role],
locations: [location],
profile: profile,
comments: [comment]
}
model_attributes.reverse_merge!(default_model_attributes)
Class.new(ActiveModelSerializers::Model) do
attr_accessor(*model_attributes.keys)

Expand Down

0 comments on commit 35f38dc

Please sign in to comment.