active_model_serializers 0.10.6 → 0.10.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +114 -1
- data/README.md +21 -24
- data/lib/action_controller/serialization.rb +11 -1
- data/lib/active_model/serializable_resource.rb +2 -0
- data/lib/active_model/serializer/adapter/attributes.rb +2 -0
- data/lib/active_model/serializer/adapter/base.rb +2 -0
- data/lib/active_model/serializer/adapter/json.rb +2 -0
- data/lib/active_model/serializer/adapter/json_api.rb +2 -0
- data/lib/active_model/serializer/adapter/null.rb +2 -0
- data/lib/active_model/serializer/adapter.rb +2 -0
- data/lib/active_model/serializer/array_serializer.rb +2 -0
- data/lib/active_model/serializer/association.rb +2 -0
- data/lib/active_model/serializer/attribute.rb +2 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +2 -0
- data/lib/active_model/serializer/collection_serializer.rb +17 -5
- data/lib/active_model/serializer/concerns/caching.rb +7 -2
- data/lib/active_model/serializer/error_serializer.rb +2 -0
- data/lib/active_model/serializer/errors_serializer.rb +2 -0
- data/lib/active_model/serializer/field.rb +2 -0
- data/lib/active_model/serializer/fieldset.rb +3 -1
- data/lib/active_model/serializer/has_many_reflection.rb +2 -0
- data/lib/active_model/serializer/has_one_reflection.rb +2 -0
- data/lib/active_model/serializer/lazy_association.rb +6 -2
- data/lib/active_model/serializer/link.rb +23 -0
- data/lib/active_model/serializer/lint.rb +2 -0
- data/lib/active_model/serializer/null.rb +2 -0
- data/lib/active_model/serializer/reflection.rb +10 -5
- data/lib/active_model/serializer/version.rb +3 -1
- data/lib/active_model/serializer.rb +37 -19
- data/lib/active_model_serializers/adapter/attributes.rb +23 -0
- data/lib/active_model_serializers/adapter/base.rb +2 -0
- data/lib/active_model_serializers/adapter/json.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +3 -1
- data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +45 -20
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +18 -4
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +33 -25
- data/lib/active_model_serializers/adapter/json_api.rb +14 -9
- data/lib/active_model_serializers/adapter/null.rb +2 -0
- data/lib/active_model_serializers/adapter.rb +3 -1
- data/lib/active_model_serializers/callbacks.rb +2 -0
- data/lib/active_model_serializers/deprecate.rb +2 -0
- data/lib/active_model_serializers/deserialization.rb +2 -0
- data/lib/active_model_serializers/json_pointer.rb +2 -0
- data/lib/active_model_serializers/logging.rb +2 -0
- data/lib/active_model_serializers/lookup_chain.rb +2 -0
- data/lib/active_model_serializers/model/caching.rb +26 -0
- data/lib/active_model_serializers/model.rb +2 -0
- data/lib/active_model_serializers/railtie.rb +4 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +2 -0
- data/lib/active_model_serializers/serializable_resource.rb +4 -2
- data/lib/active_model_serializers/serialization_context.rb +2 -0
- data/lib/active_model_serializers/test/schema.rb +2 -0
- data/lib/active_model_serializers/test/serializer.rb +2 -0
- data/lib/active_model_serializers/test.rb +2 -0
- data/lib/active_model_serializers.rb +21 -11
- data/lib/generators/rails/resource_override.rb +2 -0
- data/lib/generators/rails/serializer_generator.rb +2 -0
- data/lib/grape/active_model_serializers.rb +2 -0
- data/lib/grape/formatters/active_model_serializers.rb +2 -0
- data/lib/grape/helpers/active_model_serializers.rb +2 -0
- data/lib/tasks/rubocop.rake +2 -0
- metadata +43 -298
- data/.github/ISSUE_TEMPLATE.md +0 -29
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
- data/.gitignore +0 -35
- data/.rubocop.yml +0 -105
- data/.simplecov +0 -110
- data/.travis.yml +0 -55
- data/CODE_OF_CONDUCT.md +0 -74
- data/CONTRIBUTING.md +0 -105
- data/Gemfile +0 -56
- data/Rakefile +0 -74
- data/active_model_serializers.gemspec +0 -63
- data/appveyor.yml +0 -30
- data/bin/bench +0 -171
- data/bin/bench_regression +0 -316
- data/bin/rubocop +0 -38
- data/bin/serve_benchmark +0 -39
- data/docs/README.md +0 -41
- data/docs/STYLE.md +0 -58
- data/docs/general/adapters.md +0 -263
- data/docs/general/caching.md +0 -58
- data/docs/general/configuration_options.md +0 -169
- data/docs/general/deserialization.md +0 -100
- data/docs/general/fields.md +0 -31
- data/docs/general/getting_started.md +0 -133
- data/docs/general/instrumentation.md +0 -40
- data/docs/general/key_transforms.md +0 -40
- data/docs/general/logging.md +0 -21
- data/docs/general/rendering.md +0 -293
- data/docs/general/serializers.md +0 -480
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +0 -138
- data/docs/howto/add_relationship_links.md +0 -140
- data/docs/howto/add_root_key.md +0 -55
- data/docs/howto/grape_integration.md +0 -42
- data/docs/howto/outside_controller_use.md +0 -66
- data/docs/howto/passing_arbitrary_options.md +0 -27
- data/docs/howto/serialize_poro.md +0 -73
- data/docs/howto/test.md +0 -154
- data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
- data/docs/integrations/ember-and-json-api.md +0 -147
- data/docs/integrations/grape.md +0 -19
- data/docs/jsonapi/errors.md +0 -56
- data/docs/jsonapi/schema/schema.json +0 -366
- data/docs/jsonapi/schema.md +0 -151
- data/docs/rfcs/0000-namespace.md +0 -106
- data/docs/rfcs/template.md +0 -15
- data/test/action_controller/adapter_selector_test.rb +0 -62
- data/test/action_controller/explicit_serializer_test.rb +0 -135
- data/test/action_controller/json/include_test.rb +0 -246
- data/test/action_controller/json_api/deserialization_test.rb +0 -112
- data/test/action_controller/json_api/errors_test.rb +0 -40
- data/test/action_controller/json_api/fields_test.rb +0 -66
- data/test/action_controller/json_api/linked_test.rb +0 -202
- data/test/action_controller/json_api/pagination_test.rb +0 -116
- data/test/action_controller/json_api/transform_test.rb +0 -189
- data/test/action_controller/lookup_proc_test.rb +0 -49
- data/test/action_controller/namespace_lookup_test.rb +0 -232
- data/test/action_controller/serialization_scope_name_test.rb +0 -235
- data/test/action_controller/serialization_test.rb +0 -472
- data/test/active_model_serializers/adapter_for_test.rb +0 -208
- data/test/active_model_serializers/json_pointer_test.rb +0 -22
- data/test/active_model_serializers/logging_test.rb +0 -77
- data/test/active_model_serializers/model_test.rb +0 -142
- data/test/active_model_serializers/railtie_test_isolated.rb +0 -68
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -161
- data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -71
- data/test/active_model_serializers/test/schema_test.rb +0 -131
- data/test/active_model_serializers/test/serializer_test.rb +0 -62
- data/test/active_record_test.rb +0 -9
- data/test/adapter/attributes_test.rb +0 -40
- data/test/adapter/deprecation_test.rb +0 -100
- data/test/adapter/json/belongs_to_test.rb +0 -45
- data/test/adapter/json/collection_test.rb +0 -104
- data/test/adapter/json/has_many_test.rb +0 -53
- data/test/adapter/json/transform_test.rb +0 -93
- data/test/adapter/json_api/belongs_to_test.rb +0 -155
- data/test/adapter/json_api/collection_test.rb +0 -96
- data/test/adapter/json_api/errors_test.rb +0 -76
- data/test/adapter/json_api/fields_test.rb +0 -96
- data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -96
- data/test/adapter/json_api/has_many_test.rb +0 -173
- data/test/adapter/json_api/has_one_test.rb +0 -80
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -183
- data/test/adapter/json_api/json_api_test.rb +0 -33
- data/test/adapter/json_api/linked_test.rb +0 -413
- data/test/adapter/json_api/links_test.rb +0 -95
- data/test/adapter/json_api/pagination_links_test.rb +0 -193
- data/test/adapter/json_api/parse_test.rb +0 -137
- data/test/adapter/json_api/relationship_test.rb +0 -397
- data/test/adapter/json_api/resource_identifier_test.rb +0 -110
- data/test/adapter/json_api/resource_meta_test.rb +0 -100
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -82
- data/test/adapter/json_api/transform_test.rb +0 -512
- data/test/adapter/json_api/type_test.rb +0 -61
- data/test/adapter/json_test.rb +0 -46
- data/test/adapter/null_test.rb +0 -22
- data/test/adapter/polymorphic_test.rb +0 -171
- data/test/adapter_test.rb +0 -67
- data/test/array_serializer_test.rb +0 -22
- data/test/benchmark/app.rb +0 -65
- data/test/benchmark/benchmarking_support.rb +0 -67
- data/test/benchmark/bm_active_record.rb +0 -81
- data/test/benchmark/bm_adapter.rb +0 -38
- data/test/benchmark/bm_caching.rb +0 -119
- data/test/benchmark/bm_lookup_chain.rb +0 -83
- data/test/benchmark/bm_transform.rb +0 -45
- data/test/benchmark/config.ru +0 -3
- data/test/benchmark/controllers.rb +0 -83
- data/test/benchmark/fixtures.rb +0 -219
- data/test/cache_test.rb +0 -651
- data/test/collection_serializer_test.rb +0 -123
- data/test/fixtures/active_record.rb +0 -113
- data/test/fixtures/poro.rb +0 -225
- data/test/generators/scaffold_controller_generator_test.rb +0 -24
- data/test/generators/serializer_generator_test.rb +0 -75
- data/test/grape_test.rb +0 -196
- data/test/lint_test.rb +0 -49
- data/test/logger_test.rb +0 -20
- data/test/poro_test.rb +0 -9
- data/test/serializable_resource_test.rb +0 -79
- data/test/serializers/association_macros_test.rb +0 -37
- data/test/serializers/associations_test.rb +0 -424
- data/test/serializers/attribute_test.rb +0 -153
- data/test/serializers/attributes_test.rb +0 -52
- data/test/serializers/caching_configuration_test_isolated.rb +0 -170
- data/test/serializers/configuration_test.rb +0 -32
- data/test/serializers/fieldset_test.rb +0 -14
- data/test/serializers/meta_test.rb +0 -202
- data/test/serializers/options_test.rb +0 -32
- data/test/serializers/read_attribute_for_serialization_test.rb +0 -79
- data/test/serializers/reflection_test.rb +0 -427
- data/test/serializers/root_test.rb +0 -21
- data/test/serializers/serialization_test.rb +0 -55
- data/test/serializers/serializer_for_test.rb +0 -136
- data/test/serializers/serializer_for_with_namespace_test.rb +0 -88
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/isolated_unit.rb +0 -82
- data/test/support/rails5_shims.rb +0 -53
- data/test/support/rails_app.rb +0 -38
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
- data/test/support/schemas/custom/show.json +0 -7
- data/test/support/schemas/hyper_schema.json +0 -93
- data/test/support/schemas/render_using_json_api.json +0 -43
- data/test/support/schemas/simple_json_pointers.json +0 -10
- data/test/support/serialization_testing.rb +0 -71
- data/test/test_helper.rb +0 -70
@@ -1,77 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class LoggingTest < ActiveSupport::TestCase
|
6
|
-
class TestLogger < ActiveSupport::Logger
|
7
|
-
def initialize
|
8
|
-
@file = StringIO.new
|
9
|
-
super(@file)
|
10
|
-
end
|
11
|
-
|
12
|
-
def messages
|
13
|
-
@file.rewind
|
14
|
-
@file.read
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def setup
|
19
|
-
@author = Author.new(name: 'Steve K.')
|
20
|
-
@post = Post.new(title: 'New Post', body: 'Body')
|
21
|
-
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
22
|
-
@post.comments = [@comment]
|
23
|
-
@comment.post = @post
|
24
|
-
@post.author = @author
|
25
|
-
@author.posts = [@post]
|
26
|
-
@post_serializer = PostSerializer.new(@post, custom_options: true)
|
27
|
-
|
28
|
-
@old_logger = ActiveModelSerializers.logger
|
29
|
-
@logger = ActiveSupport::TaggedLogging.new(TestLogger.new)
|
30
|
-
logger @logger
|
31
|
-
end
|
32
|
-
|
33
|
-
def teardown
|
34
|
-
logger @old_logger
|
35
|
-
end
|
36
|
-
|
37
|
-
def logger(logger)
|
38
|
-
ActiveModelSerializers.logger = logger
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_uses_ams_as_tag
|
42
|
-
ActiveModelSerializers::SerializableResource.new(@post).serializable_hash
|
43
|
-
assert_match(/\[active_model_serializers\]/, @logger.messages)
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_logs_when_call_serializable_hash
|
47
|
-
ActiveModelSerializers::SerializableResource.new(@post).serializable_hash
|
48
|
-
assert_match(/Rendered/, @logger.messages)
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_logs_when_call_as_json
|
52
|
-
ActiveModelSerializers::SerializableResource.new(@post).as_json
|
53
|
-
assert_match(/Rendered/, @logger.messages)
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_logs_when_call_to_json
|
57
|
-
ActiveModelSerializers::SerializableResource.new(@post).to_json
|
58
|
-
assert_match(/Rendered/, @logger.messages)
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_logs_correct_serializer
|
62
|
-
ActiveModelSerializers::SerializableResource.new(@post).serializable_hash
|
63
|
-
assert_match(/PostSerializer/, @logger.messages)
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_logs_correct_adapter
|
67
|
-
ActiveModelSerializers::SerializableResource.new(@post).serializable_hash
|
68
|
-
assert_match(/ActiveModelSerializers::Adapter::Attributes/, @logger.messages)
|
69
|
-
end
|
70
|
-
|
71
|
-
def test_logs_the_duration
|
72
|
-
ActiveModelSerializers::SerializableResource.new(@post).serializable_hash
|
73
|
-
assert_match(/\(\d+\.\d+ms\)/, @logger.messages)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
@@ -1,142 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModelSerializers
|
4
|
-
class ModelTest < ActiveSupport::TestCase
|
5
|
-
include ActiveModel::Serializer::Lint::Tests
|
6
|
-
|
7
|
-
setup do
|
8
|
-
@resource = ActiveModelSerializers::Model.new
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_initialization_with_string_keys
|
12
|
-
klass = Class.new(ActiveModelSerializers::Model) do
|
13
|
-
attributes :key
|
14
|
-
end
|
15
|
-
value = 'value'
|
16
|
-
|
17
|
-
model_instance = klass.new('key' => value)
|
18
|
-
|
19
|
-
assert_equal model_instance.read_attribute_for_serialization(:key), value
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_attributes_can_be_read_for_serialization
|
23
|
-
klass = Class.new(ActiveModelSerializers::Model) do
|
24
|
-
attributes :one, :two, :three
|
25
|
-
end
|
26
|
-
original_attributes = { one: 1, two: 2, three: 3 }
|
27
|
-
original_instance = klass.new(original_attributes)
|
28
|
-
|
29
|
-
# Initial value
|
30
|
-
instance = original_instance
|
31
|
-
expected_attributes = { one: 1, two: 2, three: 3 }.with_indifferent_access
|
32
|
-
assert_equal expected_attributes, instance.attributes
|
33
|
-
assert_equal 1, instance.one
|
34
|
-
assert_equal 1, instance.read_attribute_for_serialization(:one)
|
35
|
-
|
36
|
-
# FIXME: Change via accessor has no effect on attributes.
|
37
|
-
instance = original_instance.dup
|
38
|
-
instance.one = :not_one
|
39
|
-
assert_equal expected_attributes, instance.attributes
|
40
|
-
assert_equal :not_one, instance.one
|
41
|
-
assert_equal :not_one, instance.read_attribute_for_serialization(:one)
|
42
|
-
|
43
|
-
# FIXME: Change via mutating attributes
|
44
|
-
instance = original_instance.dup
|
45
|
-
instance.attributes[:one] = :not_one
|
46
|
-
expected_attributes = { one: :not_one, two: 2, three: 3 }.with_indifferent_access
|
47
|
-
assert_equal expected_attributes, instance.attributes
|
48
|
-
assert_equal 1, instance.one
|
49
|
-
assert_equal 1, instance.read_attribute_for_serialization(:one)
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_attributes_can_be_read_for_serialization_with_attributes_accessors_fix
|
53
|
-
klass = Class.new(ActiveModelSerializers::Model) do
|
54
|
-
derive_attributes_from_names_and_fix_accessors
|
55
|
-
attributes :one, :two, :three
|
56
|
-
end
|
57
|
-
original_attributes = { one: 1, two: 2, three: 3 }
|
58
|
-
original_instance = klass.new(original_attributes)
|
59
|
-
|
60
|
-
# Initial value
|
61
|
-
instance = original_instance
|
62
|
-
expected_attributes = { one: 1, two: 2, three: 3 }.with_indifferent_access
|
63
|
-
assert_equal expected_attributes, instance.attributes
|
64
|
-
assert_equal 1, instance.one
|
65
|
-
assert_equal 1, instance.read_attribute_for_serialization(:one)
|
66
|
-
|
67
|
-
expected_attributes = { one: :not_one, two: 2, three: 3 }.with_indifferent_access
|
68
|
-
# Change via accessor
|
69
|
-
instance = original_instance.dup
|
70
|
-
instance.one = :not_one
|
71
|
-
assert_equal expected_attributes, instance.attributes
|
72
|
-
assert_equal :not_one, instance.one
|
73
|
-
assert_equal :not_one, instance.read_attribute_for_serialization(:one)
|
74
|
-
|
75
|
-
# Attributes frozen
|
76
|
-
assert instance.attributes.frozen?
|
77
|
-
end
|
78
|
-
|
79
|
-
def test_id_attribute_can_be_read_for_serialization
|
80
|
-
klass = Class.new(ActiveModelSerializers::Model) do
|
81
|
-
attributes :id, :one, :two, :three
|
82
|
-
end
|
83
|
-
self.class.const_set(:SomeTestModel, klass)
|
84
|
-
original_attributes = { id: :ego, one: 1, two: 2, three: 3 }
|
85
|
-
original_instance = klass.new(original_attributes)
|
86
|
-
|
87
|
-
# Initial value
|
88
|
-
instance = original_instance.dup
|
89
|
-
expected_attributes = { id: :ego, one: 1, two: 2, three: 3 }.with_indifferent_access
|
90
|
-
assert_equal expected_attributes, instance.attributes
|
91
|
-
assert_equal :ego, instance.id
|
92
|
-
assert_equal :ego, instance.read_attribute_for_serialization(:id)
|
93
|
-
|
94
|
-
# FIXME: Change via accessor has no effect on attributes.
|
95
|
-
instance = original_instance.dup
|
96
|
-
instance.id = :superego
|
97
|
-
assert_equal expected_attributes, instance.attributes
|
98
|
-
assert_equal :superego, instance.id
|
99
|
-
assert_equal :superego, instance.read_attribute_for_serialization(:id)
|
100
|
-
|
101
|
-
# FIXME: Change via mutating attributes
|
102
|
-
instance = original_instance.dup
|
103
|
-
instance.attributes[:id] = :superego
|
104
|
-
expected_attributes = { id: :superego, one: 1, two: 2, three: 3 }.with_indifferent_access
|
105
|
-
assert_equal expected_attributes, instance.attributes
|
106
|
-
assert_equal :ego, instance.id
|
107
|
-
assert_equal :ego, instance.read_attribute_for_serialization(:id)
|
108
|
-
ensure
|
109
|
-
self.class.send(:remove_const, :SomeTestModel)
|
110
|
-
end
|
111
|
-
|
112
|
-
def test_id_attribute_can_be_read_for_serialization_with_attributes_accessors_fix
|
113
|
-
klass = Class.new(ActiveModelSerializers::Model) do
|
114
|
-
derive_attributes_from_names_and_fix_accessors
|
115
|
-
attributes :id, :one, :two, :three
|
116
|
-
end
|
117
|
-
self.class.const_set(:SomeTestModel, klass)
|
118
|
-
original_attributes = { id: :ego, one: 1, two: 2, three: 3 }
|
119
|
-
original_instance = klass.new(original_attributes)
|
120
|
-
|
121
|
-
# Initial value
|
122
|
-
instance = original_instance.dup
|
123
|
-
expected_attributes = { id: :ego, one: 1, two: 2, three: 3 }.with_indifferent_access
|
124
|
-
assert_equal expected_attributes, instance.attributes
|
125
|
-
assert_equal :ego, instance.id
|
126
|
-
assert_equal :ego, instance.read_attribute_for_serialization(:id)
|
127
|
-
|
128
|
-
expected_attributes = { id: :superego, one: 1, two: 2, three: 3 }.with_indifferent_access
|
129
|
-
# Change via accessor
|
130
|
-
instance = original_instance.dup
|
131
|
-
instance.id = :superego
|
132
|
-
assert_equal expected_attributes, instance.attributes
|
133
|
-
assert_equal :superego, instance.id
|
134
|
-
assert_equal :superego, instance.read_attribute_for_serialization(:id)
|
135
|
-
|
136
|
-
# Attributes frozen
|
137
|
-
assert instance.attributes.frozen?
|
138
|
-
ensure
|
139
|
-
self.class.send(:remove_const, :SomeTestModel)
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
@@ -1,68 +0,0 @@
|
|
1
|
-
# Execute this test in isolation
|
2
|
-
require 'support/isolated_unit'
|
3
|
-
|
4
|
-
class RailtieTest < ActiveSupport::TestCase
|
5
|
-
include ActiveSupport::Testing::Isolation
|
6
|
-
|
7
|
-
class WithRailsRequiredFirst < RailtieTest
|
8
|
-
setup do
|
9
|
-
require 'rails'
|
10
|
-
require 'active_model_serializers'
|
11
|
-
make_basic_app do |app|
|
12
|
-
app.config.action_controller.perform_caching = true
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
test 'mixes ActionController::Serialization into ActionController::Base' do
|
17
|
-
assert ActionController.const_defined?(:Serialization),
|
18
|
-
"ActionController::Serialization should be defined, but isn't"
|
19
|
-
assert ::ActionController::Base.included_modules.include?(::ActionController::Serialization),
|
20
|
-
"ActionController::Serialization should be included in ActionController::Base, but isn't"
|
21
|
-
end
|
22
|
-
|
23
|
-
test 'prepares url_helpers for SerializationContext' do
|
24
|
-
assert ActiveModelSerializers::SerializationContext.url_helpers.respond_to? :url_for
|
25
|
-
assert_equal Rails.application.routes.default_url_options,
|
26
|
-
ActiveModelSerializers::SerializationContext.default_url_options
|
27
|
-
end
|
28
|
-
|
29
|
-
test 'sets the ActiveModelSerializers.logger to Rails.logger' do
|
30
|
-
refute_nil Rails.logger
|
31
|
-
refute_nil ActiveModelSerializers.logger
|
32
|
-
assert_equal Rails.logger, ActiveModelSerializers.logger
|
33
|
-
end
|
34
|
-
|
35
|
-
test 'it is configured for caching' do
|
36
|
-
assert_equal ActionController::Base.cache_store, ActiveModelSerializers.config.cache_store
|
37
|
-
assert_equal true, Rails.configuration.action_controller.perform_caching
|
38
|
-
assert_equal true, ActiveModelSerializers.config.perform_caching
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
class WithoutRailsRequiredFirst < RailtieTest
|
43
|
-
setup do
|
44
|
-
require 'active_model_serializers'
|
45
|
-
make_basic_app do |app|
|
46
|
-
app.config.action_controller.perform_caching = true
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
test 'does not mix ActionController::Serialization into ActionController::Base' do
|
51
|
-
refute ActionController.const_defined?(:Serialization),
|
52
|
-
'ActionController::Serialization should not be defined, but is'
|
53
|
-
end
|
54
|
-
|
55
|
-
test 'has its own logger at ActiveModelSerializers.logger' do
|
56
|
-
refute_nil Rails.logger
|
57
|
-
refute_nil ActiveModelSerializers.logger
|
58
|
-
refute_equal Rails.logger, ActiveModelSerializers.logger
|
59
|
-
end
|
60
|
-
|
61
|
-
test 'it is not configured for caching' do
|
62
|
-
refute_nil ActionController::Base.cache_store
|
63
|
-
assert_nil ActiveModelSerializers.config.cache_store
|
64
|
-
assert_equal true, Rails.configuration.action_controller.perform_caching
|
65
|
-
assert_nil ActiveModelSerializers.config.perform_caching
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
@@ -1,161 +0,0 @@
|
|
1
|
-
require 'support/isolated_unit'
|
2
|
-
require 'minitest/mock'
|
3
|
-
require 'action_dispatch'
|
4
|
-
require 'action_controller'
|
5
|
-
|
6
|
-
class JsonApiRendererTest < ActionDispatch::IntegrationTest
|
7
|
-
include ActiveSupport::Testing::Isolation
|
8
|
-
|
9
|
-
class TestController < ActionController::Base
|
10
|
-
class << self
|
11
|
-
attr_accessor :last_request_parameters
|
12
|
-
end
|
13
|
-
|
14
|
-
def render_with_jsonapi_renderer
|
15
|
-
permitted_params = params.permit(data: [:id, :type, attributes: [:name]])
|
16
|
-
permitted_params = permitted_params.to_h.with_indifferent_access
|
17
|
-
attributes =
|
18
|
-
if permitted_params[:data]
|
19
|
-
permitted_params[:data][:attributes].merge(id: permitted_params[:data][:id])
|
20
|
-
else
|
21
|
-
# Rails returns empty params when no mime type can be negotiated.
|
22
|
-
# (Until https://github.com/rails/rails/pull/26632 is reviewed.)
|
23
|
-
permitted_params
|
24
|
-
end
|
25
|
-
author = Author.new(attributes)
|
26
|
-
render jsonapi: author
|
27
|
-
end
|
28
|
-
|
29
|
-
def parse
|
30
|
-
self.class.last_request_parameters = request.request_parameters
|
31
|
-
head :ok
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def teardown
|
36
|
-
TestController.last_request_parameters = nil
|
37
|
-
end
|
38
|
-
|
39
|
-
def assert_parses(expected, actual, headers = {})
|
40
|
-
post '/parse', params: actual, headers: headers
|
41
|
-
assert_response :ok
|
42
|
-
assert_equal(expected, TestController.last_request_parameters)
|
43
|
-
end
|
44
|
-
|
45
|
-
def define_author_model_and_serializer
|
46
|
-
TestController.const_set(:Author, Class.new(ActiveModelSerializers::Model) do
|
47
|
-
attributes :id, :name
|
48
|
-
end)
|
49
|
-
TestController.const_set(:AuthorSerializer, Class.new(ActiveModel::Serializer) do
|
50
|
-
type 'users'
|
51
|
-
attribute :id
|
52
|
-
attribute :name
|
53
|
-
end)
|
54
|
-
end
|
55
|
-
|
56
|
-
class WithoutRenderer < JsonApiRendererTest
|
57
|
-
setup do
|
58
|
-
require 'rails'
|
59
|
-
require 'active_record'
|
60
|
-
require 'support/rails5_shims'
|
61
|
-
require 'active_model_serializers'
|
62
|
-
require 'fixtures/poro'
|
63
|
-
|
64
|
-
make_basic_app
|
65
|
-
|
66
|
-
Rails.application.routes.draw do
|
67
|
-
ActiveSupport::Deprecation.silence do
|
68
|
-
match ':action', to: TestController, via: [:get, :post]
|
69
|
-
end
|
70
|
-
end
|
71
|
-
define_author_model_and_serializer
|
72
|
-
end
|
73
|
-
|
74
|
-
def test_jsonapi_parser_not_registered
|
75
|
-
parsers = if Rails::VERSION::MAJOR >= 5
|
76
|
-
ActionDispatch::Request.parameter_parsers
|
77
|
-
else
|
78
|
-
ActionDispatch::ParamsParser::DEFAULT_PARSERS
|
79
|
-
end
|
80
|
-
assert_nil parsers[Mime[:jsonapi]]
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_jsonapi_renderer_not_registered
|
84
|
-
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765"}}'
|
85
|
-
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
|
86
|
-
post '/render_with_jsonapi_renderer', params: payload, headers: headers
|
87
|
-
assert_equal '', response.body
|
88
|
-
assert_equal 500, response.status
|
89
|
-
assert_equal ActionView::MissingTemplate, request.env['action_dispatch.exception'].class
|
90
|
-
end
|
91
|
-
|
92
|
-
def test_jsonapi_parser
|
93
|
-
assert_parses(
|
94
|
-
{},
|
95
|
-
'',
|
96
|
-
'CONTENT_TYPE' => 'application/vnd.api+json'
|
97
|
-
)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
class WithRenderer < JsonApiRendererTest
|
102
|
-
setup do
|
103
|
-
require 'rails'
|
104
|
-
require 'active_record'
|
105
|
-
require 'support/rails5_shims'
|
106
|
-
require 'active_model_serializers'
|
107
|
-
require 'fixtures/poro'
|
108
|
-
require 'active_model_serializers/register_jsonapi_renderer'
|
109
|
-
|
110
|
-
make_basic_app
|
111
|
-
|
112
|
-
Rails.application.routes.draw do
|
113
|
-
ActiveSupport::Deprecation.silence do
|
114
|
-
match ':action', to: TestController, via: [:get, :post]
|
115
|
-
end
|
116
|
-
end
|
117
|
-
define_author_model_and_serializer
|
118
|
-
end
|
119
|
-
|
120
|
-
def test_jsonapi_parser_registered
|
121
|
-
if Rails::VERSION::MAJOR >= 5
|
122
|
-
parsers = ActionDispatch::Request.parameter_parsers
|
123
|
-
assert_equal Proc, parsers[:jsonapi].class
|
124
|
-
else
|
125
|
-
parsers = ActionDispatch::ParamsParser::DEFAULT_PARSERS
|
126
|
-
assert_equal Proc, parsers[Mime[:jsonapi]].class
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
def test_jsonapi_renderer_registered
|
131
|
-
expected = {
|
132
|
-
'data' => {
|
133
|
-
'id' => '36c9c04e-86b1-4636-a5b0-8616672d1765',
|
134
|
-
'type' => 'users',
|
135
|
-
'attributes' => { 'name' => 'Johnny Rico' }
|
136
|
-
}
|
137
|
-
}
|
138
|
-
|
139
|
-
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765"}}'
|
140
|
-
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
|
141
|
-
post '/render_with_jsonapi_renderer', params: payload, headers: headers
|
142
|
-
assert_equal expected.to_json, response.body
|
143
|
-
end
|
144
|
-
|
145
|
-
def test_jsonapi_parser
|
146
|
-
assert_parses(
|
147
|
-
{
|
148
|
-
'data' => {
|
149
|
-
'attributes' => {
|
150
|
-
'name' => 'John Doe'
|
151
|
-
},
|
152
|
-
'type' => 'users',
|
153
|
-
'id' => '36c9c04e-86b1-4636-a5b0-8616672d1765'
|
154
|
-
}
|
155
|
-
},
|
156
|
-
'{"data": {"attributes": {"name": "John Doe"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765"}}',
|
157
|
-
'CONTENT_TYPE' => 'application/vnd.api+json'
|
158
|
-
)
|
159
|
-
end
|
160
|
-
end
|
161
|
-
end
|
@@ -1,71 +0,0 @@
|
|
1
|
-
# Execute this test in isolation
|
2
|
-
require 'support/isolated_unit'
|
3
|
-
require 'minitest/mock'
|
4
|
-
|
5
|
-
class SerializationContextTest < ActiveSupport::TestCase
|
6
|
-
include ActiveSupport::Testing::Isolation
|
7
|
-
|
8
|
-
class WithRails < SerializationContextTest
|
9
|
-
def create_request
|
10
|
-
request = ActionDispatch::Request.new({})
|
11
|
-
def request.original_url
|
12
|
-
'http://example.com/articles?page=2'
|
13
|
-
end
|
14
|
-
|
15
|
-
def request.query_parameters
|
16
|
-
{ 'page' => 2 }
|
17
|
-
end
|
18
|
-
request
|
19
|
-
end
|
20
|
-
|
21
|
-
setup do
|
22
|
-
require 'rails'
|
23
|
-
require 'active_model_serializers'
|
24
|
-
make_basic_app
|
25
|
-
@context = ActiveModelSerializers::SerializationContext.new(create_request)
|
26
|
-
end
|
27
|
-
|
28
|
-
test 'create context with request url and query parameters' do
|
29
|
-
assert_equal @context.request_url, 'http://example.com/articles'
|
30
|
-
assert_equal @context.query_parameters, 'page' => 2
|
31
|
-
end
|
32
|
-
|
33
|
-
test 'url_helpers is set up for Rails url_helpers' do
|
34
|
-
assert_equal Module, ActiveModelSerializers::SerializationContext.url_helpers.class
|
35
|
-
assert ActiveModelSerializers::SerializationContext.url_helpers.respond_to? :url_for
|
36
|
-
end
|
37
|
-
|
38
|
-
test 'default_url_options returns Rails.application.routes.default_url_options' do
|
39
|
-
assert_equal Rails.application.routes.default_url_options,
|
40
|
-
ActiveModelSerializers::SerializationContext.default_url_options
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
class WithoutRails < SerializationContextTest
|
45
|
-
def create_request
|
46
|
-
{
|
47
|
-
request_url: 'http://example.com/articles',
|
48
|
-
query_parameters: { 'page' => 2 }
|
49
|
-
}
|
50
|
-
end
|
51
|
-
|
52
|
-
setup do
|
53
|
-
require 'active_model_serializers/serialization_context'
|
54
|
-
@context = ActiveModelSerializers::SerializationContext.new(create_request)
|
55
|
-
end
|
56
|
-
|
57
|
-
test 'create context with request url and query parameters' do
|
58
|
-
assert_equal @context.request_url, 'http://example.com/articles'
|
59
|
-
assert_equal @context.query_parameters, 'page' => 2
|
60
|
-
end
|
61
|
-
|
62
|
-
test 'url_helpers is a module when Rails is not present' do
|
63
|
-
assert_equal Module, ActiveModelSerializers::SerializationContext.url_helpers.class
|
64
|
-
refute ActiveModelSerializers::SerializationContext.url_helpers.respond_to? :url_for
|
65
|
-
end
|
66
|
-
|
67
|
-
test 'default_url_options return a Hash' do
|
68
|
-
assert Hash, ActiveModelSerializers::SerializationContext.default_url_options.class
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
@@ -1,131 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModelSerializers
|
4
|
-
module Test
|
5
|
-
class SchemaTest < ActionController::TestCase
|
6
|
-
include ActiveModelSerializers::Test::Schema
|
7
|
-
|
8
|
-
class MyController < ActionController::Base
|
9
|
-
def index
|
10
|
-
render json: profile
|
11
|
-
end
|
12
|
-
|
13
|
-
def show
|
14
|
-
index
|
15
|
-
end
|
16
|
-
|
17
|
-
def name_as_a_integer
|
18
|
-
profile.name = 1
|
19
|
-
index
|
20
|
-
end
|
21
|
-
|
22
|
-
def render_using_json_api
|
23
|
-
render json: profile, adapter: :json_api
|
24
|
-
end
|
25
|
-
|
26
|
-
def invalid_json_body
|
27
|
-
render json: ''
|
28
|
-
end
|
29
|
-
|
30
|
-
private
|
31
|
-
|
32
|
-
def profile
|
33
|
-
@profile ||= Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
tests MyController
|
38
|
-
|
39
|
-
def test_that_assert_with_a_valid_schema
|
40
|
-
get :index
|
41
|
-
assert_response_schema
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_that_raises_a_minitest_error_with_a_invalid_schema
|
45
|
-
message = "#/name: failed schema #/properties/name: For 'properties/name', \"Name 1\" is not an integer. and #/description: failed schema #/properties/description: For 'properties/description', \"Description 1\" is not a boolean."
|
46
|
-
|
47
|
-
get :show
|
48
|
-
|
49
|
-
error = assert_raises Minitest::Assertion do
|
50
|
-
assert_response_schema
|
51
|
-
end
|
52
|
-
assert_equal(message, error.message)
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_that_raises_error_with_a_custom_message_with_a_invalid_schema
|
56
|
-
message = 'oh boy the show is broken'
|
57
|
-
exception_message = "#/name: failed schema #/properties/name: For 'properties/name', \"Name 1\" is not an integer. and #/description: failed schema #/properties/description: For 'properties/description', \"Description 1\" is not a boolean."
|
58
|
-
expected_message = "#{message}: #{exception_message}"
|
59
|
-
|
60
|
-
get :show
|
61
|
-
|
62
|
-
error = assert_raises Minitest::Assertion do
|
63
|
-
assert_response_schema(nil, message)
|
64
|
-
end
|
65
|
-
assert_equal(expected_message, error.message)
|
66
|
-
end
|
67
|
-
|
68
|
-
def test_that_assert_with_a_custom_schema
|
69
|
-
get :show
|
70
|
-
assert_response_schema('custom/show.json')
|
71
|
-
end
|
72
|
-
|
73
|
-
def test_that_assert_with_a_hyper_schema
|
74
|
-
get :show
|
75
|
-
assert_response_schema('hyper_schema.json')
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_simple_json_pointers
|
79
|
-
get :show
|
80
|
-
assert_response_schema('simple_json_pointers.json')
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_simple_json_pointers_that_doesnt_match
|
84
|
-
get :name_as_a_integer
|
85
|
-
|
86
|
-
assert_raises Minitest::Assertion do
|
87
|
-
assert_response_schema('simple_json_pointers.json')
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
def test_json_api_schema
|
92
|
-
get :render_using_json_api
|
93
|
-
assert_response_schema('render_using_json_api.json')
|
94
|
-
end
|
95
|
-
|
96
|
-
def test_that_assert_with_a_custom_schema_directory
|
97
|
-
original_schema_path = ActiveModelSerializers.config.schema_path
|
98
|
-
ActiveModelSerializers.config.schema_path = 'test/support/custom_schemas'
|
99
|
-
|
100
|
-
get :index
|
101
|
-
assert_response_schema
|
102
|
-
|
103
|
-
ActiveModelSerializers.config.schema_path = original_schema_path
|
104
|
-
end
|
105
|
-
|
106
|
-
def test_with_a_non_existent_file
|
107
|
-
message = 'No Schema file at test/support/schemas/non-existent.json'
|
108
|
-
|
109
|
-
get :show
|
110
|
-
|
111
|
-
error = assert_raises ActiveModelSerializers::Test::Schema::MissingSchema do
|
112
|
-
assert_response_schema('non-existent.json')
|
113
|
-
end
|
114
|
-
assert_equal(message, error.message)
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_that_raises_with_a_invalid_json_body
|
118
|
-
# message changes from JSON gem 2.0.2 to 2.2.0
|
119
|
-
message = /A JSON text must at least contain two octets!|unexpected token at ''/
|
120
|
-
|
121
|
-
get :invalid_json_body
|
122
|
-
|
123
|
-
error = assert_raises ActiveModelSerializers::Test::Schema::InvalidSchemaError do
|
124
|
-
assert_response_schema('custom/show.json')
|
125
|
-
end
|
126
|
-
|
127
|
-
assert_match(message, error.message)
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|