Skip to content

Commit 82e9009

Browse files
committed
Put the similar jsonapi type tests together
1 parent b439fe6 commit 82e9009

File tree

2 files changed

+130
-140
lines changed

2 files changed

+130
-140
lines changed

test/adapter/json_api/resource_identifier_test.rb

Lines changed: 0 additions & 109 deletions
This file was deleted.

test/adapter/json_api/type_test.rb

Lines changed: 130 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,151 @@
11
require 'test_helper'
22

3-
module ActiveModel
4-
class Serializer
5-
module Adapter
6-
class JsonApi
7-
class TypeTest < ActiveSupport::TestCase
8-
class StringTypeSerializer < ActiveModel::Serializer
9-
attribute :name
10-
type 'profile'
11-
end
3+
module ActiveModelSerializers
4+
module Adapter
5+
class JsonApi
6+
class TypeTest < ActiveSupport::TestCase
7+
class StringTypeSerializer < ActiveModel::Serializer
8+
attribute :name
9+
type 'profile'
10+
end
11+
12+
class SymbolTypeSerializer < ActiveModel::Serializer
13+
attribute :name
14+
type :profile
15+
end
16+
17+
setup do
18+
@author = Author.new(id: 1, name: 'Steve K.')
19+
end
1220

13-
class SymbolTypeSerializer < ActiveModel::Serializer
14-
attribute :name
15-
type :profile
21+
def test_config_plural
22+
with_jsonapi_inflection :plural do
23+
assert_type(@author, 'authors')
1624
end
25+
end
1726

18-
setup do
19-
@author = Author.new(id: 1, name: 'Steve K.')
27+
def test_config_singular
28+
with_jsonapi_inflection :singular do
29+
assert_type(@author, 'author')
2030
end
31+
end
32+
33+
def test_explicit_string_type_value
34+
assert_type(@author, 'profile', serializer: StringTypeSerializer)
35+
end
2136

22-
def test_config_plural
23-
with_jsonapi_inflection :plural do
24-
assert_type(@author, 'authors')
25-
end
37+
def test_explicit_symbol_type_value
38+
assert_type(@author, 'profile', serializer: SymbolTypeSerializer)
39+
end
40+
41+
private
42+
43+
def assert_type(resource, expected_type, opts = {})
44+
opts = opts.reverse_merge(adapter: :json_api)
45+
hash = serializable(resource, opts).serializable_hash
46+
assert_equal(expected_type, hash.fetch(:data).fetch(:type))
47+
end
48+
end
49+
class ResourceIdentifierTest < ActiveSupport::TestCase
50+
class WithDefinedTypeSerializer < ActiveModel::Serializer
51+
type 'with_defined_type'
52+
end
53+
54+
class WithDefinedIdSerializer < ActiveModel::Serializer
55+
def id
56+
'special_id'
2657
end
58+
end
2759

28-
def test_config_singular
29-
with_jsonapi_inflection :singular do
30-
assert_type(@author, 'author')
31-
end
60+
class FragmentedSerializer < ActiveModel::Serializer
61+
cache only: :id
62+
63+
def id
64+
'special_id'
3265
end
66+
end
3367

34-
def test_explicit_string_type_value
35-
assert_type(@author, 'profile', serializer: StringTypeSerializer)
68+
setup do
69+
@model = Author.new(id: 1, name: 'Steve K.')
70+
ActionController::Base.cache_store.clear
71+
end
72+
73+
def test_defined_type
74+
actual = actual_resource_identifier_object(WithDefinedTypeSerializer)
75+
expected = { id: expected_model_id, type: 'with-defined-type' }
76+
assert_equal actual, expected
77+
end
78+
79+
def test_singular_type
80+
actual = with_jsonapi_inflection :singular do
81+
actual_resource_identifier_object(AuthorSerializer)
3682
end
83+
expected = { id: expected_model_id, type: 'author' }
84+
assert_equal actual, expected
85+
end
3786

38-
def test_explicit_symbol_type_value
39-
assert_type(@author, 'profile', serializer: SymbolTypeSerializer)
87+
def test_plural_type
88+
actual = with_jsonapi_inflection :plural do
89+
actual_resource_identifier_object(AuthorSerializer)
4090
end
91+
expected = { id: expected_model_id, type: 'authors' }
92+
assert_equal actual, expected
93+
end
4194

42-
private
95+
def test_type_with_namespace
96+
Object.const_set(:Admin, Module.new)
97+
model = Class.new(::Model)
98+
Admin.const_set(:PowerUser, model)
99+
serializer = Class.new(ActiveModel::Serializer)
100+
Admin.const_set(:PowerUserSerializer, serializer)
101+
with_namespace_separator '--' do
102+
admin_user = Admin::PowerUser.new
103+
serializer = Admin::PowerUserSerializer.new(admin_user)
104+
expected = {
105+
id: admin_user.id,
106+
type: 'admin--power-users'
107+
}
43108

44-
def assert_type(resource, expected_type, opts = {})
45-
opts = opts.reverse_merge(adapter: :json_api)
46-
hash = serializable(resource, opts).serializable_hash
47-
assert_equal(expected_type, hash.fetch(:data).fetch(:type))
109+
identifier = ResourceIdentifier.new(serializer, {})
110+
actual = identifier.as_json
111+
assert_equal(expected, actual)
48112
end
49113
end
114+
115+
def test_id_defined_on_object
116+
actual = actual_resource_identifier_object(AuthorSerializer)
117+
expected = { id: @model.id.to_s, type: expected_model_type }
118+
assert_equal actual, expected
119+
end
120+
121+
def test_id_defined_on_serializer
122+
actual = actual_resource_identifier_object(WithDefinedIdSerializer)
123+
expected = { id: 'special_id', type: expected_model_type }
124+
assert_equal actual, expected
125+
end
126+
127+
def test_id_defined_on_fragmented
128+
actual = actual_resource_identifier_object(FragmentedSerializer)
129+
expected = { id: 'special_id', type: expected_model_type }
130+
assert_equal actual, expected
131+
end
132+
133+
private
134+
135+
def actual_resource_identifier_object(serializer_class)
136+
serializer = serializer_class.new(@model)
137+
resource_identifier = ResourceIdentifier.new(serializer, nil)
138+
resource_identifier.as_json
139+
end
140+
141+
def expected_model_type
142+
inflection = ActiveModelSerializers.config.jsonapi_resource_type
143+
@model.class.model_name.send(inflection)
144+
end
145+
146+
def expected_model_id
147+
@model.id.to_s
148+
end
50149
end
51150
end
52151
end

0 commit comments

Comments
 (0)