File: has_one_test.rb

package info (click to toggle)
ruby-active-model-serializers 0.10.12-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,752 kB
  • sloc: ruby: 13,138; sh: 53; makefile: 6
file content (82 lines) | stat: -rw-r--r-- 2,632 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# frozen_string_literal: true

require 'test_helper'

module ActiveModelSerializers
  module Adapter
    class JsonApi
      class HasOneTest < ActiveSupport::TestCase
        def setup
          @author = Author.new(id: 1, name: 'Steve K.')
          @bio = Bio.new(id: 43, content: 'AMS Contributor')
          @author.bio = @bio
          @bio.author = @author
          @post = Post.new(id: 42, title: 'New Post', body: 'Body')
          @anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
          @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
          @post.comments = [@comment]
          @anonymous_post.comments = []
          @comment.post = @post
          @comment.author = nil
          @post.author = @author
          @anonymous_post.author = nil
          @blog = Blog.new(id: 1, name: 'My Blog!!')
          @blog.writer = @author
          @blog.articles = [@post, @anonymous_post]
          @author.posts = []
          @author.roles = []

          @virtual_value = VirtualValue.new(id: 1)

          @serializer = AuthorSerializer.new(@author)
          @adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:bio, :posts])
        end

        def test_includes_bio_id
          expected = { data: { type: 'bios', id: '43' } }

          assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:bio])
        end

        def test_includes_linked_bio
          @adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer, include: [:bio])

          expected = [
            {
              id: '43',
              type: 'bios',
              attributes: {
                content: 'AMS Contributor',
                rating: nil
              },
              relationships: {
                author: { data: { type: 'authors', id: '1' } }
              }
            }
          ]

          assert_equal(expected, @adapter.serializable_hash[:included])
        end

        def test_has_one_with_virtual_value
          serializer = VirtualValueSerializer.new(@virtual_value)
          adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)

          expected = {
            data: {
              id: '1',
              type: 'virtual-values',
              relationships: {
                maker: { data: { type: 'makers', id: '1' } },
                reviews: { data: [{ type: 'reviews', id: '1' },
                                  { type: 'reviews', id: '2' }] }
              }
            }
          }

          assert_equal(expected, adapter.serializable_hash)
        end
      end
    end
  end
end