active_model_serializers 0.8.3 → 0.10.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/ISSUE_TEMPLATE.md +29 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.gitignore +17 -0
- data/.rubocop.yml +105 -0
- data/.simplecov +110 -0
- data/.travis.yml +50 -24
- data/CHANGELOG.md +650 -6
- data/CODE_OF_CONDUCT.md +74 -0
- data/CONTRIBUTING.md +105 -0
- data/Gemfile +69 -1
- data/{MIT-LICENSE.txt → MIT-LICENSE} +3 -2
- data/README.md +195 -545
- data/Rakefile +64 -8
- data/active_model_serializers.gemspec +62 -23
- data/appveyor.yml +28 -0
- data/bin/bench +171 -0
- data/bin/bench_regression +316 -0
- data/bin/rubocop +38 -0
- data/bin/serve_benchmark +39 -0
- data/docs/README.md +41 -0
- data/docs/STYLE.md +58 -0
- data/docs/general/adapters.md +269 -0
- data/docs/general/caching.md +58 -0
- data/docs/general/configuration_options.md +185 -0
- data/docs/general/deserialization.md +100 -0
- data/docs/general/fields.md +31 -0
- data/docs/general/getting_started.md +133 -0
- data/docs/general/instrumentation.md +40 -0
- data/docs/general/key_transforms.md +40 -0
- data/docs/general/logging.md +21 -0
- data/docs/general/rendering.md +293 -0
- data/docs/general/serializers.md +495 -0
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +138 -0
- data/docs/howto/add_relationship_links.md +140 -0
- data/docs/howto/add_root_key.md +62 -0
- data/docs/howto/grape_integration.md +42 -0
- data/docs/howto/outside_controller_use.md +66 -0
- data/docs/howto/passing_arbitrary_options.md +27 -0
- data/docs/howto/serialize_poro.md +73 -0
- data/docs/howto/test.md +154 -0
- data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
- data/docs/integrations/ember-and-json-api.md +147 -0
- data/docs/integrations/grape.md +19 -0
- data/docs/jsonapi/errors.md +56 -0
- data/docs/jsonapi/schema/schema.json +366 -0
- data/docs/jsonapi/schema.md +151 -0
- data/docs/rfcs/0000-namespace.md +106 -0
- data/docs/rfcs/template.md +15 -0
- data/lib/action_controller/serialization.rb +43 -38
- data/lib/active_model/serializable_resource.rb +11 -0
- data/lib/active_model/serializer/adapter/attributes.rb +15 -0
- data/lib/active_model/serializer/adapter/base.rb +18 -0
- data/lib/active_model/serializer/adapter/json.rb +15 -0
- data/lib/active_model/serializer/adapter/json_api.rb +15 -0
- data/lib/active_model/serializer/adapter/null.rb +15 -0
- data/lib/active_model/serializer/adapter.rb +24 -0
- data/lib/active_model/serializer/array_serializer.rb +12 -0
- data/lib/active_model/serializer/association.rb +71 -0
- data/lib/active_model/serializer/attribute.rb +25 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +11 -0
- data/lib/active_model/serializer/collection_serializer.rb +88 -0
- data/lib/active_model/serializer/concerns/caching.rb +300 -0
- data/lib/active_model/serializer/error_serializer.rb +14 -0
- data/lib/active_model/serializer/errors_serializer.rb +32 -0
- data/lib/active_model/serializer/field.rb +90 -0
- data/lib/active_model/serializer/fieldset.rb +31 -0
- data/lib/active_model/serializer/has_many_reflection.rb +10 -0
- data/lib/active_model/serializer/has_one_reflection.rb +7 -0
- data/lib/active_model/serializer/lazy_association.rb +96 -0
- data/lib/active_model/serializer/link.rb +21 -0
- data/lib/active_model/serializer/lint.rb +150 -0
- data/lib/active_model/serializer/null.rb +17 -0
- data/lib/active_model/serializer/reflection.rb +210 -0
- data/lib/active_model/{serializers → serializer}/version.rb +1 -1
- data/lib/active_model/serializer.rb +343 -442
- data/lib/active_model_serializers/adapter/attributes.rb +13 -0
- data/lib/active_model_serializers/adapter/base.rb +83 -0
- data/lib/active_model_serializers/adapter/json.rb +21 -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 +88 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +104 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +66 -0
- data/lib/active_model_serializers/adapter/json_api.rb +533 -0
- data/lib/active_model_serializers/adapter/null.rb +9 -0
- data/lib/active_model_serializers/adapter.rb +98 -0
- data/lib/active_model_serializers/callbacks.rb +55 -0
- data/lib/active_model_serializers/deprecate.rb +54 -0
- data/lib/active_model_serializers/deserialization.rb +15 -0
- data/lib/active_model_serializers/json_pointer.rb +14 -0
- data/lib/active_model_serializers/logging.rb +122 -0
- data/lib/active_model_serializers/lookup_chain.rb +80 -0
- data/lib/active_model_serializers/model.rb +130 -0
- data/lib/active_model_serializers/railtie.rb +50 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +78 -0
- data/lib/active_model_serializers/serializable_resource.rb +82 -0
- data/lib/active_model_serializers/serialization_context.rb +39 -0
- data/lib/active_model_serializers/test/schema.rb +138 -0
- data/lib/active_model_serializers/test/serializer.rb +125 -0
- data/lib/active_model_serializers/test.rb +7 -0
- data/lib/active_model_serializers.rb +47 -81
- data/lib/generators/rails/USAGE +6 -0
- data/lib/generators/rails/resource_override.rb +10 -0
- data/lib/generators/rails/serializer_generator.rb +36 -0
- data/lib/generators/rails/templates/serializer.rb.erb +8 -0
- data/lib/grape/active_model_serializers.rb +16 -0
- data/lib/grape/formatters/active_model_serializers.rb +32 -0
- data/lib/grape/helpers/active_model_serializers.rb +17 -0
- data/lib/tasks/rubocop.rake +53 -0
- data/test/action_controller/adapter_selector_test.rb +62 -0
- data/test/action_controller/explicit_serializer_test.rb +135 -0
- data/test/action_controller/json/include_test.rb +246 -0
- data/test/action_controller/json_api/deserialization_test.rb +112 -0
- data/test/action_controller/json_api/errors_test.rb +40 -0
- data/test/action_controller/json_api/fields_test.rb +66 -0
- data/test/action_controller/json_api/linked_test.rb +202 -0
- data/test/action_controller/json_api/pagination_test.rb +124 -0
- data/test/action_controller/json_api/transform_test.rb +189 -0
- data/test/action_controller/lookup_proc_test.rb +49 -0
- data/test/action_controller/namespace_lookup_test.rb +232 -0
- data/test/action_controller/serialization_scope_name_test.rb +235 -0
- data/test/action_controller/serialization_test.rb +478 -0
- data/test/active_model_serializers/adapter_for_test.rb +208 -0
- data/test/active_model_serializers/json_pointer_test.rb +22 -0
- data/test/active_model_serializers/logging_test.rb +77 -0
- data/test/active_model_serializers/model_test.rb +142 -0
- data/test/active_model_serializers/railtie_test_isolated.rb +68 -0
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +161 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +71 -0
- data/test/active_model_serializers/test/schema_test.rb +131 -0
- data/test/active_model_serializers/test/serializer_test.rb +62 -0
- data/test/active_record_test.rb +9 -0
- data/test/adapter/attributes_test.rb +40 -0
- data/test/adapter/deprecation_test.rb +100 -0
- data/test/adapter/json/belongs_to_test.rb +45 -0
- data/test/adapter/json/collection_test.rb +104 -0
- data/test/adapter/json/has_many_test.rb +53 -0
- data/test/adapter/json/transform_test.rb +93 -0
- data/test/adapter/json_api/belongs_to_test.rb +155 -0
- data/test/adapter/json_api/collection_test.rb +96 -0
- data/test/adapter/json_api/errors_test.rb +76 -0
- data/test/adapter/json_api/fields_test.rb +96 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +96 -0
- data/test/adapter/json_api/has_many_test.rb +173 -0
- data/test/adapter/json_api/has_one_test.rb +80 -0
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +213 -0
- data/test/adapter/json_api/json_api_test.rb +33 -0
- data/test/adapter/json_api/linked_test.rb +413 -0
- data/test/adapter/json_api/links_test.rb +110 -0
- data/test/adapter/json_api/pagination_links_test.rb +206 -0
- data/test/adapter/json_api/parse_test.rb +137 -0
- data/test/adapter/json_api/relationship_test.rb +397 -0
- data/test/adapter/json_api/resource_meta_test.rb +100 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +82 -0
- data/test/adapter/json_api/transform_test.rb +512 -0
- data/test/adapter/json_api/type_test.rb +193 -0
- data/test/adapter/json_test.rb +46 -0
- data/test/adapter/null_test.rb +22 -0
- data/test/adapter/polymorphic_test.rb +218 -0
- data/test/adapter_test.rb +67 -0
- data/test/array_serializer_test.rb +20 -73
- data/test/benchmark/app.rb +65 -0
- data/test/benchmark/benchmarking_support.rb +67 -0
- data/test/benchmark/bm_active_record.rb +81 -0
- data/test/benchmark/bm_adapter.rb +38 -0
- data/test/benchmark/bm_caching.rb +119 -0
- data/test/benchmark/bm_lookup_chain.rb +83 -0
- data/test/benchmark/bm_transform.rb +45 -0
- data/test/benchmark/config.ru +3 -0
- data/test/benchmark/controllers.rb +83 -0
- data/test/benchmark/fixtures.rb +219 -0
- data/test/cache_test.rb +651 -0
- data/test/collection_serializer_test.rb +127 -0
- data/test/fixtures/active_record.rb +113 -0
- data/test/fixtures/poro.rb +225 -0
- data/test/generators/scaffold_controller_generator_test.rb +24 -0
- data/test/generators/serializer_generator_test.rb +75 -0
- data/test/grape_test.rb +196 -0
- data/test/lint_test.rb +49 -0
- data/test/logger_test.rb +20 -0
- data/test/poro_test.rb +9 -0
- data/test/serializable_resource_test.rb +79 -0
- data/test/serializers/association_macros_test.rb +37 -0
- data/test/serializers/associations_test.rb +518 -0
- data/test/serializers/attribute_test.rb +153 -0
- data/test/serializers/attributes_test.rb +52 -0
- data/test/serializers/caching_configuration_test_isolated.rb +170 -0
- data/test/serializers/configuration_test.rb +32 -0
- data/test/serializers/fieldset_test.rb +14 -0
- data/test/serializers/meta_test.rb +202 -0
- data/test/serializers/options_test.rb +32 -0
- data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
- data/test/serializers/reflection_test.rb +479 -0
- data/test/serializers/root_test.rb +21 -0
- data/test/serializers/serialization_test.rb +55 -0
- data/test/serializers/serializer_for_test.rb +136 -0
- data/test/serializers/serializer_for_with_namespace_test.rb +88 -0
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/isolated_unit.rb +84 -0
- data/test/support/rails5_shims.rb +53 -0
- data/test/support/rails_app.rb +38 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
- data/test/support/schemas/custom/show.json +7 -0
- data/test/support/schemas/hyper_schema.json +93 -0
- data/test/support/schemas/render_using_json_api.json +43 -0
- data/test/support/schemas/simple_json_pointers.json +10 -0
- data/test/support/serialization_testing.rb +79 -0
- data/test/test_helper.rb +59 -21
- metadata +529 -43
- data/DESIGN.textile +0 -586
- data/Gemfile.edge +0 -9
- data/bench/perf.rb +0 -43
- data/cruft.md +0 -19
- data/lib/active_model/array_serializer.rb +0 -104
- data/lib/active_model/serializer/associations.rb +0 -233
- data/lib/active_record/serializer_override.rb +0 -16
- data/lib/generators/resource_override.rb +0 -13
- data/lib/generators/serializer/USAGE +0 -9
- data/lib/generators/serializer/serializer_generator.rb +0 -42
- data/lib/generators/serializer/templates/serializer.rb +0 -19
- data/test/association_test.rb +0 -592
- data/test/caching_test.rb +0 -96
- data/test/generators_test.rb +0 -85
- data/test/no_serialization_scope_test.rb +0 -34
- data/test/serialization_scope_name_test.rb +0 -67
- data/test/serialization_test.rb +0 -392
- data/test/serializer_support_test.rb +0 -51
- data/test/serializer_test.rb +0 -1465
- data/test/test_fakes.rb +0 -217
@@ -0,0 +1,77 @@
|
|
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
|
@@ -0,0 +1,142 @@
|
|
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
|
@@ -0,0 +1,68 @@
|
|
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
|
@@ -0,0 +1,161 @@
|
|
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
|
@@ -0,0 +1,71 @@
|
|
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
|
@@ -0,0 +1,131 @@
|
|
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
|