active_model_serializers 0.8.3 → 0.10.0.rc3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +5 -0
- data/.rubocop.yml +82 -0
- data/.rubocop_todo.yml +315 -0
- data/.simplecov +99 -0
- data/.travis.yml +26 -20
- data/CHANGELOG.md +14 -67
- data/CONTRIBUTING.md +31 -0
- data/Gemfile +45 -1
- data/{MIT-LICENSE.txt → LICENSE.txt} +3 -2
- data/README.md +186 -488
- data/Rakefile +33 -12
- data/active_model_serializers.gemspec +49 -23
- data/appveyor.yml +25 -0
- data/docs/README.md +29 -0
- data/docs/general/adapters.md +110 -0
- data/docs/general/configuration_options.md +11 -0
- data/docs/general/getting_started.md +73 -0
- data/docs/howto/add_pagination_links.md +112 -0
- data/docs/howto/add_root_key.md +51 -0
- data/docs/howto/outside_controller_use.md +42 -0
- data/lib/action_controller/serialization.rb +31 -31
- data/lib/active_model/serializable_resource.rb +70 -0
- data/lib/active_model/serializer/adapter/flatten_json.rb +12 -0
- data/lib/active_model/serializer/adapter/fragment_cache.rb +75 -0
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +5 -0
- data/lib/active_model/serializer/adapter/json.rb +47 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +13 -0
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +50 -0
- data/lib/active_model/serializer/adapter/json_api.rb +158 -0
- data/lib/active_model/serializer/adapter/null.rb +5 -0
- data/lib/active_model/serializer/adapter.rb +159 -0
- data/lib/active_model/serializer/array_serializer.rb +40 -0
- data/lib/active_model/serializer/association.rb +20 -0
- data/lib/active_model/serializer/associations.rb +83 -219
- data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
- data/lib/active_model/serializer/collection_reflection.rb +7 -0
- data/lib/active_model/serializer/configuration.rb +14 -0
- data/lib/active_model/serializer/fieldset.rb +40 -0
- data/lib/active_model/serializer/has_many_reflection.rb +10 -0
- data/lib/active_model/serializer/has_one_reflection.rb +10 -0
- data/lib/active_model/serializer/lint.rb +129 -0
- data/lib/active_model/serializer/railtie.rb +15 -0
- data/lib/active_model/serializer/reflection.rb +74 -0
- data/lib/active_model/serializer/singular_reflection.rb +7 -0
- data/lib/active_model/serializer/utils.rb +35 -0
- data/lib/active_model/{serializers → serializer}/version.rb +1 -1
- data/lib/active_model/serializer.rb +121 -465
- data/lib/active_model_serializers.rb +26 -88
- data/lib/generators/serializer/USAGE +0 -3
- data/lib/generators/serializer/resource_override.rb +12 -0
- data/lib/generators/serializer/serializer_generator.rb +8 -13
- data/lib/generators/serializer/templates/serializer.rb.erb +8 -0
- data/lib/tasks/rubocop.rake +0 -0
- data/test/action_controller/adapter_selector_test.rb +53 -0
- data/test/action_controller/explicit_serializer_test.rb +134 -0
- data/test/action_controller/json_api/linked_test.rb +179 -0
- data/test/action_controller/json_api/pagination_test.rb +116 -0
- data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb} +15 -15
- data/test/action_controller/serialization_test.rb +420 -0
- data/test/active_record_test.rb +9 -0
- data/test/adapter/fragment_cache_test.rb +37 -0
- data/test/adapter/json/belongs_to_test.rb +47 -0
- data/test/adapter/json/collection_test.rb +82 -0
- data/test/adapter/json/has_many_test.rb +47 -0
- data/test/adapter/json_api/belongs_to_test.rb +157 -0
- data/test/adapter/json_api/collection_test.rb +95 -0
- data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
- data/test/adapter/json_api/has_many_test.rb +145 -0
- data/test/adapter/json_api/has_one_test.rb +81 -0
- data/test/adapter/json_api/json_api_test.rb +37 -0
- data/test/adapter/json_api/linked_test.rb +283 -0
- data/test/adapter/json_api/pagination_links_test.rb +115 -0
- data/test/adapter/json_api/resource_type_config_test.rb +59 -0
- data/test/adapter/json_test.rb +47 -0
- data/test/adapter/null_test.rb +25 -0
- data/test/adapter_test.rb +42 -0
- data/test/array_serializer_test.rb +99 -73
- data/test/capture_warnings.rb +65 -0
- data/test/fixtures/active_record.rb +56 -0
- data/test/fixtures/poro.rb +261 -0
- data/test/generators/scaffold_controller_generator_test.rb +23 -0
- data/test/generators/serializer_generator_test.rb +56 -0
- data/test/lint_test.rb +37 -0
- data/test/logger_test.rb +18 -0
- data/test/poro_test.rb +9 -0
- data/test/serializable_resource_test.rb +27 -0
- data/test/serializers/adapter_for_test.rb +170 -0
- data/test/serializers/association_macros_test.rb +36 -0
- data/test/serializers/associations_test.rb +150 -0
- data/test/serializers/attribute_test.rb +62 -0
- data/test/serializers/attributes_test.rb +57 -0
- data/test/serializers/cache_test.rb +165 -0
- data/test/serializers/configuration_test.rb +15 -0
- data/test/serializers/fieldset_test.rb +25 -0
- data/test/serializers/meta_test.rb +121 -0
- data/test/serializers/options_test.rb +21 -0
- data/test/serializers/root_test.rb +21 -0
- data/test/serializers/serializer_for_test.rb +65 -0
- data/test/support/rails_app.rb +21 -0
- data/test/support/serialization_testing.rb +13 -0
- data/test/support/simplecov.rb +6 -0
- data/test/support/stream_capture.rb +50 -0
- data/test/support/test_case.rb +5 -0
- data/test/test_helper.rb +48 -24
- data/test/utils/include_args_to_hash_test.rb +79 -0
- metadata +219 -47
- 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_record/serializer_override.rb +0 -16
- data/lib/generators/resource_override.rb +0 -13
- 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_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,15 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class ConfigurationTest < Minitest::Test
|
6
|
+
def test_array_serializer
|
7
|
+
assert_equal ActiveModel::Serializer::ArraySerializer, ActiveModel::Serializer.config.array_serializer
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_default_adapter
|
11
|
+
assert_equal :flatten_json, ActiveModel::Serializer.config.adapter
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class FieldsetTest < Minitest::Test
|
6
|
+
def test_fieldset_with_hash
|
7
|
+
fieldset = ActiveModel::Serializer::Fieldset.new({ 'post' => %w(id title), 'coment' => ['body'] })
|
8
|
+
|
9
|
+
assert_equal(
|
10
|
+
{ :post => [:id, :title], :coment => [:body] },
|
11
|
+
fieldset.fields
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_fieldset_with_array_of_fields_and_root_name
|
16
|
+
fieldset = ActiveModel::Serializer::Fieldset.new(['title'], 'post')
|
17
|
+
|
18
|
+
assert_equal(
|
19
|
+
{ :post => [:title] },
|
20
|
+
fieldset.fields
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class MetaTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
ActionController::Base.cache_store.clear
|
8
|
+
@blog = Blog.new(id: 1,
|
9
|
+
name: 'AMS Hints',
|
10
|
+
writer: Author.new(id: 2, name: 'Steve'),
|
11
|
+
articles: [Post.new(id: 3, title: 'AMS')])
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_meta_is_present_with_root
|
15
|
+
serializer = AlternateBlogSerializer.new(@blog, meta: { total: 10 })
|
16
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
17
|
+
expected = {
|
18
|
+
blog: {
|
19
|
+
id: 1,
|
20
|
+
title: 'AMS Hints'
|
21
|
+
},
|
22
|
+
'meta' => {
|
23
|
+
total: 10
|
24
|
+
}
|
25
|
+
}
|
26
|
+
assert_equal expected, adapter.as_json
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_meta_is_not_included_when_root_is_missing
|
30
|
+
# load_adapter uses FlattenJson Adapter
|
31
|
+
adapter = load_adapter(meta: { total: 10 })
|
32
|
+
expected = {
|
33
|
+
id: 1,
|
34
|
+
title: 'AMS Hints'
|
35
|
+
}
|
36
|
+
assert_equal expected, adapter.as_json
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_meta_key_is_used
|
40
|
+
serializer = AlternateBlogSerializer.new(@blog, meta: { total: 10 }, meta_key: 'haha_meta')
|
41
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
42
|
+
expected = {
|
43
|
+
blog: {
|
44
|
+
id: 1,
|
45
|
+
title: 'AMS Hints'
|
46
|
+
},
|
47
|
+
'haha_meta' => {
|
48
|
+
total: 10
|
49
|
+
}
|
50
|
+
}
|
51
|
+
assert_equal expected, adapter.as_json
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_meta_key_is_used_with_json_api
|
55
|
+
serializer = AlternateBlogSerializer.new(@blog, meta: { total: 10 }, meta_key: 'haha_meta')
|
56
|
+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
57
|
+
expected = {
|
58
|
+
data: {
|
59
|
+
id: '1',
|
60
|
+
type: 'blogs',
|
61
|
+
attributes: { title: 'AMS Hints' }
|
62
|
+
},
|
63
|
+
'haha_meta' => { total: 10 }
|
64
|
+
}
|
65
|
+
assert_equal expected, adapter.as_json
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_meta_is_not_present_on_arrays_without_root
|
69
|
+
serializer = ArraySerializer.new([@blog], meta: { total: 10 })
|
70
|
+
# FlattenJSON doesn't have support to root
|
71
|
+
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(serializer)
|
72
|
+
expected = [{
|
73
|
+
id: 1,
|
74
|
+
name: 'AMS Hints',
|
75
|
+
writer: {
|
76
|
+
id: 2,
|
77
|
+
name: 'Steve'
|
78
|
+
},
|
79
|
+
articles: [{
|
80
|
+
id: 3,
|
81
|
+
title: 'AMS',
|
82
|
+
body: nil
|
83
|
+
}]
|
84
|
+
}]
|
85
|
+
assert_equal expected, adapter.as_json
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_meta_is_present_on_arrays_with_root
|
89
|
+
serializer = ArraySerializer.new([@blog], meta: { total: 10 }, meta_key: 'haha_meta')
|
90
|
+
# JSON adapter adds root by default
|
91
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
92
|
+
expected = {
|
93
|
+
blogs: [{
|
94
|
+
id: 1,
|
95
|
+
name: 'AMS Hints',
|
96
|
+
writer: {
|
97
|
+
id: 2,
|
98
|
+
name: 'Steve'
|
99
|
+
},
|
100
|
+
articles: [{
|
101
|
+
id: 3,
|
102
|
+
title: 'AMS',
|
103
|
+
body: nil
|
104
|
+
}]
|
105
|
+
}],
|
106
|
+
'haha_meta' => {
|
107
|
+
total: 10
|
108
|
+
}
|
109
|
+
}
|
110
|
+
assert_equal expected, adapter.as_json
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
|
115
|
+
def load_adapter(options)
|
116
|
+
options = options.merge(adapter: :flatten_json, serializer: AlternateBlogSerializer)
|
117
|
+
ActiveModel::SerializableResource.new(@blog, options)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class OptionsTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
@profile = Profile.new(name: 'Name 1', description: 'Description 1')
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_options_are_accessible
|
11
|
+
@profile_serializer = ProfileSerializer.new(@profile, my_options: :accessible)
|
12
|
+
assert @profile_serializer.arguments_passed_in?
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_no_option_is_passed_in
|
16
|
+
@profile_serializer = ProfileSerializer.new(@profile)
|
17
|
+
refute @profile_serializer.arguments_passed_in?
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class RootTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
@virtual_value = VirtualValue.new(id: 1)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_overwrite_root
|
11
|
+
serializer = VirtualValueSerializer.new(@virtual_value, { root: 'smth' })
|
12
|
+
assert_equal('smth', serializer.json_key)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_underscore_in_root
|
16
|
+
serializer = VirtualValueSerializer.new(@virtual_value)
|
17
|
+
assert_equal('virtual_value', serializer.json_key)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class SerializerForTest < Minitest::Test
|
6
|
+
class ArraySerializerTest < Minitest::Test
|
7
|
+
def setup
|
8
|
+
@array = [1, 2, 3]
|
9
|
+
@previous_array_serializer = ActiveModel::Serializer.config.array_serializer
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
ActiveModel::Serializer.config.array_serializer = @previous_array_serializer
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_serializer_for_array
|
17
|
+
serializer = ActiveModel::Serializer.serializer_for(@array)
|
18
|
+
assert_equal ActiveModel::Serializer.config.array_serializer, serializer
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_overwritten_serializer_for_array
|
22
|
+
new_array_serializer = Class.new
|
23
|
+
ActiveModel::Serializer.config.array_serializer = new_array_serializer
|
24
|
+
serializer = ActiveModel::Serializer.serializer_for(@array)
|
25
|
+
assert_equal new_array_serializer, serializer
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class SerializerTest < Minitest::Test
|
30
|
+
class MyProfile < Profile
|
31
|
+
end
|
32
|
+
class CustomProfile
|
33
|
+
def serializer_class; ProfileSerializer; end
|
34
|
+
end
|
35
|
+
|
36
|
+
def setup
|
37
|
+
@profile = Profile.new
|
38
|
+
@my_profile = MyProfile.new
|
39
|
+
@custom_profile = CustomProfile.new
|
40
|
+
@model = ::Model.new
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_serializer_for_existing_serializer
|
44
|
+
serializer = ActiveModel::Serializer.serializer_for(@profile)
|
45
|
+
assert_equal ProfileSerializer, serializer
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_serializer_for_not_existing_serializer
|
49
|
+
serializer = ActiveModel::Serializer.serializer_for(@model)
|
50
|
+
assert_equal nil, serializer
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_serializer_inherited_serializer
|
54
|
+
serializer = ActiveModel::Serializer.serializer_for(@my_profile)
|
55
|
+
assert_equal ProfileSerializer, serializer
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_serializer_custom_serializer
|
59
|
+
serializer = ActiveModel::Serializer.serializer_for(@custom_profile)
|
60
|
+
assert_equal ProfileSerializer, serializer
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Foo < Rails::Application
|
2
|
+
if Rails::VERSION::MAJOR >= 4
|
3
|
+
config.eager_load = false
|
4
|
+
config.secret_key_base = 'abc123'
|
5
|
+
config.action_controller.perform_caching = true
|
6
|
+
config.active_support.test_order = :random
|
7
|
+
config.logger = Logger.new(nil)
|
8
|
+
ActionController::Base.cache_store = :memory_store
|
9
|
+
end
|
10
|
+
end
|
11
|
+
Foo.initialize!
|
12
|
+
|
13
|
+
module TestHelper
|
14
|
+
Routes = ActionDispatch::Routing::RouteSet.new
|
15
|
+
Routes.draw do
|
16
|
+
get ':controller(/:action(/:id))'
|
17
|
+
get ':controller(/:action)'
|
18
|
+
end
|
19
|
+
|
20
|
+
ActionController::Base.send :include, Routes.url_helpers
|
21
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class Minitest::Test
|
2
|
+
def before_setup
|
3
|
+
ActionController::Base.cache_store.clear
|
4
|
+
end
|
5
|
+
|
6
|
+
def with_adapter(adapter)
|
7
|
+
old_adapter = ActiveModel::Serializer.config.adapter
|
8
|
+
ActiveModel::Serializer.config.adapter = adapter
|
9
|
+
yield
|
10
|
+
ensure
|
11
|
+
ActiveModel::Serializer.config.adapter = old_adapter
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# Use cleaner stream testing interface from Rails 5 if available
|
2
|
+
# see https://github.com/rails/rails/blob/29959eb59d/activesupport/lib/active_support/testing/stream.rb
|
3
|
+
begin
|
4
|
+
require 'active_support/testing/stream'
|
5
|
+
rescue LoadError
|
6
|
+
require 'tempfile'
|
7
|
+
module ActiveSupport
|
8
|
+
module Testing
|
9
|
+
module Stream #:nodoc:
|
10
|
+
private
|
11
|
+
|
12
|
+
def silence_stream(stream)
|
13
|
+
old_stream = stream.dup
|
14
|
+
stream.reopen(IO::NULL)
|
15
|
+
stream.sync = true
|
16
|
+
yield
|
17
|
+
ensure
|
18
|
+
stream.reopen(old_stream)
|
19
|
+
old_stream.close
|
20
|
+
end
|
21
|
+
|
22
|
+
def quietly
|
23
|
+
silence_stream(STDOUT) do
|
24
|
+
silence_stream(STDERR) do
|
25
|
+
yield
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def capture(stream)
|
31
|
+
stream = stream.to_s
|
32
|
+
captured_stream = Tempfile.new(stream)
|
33
|
+
stream_io = eval("$#{stream}") # rubocop:disable Lint/Eval
|
34
|
+
origin_stream = stream_io.dup
|
35
|
+
stream_io.reopen(captured_stream)
|
36
|
+
|
37
|
+
yield
|
38
|
+
|
39
|
+
stream_io.rewind
|
40
|
+
return captured_stream.read
|
41
|
+
ensure
|
42
|
+
captured_stream.close
|
43
|
+
captured_stream.unlink
|
44
|
+
stream_io.reopen(origin_stream)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
data/test/test_helper.rb
CHANGED
@@ -1,32 +1,56 @@
|
|
1
|
-
require
|
2
|
-
require "bundler/setup"
|
1
|
+
require 'bundler/setup'
|
3
2
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
begin
|
4
|
+
require 'simplecov'
|
5
|
+
# HACK: till https://github.com/colszowka/simplecov/pull/400 is merged and released.
|
6
|
+
# Otherwise you may get:
|
7
|
+
# simplecov-0.10.0/lib/simplecov/defaults.rb:50: warning: global variable `$ERROR_INFO' not initialized
|
8
|
+
require 'support/simplecov'
|
9
|
+
AppCoverage.start
|
10
|
+
rescue LoadError
|
11
|
+
STDERR.puts 'Running without SimpleCov'
|
12
|
+
end
|
9
13
|
|
14
|
+
require 'timecop'
|
10
15
|
require 'rails'
|
16
|
+
require 'action_controller'
|
17
|
+
require 'action_controller/test_case'
|
18
|
+
require 'action_controller/railtie'
|
19
|
+
require 'active_support/json'
|
20
|
+
require 'fileutils'
|
21
|
+
FileUtils.mkdir_p(File.expand_path('../../tmp/cache', __FILE__))
|
11
22
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
23
|
+
gem 'minitest'
|
24
|
+
require 'minitest/autorun'
|
25
|
+
if defined?(Minitest::Test)
|
26
|
+
# Minitest 5
|
27
|
+
# https://github.com/seattlerb/minitest/blob/e21fdda9d/lib/minitest/autorun.rb
|
28
|
+
# https://github.com/seattlerb/minitest/blob/e21fdda9d/lib/minitest.rb#L45-L59
|
29
|
+
else
|
30
|
+
# Minitest 4
|
31
|
+
# https://github.com/seattlerb/minitest/blob/644a52fd0/lib/minitest/autorun.rb
|
32
|
+
# https://github.com/seattlerb/minitest/blob/644a52fd0/lib/minitest/unit.rb#L768-L787
|
33
|
+
# Ensure backward compatibility with Minitest 4
|
34
|
+
Minitest = MiniTest unless defined?(Minitest)
|
35
|
+
Minitest::Test = MiniTest::Unit::TestCase
|
36
|
+
def Minitest.after_run(&block)
|
37
|
+
MiniTest::Unit.after_tests(&block)
|
18
38
|
end
|
19
|
-
|
20
|
-
ActionController::Base.send :include, Routes.url_helpers
|
21
|
-
ActiveModel::Serializer.send :include, Routes.url_helpers
|
22
39
|
end
|
23
40
|
|
24
|
-
|
25
|
-
|
26
|
-
@routes = ::TestHelper::Routes
|
27
|
-
end
|
28
|
-
end
|
41
|
+
require 'capture_warnings'
|
42
|
+
CaptureWarnings.new(_fail_build = true).execute!
|
29
43
|
|
30
|
-
|
31
|
-
|
32
|
-
|
44
|
+
require 'active_model_serializers'
|
45
|
+
|
46
|
+
require 'support/stream_capture'
|
47
|
+
|
48
|
+
require 'support/rails_app'
|
49
|
+
|
50
|
+
require 'support/test_case'
|
51
|
+
|
52
|
+
require 'support/serialization_testing'
|
53
|
+
|
54
|
+
require 'fixtures/active_record'
|
55
|
+
|
56
|
+
require 'fixtures/poro'
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
module Utils
|
6
|
+
class IncludeArgsToHashTest < Minitest::Test
|
7
|
+
def test_nil
|
8
|
+
input = nil
|
9
|
+
expected = {}
|
10
|
+
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
11
|
+
assert_equal(expected, actual)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_empty_string
|
15
|
+
input = ''
|
16
|
+
expected = {}
|
17
|
+
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
18
|
+
assert_equal(expected, actual)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_single_string
|
22
|
+
input = 'author'
|
23
|
+
expected = { author: {} }
|
24
|
+
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
25
|
+
assert_equal(expected, actual)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_multiple_strings
|
29
|
+
input = 'author,comments'
|
30
|
+
expected = { author: {}, comments: {} }
|
31
|
+
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
32
|
+
assert_equal(expected, actual)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_multiple_strings_with_space
|
36
|
+
input = 'author, comments'
|
37
|
+
expected = { author: {}, comments: {} }
|
38
|
+
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
39
|
+
assert_equal(expected, actual)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_nested_string
|
43
|
+
input = 'posts.author'
|
44
|
+
expected = { posts: { author: {} } }
|
45
|
+
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
46
|
+
assert_equal(expected, actual)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_multiple_nested_string
|
50
|
+
input = 'posts.author,posts.comments.author,comments'
|
51
|
+
expected = { posts: { author: {}, comments: { author: {} } }, comments: {} }
|
52
|
+
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
53
|
+
assert_equal(expected, actual)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_empty_array
|
57
|
+
input = []
|
58
|
+
expected = {}
|
59
|
+
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
60
|
+
assert_equal(expected, actual)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_simple_array
|
64
|
+
input = [:comments, :author]
|
65
|
+
expected = { author: {}, comments: {} }
|
66
|
+
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
67
|
+
assert_equal(expected, actual)
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_nested_array
|
71
|
+
input = [:comments, posts: [:author, comments: [:author]]]
|
72
|
+
expected = { posts: { author: {}, comments: { author: {} } }, comments: {} }
|
73
|
+
actual = ActiveModel::Serializer::Utils.include_args_to_hash(input)
|
74
|
+
assert_equal(expected, actual)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|