Skip to content

Commit 097d7d8

Browse files
committed
Cleanup PORO fixtures
1 parent 42dc46a commit 097d7d8

File tree

5 files changed

+52
-50
lines changed

5 files changed

+52
-50
lines changed

test/cache_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ class InheritedRoleSerializer < RoleSerializer
5252
class Comment < ::Model
5353
attributes :body
5454
associations :post, :author
55+
56+
# Uses a custom non-time-based cache key
57+
def cache_key
58+
"comment/#{id}"
59+
end
5560
end
5661

5762
setup do
@@ -329,11 +334,6 @@ def test_cache_digest_definition
329334
end
330335

331336
def test_object_cache_keys
332-
class << @comment
333-
def cache_key
334-
"comment/#{id}"
335-
end
336-
end
337337
serializable = ActiveModelSerializers::SerializableResource.new([@comment, @comment])
338338
include_directive = JSONAPI::IncludeDirective.new('*', allow_wildcard: true)
339339

test/collection_serializer_test.rb

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,27 @@
33
module ActiveModel
44
class Serializer
55
class CollectionSerializerTest < ActiveSupport::TestCase
6+
SingularModel = poro_without_caching_support
7+
class SingularModelSerializer < ActiveModel::Serializer
8+
end
9+
HasManyModel = poro_without_caching_support(::Model) do
10+
associations :singular_models
11+
end
12+
class HasManyModelSerializer < ActiveModel::Serializer
13+
has_many :singular_models
14+
15+
def custom_options
16+
instance_options
17+
end
18+
end
619
class MessagesSerializer < ActiveModel::Serializer
720
type 'messages'
821
end
922

1023
def setup
11-
@comment = Comment.new
12-
@post = Post.new
13-
@resource = build_named_collection @comment, @post
24+
@singular_model = SingularModel.new
25+
@has_many_model = HasManyModel.new
26+
@resource = build_named_collection @singular_model, @has_many_model
1427
@serializer = collection_serializer.new(@resource, some: :options)
1528
end
1629

@@ -34,29 +47,29 @@ def test_respond_to_each
3447
def test_each_object_should_be_serialized_with_appropriate_serializer
3548
serializers = @serializer.to_a
3649

37-
assert_kind_of CommentSerializer, serializers.first
38-
assert_kind_of Comment, serializers.first.object
50+
assert_kind_of SingularModelSerializer, serializers.first
51+
assert_kind_of SingularModel, serializers.first.object
3952

40-
assert_kind_of PostSerializer, serializers.last
41-
assert_kind_of Post, serializers.last.object
53+
assert_kind_of HasManyModelSerializer, serializers.last
54+
assert_kind_of HasManyModel, serializers.last.object
4255

4356
assert_equal :options, serializers.last.custom_options[:some]
4457
end
4558

4659
def test_serializer_option_not_passed_to_each_serializer
47-
serializers = collection_serializer.new([@post], serializer: PostSerializer).to_a
60+
serializers = collection_serializer.new([@has_many_model], serializer: HasManyModelSerializer).to_a
4861

4962
refute serializers.first.custom_options.key?(:serializer)
5063
end
5164

5265
def test_root_default
53-
@serializer = collection_serializer.new([@comment, @post])
66+
@serializer = collection_serializer.new([@singular_model, @has_many_model])
5467
assert_nil @serializer.root
5568
end
5669

5770
def test_root
5871
expected = 'custom_root'
59-
@serializer = collection_serializer.new([@comment, @post], root: expected)
72+
@serializer = collection_serializer.new([@singular_model, @has_many_model], root: expected)
6073
assert_equal expected, @serializer.root
6174
end
6275

