active_model_serializers 0.10.3 → 0.10.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +154 -2
- data/README.md +153 -15
- data/lib/action_controller/serialization.rb +11 -1
- data/lib/active_model/serializable_resource.rb +2 -0
- data/lib/active_model/serializer.rb +275 -81
- data/lib/active_model/serializer/adapter.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/array_serializer.rb +2 -0
- data/lib/active_model/serializer/association.rb +53 -14
- data/lib/active_model/serializer/attribute.rb +2 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +7 -1
- data/lib/active_model/serializer/collection_serializer.rb +8 -5
- data/lib/active_model/serializer/concerns/caching.rb +36 -23
- 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 +6 -1
- data/lib/active_model/serializer/has_one_reflection.rb +3 -1
- data/lib/active_model/serializer/lazy_association.rb +99 -0
- 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 +122 -73
- data/lib/active_model/serializer/version.rb +3 -1
- data/lib/active_model_serializers.rb +29 -11
- data/lib/active_model_serializers/adapter.rb +3 -1
- data/lib/active_model_serializers/adapter/attributes.rb +23 -0
- data/lib/active_model_serializers/adapter/base.rb +4 -2
- data/lib/active_model_serializers/adapter/json.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api.rb +44 -26
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +4 -2
- 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 +42 -21
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -9
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +35 -18
- data/lib/active_model_serializers/adapter/null.rb +2 -0
- 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.rb +111 -30
- data/lib/active_model_serializers/model/caching.rb +25 -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.rb +2 -0
- data/lib/active_model_serializers/test/schema.rb +4 -2
- data/lib/active_model_serializers/test/serializer.rb +2 -0
- data/lib/generators/rails/resource_override.rb +3 -1
- 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 +55 -0
- metadata +74 -291
- data/.github/ISSUE_TEMPLATE.md +0 -29
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
- data/.gitignore +0 -35
- data/.rubocop.yml +0 -102
- data/.simplecov +0 -110
- data/.travis.yml +0 -51
- data/CODE_OF_CONDUCT.md +0 -74
- data/CONTRIBUTING.md +0 -105
- data/Gemfile +0 -56
- data/Rakefile +0 -103
- data/active_model_serializers.gemspec +0 -62
- data/appveyor.yml +0 -24
- data/bin/bench +0 -171
- data/bin/bench_regression +0 -316
- data/bin/serve_benchmark +0 -39
- data/docs/ARCHITECTURE.md +0 -125
- data/docs/README.md +0 -42
- data/docs/STYLE.md +0 -58
- data/docs/general/adapters.md +0 -247
- 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 -14
- data/docs/general/rendering.md +0 -294
- data/docs/general/serializers.md +0 -461
- 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 -137
- 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 -65
- data/docs/howto/passing_arbitrary_options.md +0 -27
- data/docs/howto/serialize_poro.md +0 -32
- 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 -144
- data/docs/integrations/grape.md +0 -19
- data/docs/jsonapi/errors.md +0 -56
- data/docs/jsonapi/schema.md +0 -151
- data/docs/jsonapi/schema/schema.json +0 -366
- data/docs/rfcs/0000-namespace.md +0 -106
- data/docs/rfcs/template.md +0 -15
- data/lib/active_model/serializer/collection_reflection.rb +0 -7
- data/lib/active_model/serializer/concerns/associations.rb +0 -102
- data/lib/active_model/serializer/concerns/attributes.rb +0 -82
- data/lib/active_model/serializer/concerns/configuration.rb +0 -59
- data/lib/active_model/serializer/concerns/links.rb +0 -35
- data/lib/active_model/serializer/concerns/meta.rb +0 -29
- data/lib/active_model/serializer/concerns/type.rb +0 -25
- data/lib/active_model/serializer/singular_reflection.rb +0 -7
- data/lib/active_model_serializers/key_transform.rb +0 -74
- data/test/action_controller/adapter_selector_test.rb +0 -53
- 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 -57
- 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 -181
- data/test/action_controller/lookup_proc_test.rb +0 -49
- data/test/action_controller/namespace_lookup_test.rb +0 -226
- data/test/action_controller/serialization_scope_name_test.rb +0 -229
- 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/key_transform_test.rb +0 -297
- data/test/active_model_serializers/logging_test.rb +0 -77
- data/test/active_model_serializers/model_test.rb +0 -22
- data/test/active_model_serializers/railtie_test_isolated.rb +0 -63
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -143
- data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -71
- data/test/active_model_serializers/test/schema_test.rb +0 -130
- 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 -43
- 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 -45
- 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 -88
- 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 -165
- data/test/adapter/json_api/has_one_test.rb +0 -80
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -166
- 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 -504
- 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 -579
- data/test/collection_serializer_test.rb +0 -110
- data/test/fixtures/active_record.rb +0 -78
- data/test/fixtures/poro.rb +0 -286
- data/test/generators/scaffold_controller_generator_test.rb +0 -24
- data/test/generators/serializer_generator_test.rb +0 -74
- data/test/grape_test.rb +0 -178
- 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 -370
- data/test/serializers/attribute_test.rb +0 -151
- 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 -21
- data/test/serializers/read_attribute_for_serialization_test.rb +0 -79
- 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 -87
- 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 -36
- 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 -58
@@ -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,22 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModelSerializers
|
4
|
-
class ModelTest < ActiveSupport::TestCase
|
5
|
-
include ActiveModel::Serializer::Lint::Tests
|
6
|
-
|
7
|
-
def setup
|
8
|
-
@resource = ActiveModelSerializers::Model.new
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_initialization_with_string_keys
|
12
|
-
klass = Class.new(ActiveModelSerializers::Model) do
|
13
|
-
attr_accessor :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
|
-
end
|
22
|
-
end
|
@@ -1,63 +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 WithRails < RailtieTest
|
8
|
-
setup do
|
9
|
-
require 'rails'
|
10
|
-
require 'active_model_serializers'
|
11
|
-
make_basic_app
|
12
|
-
end
|
13
|
-
|
14
|
-
test 'mixes ActionController::Serialization into ActionController::Base' do
|
15
|
-
assert ActionController.const_defined?(:Serialization),
|
16
|
-
"ActionController::Serialization should be defined, but isn't"
|
17
|
-
assert ::ActionController::Base.included_modules.include?(::ActionController::Serialization),
|
18
|
-
"ActionController::Serialization should be included in ActionController::Base, but isn't"
|
19
|
-
end
|
20
|
-
|
21
|
-
test 'prepares url_helpers for SerializationContext' do
|
22
|
-
assert ActiveModelSerializers::SerializationContext.url_helpers.respond_to? :url_for
|
23
|
-
assert_equal Rails.application.routes.default_url_options,
|
24
|
-
ActiveModelSerializers::SerializationContext.default_url_options
|
25
|
-
end
|
26
|
-
|
27
|
-
test 'sets the ActiveModelSerializers.logger to Rails.logger' do
|
28
|
-
refute_nil Rails.logger
|
29
|
-
refute_nil ActiveModelSerializers.logger
|
30
|
-
assert_equal Rails.logger, ActiveModelSerializers.logger
|
31
|
-
end
|
32
|
-
|
33
|
-
test 'it is configured for caching' do
|
34
|
-
assert_equal ActionController::Base.cache_store, ActiveModelSerializers.config.cache_store
|
35
|
-
assert_equal Rails.configuration.action_controller.perform_caching, ActiveModelSerializers.config.perform_caching
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
class WithoutRails < RailtieTest
|
40
|
-
setup do
|
41
|
-
require 'active_model_serializers'
|
42
|
-
make_basic_app
|
43
|
-
end
|
44
|
-
|
45
|
-
test 'does not mix ActionController::Serialization into ActionController::Base' do
|
46
|
-
refute ActionController.const_defined?(:Serialization),
|
47
|
-
'ActionController::Serialization should not be defined, but is'
|
48
|
-
end
|
49
|
-
|
50
|
-
test 'has its own logger at ActiveModelSerializers.logger' do
|
51
|
-
refute_nil Rails.logger
|
52
|
-
refute_nil ActiveModelSerializers.logger
|
53
|
-
refute_equal Rails.logger, ActiveModelSerializers.logger
|
54
|
-
end
|
55
|
-
|
56
|
-
test 'it is not configured for caching' do
|
57
|
-
refute_nil ActionController::Base.cache_store
|
58
|
-
assert_nil ActiveModelSerializers.config.cache_store
|
59
|
-
refute Rails.configuration.action_controller.perform_caching
|
60
|
-
refute ActiveModelSerializers.config.perform_caching
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
@@ -1,143 +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
|
-
author = Author.new(params[:data][:attributes])
|
16
|
-
render jsonapi: author
|
17
|
-
end
|
18
|
-
|
19
|
-
def parse
|
20
|
-
self.class.last_request_parameters = request.request_parameters
|
21
|
-
head :ok
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def teardown
|
26
|
-
TestController.last_request_parameters = nil
|
27
|
-
end
|
28
|
-
|
29
|
-
def assert_parses(expected, actual, headers = {})
|
30
|
-
post '/parse', params: actual, headers: headers
|
31
|
-
assert_response :ok
|
32
|
-
assert_equal(expected, TestController.last_request_parameters)
|
33
|
-
end
|
34
|
-
|
35
|
-
class WithoutRenderer < JsonApiRendererTest
|
36
|
-
setup do
|
37
|
-
require 'rails'
|
38
|
-
require 'active_record'
|
39
|
-
require 'support/rails5_shims'
|
40
|
-
require 'active_model_serializers'
|
41
|
-
require 'fixtures/poro'
|
42
|
-
|
43
|
-
make_basic_app
|
44
|
-
|
45
|
-
Rails.application.routes.draw do
|
46
|
-
ActiveSupport::Deprecation.silence do
|
47
|
-
match ':action', to: TestController, via: [:get, :post]
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_jsonapi_parser_not_registered
|
53
|
-
parsers = if Rails::VERSION::MAJOR >= 5
|
54
|
-
ActionDispatch::Request.parameter_parsers
|
55
|
-
else
|
56
|
-
ActionDispatch::ParamsParser::DEFAULT_PARSERS
|
57
|
-
end
|
58
|
-
assert_nil parsers[Mime[:jsonapi]]
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_jsonapi_renderer_not_registered
|
62
|
-
expected = {
|
63
|
-
'data' => {
|
64
|
-
'attributes' => {
|
65
|
-
'name' => 'Johnny Rico'
|
66
|
-
},
|
67
|
-
'type' => 'users'
|
68
|
-
}
|
69
|
-
}
|
70
|
-
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}'
|
71
|
-
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
|
72
|
-
post '/render_with_jsonapi_renderer', params: payload, headers: headers
|
73
|
-
assert expected, response.body
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_jsonapi_parser
|
77
|
-
assert_parses(
|
78
|
-
{},
|
79
|
-
'',
|
80
|
-
'CONTENT_TYPE' => 'application/vnd.api+json'
|
81
|
-
)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
class WithRenderer < JsonApiRendererTest
|
86
|
-
setup do
|
87
|
-
require 'rails'
|
88
|
-
require 'active_record'
|
89
|
-
require 'support/rails5_shims'
|
90
|
-
require 'active_model_serializers'
|
91
|
-
require 'fixtures/poro'
|
92
|
-
require 'active_model_serializers/register_jsonapi_renderer'
|
93
|
-
|
94
|
-
make_basic_app
|
95
|
-
|
96
|
-
Rails.application.routes.draw do
|
97
|
-
ActiveSupport::Deprecation.silence do
|
98
|
-
match ':action', to: TestController, via: [:get, :post]
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_jsonapi_parser_registered
|
104
|
-
if Rails::VERSION::MAJOR >= 5
|
105
|
-
parsers = ActionDispatch::Request.parameter_parsers
|
106
|
-
assert_equal Proc, parsers[:jsonapi].class
|
107
|
-
else
|
108
|
-
parsers = ActionDispatch::ParamsParser::DEFAULT_PARSERS
|
109
|
-
assert_equal Proc, parsers[Mime[:jsonapi]].class
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
def test_jsonapi_renderer_registered
|
114
|
-
expected = {
|
115
|
-
'data' => {
|
116
|
-
'attributes' => {
|
117
|
-
'name' => 'Johnny Rico'
|
118
|
-
},
|
119
|
-
'type' => 'users'
|
120
|
-
}
|
121
|
-
}
|
122
|
-
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}'
|
123
|
-
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
|
124
|
-
post '/render_with_jsonapi_renderer', params: payload, headers: headers
|
125
|
-
assert expected, response.body
|
126
|
-
end
|
127
|
-
|
128
|
-
def test_jsonapi_parser
|
129
|
-
assert_parses(
|
130
|
-
{
|
131
|
-
'data' => {
|
132
|
-
'attributes' => {
|
133
|
-
'name' => 'John Doe'
|
134
|
-
},
|
135
|
-
'type' => 'users'
|
136
|
-
}
|
137
|
-
},
|
138
|
-
'{"data": {"attributes": {"name": "John Doe"}, "type": "users"}}',
|
139
|
-
'CONTENT_TYPE' => 'application/vnd.api+json'
|
140
|
-
)
|
141
|
-
end
|
142
|
-
end
|
143
|
-
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,130 +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 = 'A JSON text must at least contain two octets!'
|
119
|
-
|
120
|
-
get :invalid_json_body
|
121
|
-
|
122
|
-
error = assert_raises ActiveModelSerializers::Test::Schema::InvalidSchemaError do
|
123
|
-
assert_response_schema('custom/show.json')
|
124
|
-
end
|
125
|
-
|
126
|
-
assert_equal(message, error.message)
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|