active_model_serializers 0.10.0.rc4 → 0.10.0.rc5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/ISSUE_TEMPLATE.md +29 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +19 -1
- data/.rubocop_todo.yml +30 -103
- data/.simplecov +0 -1
- data/.travis.yml +20 -8
- data/CHANGELOG.md +89 -5
- data/CONTRIBUTING.md +54 -179
- data/Gemfile +7 -2
- data/{LICENSE.txt → MIT-LICENSE} +0 -0
- data/README.md +27 -5
- data/Rakefile +44 -16
- data/active_model_serializers.gemspec +9 -1
- data/appveyor.yml +1 -0
- data/bin/bench +171 -0
- data/bin/bench_regression +316 -0
- data/bin/serve_benchmark +39 -0
- data/docs/ARCHITECTURE.md +13 -7
- data/docs/README.md +5 -1
- data/docs/STYLE.md +58 -0
- data/docs/general/adapters.md +99 -16
- data/docs/general/configuration_options.md +87 -14
- data/docs/general/deserialization.md +100 -0
- data/docs/general/getting_started.md +35 -0
- data/docs/general/instrumentation.md +1 -1
- data/docs/general/key_transforms.md +40 -0
- data/docs/general/rendering.md +115 -13
- data/docs/general/serializers.md +138 -6
- data/docs/howto/add_pagination_links.md +36 -18
- data/docs/howto/outside_controller_use.md +4 -4
- data/docs/howto/passing_arbitrary_options.md +27 -0
- data/docs/jsonapi/errors.md +56 -0
- data/docs/jsonapi/schema.md +29 -18
- data/docs/rfcs/0000-namespace.md +106 -0
- data/docs/rfcs/template.md +15 -0
- data/lib/action_controller/serialization.rb +10 -19
- data/lib/active_model/serializable_resource.rb +4 -65
- data/lib/active_model/serializer.rb +73 -18
- data/lib/active_model/serializer/adapter.rb +15 -82
- data/lib/active_model/serializer/adapter/attributes.rb +5 -56
- data/lib/active_model/serializer/adapter/base.rb +5 -47
- data/lib/active_model/serializer/adapter/json.rb +6 -12
- data/lib/active_model/serializer/adapter/json_api.rb +5 -213
- data/lib/active_model/serializer/adapter/null.rb +7 -3
- data/lib/active_model/serializer/array_serializer.rb +3 -3
- data/lib/active_model/serializer/association.rb +4 -5
- data/lib/active_model/serializer/attributes.rb +1 -1
- data/lib/active_model/serializer/caching.rb +56 -5
- data/lib/active_model/serializer/collection_serializer.rb +30 -13
- data/lib/active_model/serializer/configuration.rb +7 -0
- data/lib/active_model/serializer/error_serializer.rb +10 -0
- data/lib/active_model/serializer/errors_serializer.rb +27 -0
- data/lib/active_model/serializer/links.rb +4 -2
- data/lib/active_model/serializer/lint.rb +14 -0
- data/lib/active_model/serializer/meta.rb +29 -0
- data/lib/active_model/serializer/null.rb +17 -0
- data/lib/active_model/serializer/reflection.rb +57 -1
- data/lib/active_model/serializer/type.rb +1 -1
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +17 -0
- data/lib/active_model_serializers/adapter.rb +92 -0
- data/lib/active_model_serializers/adapter/attributes.rb +94 -0
- data/lib/active_model_serializers/adapter/base.rb +90 -0
- data/lib/active_model_serializers/adapter/json.rb +11 -0
- data/lib/active_model_serializers/adapter/json_api.rb +513 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +213 -0
- data/lib/active_model_serializers/adapter/json_api/error.rb +96 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +49 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +83 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +37 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +57 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +37 -0
- data/lib/active_model_serializers/adapter/null.rb +10 -0
- data/lib/active_model_serializers/cached_serializer.rb +87 -0
- data/lib/active_model_serializers/callbacks.rb +1 -1
- data/lib/active_model_serializers/deprecate.rb +55 -0
- data/lib/active_model_serializers/deserialization.rb +2 -2
- data/lib/active_model_serializers/fragment_cache.rb +118 -0
- data/lib/active_model_serializers/json_pointer.rb +14 -0
- data/lib/active_model_serializers/key_transform.rb +70 -0
- data/lib/active_model_serializers/logging.rb +4 -1
- data/lib/active_model_serializers/model.rb +11 -1
- data/lib/active_model_serializers/railtie.rb +9 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +64 -0
- data/lib/active_model_serializers/serializable_resource.rb +81 -0
- data/lib/active_model_serializers/serialization_context.rb +24 -2
- data/lib/active_model_serializers/test/schema.rb +2 -2
- data/lib/grape/formatters/active_model_serializers.rb +1 -1
- data/test/action_controller/adapter_selector_test.rb +1 -1
- data/test/action_controller/json_api/deserialization_test.rb +56 -3
- data/test/action_controller/json_api/errors_test.rb +41 -0
- data/test/action_controller/json_api/linked_test.rb +10 -9
- data/test/action_controller/json_api/pagination_test.rb +2 -2
- data/test/action_controller/json_api/transform_test.rb +180 -0
- data/test/action_controller/serialization_scope_name_test.rb +201 -35
- data/test/action_controller/serialization_test.rb +39 -7
- data/test/active_model_serializers/adapter_for_test.rb +208 -0
- data/test/active_model_serializers/cached_serializer_test.rb +80 -0
- data/test/active_model_serializers/fragment_cache_test.rb +34 -0
- data/test/active_model_serializers/json_pointer_test.rb +20 -0
- data/test/active_model_serializers/key_transform_test.rb +263 -0
- data/test/active_model_serializers/logging_test.rb +8 -8
- data/test/active_model_serializers/railtie_test_isolated.rb +6 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +58 -0
- data/test/adapter/deprecation_test.rb +100 -0
- data/test/adapter/json/belongs_to_test.rb +32 -34
- data/test/adapter/json/collection_test.rb +73 -75
- data/test/adapter/json/has_many_test.rb +36 -38
- data/test/adapter/json/transform_test.rb +93 -0
- data/test/adapter/json_api/belongs_to_test.rb +127 -129
- data/test/adapter/json_api/collection_test.rb +80 -82
- data/test/adapter/json_api/errors_test.rb +78 -0
- data/test/adapter/json_api/fields_test.rb +68 -70
- data/test/adapter/json_api/has_many_embed_ids_test.rb +32 -34
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +75 -77
- data/test/adapter/json_api/has_many_test.rb +121 -123
- data/test/adapter/json_api/has_one_test.rb +59 -61
- data/test/adapter/json_api/json_api_test.rb +28 -30
- data/test/adapter/json_api/linked_test.rb +319 -321
- data/test/adapter/json_api/links_test.rb +75 -50
- data/test/adapter/json_api/pagination_links_test.rb +115 -82
- data/test/adapter/json_api/parse_test.rb +114 -116
- data/test/adapter/json_api/relationship_test.rb +161 -0
- data/test/adapter/json_api/relationships_test.rb +199 -0
- data/test/adapter/json_api/resource_identifier_test.rb +85 -0
- data/test/adapter/json_api/resource_meta_test.rb +100 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +61 -63
- data/test/adapter/json_api/transform_test.rb +500 -0
- data/test/adapter/json_api/type_test.rb +61 -0
- data/test/adapter/json_test.rb +35 -37
- data/test/adapter/null_test.rb +13 -15
- data/test/adapter/polymorphic_test.rb +72 -0
- data/test/adapter_test.rb +27 -29
- data/test/array_serializer_test.rb +7 -8
- data/test/benchmark/app.rb +65 -0
- data/test/benchmark/benchmarking_support.rb +67 -0
- data/test/benchmark/bm_caching.rb +117 -0
- data/test/benchmark/bm_transform.rb +34 -0
- data/test/benchmark/config.ru +3 -0
- data/test/benchmark/controllers.rb +77 -0
- data/test/benchmark/fixtures.rb +167 -0
- data/test/cache_test.rb +388 -0
- data/test/collection_serializer_test.rb +10 -0
- data/test/fixtures/active_record.rb +12 -0
- data/test/fixtures/poro.rb +28 -3
- data/test/grape_test.rb +5 -5
- data/test/lint_test.rb +9 -0
- data/test/serializable_resource_test.rb +59 -3
- data/test/serializers/associations_test.rb +8 -8
- data/test/serializers/attribute_test.rb +7 -7
- data/test/serializers/caching_configuration_test_isolated.rb +170 -0
- data/test/serializers/meta_test.rb +74 -6
- data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
- data/test/serializers/serialization_test.rb +55 -0
- data/test/support/isolated_unit.rb +3 -0
- data/test/support/rails5_shims.rb +26 -8
- data/test/support/rails_app.rb +38 -18
- data/test/support/serialization_testing.rb +5 -5
- data/test/test_helper.rb +6 -10
- metadata +132 -37
- data/docs/DESIGN.textile +7 -1
- data/lib/active_model/serializer/adapter/cached_serializer.rb +0 -45
- data/lib/active_model/serializer/adapter/fragment_cache.rb +0 -111
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +0 -13
- data/lib/active_model/serializer/adapter/json_api/deserialization.rb +0 -207
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +0 -21
- data/lib/active_model/serializer/adapter/json_api/link.rb +0 -44
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +0 -58
- data/test/active_model_serializers/serialization_context_test.rb +0 -18
- data/test/adapter/fragment_cache_test.rb +0 -38
- data/test/adapter/json_api/resource_type_config_test.rb +0 -71
- data/test/serializers/adapter_for_test.rb +0 -166
- data/test/serializers/cache_test.rb +0 -209
- data/test/support/simplecov.rb +0 -6
- data/test/support/stream_capture.rb +0 -50
- data/test/support/test_case.rb +0 -19
@@ -1,66 +1,91 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
class
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
class JsonApi
|
6
|
+
class LinksTest < ActiveSupport::TestCase
|
7
|
+
LinkAuthor = Class.new(::Model)
|
8
|
+
class LinkAuthorSerializer < ActiveModel::Serializer
|
9
|
+
link :self do
|
10
|
+
href "http://example.com/link_author/#{object.id}"
|
11
|
+
meta stuff: 'value'
|
12
|
+
end
|
13
|
+
link(:author) { link_author_url(object.id) }
|
14
|
+
link(:link_authors) { url_for(controller: 'link_authors', action: 'index', only_path: false) }
|
15
|
+
link(:posts) { link_author_posts_url(object.id) }
|
16
|
+
link :resource, 'http://example.com/resource'
|
17
|
+
link :yet_another do
|
18
|
+
"http://example.com/resource/#{object.id}"
|
20
19
|
end
|
20
|
+
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
def setup
|
23
|
+
Rails.application.routes.draw do
|
24
|
+
resources :link_authors do
|
25
|
+
resources :posts
|
26
|
+
end
|
25
27
|
end
|
28
|
+
@post = Post.new(id: 1337, comments: [], author: nil)
|
29
|
+
@author = LinkAuthor.new(id: 1337, posts: [@post])
|
30
|
+
end
|
26
31
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
self: {
|
33
|
-
href: '//example.com/posts',
|
34
|
-
meta: {
|
35
|
-
stuff: 'value'
|
36
|
-
}
|
37
|
-
}
|
38
|
-
}).serializable_hash
|
39
|
-
expected = {
|
32
|
+
def test_toplevel_links
|
33
|
+
hash = ActiveModelSerializers::SerializableResource.new(
|
34
|
+
@post,
|
35
|
+
adapter: :json_api,
|
36
|
+
links: {
|
40
37
|
self: {
|
41
|
-
href: '
|
38
|
+
href: 'http://example.com/posts',
|
42
39
|
meta: {
|
43
40
|
stuff: 'value'
|
44
41
|
}
|
45
42
|
}
|
43
|
+
}).serializable_hash
|
44
|
+
expected = {
|
45
|
+
self: {
|
46
|
+
href: 'http://example.com/posts',
|
47
|
+
meta: {
|
48
|
+
stuff: 'value'
|
49
|
+
}
|
46
50
|
}
|
47
|
-
|
48
|
-
|
51
|
+
}
|
52
|
+
assert_equal(expected, hash[:links])
|
53
|
+
end
|
49
54
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
55
|
+
def test_nil_toplevel_links
|
56
|
+
hash = ActiveModelSerializers::SerializableResource.new(
|
57
|
+
@post,
|
58
|
+
adapter: :json_api,
|
59
|
+
links: nil
|
60
|
+
).serializable_hash
|
61
|
+
refute hash.key?(:links), 'No links key to be output'
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_nil_toplevel_links_json_adapter
|
65
|
+
hash = ActiveModelSerializers::SerializableResource.new(
|
66
|
+
@post,
|
67
|
+
adapter: :json,
|
68
|
+
links: nil
|
69
|
+
).serializable_hash
|
70
|
+
refute hash.key?(:links), 'No links key to be output'
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_resource_links
|
74
|
+
hash = serializable(@author, adapter: :json_api).serializable_hash
|
75
|
+
expected = {
|
76
|
+
self: {
|
77
|
+
href: 'http://example.com/link_author/1337',
|
78
|
+
meta: {
|
79
|
+
stuff: 'value'
|
80
|
+
}
|
81
|
+
},
|
82
|
+
author: 'http://example.com/link_authors/1337',
|
83
|
+
:"link-authors" => 'http://example.com/link_authors',
|
84
|
+
resource: 'http://example.com/resource',
|
85
|
+
posts: 'http://example.com/link_authors/1337/posts',
|
86
|
+
:"yet-another" => 'http://example.com/resource/1337'
|
87
|
+
}
|
88
|
+
assert_equal(expected, hash[:data][:links])
|
64
89
|
end
|
65
90
|
end
|
66
91
|
end
|
@@ -4,110 +4,143 @@ require 'kaminari'
|
|
4
4
|
require 'kaminari/hooks'
|
5
5
|
::Kaminari::Hooks.init
|
6
6
|
|
7
|
-
module
|
8
|
-
|
9
|
-
|
10
|
-
class
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
7
|
+
module ActiveModelSerializers
|
8
|
+
module Adapter
|
9
|
+
class JsonApi
|
10
|
+
class PaginationLinksTest < ActiveSupport::TestCase
|
11
|
+
URI = 'http://example.com'.freeze
|
12
|
+
|
13
|
+
def setup
|
14
|
+
ActionController::Base.cache_store.clear
|
15
|
+
@array = [
|
16
|
+
Profile.new({ id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
17
|
+
Profile.new({ id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }),
|
18
|
+
Profile.new({ id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3' }),
|
19
|
+
Profile.new({ id: 4, name: 'Name 4', description: 'Description 4', comments: 'Comments 4' }),
|
20
|
+
Profile.new({ id: 5, name: 'Name 5', description: 'Description 5', comments: 'Comments 5' })
|
21
|
+
]
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
24
|
+
def mock_request(query_parameters = {}, original_url = URI)
|
25
|
+
context = Minitest::Mock.new
|
26
|
+
context.expect(:request_url, original_url)
|
27
|
+
context.expect(:query_parameters, query_parameters)
|
28
|
+
@options = {}
|
29
|
+
@options[:serialization_context] = context
|
30
|
+
end
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
32
|
+
def load_adapter(paginated_collection, options = {})
|
33
|
+
options = options.merge(adapter: :json_api)
|
34
|
+
ActiveModelSerializers::SerializableResource.new(paginated_collection, options)
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
37
|
+
def using_kaminari(page = 2)
|
38
|
+
Kaminari.paginate_array(@array).page(page).per(2)
|
39
|
+
end
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
41
|
+
def using_will_paginate(page = 2)
|
42
|
+
@array.paginate(page: page, per_page: 2)
|
43
|
+
end
|
44
|
+
|
45
|
+
def data
|
46
|
+
{ data: [
|
47
|
+
{ id: '1', type: 'profiles', attributes: { name: 'Name 1', description: 'Description 1' } },
|
48
|
+
{ id: '2', type: 'profiles', attributes: { name: 'Name 2', description: 'Description 2' } },
|
49
|
+
{ id: '3', type: 'profiles', attributes: { name: 'Name 3', description: 'Description 3' } },
|
50
|
+
{ id: '4', type: 'profiles', attributes: { name: 'Name 4', description: 'Description 4' } },
|
51
|
+
{ id: '5', type: 'profiles', attributes: { name: 'Name 5', description: 'Description 5' } }
|
52
|
+
]
|
53
|
+
}
|
54
|
+
end
|
43
55
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
56
|
+
def links
|
57
|
+
{
|
58
|
+
links: {
|
59
|
+
self: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
|
60
|
+
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
|
61
|
+
prev: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
|
62
|
+
next: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2",
|
63
|
+
last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2"
|
50
64
|
}
|
51
|
-
|
65
|
+
}
|
66
|
+
end
|
52
67
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
next: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
60
|
-
last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"
|
61
|
-
}
|
68
|
+
def last_page_links
|
69
|
+
{
|
70
|
+
links: {
|
71
|
+
self: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2",
|
72
|
+
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
|
73
|
+
prev: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2"
|
62
74
|
}
|
63
|
-
|
75
|
+
}
|
76
|
+
end
|
64
77
|
|
65
|
-
|
66
|
-
|
78
|
+
def expected_response_without_pagination_links
|
79
|
+
data
|
80
|
+
end
|
81
|
+
|
82
|
+
def expected_response_with_pagination_links
|
83
|
+
{}.tap do |hash|
|
84
|
+
hash[:data] = data.values.flatten[2..3]
|
85
|
+
hash.merge! links
|
67
86
|
end
|
87
|
+
end
|
68
88
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
89
|
+
def expected_response_with_pagination_links_and_additional_params
|
90
|
+
new_links = links[:links].each_with_object({}) { |(key, value), hash| hash[key] = "#{value}&test=test" }
|
91
|
+
{}.tap do |hash|
|
92
|
+
hash[:data] = data.values.flatten[2..3]
|
93
|
+
hash.merge! links: new_links
|
74
94
|
end
|
95
|
+
end
|
75
96
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
hash.merge! links: new_links
|
81
|
-
end
|
97
|
+
def expected_response_with_last_page_pagination_links
|
98
|
+
{}.tap do |hash|
|
99
|
+
hash[:data] = [data.values.flatten.last]
|
100
|
+
hash.merge! last_page_links
|
82
101
|
end
|
102
|
+
end
|
83
103
|
|
84
|
-
|
85
|
-
|
104
|
+
def test_pagination_links_using_kaminari
|
105
|
+
adapter = load_adapter(using_kaminari)
|
86
106
|
|
87
|
-
|
88
|
-
|
89
|
-
|
107
|
+
mock_request
|
108
|
+
assert_equal expected_response_with_pagination_links, adapter.serializable_hash(@options)
|
109
|
+
end
|
90
110
|
|
91
|
-
|
92
|
-
|
111
|
+
def test_pagination_links_using_will_paginate
|
112
|
+
adapter = load_adapter(using_will_paginate)
|
93
113
|
|
94
|
-
|
95
|
-
|
96
|
-
|
114
|
+
mock_request
|
115
|
+
assert_equal expected_response_with_pagination_links, adapter.serializable_hash(@options)
|
116
|
+
end
|
97
117
|
|
98
|
-
|
99
|
-
|
118
|
+
def test_pagination_links_with_additional_params
|
119
|
+
adapter = load_adapter(using_will_paginate)
|
100
120
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
121
|
+
mock_request({ test: 'test' })
|
122
|
+
assert_equal expected_response_with_pagination_links_and_additional_params,
|
123
|
+
adapter.serializable_hash(@options)
|
124
|
+
end
|
105
125
|
|
106
|
-
|
107
|
-
|
126
|
+
def test_last_page_pagination_links_using_kaminari
|
127
|
+
adapter = load_adapter(using_kaminari(3))
|
108
128
|
|
109
|
-
|
110
|
-
|
129
|
+
mock_request
|
130
|
+
assert_equal expected_response_with_last_page_pagination_links, adapter.serializable_hash(@options)
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_last_page_pagination_links_using_will_paginate
|
134
|
+
adapter = load_adapter(using_will_paginate(3))
|
135
|
+
|
136
|
+
mock_request
|
137
|
+
assert_equal expected_response_with_last_page_pagination_links, adapter.serializable_hash(@options)
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_not_showing_pagination_links
|
141
|
+
adapter = load_adapter(@array)
|
142
|
+
|
143
|
+
assert_equal expected_response_without_pagination_links, adapter.serializable_hash
|
111
144
|
end
|
112
145
|
end
|
113
146
|
end
|
@@ -1,136 +1,134 @@
|
|
1
1
|
require 'test_helper'
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
'
|
11
|
-
|
12
|
-
|
13
|
-
'
|
14
|
-
|
15
|
-
|
2
|
+
module ActiveModelSerializers
|
3
|
+
module Adapter
|
4
|
+
class JsonApi
|
5
|
+
module Deserialization
|
6
|
+
class ParseTest < Minitest::Test
|
7
|
+
def setup
|
8
|
+
@hash = {
|
9
|
+
'data' => {
|
10
|
+
'type' => 'photos',
|
11
|
+
'id' => 'zorglub',
|
12
|
+
'attributes' => {
|
13
|
+
'title' => 'Ember Hamster',
|
14
|
+
'src' => 'http://example.com/images/productivity.png'
|
15
|
+
},
|
16
|
+
'relationships' => {
|
17
|
+
'author' => {
|
18
|
+
'data' => nil
|
16
19
|
},
|
17
|
-
'
|
18
|
-
'
|
19
|
-
|
20
|
-
|
21
|
-
'
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
'data' => [
|
26
|
-
{ 'type' => 'comments', 'id' => '1' },
|
27
|
-
{ 'type' => 'comments', 'id' => '2' }
|
28
|
-
]
|
29
|
-
}
|
20
|
+
'photographer' => {
|
21
|
+
'data' => { 'type' => 'people', 'id' => '9' }
|
22
|
+
},
|
23
|
+
'comments' => {
|
24
|
+
'data' => [
|
25
|
+
{ 'type' => 'comments', 'id' => '1' },
|
26
|
+
{ 'type' => 'comments', 'id' => '2' }
|
27
|
+
]
|
30
28
|
}
|
31
29
|
}
|
32
30
|
}
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
31
|
+
}
|
32
|
+
@params = ActionController::Parameters.new(@hash)
|
33
|
+
@expected = {
|
34
|
+
id: 'zorglub',
|
35
|
+
title: 'Ember Hamster',
|
36
|
+
src: 'http://example.com/images/productivity.png',
|
37
|
+
author_id: nil,
|
38
|
+
photographer_id: '9',
|
39
|
+
comment_ids: %w(1 2)
|
40
|
+
}
|
42
41
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
42
|
+
@illformed_payloads = [nil,
|
43
|
+
{},
|
44
|
+
{
|
45
|
+
'data' => nil
|
46
|
+
}, {
|
47
|
+
'data' => { 'attributes' => [] }
|
48
|
+
}, {
|
49
|
+
'data' => { 'relationships' => [] }
|
50
|
+
}, {
|
51
|
+
'data' => {
|
52
|
+
'relationships' => { 'rel' => nil }
|
53
|
+
}
|
54
|
+
}, {
|
55
|
+
'data' => {
|
56
|
+
'relationships' => { 'rel' => {} }
|
57
|
+
}
|
58
|
+
}]
|
59
|
+
end
|
61
60
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
61
|
+
def test_hash
|
62
|
+
parsed_hash = ActiveModelSerializers::Adapter::JsonApi::Deserialization.parse!(@hash)
|
63
|
+
assert_equal(@expected, parsed_hash)
|
64
|
+
end
|
66
65
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
66
|
+
def test_actioncontroller_parameters
|
67
|
+
assert_equal(false, @params.permitted?)
|
68
|
+
parsed_hash = ActiveModelSerializers::Adapter::JsonApi::Deserialization.parse!(@params)
|
69
|
+
assert_equal(@expected, parsed_hash)
|
70
|
+
end
|
72
71
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
end
|
72
|
+
def test_illformed_payloads_safe
|
73
|
+
@illformed_payloads.each do |p|
|
74
|
+
parsed_hash = ActiveModelSerializers::Adapter::JsonApi::Deserialization.parse(p)
|
75
|
+
assert_equal({}, parsed_hash)
|
78
76
|
end
|
77
|
+
end
|
79
78
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
end
|
79
|
+
def test_illformed_payloads_unsafe
|
80
|
+
@illformed_payloads.each do |p|
|
81
|
+
assert_raises(InvalidDocument) do
|
82
|
+
ActiveModelSerializers::Adapter::JsonApi::Deserialization.parse!(p)
|
85
83
|
end
|
86
84
|
end
|
85
|
+
end
|
87
86
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
87
|
+
def test_filter_fields_only
|
88
|
+
parsed_hash = ActiveModelSerializers::Adapter::JsonApi::Deserialization.parse!(@hash, only: [:id, :title, :author])
|
89
|
+
expected = {
|
90
|
+
id: 'zorglub',
|
91
|
+
title: 'Ember Hamster',
|
92
|
+
author_id: nil
|
93
|
+
}
|
94
|
+
assert_equal(expected, parsed_hash)
|
95
|
+
end
|
97
96
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
97
|
+
def test_filter_fields_except
|
98
|
+
parsed_hash = ActiveModelSerializers::Adapter::JsonApi::Deserialization.parse!(@hash, except: [:id, :title, :author])
|
99
|
+
expected = {
|
100
|
+
src: 'http://example.com/images/productivity.png',
|
101
|
+
photographer_id: '9',
|
102
|
+
comment_ids: %w(1 2)
|
103
|
+
}
|
104
|
+
assert_equal(expected, parsed_hash)
|
105
|
+
end
|
107
106
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
107
|
+
def test_keys
|
108
|
+
parsed_hash = ActiveModelSerializers::Adapter::JsonApi::Deserialization.parse!(@hash, keys: { author: :user, title: :post_title })
|
109
|
+
expected = {
|
110
|
+
id: 'zorglub',
|
111
|
+
post_title: 'Ember Hamster',
|
112
|
+
src: 'http://example.com/images/productivity.png',
|
113
|
+
user_id: nil,
|
114
|
+
photographer_id: '9',
|
115
|
+
comment_ids: %w(1 2)
|
116
|
+
}
|
117
|
+
assert_equal(expected, parsed_hash)
|
118
|
+
end
|
120
119
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
end
|
120
|
+
def test_polymorphic
|
121
|
+
parsed_hash = ActiveModelSerializers::Adapter::JsonApi::Deserialization.parse!(@hash, polymorphic: [:photographer])
|
122
|
+
expected = {
|
123
|
+
id: 'zorglub',
|
124
|
+
title: 'Ember Hamster',
|
125
|
+
src: 'http://example.com/images/productivity.png',
|
126
|
+
author_id: nil,
|
127
|
+
photographer_id: '9',
|
128
|
+
photographer_type: 'people',
|
129
|
+
comment_ids: %w(1 2)
|
130
|
+
}
|
131
|
+
assert_equal(expected, parsed_hash)
|
134
132
|
end
|
135
133
|
end
|
136
134
|
end
|