test/fixtures/poro.rb

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@ class Profile < Model
4040
end
4141
class ProfileSerializer < ActiveModel::Serializer
4242
attributes :name, :description
43-
44-
# ActiveModel::Serializer::OptionsTest#test_options_are_accessible:
45-
# test/serializers/options_test.rb:12:in `test_options_are_accessible'
46-
# test/serializers/options_test.rb:17:in `test_no_option_is_passed_in'
47-
def arguments_passed_in?
48-
instance_options[:my_options] == :accessible
49-
end
5043
end
5144
class ProfilePreviewSerializer < ActiveModel::Serializer
5245
attributes :name
@@ -73,21 +66,12 @@ class AuthorPreviewSerializer < ActiveModel::Serializer
7366
class Comment < Model
7467
attributes :body, :date
7568
associations :post, :author, :likes
76-
77-
# Uses a custom non-time-based cache key
78-
def cache_key
79-
"#{self.class.name.downcase}/#{id}"
80-
end
8169
end
8270
class CommentSerializer < ActiveModel::Serializer
8371
cache expires_in: 1.day, skip_digest: true
8472
attributes :id, :body
8573
belongs_to :post
8674
belongs_to :author
87-
88-
def custom_options
89-
instance_options
90-
end
9175
end
9276
class CommentPreviewSerializer < ActiveModel::Serializer
9377
attributes :id
@@ -110,18 +94,6 @@ class PostSerializer < ActiveModel::Serializer
11094
def blog
11195
Blog.new(id: 999, name: 'Custom blog')
11296
end
113-
114-
# ActiveModel::Serializer::ArraySerializerTest#test_each_object_should_be_serialized_with_appropriate_serializer:
115-
# test/collection_serializer_test.rb:43:in `test_each_object_should_be_serialized_with_appropriate_serializer'
116-
# ActiveModel::Serializer::ArraySerializerTest#test_serializer_option_not_passed_to_each_serializer:
117-
# test/collection_serializer_test.rb:49:in `test_serializer_option_not_passed_to_each_serializer'
118-
# ActiveModel::Serializer::CollectionSerializerTest#test_each_object_should_be_serialized_with_appropriate_serializer:
119-
# test/collection_serializer_test.rb:43:in `test_each_object_should_be_serialized_with_appropriate_serializer'
120-
# ActiveModel::Serializer::CollectionSerializerTest#test_serializer_option_not_passed_to_each_serializer:
121-
# test/collection_serializer_test.rb:49:in `test_serializer_option_not_passed_to_each_serializer'
122-
def custom_options
123-
instance_options
124-
end
12597
end
12698
class SpammyPostSerializer < ActiveModel::Serializer
12799
attributes :id

test/serializers/associations_test.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ def test_serializer_options_are_passed_into_associations_serializers
6262
.associations
6363
.detect { |assoc| assoc.key == :comments }
6464

65-
assert association.serializer.first.custom_options[:custom_options]
65+
comment_serializer = association.serializer.first
66+
class << comment_serializer
67+
def custom_options
68+
instance_options
69+
end
70+
end
71+
assert comment_serializer.custom_options
6672
end
6773

6874
def test_belongs_to

test/serializers/options_test.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,29 @@
33
module ActiveModel
44
class Serializer
55
class OptionsTest < ActiveSupport::TestCase
6-
def setup
7-
@profile = Profile.new(name: 'Name 1', description: 'Description 1')
6+
ModelWithOptions = poro_without_caching_support do
7+
attributes :name, :description
8+
end
9+
class ModelWithOptionsSerializer < ActiveModel::Serializer
10+
attributes :name, :description
11+
12+
def arguments_passed_in?
13+
instance_options[:my_options] == :accessible
14+
end
15+
end
16+
17+
setup do
18+
@model_with_options = ModelWithOptions.new(name: 'Name 1', description: 'Description 1')
819
end
920

1021
def test_options_are_accessible
11-
@profile_serializer = ProfileSerializer.new(@profile, my_options: :accessible)
12-
assert @profile_serializer.arguments_passed_in?
22+
model_with_options_serializer = ModelWithOptionsSerializer.new(@model_with_options, my_options: :accessible)
23+
assert model_with_options_serializer.arguments_passed_in?
1324
end
1425

1526
def test_no_option_is_passed_in
16-
@profile_serializer = ProfileSerializer.new(@profile)
17-
refute @profile_serializer.arguments_passed_in?
27+
model_with_options_serializer = ModelWithOptionsSerializer.new(@model_with_options)
28+
refute model_with_options_serializer.arguments_passed_in?
1829
end
1930
end
2031
end

0 commit comments

Comments
 (0)