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
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
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
|
12
|
+
|
13
|
+
class SymbolTypeSerializer < ActiveModel::Serializer
|
14
|
+
attribute :name
|
15
|
+
type :profile
|
16
|
+
end
|
17
|
+
|
18
|
+
setup do
|
19
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_config_plural
|
23
|
+
with_jsonapi_resource_type :plural do
|
24
|
+
assert_type(@author, 'authors')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_config_singular
|
29
|
+
with_jsonapi_resource_type :singular do
|
30
|
+
assert_type(@author, 'author')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_explicit_string_type_value
|
35
|
+
assert_type(@author, 'profile', serializer: StringTypeSerializer)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_explicit_symbol_type_value
|
39
|
+
assert_type(@author, 'profile', serializer: SymbolTypeSerializer)
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
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))
|
48
|
+
end
|
49
|
+
|
50
|
+
def with_jsonapi_resource_type inflection
|
51
|
+
old_inflection = ActiveModelSerializers.config.jsonapi_resource_type
|
52
|
+
ActiveModelSerializers.config.jsonapi_resource_type = inflection
|
53
|
+
yield
|
54
|
+
ensure
|
55
|
+
ActiveModelSerializers.config.jsonapi_resource_type = old_inflection
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/test/adapter/json_test.rb
CHANGED
@@ -1,46 +1,44 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
@post.blog = @blog
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
class JsonTest < ActiveSupport::TestCase
|
6
|
+
def setup
|
7
|
+
ActionController::Base.cache_store.clear
|
8
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
9
|
+
@post = Post.new(id: 1, title: 'New Post', body: 'Body')
|
10
|
+
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
11
|
+
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
12
|
+
@post.comments = [@first_comment, @second_comment]
|
13
|
+
@first_comment.post = @post
|
14
|
+
@second_comment.post = @post
|
15
|
+
@post.author = @author
|
16
|
+
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
17
|
+
@post.blog = @blog
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
@serializer = PostSerializer.new(@post)
|
20
|
+
@adapter = ActiveModelSerializers::Adapter::Json.new(@serializer)
|
21
|
+
end
|
23
22
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
23
|
+
def test_has_many
|
24
|
+
assert_equal([
|
25
|
+
{ id: 1, body: 'ZOMG A COMMENT' },
|
26
|
+
{ id: 2, body: 'ZOMG ANOTHER COMMENT' }
|
27
|
+
], @adapter.serializable_hash[:post][:comments])
|
28
|
+
end
|
30
29
|
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
def test_custom_keys
|
31
|
+
serializer = PostWithCustomKeysSerializer.new(@post)
|
32
|
+
adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
|
34
33
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
34
|
+
assert_equal({
|
35
|
+
id: 1,
|
36
|
+
reviews: [{ id: 1, body: 'ZOMG A COMMENT' },
|
37
|
+
{ id: 2, body: 'ZOMG ANOTHER COMMENT' }
|
38
|
+
],
|
39
|
+
writer: { id: 1, name: 'Steve K.' },
|
40
|
+
site: { id: 1, name: 'My Blog!!' }
|
41
|
+
}, adapter.serializable_hash[:post])
|
44
42
|
end
|
45
43
|
end
|
46
44
|
end
|
data/test/adapter/null_test.rb
CHANGED
@@ -1,23 +1,21 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
serializer = ProfileSerializer.new(profile)
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
class NullTest < ActiveSupport::TestCase
|
6
|
+
def setup
|
7
|
+
profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
8
|
+
serializer = ProfileSerializer.new(profile)
|
10
9
|
|
11
|
-
|
12
|
-
|
10
|
+
@adapter = Null.new(serializer)
|
11
|
+
end
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
def test_serializable_hash
|
14
|
+
assert_equal({}, @adapter.serializable_hash)
|
15
|
+
end
|
17
16
|
|
18
|
-
|
19
|
-
|
20
|
-
end
|
17
|
+
def test_it_returns_empty_json
|
18
|
+
assert_equal('{}', @adapter.to_json)
|
21
19
|
end
|
22
20
|
end
|
23
21
|
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
module Adapter
|
6
|
+
class PolymorphicTest < ActiveSupport::TestCase
|
7
|
+
setup do
|
8
|
+
@employee = Employee.new(id: 42, name: 'Zoop Zoopler', email: 'zoop@example.com')
|
9
|
+
@picture = @employee.pictures.new(id: 1, title: 'headshot-1.jpg')
|
10
|
+
@picture.imageable = @employee
|
11
|
+
|
12
|
+
@attributes_serialization = serializable(@picture, serializer: PolymorphicBelongsToSerializer) # uses default adapter: attributes
|
13
|
+
@json_serialization = serializable(@picture, adapter: :json, serializer: PolymorphicBelongsToSerializer)
|
14
|
+
@json_api_serialization = serializable(@picture, adapter: :json_api, serializer: PolymorphicBelongsToSerializer)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_attributes_serialization
|
18
|
+
expected =
|
19
|
+
{
|
20
|
+
id: 1,
|
21
|
+
title: 'headshot-1.jpg',
|
22
|
+
imageable: {
|
23
|
+
id: 42,
|
24
|
+
name: 'Zoop Zoopler'
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
assert_equal(expected, @attributes_serialization.as_json)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_json_serializer
|
32
|
+
expected =
|
33
|
+
{
|
34
|
+
picture: {
|
35
|
+
id: 1,
|
36
|
+
title: 'headshot-1.jpg',
|
37
|
+
imageable: {
|
38
|
+
id: 42,
|
39
|
+
name: 'Zoop Zoopler'
|
40
|
+
}
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
assert_equal(expected, @json_serialization.as_json)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_json_api_serializer
|
48
|
+
expected =
|
49
|
+
{
|
50
|
+
data: {
|
51
|
+
id: '1',
|
52
|
+
type: 'pictures',
|
53
|
+
attributes: {
|
54
|
+
title: 'headshot-1.jpg'
|
55
|
+
},
|
56
|
+
relationships: {
|
57
|
+
imageable: {
|
58
|
+
data: {
|
59
|
+
id: '42',
|
60
|
+
type: 'employees'
|
61
|
+
}
|
62
|
+
}
|
63
|
+
}
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
assert_equal(expected, @json_api_serialization.as_json)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/test/adapter_test.rb
CHANGED
@@ -1,42 +1,40 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
module
|
4
|
-
class
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
3
|
+
module ActiveModelSerializers
|
4
|
+
class AdapterTest < ActiveSupport::TestCase
|
5
|
+
def setup
|
6
|
+
profile = Profile.new
|
7
|
+
@serializer = ProfileSerializer.new(profile)
|
8
|
+
@adapter = ActiveModelSerializers::Adapter::Base.new(@serializer)
|
9
|
+
end
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
11
|
+
def test_serializable_hash_is_abstract_method
|
12
|
+
assert_raises(NotImplementedError) do
|
13
|
+
@adapter.serializable_hash(only: [:name])
|
16
14
|
end
|
15
|
+
end
|
17
16
|
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
def test_serializer
|
18
|
+
assert_equal @serializer, @adapter.serializer
|
19
|
+
end
|
21
20
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
def test_create_adapter
|
22
|
+
adapter = ActiveModelSerializers::Adapter.create(@serializer)
|
23
|
+
assert_equal ActiveModelSerializers::Adapter::Attributes, adapter.class
|
24
|
+
end
|
26
25
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
26
|
+
def test_create_adapter_with_override
|
27
|
+
adapter = ActiveModelSerializers::Adapter.create(@serializer, { adapter: :json_api })
|
28
|
+
assert_equal ActiveModelSerializers::Adapter::JsonApi, adapter.class
|
29
|
+
end
|
31
30
|
|
32
|
-
|
33
|
-
|
34
|
-
|
31
|
+
def test_inflected_adapter_class_for_known_adapter
|
32
|
+
ActiveSupport::Inflector.inflections(:en) { |inflect| inflect.acronym 'API' }
|
33
|
+
klass = ActiveModelSerializers::Adapter.adapter_class(:json_api)
|
35
34
|
|
36
|
-
|
35
|
+
ActiveSupport::Inflector.inflections.acronyms.clear
|
37
36
|
|
38
|
-
|
39
|
-
end
|
37
|
+
assert_equal ActiveModelSerializers::Adapter::JsonApi, klass
|
40
38
|
end
|
41
39
|
end
|
42
40
|
end
|
@@ -6,12 +6,12 @@ module ActiveModel
|
|
6
6
|
# Minitest.run_one_method isn't present in minitest 4
|
7
7
|
if $minitest_version > 4 # rubocop:disable Style/GlobalVars
|
8
8
|
class ArraySerializerTest < CollectionSerializerTest
|
9
|
-
extend
|
9
|
+
extend Minitest::Assertions
|
10
10
|
def self.run_one_method(*)
|
11
|
-
stderr =
|
11
|
+
_, stderr = capture_io do
|
12
12
|
super
|
13
|
-
end
|
14
|
-
if stderr !~ /
|
13
|
+
end
|
14
|
+
if stderr !~ /NOTE: ActiveModel::Serializer::ArraySerializer.new is deprecated/
|
15
15
|
fail Minitest::Assertion, stderr
|
16
16
|
end
|
17
17
|
end
|
@@ -22,15 +22,14 @@ module ActiveModel
|
|
22
22
|
end
|
23
23
|
else
|
24
24
|
class ArraySerializerTest < ActiveSupport::TestCase
|
25
|
-
extend ActiveSupport::Testing::Stream
|
26
25
|
def test_json_key_with_root_warns_when_using_array_serializer
|
27
|
-
stderr =
|
26
|
+
_, stderr = capture_io do
|
28
27
|
comment = Comment.new
|
29
28
|
post = Post.new
|
30
29
|
serializer = ArraySerializer.new([comment, post])
|
31
30
|
assert_equal 'comments', serializer.json_key
|
32
|
-
end
|
33
|
-
assert_match(/
|
31
|
+
end
|
32
|
+
assert_match(/NOTE: ActiveModel::Serializer::ArraySerializer.new is deprecated/, stderr)
|
34
33
|
end
|
35
34
|
end
|
36
35
|
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# https://github.com/rails-api/active_model_serializers/pull/872
|
2
|
+
# approx ref 792fb8a9053f8db3c562dae4f40907a582dd1720 to test against
|
3
|
+
require 'bundler/setup'
|
4
|
+
|
5
|
+
require 'rails'
|
6
|
+
require 'active_model'
|
7
|
+
require 'active_support'
|
8
|
+
require 'active_support/json'
|
9
|
+
require 'action_controller'
|
10
|
+
require 'action_controller/test_case'
|
11
|
+
require 'action_controller/railtie'
|
12
|
+
abort "Rails application already defined: #{Rails.application.class}" if Rails.application
|
13
|
+
|
14
|
+
class NullLogger < Logger
|
15
|
+
def initialize(*_args)
|
16
|
+
end
|
17
|
+
|
18
|
+
def add(*_args, &_block)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
class BenchmarkLogger < ActiveSupport::Logger
|
22
|
+
def initialize
|
23
|
+
@file = StringIO.new
|
24
|
+
super(@file)
|
25
|
+
end
|
26
|
+
|
27
|
+
def messages
|
28
|
+
@file.rewind
|
29
|
+
@file.read
|
30
|
+
end
|
31
|
+
end
|
32
|
+
# ref: https://gist.github.com/bf4/8744473
|
33
|
+
class BenchmarkApp < Rails::Application
|
34
|
+
# Set up production configuration
|
35
|
+
config.eager_load = true
|
36
|
+
config.cache_classes = true
|
37
|
+
# CONFIG: CACHE_ON={on,off}
|
38
|
+
config.action_controller.perform_caching = ENV['CACHE_ON'] != 'off'
|
39
|
+
config.action_controller.cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
|
40
|
+
|
41
|
+
config.active_support.test_order = :random
|
42
|
+
config.secret_token = 'S' * 30
|
43
|
+
config.secret_key_base = 'abc123'
|
44
|
+
config.consider_all_requests_local = false
|
45
|
+
|
46
|
+
# otherwise deadlock occured
|
47
|
+
config.middleware.delete 'Rack::Lock'
|
48
|
+
|
49
|
+
# to disable log files
|
50
|
+
config.logger = NullLogger.new
|
51
|
+
config.active_support.deprecation = :log
|
52
|
+
config.log_level = :info
|
53
|
+
end
|
54
|
+
|
55
|
+
require 'active_model_serializers'
|
56
|
+
|
57
|
+
# Initialize app before any serializers are defined, for running across revisions.
|
58
|
+
# ref: https://github.com/rails-api/active_model_serializers/pull/1478
|
59
|
+
Rails.application.initialize!
|
60
|
+
# HACK: Serializer::cache depends on the ActionController-dependent configs being set.
|
61
|
+
ActiveSupport.on_load(:action_controller) do
|
62
|
+
require_relative 'fixtures'
|
63
|
+
end
|
64
|
+
|
65
|
+
require_relative 'controllers'
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'benchmark/ips'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
# Add benchmarking runner from ruby-bench-suite
|
5
|
+
# https://github.com/ruby-bench/ruby-bench-suite/blob/master/rails/benchmarks/support/benchmark_rails.rb
|
6
|
+
module Benchmark
|
7
|
+
module ActiveModelSerializers
|
8
|
+
module TestMethods
|
9
|
+
def request(method, path)
|
10
|
+
response = Rack::MockRequest.new(BenchmarkApp).send(method, path)
|
11
|
+
if response.status.in?([404, 500])
|
12
|
+
fail "omg, #{method}, #{path}, '#{response.status}', '#{response.body}'"
|
13
|
+
end
|
14
|
+
response
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# extend Benchmark with an `ams` method
|
19
|
+
def ams(label = nil, time:, disable_gc: true, warmup: 3, &block)
|
20
|
+
fail ArgumentError.new, 'block should be passed' unless block_given?
|
21
|
+
|
22
|
+
if disable_gc
|
23
|
+
GC.disable
|
24
|
+
else
|
25
|
+
GC.enable
|
26
|
+
end
|
27
|
+
|
28
|
+
report = Benchmark.ips(time, warmup, true) do |x|
|
29
|
+
x.report(label) { yield }
|
30
|
+
end
|
31
|
+
|
32
|
+
entry = report.entries.first
|
33
|
+
|
34
|
+
output = {
|
35
|
+
label: label,
|
36
|
+
version: ::ActiveModel::Serializer::VERSION.to_s,
|
37
|
+
rails_version: ::Rails.version.to_s,
|
38
|
+
iterations_per_second: entry.ips,
|
39
|
+
iterations_per_second_standard_deviation: entry.stddev_percentage,
|
40
|
+
total_allocated_objects_per_iteration: count_total_allocated_objects(&block)
|
41
|
+
}.to_json
|
42
|
+
|
43
|
+
puts output
|
44
|
+
output
|
45
|
+
end
|
46
|
+
|
47
|
+
def count_total_allocated_objects
|
48
|
+
if block_given?
|
49
|
+
key =
|
50
|
+
if RUBY_VERSION < '2.2'
|
51
|
+
:total_allocated_object
|
52
|
+
else
|
53
|
+
:total_allocated_objects
|
54
|
+
end
|
55
|
+
|
56
|
+
before = GC.stat[key]
|
57
|
+
yield
|
58
|
+
after = GC.stat[key]
|
59
|
+
after - before
|
60
|
+
else
|
61
|
+
-1
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
extend Benchmark::ActiveModelSerializers
|
67
|
+
end
|