|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +module ActiveModel |
| 4 | + class Serializer |
| 5 | + class HasManyPolymorphicTest < ActiveModel::TestCase |
| 6 | + def setup |
| 7 | + @association = MailSerializer._associations[:attachments] |
| 8 | + @old_association = @association.dup |
| 9 | + |
| 10 | + @mail = Mail.new({ body: 'Body 1' }) |
| 11 | + @mail_serializer = MailSerializer.new(@mail) |
| 12 | + end |
| 13 | + |
| 14 | + def teardown |
| 15 | + MailSerializer._associations[:attachments] = @old_association |
| 16 | + end |
| 17 | + |
| 18 | + def model_name(object) |
| 19 | + object.class.to_s.demodulize.underscore.to_sym |
| 20 | + end |
| 21 | + |
| 22 | + def test_associations_definition |
| 23 | + assert_equal 1, MailSerializer._associations.length |
| 24 | + assert_kind_of Association::HasMany, @association |
| 25 | + assert_equal true, @association.polymorphic |
| 26 | + assert_equal 'attachments', @association.name |
| 27 | + end |
| 28 | + |
| 29 | + def test_associations_embedding_ids_serialization_using_serializable_hash |
| 30 | + @association.embed = :ids |
| 31 | + |
| 32 | + assert_equal({ |
| 33 | + body: 'Body 1', |
| 34 | + 'attachment_ids' => @mail.attachments.map do |c| |
| 35 | + { id: c.object_id, type: model_name(c) } |
| 36 | + end |
| 37 | + }, @mail_serializer.serializable_hash) |
| 38 | + end |
| 39 | + |
| 40 | + def test_associations_embedding_ids_serialization_using_as_json |
| 41 | + @association.embed = :ids |
| 42 | + |
| 43 | + assert_equal({ |
| 44 | + 'mail' => { |
| 45 | + :body => 'Body 1', |
| 46 | + 'attachment_ids' => @mail.attachments.map do |c| |
| 47 | + { id: c.object_id, type: model_name(c) } |
| 48 | + end |
| 49 | + } |
| 50 | + }, @mail_serializer.as_json) |
| 51 | + end |
| 52 | + |
| 53 | + def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options |
| 54 | + @association.embed = :ids |
| 55 | + @association.key = 'key' |
| 56 | + |
| 57 | + assert_equal({ |
| 58 | + body: 'Body 1', |
| 59 | + 'key' => @mail.attachments.map do |c| |
| 60 | + { id: c.object_id, type: model_name(c) } |
| 61 | + end |
| 62 | + }, @mail_serializer.serializable_hash) |
| 63 | + end |
| 64 | + |
| 65 | + def test_associations_embedding_objects_serialization_using_serializable_hash |
| 66 | + @association.embed = :objects |
| 67 | + |
| 68 | + assert_equal({ |
| 69 | + body: 'Body 1', |
| 70 | + :attachments => [ |
| 71 | + { type: :image, image: { url: 'U1' }}, |
| 72 | + { type: :video, video: { html: 'H1' }} |
| 73 | + ] |
| 74 | + }, @mail_serializer.serializable_hash) |
| 75 | + end |
| 76 | + |
| 77 | + def test_associations_embedding_objects_serialization_using_as_json |
| 78 | + @association.embed = :objects |
| 79 | + |
| 80 | + assert_equal({ |
| 81 | + 'mail' => { |
| 82 | + body: 'Body 1', |
| 83 | + attachments: [ |
| 84 | + { type: :image, image: { url: 'U1' }}, |
| 85 | + { type: :video, video: { html: 'H1' }} |
| 86 | + ] |
| 87 | + } |
| 88 | + }, @mail_serializer.as_json) |
| 89 | + end |
| 90 | + |
| 91 | + def test_associations_embedding_nil_objects_serialization_using_as_json |
| 92 | + @association.embed = :objects |
| 93 | + @mail.instance_eval do |
| 94 | + def attachments |
| 95 | + [nil] |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + assert_equal({ |
| 100 | + 'mail' => { |
| 101 | + :body => 'Body 1', |
| 102 | + :attachments => [nil] |
| 103 | + } |
| 104 | + }, @mail_serializer.as_json) |
| 105 | + end |
| 106 | + |
| 107 | + def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options |
| 108 | + @association.embed = :objects |
| 109 | + @association.embedded_key = 'root' |
| 110 | + |
| 111 | + assert_equal({ |
| 112 | + body: 'Body 1', |
| 113 | + 'root' => [ |
| 114 | + { type: :image, image: { url: 'U1' }}, |
| 115 | + { type: :video, video: { html: 'H1' }} |
| 116 | + ] |
| 117 | + }, @mail_serializer.serializable_hash) |
| 118 | + end |
| 119 | + |
| 120 | + def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash |
| 121 | + @association.embed = :ids |
| 122 | + @association.embed_in_root = true |
| 123 | + |
| 124 | + assert_equal({ |
| 125 | + body: 'Body 1', |
| 126 | + 'attachment_ids' => @mail.attachments.map do |c| |
| 127 | + { id: c.object_id, type: model_name(c) } |
| 128 | + end |
| 129 | + }, @mail_serializer.serializable_hash) |
| 130 | + end |
| 131 | + |
| 132 | + def test_associations_embedding_ids_including_objects_serialization_using_as_json |
| 133 | + @association.embed = :ids |
| 134 | + @association.embed_in_root = true |
| 135 | + |
| 136 | + assert_equal({ |
| 137 | + 'mail' => { |
| 138 | + body: 'Body 1', |
| 139 | + 'attachment_ids' => @mail.attachments.map do |c| |
| 140 | + { id: c.object_id, type: model_name(c) } |
| 141 | + end, |
| 142 | + }, |
| 143 | + attachments: [ |
| 144 | + { type: :image, image: { url: 'U1' }}, |
| 145 | + { type: :video, video: { html: 'H1' }} |
| 146 | + ] |
| 147 | + }, @mail_serializer.as_json) |
| 148 | + end |
| 149 | + |
| 150 | + def test_associations_embedding_nothing_including_objects_serialization_using_as_json |
| 151 | + @association.embed = nil |
| 152 | + @association.embed_in_root = true |
| 153 | + |
| 154 | + assert_equal({ |
| 155 | + 'mail' => { body: 'Body 1' }, |
| 156 | + attachments: [ |
| 157 | + { type: :image, image: { url: 'U1' }}, |
| 158 | + { type: :video, video: { html: 'H1' }} |
| 159 | + ] |
| 160 | + }, @mail_serializer.as_json) |
| 161 | + end |
| 162 | + |
| 163 | + def test_associations_using_a_given_serializer |
| 164 | + @association.embed = :ids |
| 165 | + @association.embed_in_root = true |
| 166 | + @association.serializer_from_options = Class.new(ActiveModel::Serializer) do |
| 167 | + def fake |
| 168 | + 'fake' |
| 169 | + end |
| 170 | + |
| 171 | + attributes :fake |
| 172 | + end |
| 173 | + |
| 174 | + assert_equal({ |
| 175 | + 'mail' => { |
| 176 | + body: 'Body 1', |
| 177 | + 'attachment_ids' => @mail.attachments.map do |c| |
| 178 | + { id: c.object_id, type: model_name(c) } |
| 179 | + end |
| 180 | + }, |
| 181 | + attachments: [ |
| 182 | + { type: :image, image: { fake: 'fake' }}, |
| 183 | + { type: :video, video: { fake: 'fake' }} |
| 184 | + ] |
| 185 | + }, @mail_serializer.as_json) |
| 186 | + end |
| 187 | + end |
| 188 | + end |
| 189 | +end |
0 commit comments