rspec_jsonapi_serializer 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7ed8b7ea471eb94db672c660a12559e64b185bc7d9e47326f9623be38859d9b6
4
+ data.tar.gz: eacf5f0be70a08e50cb2b42f02788c14b089722cbee3d4d1e28a1efda1e8b7a2
5
+ SHA512:
6
+ metadata.gz: '090a203b5643c3930787cf553d6ea1146fffc51964b4a2e94e01b318a31900b590ef3b61e50ec63cb9d1b4d6cc66c7040ca74e60719e3e37470bd7e525940936'
7
+ data.tar.gz: e7dc94dce570f0a299389743e42183dfe119c723f76573687577f10b5de2175d655ebdbeb2dbb5cc5583027dae58580e07590fe7940eca09035a1270ad924fc4
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec_jsonapi_serializer/version"
4
+ require "rspec_jsonapi_serializer/matchers"
5
+
6
+ module RSpecJSONAPISerializer
7
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec_jsonapi_serializer/matchers/belong_to_matcher"
4
+ require "rspec_jsonapi_serializer/matchers/have_attribute_matcher"
5
+ require "rspec_jsonapi_serializer/matchers/have_id_matcher"
6
+ require "rspec_jsonapi_serializer/matchers/have_link_matcher"
7
+ require "rspec_jsonapi_serializer/matchers/have_many_matcher"
8
+ require "rspec_jsonapi_serializer/matchers/have_meta_matcher"
9
+ require "rspec_jsonapi_serializer/matchers/have_one_matcher"
10
+ require "rspec_jsonapi_serializer/matchers/have_type_matcher"
11
+
12
+ module RSpecJSONAPISerializer
13
+ module Matchers
14
+ # This allows us to assert attributes on a serializer, e.g.:
15
+ # expect(serializer).to belong_to(:team)
16
+ def belong_to(expected)
17
+ BelongToMatcher.new(expected)
18
+ end
19
+
20
+ # This allows us to assert attributes on a serializer, e.g.:
21
+ # expect(serializer).to have_attribute(:email)
22
+ # You can test custom attributes by using the submatcher "as".
23
+ # expect(serializer).to have_attribute(:email).as('john.doe@example.com')
24
+ def have_attribute(expected)
25
+ HaveAttributeMatcher.new(expected)
26
+ end
27
+
28
+ alias_method :serialize_attribute, :have_attribute
29
+
30
+ # This allows us to assert ids on a serializer, e.g.:
31
+ # expect(serializer).to have_id(:slug)
32
+ def have_id(expected)
33
+ HaveIdMatcher.new(expected)
34
+ end
35
+
36
+ # This allows us to assert links on a serializer, e.g.:
37
+ # expect(serializer).to have_link(:self)
38
+ # You can test custom links by using the submatcher "as".
39
+ # expect(serializer).to have_link(:self).as('https://example.com/users/1')
40
+ def have_link(expected)
41
+ HaveLinkMatcher.new(expected)
42
+ end
43
+
44
+ alias_method :serialize_link, :have_link
45
+
46
+ # This allows us to assert attributes on a serializer, e.g.:
47
+ # expect(serializer).to have_many(:teams)
48
+ def have_many(expected)
49
+ HaveManyMatcher.new(expected)
50
+ end
51
+
52
+ # This allows us to assert metadata on a serializer, e.g.:
53
+ # expect(serializer).to have_meta(:foo)
54
+ # You can test custom metadata by using the submatcher "as".
55
+ # expect(serializer).to have_meta(:foo).as('bar')
56
+ def have_meta(expected)
57
+ HaveMetaMatcher.new(expected)
58
+ end
59
+
60
+ alias_method :serialize_meta, :have_meta
61
+
62
+ # This allows us to assert attributes on a serializer, e.g.:
63
+ # expect(serializer).to have_one(:team)
64
+ def have_one(expected)
65
+ HaveOneMatcher.new(expected)
66
+ end
67
+
68
+ # This allows us to assert types on a serializer, e.g.:
69
+ # expect(serializer).to have_type(:salesforce_serializer)
70
+ def have_type(expected)
71
+ HaveTypeMatcher.new(expected)
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec_jsonapi_serializer/matchers/base"
4
+
5
+ module RSpecJSONAPISerializer
6
+ module Matchers
7
+ class AssociationMatcher < Base
8
+ def initialize(expected, relationship_matcher, relationship_type)
9
+ super(expected)
10
+
11
+ @relationship_matcher = relationship_matcher
12
+ @relationship_type = relationship_type
13
+ end
14
+
15
+ def matches?(serializer_instance)
16
+ @serializer_instance = serializer_instance
17
+
18
+ relationships.select { |_, r| r.relationship_type == relationship_type }
19
+ .has_key?(expected)
20
+ end
21
+
22
+ def main_failure_message
23
+ [expected_message, actual_message].compact.join(", ")
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :relationship_matcher, :relationship_type
29
+
30
+ def expected_message
31
+ "expected #{serializer_name} to #{association_message} #{expected}"
32
+ end
33
+
34
+ def actual_message
35
+ actual ? "got :#{actual} instead" : nil
36
+ end
37
+
38
+ def association_message
39
+ relationship_matcher.to_s.split('_')
40
+ end
41
+
42
+ def actual
43
+ relationships.key(expected) || relationship.key(expected.to_s)
44
+ end
45
+
46
+ def relationships
47
+ @relationships ||= serializer_instance.class&.relationships_to_serialize || {}
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpecJSONAPISerializer
4
+ module Matchers
5
+ class Base
6
+ def initialize(expected)
7
+ @expected = expected
8
+ @submatchers = []
9
+ end
10
+
11
+ def matches?(serializer_instance)
12
+ raise NotImplementedError
13
+ end
14
+
15
+ def failure_message
16
+ ([main_failure_message] + submatcher_failure_messages).compact.join("\n")
17
+ end
18
+
19
+ def failure_message_when_negated
20
+ ([main_failure_message_when_negated] + submatcher_failure_messages_when_negated)
21
+ .compact
22
+ .join("\n")
23
+ end
24
+
25
+ protected
26
+
27
+ attr_reader :expected, :serializer_instance, :submatchers
28
+
29
+ def main_failure_message
30
+ raise NotImplementedError
31
+ end
32
+
33
+ def main_failure_message_when_negated
34
+ raise NotImplementedError
35
+ end
36
+
37
+ def add_submatcher(submatcher)
38
+ submatchers << submatcher
39
+ end
40
+
41
+ def submatchers_match?
42
+ submatchers.all? { |submatcher| submatcher.matches?(serializer_instance) }
43
+ end
44
+
45
+ def serializable_hash
46
+ serializer_instance.serializable_hash
47
+ end
48
+
49
+ def serializer_name
50
+ serializer_instance.class.name
51
+ end
52
+
53
+ private
54
+
55
+ def submatcher_failure_messages
56
+ failing_submatchers.map(&:failure_message)
57
+ end
58
+
59
+ def submatcher_failure_messages_when_negated
60
+ failing_submatchers.map(&:failure_message_when_negated)
61
+ end
62
+
63
+ def failing_submatchers
64
+ @failing_submatchers ||= submatchers.select do |submatcher|
65
+ !submatcher.matches?(serializer_instance)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec_jsonapi_serializer/matchers/association_matcher"
4
+
5
+ module RSpecJSONAPISerializer
6
+ module Matchers
7
+ class BelongToMatcher
8
+ def initialize(expected)
9
+ @association_matcher = AssociationMatcher.new(expected, :belong_to, :belongs_to)
10
+ end
11
+
12
+ def matches?(serializer_instance)
13
+ association_matcher.matches?(serializer_instance)
14
+ end
15
+
16
+ def main_failure_message
17
+ association_matcher.main_failure_message
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :association_matcher
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec_jsonapi_serializer/matchers/base"
4
+ require "rspec_jsonapi_serializer/matchers/have_attribute_matchers/as_matcher"
5
+
6
+ module RSpecJSONAPISerializer
7
+ module Matchers
8
+ class HaveAttributeMatcher < Base
9
+ def matches?(serializer_instance)
10
+ @serializer_instance = serializer_instance
11
+
12
+ has_attribute? && submatchers_match?
13
+ end
14
+
15
+ def as(value)
16
+ add_submatcher HaveAttributeMatchers::AsMatcher.new(expected, value)
17
+
18
+ self
19
+ end
20
+
21
+ def as_nil
22
+ add_submatcher HaveAttributeMatchers::AsMatcher.new(expected, nil)
23
+
24
+ self
25
+ end
26
+
27
+ def main_failure_message
28
+ "expected #{serializer_name} to have attribute #{expected}." unless has_attribute?
29
+ end
30
+
31
+ private
32
+
33
+ def attributes
34
+ @attributes ||= serializer_instance.class.try(:attributes_to_serialize) || {}
35
+ end
36
+
37
+ def has_attribute?
38
+ attributes.has_key?(expected)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec_jsonapi_serializer/matchers/base"
4
+
5
+ module RSpecJSONAPISerializer
6
+ module Matchers
7
+ module HaveAttributeMatchers
8
+ class AsMatcher < Base
9
+ def initialize(attribute, expected)
10
+ super(expected)
11
+
12
+ @attribute = attribute
13
+ end
14
+
15
+ def matches?(serializer_instance)
16
+ @serializer_instance = serializer_instance
17
+
18
+ actual == expected
19
+ end
20
+
21
+ def failure_message
22
+ [expected_message, actual_message].compact.join(", ")
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :attribute
28
+
29
+ def expected_message
30
+ "expected #{serializer_instance.class.name} to serialize #{attribute} as #{expected}"
31
+ end
32
+
33
+ def actual_message
34
+ "got #{actual.nil? ? 'nil' : actual} instead" if attributes.has_key?(attribute)
35
+ end
36
+
37
+ def actual
38
+ attributes[attribute]
39
+ end
40
+
41
+ def attributes
42
+ serializable_hash.dig(:data, :attributes)
43
+ end
44
+
45
+ def serializable_hash
46
+ serializer_instance.serializable_hash
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec_jsonapi_serializer/matchers/base"
4
+
5
+ module RSpecJSONAPISerializer
6
+ module Matchers
7
+ class HaveIdMatcher < Base
8
+ def matches?(serializer_instance)
9
+ @serializer_instance = serializer_instance
10
+
11
+ actual == expected
12
+ end
13
+
14
+ def main_failure_message
15
+ "expected that #{serializer_name} to have id :#{expected}, got :#{actual} instead"
16
+ end
17
+
18
+ private
19
+
20
+ def actual
21
+ serializer_instance.class.record_id
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec_jsonapi_serializer/matchers/base"
4
+ require "rspec_jsonapi_serializer/matchers/have_link_matchers/as_matcher"
5
+
6
+ module RSpecJSONAPISerializer
7
+ module Matchers
8
+ class HaveLinkMatcher < Base
9
+ def matches?(serializer_instance)
10
+ @serializer_instance = serializer_instance
11
+
12
+ has_link? && submatchers_match?
13
+ end
14
+
15
+ def as(value)
16
+ add_submatcher HaveLinkMatchers::AsMatcher.new(expected, value)
17
+
18
+ self
19
+ end
20
+
21
+ def as_nil
22
+ add_submatcher HaveLinkMatchers::AsMatcher.new(expected, nil)
23
+
24
+ self
25
+ end
26
+
27
+ def main_failure_message
28
+ "expected #{serializer_name} to have link #{expected}." unless has_link?
29
+ end
30
+
31
+ private
32
+
33
+ def has_link?
34
+ links.has_key?(expected)
35
+ end
36
+
37
+ def links
38
+ serializable_hash.dig(:data, :links)
39
+ end
40
+
41
+ def serializable_hash
42
+ serializer_instance.serializable_hash
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec_jsonapi_serializer/matchers/base"
4
+
5
+ module RSpecJSONAPISerializer
6
+ module Matchers
7
+ module HaveLinkMatchers
8
+ class AsMatcher < Base
9
+ def initialize(link, expected)
10
+ super(expected)
11
+
12
+ @link = link
13
+ end
14
+
15
+ def matches?(serializer_instance)
16
+ @serializer_instance = serializer_instance
17
+
18
+ actual == expected
19
+ end
20
+
21
+ def failure_message
22
+ [expected_message, actual_message].compact.join(", ")
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :link
28
+
29
+ def expected_message
30
+ "expected #{serializer_instance.class.name} to serialize link #{link} as #{expected}"
31
+ end
32
+
33
+ def actual_message
34
+ "got #{actual.nil? ? 'nil' : actual} instead" if links.has_key?(link)
35
+ end
36
+
37
+ def actual
38
+ links[link]
39
+ end
40
+
41
+ def links
42
+ serializable_hash.dig(:data, :links)
43
+ end
44
+
45
+ def serializable_hash
46
+ serializer_instance.serializable_hash
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec_jsonapi_serializer/matchers/association_matcher"
4
+
5
+ module RSpecJSONAPISerializer
6
+ module Matchers
7
+ class HaveManyMatcher
8
+ def initialize(expected)
9
+ @association_matcher = AssociationMatcher.new(expected, :have_many, :has_many)
10
+ end
11
+
12
+ def matches?(serializer_instance)
13
+ association_matcher.matches?(serializer_instance)
14
+ end
15
+
16
+ def main_failure_message
17
+ association_matcher.main_failure_message
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :association_matcher
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec_jsonapi_serializer/matchers/base"
4
+ require "rspec_jsonapi_serializer/matchers/have_meta_matchers/as_matcher"
5
+
6
+ module RSpecJSONAPISerializer
7
+ module Matchers
8
+ class HaveMetaMatcher < Base
9
+ def matches?(serializer_instance)
10
+ @serializer_instance = serializer_instance
11
+
12
+ has_meta? && submatchers_match?
13
+ end
14
+
15
+ def as(value)
16
+ add_submatcher HaveMetaMatchers::AsMatcher.new(expected, value)
17
+
18
+ self
19
+ end
20
+
21
+ def as_nil
22
+ add_submatcher HaveMetaMatchers::AsMatcher.new(expected, nil)
23
+
24
+ self
25
+ end
26
+
27
+ def main_failure_message
28
+ "expected #{serializer_name} to serialize meta #{expected}." unless has_meta?
29
+ end
30
+
31
+ private
32
+
33
+ def metas
34
+ @metas ||= serializable_hash.dig(:data, :meta) || {}
35
+ end
36
+
37
+ def has_meta?
38
+ metas.has_key?(expected)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec_jsonapi_serializer/matchers/base"
4
+
5
+ module RSpecJSONAPISerializer
6
+ module Matchers
7
+ module HaveMetaMatchers
8
+ class AsMatcher < Base
9
+ def initialize(meta, expected)
10
+ super(expected)
11
+
12
+ @meta = meta
13
+ end
14
+
15
+ def matches?(serializer_instance)
16
+ @serializer_instance = serializer_instance
17
+
18
+ actual == expected
19
+ end
20
+
21
+ def failure_message
22
+ [expected_message, actual_message].compact.join(", ")
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :meta
28
+
29
+ def expected_message
30
+ "expected #{serializer_instance.class.name} to serialize meta #{meta} as #{expected}"
31
+ end
32
+
33
+ def actual_message
34
+ "got #{actual.nil? ? 'nil' : actual} instead" if metas.has_key?(meta)
35
+ end
36
+
37
+ def actual
38
+ metas[meta]
39
+ end
40
+
41
+ def metas
42
+ @metas ||= serializable_hash.dig(:data, :meta) || {}
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec_jsonapi_serializer/matchers/association_matcher"
4
+
5
+ module RSpecJSONAPISerializer
6
+ module Matchers
7
+ class HaveOneMatcher
8
+ def initialize(expected)
9
+ @association_matcher = AssociationMatcher.new(expected, :have_one, :has_one)
10
+ end
11
+
12
+ def matches?(serializer_instance)
13
+ association_matcher.matches?(serializer_instance)
14
+ end
15
+
16
+ def main_failure_message
17
+ association_matcher.main_failure_message
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :association_matcher
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec_jsonapi_serializer/matchers/base"
4
+
5
+ module RSpecJSONAPISerializer
6
+ module Matchers
7
+ class HaveTypeMatcher < Base
8
+ def matches?(serializer_instance)
9
+ @serializer_instance = serializer_instance
10
+
11
+ actual == expected
12
+ end
13
+
14
+ def main_failure_message
15
+ "expected that #{serializer_name} to have type :#{expected}, got :#{actual} instead"
16
+ end
17
+
18
+ private
19
+
20
+ def actual
21
+ serializer_instance.class.record_type
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RSpecJSONAPISerializer
4
+ VERSION = "1.0.0"
5
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec_jsonapi_serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Mateus Cruz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jsonapi-serializer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description:
28
+ email:
29
+ - mateus@intricately.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/rspec_jsonapi_serializer.rb
35
+ - lib/rspec_jsonapi_serializer/matchers.rb
36
+ - lib/rspec_jsonapi_serializer/matchers/association_matcher.rb
37
+ - lib/rspec_jsonapi_serializer/matchers/base.rb
38
+ - lib/rspec_jsonapi_serializer/matchers/belong_to_matcher.rb
39
+ - lib/rspec_jsonapi_serializer/matchers/have_attribute_matcher.rb
40
+ - lib/rspec_jsonapi_serializer/matchers/have_attribute_matchers/as_matcher.rb
41
+ - lib/rspec_jsonapi_serializer/matchers/have_id_matcher.rb
42
+ - lib/rspec_jsonapi_serializer/matchers/have_link_matcher.rb
43
+ - lib/rspec_jsonapi_serializer/matchers/have_link_matchers/as_matcher.rb
44
+ - lib/rspec_jsonapi_serializer/matchers/have_many_matcher.rb
45
+ - lib/rspec_jsonapi_serializer/matchers/have_meta_matcher.rb
46
+ - lib/rspec_jsonapi_serializer/matchers/have_meta_matchers/as_matcher.rb
47
+ - lib/rspec_jsonapi_serializer/matchers/have_one_matcher.rb
48
+ - lib/rspec_jsonapi_serializer/matchers/have_type_matcher.rb
49
+ - lib/rspec_jsonapi_serializer/version.rb
50
+ homepage: https://github.com/teamintricately/rspec_jsonapi_serializer
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ homepage_uri: https://github.com/teamintricately/rspec_jsonapi_serializer
55
+ source_code_uri: https://github.com/teamintricately/rspec_jsonapi_serializer
56
+ changelog_uri: https://github.com/teamintricately/rspec_jsonapi_serializer/blob/main/CHANGELOG.md
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 2.4.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.2.16
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: RSpec matchers for jsonapi-serializer.
76
+ test_files: []