rspec_jsonapi_serializer 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c575d046628ade51e83238d8bc7179eea00ee9bc1cedde252fda0c8b98fa2269
4
- data.tar.gz: 30d857690790f7cc807ac6e432e1103ef646e140f085a7c5c09c8b6d3b75b1c6
3
+ metadata.gz: e24d0b3794bdc77750c98e578543eb472a655f79ea61190682623b2ce7f8e89f
4
+ data.tar.gz: 89cfb3714d48e15345fbad93540bdf58b02153f4001e88defc66fbc63758d672
5
5
  SHA512:
6
- metadata.gz: f1303aa1426102b7f26ebc5d4abc0fbfb0a9f712e9b231e05e5a43e6e70900d3f8a1e22731db6d00f701bcbf7c024117a7c2f22fed4530403e069682f2826556
7
- data.tar.gz: 52b6d4c3dd1e2214e4275105195c028b35dfbb4b599754c23b6d65fe9c6b6cfb86e6ecc2a2bca35b5319d4b507b1c909171513dd0e5c427ea100cfb27429d261
6
+ metadata.gz: 2693f3a69e99f02b54d6a283b377bc41f02ef74f82455d78d7c4b98965645f972939e2df2aeaea107f2546f9e0a69f2cc51e803cdaf3e6090f8b09491929ce09
7
+ data.tar.gz: 2f5f2c1c81eb3a56e974d807c6136f2908540bb4afc07e25a179c03af36e5b0b9f0e583b8a707804b7e1d1cf032b84161ff8ea6221c21bf989e1c088c8675797
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "rspec_jsonapi_serializer/matchers/base"
4
+ require "rspec_jsonapi_serializer/matchers/association_matchers/serializer_matcher"
5
+ require "rspec_jsonapi_serializer/metadata/relationships"
4
6
 
5
7
  module RSpecJSONAPISerializer
6
8
  module Matchers
@@ -15,17 +17,16 @@ module RSpecJSONAPISerializer
15
17
  def matches?(serializer_instance)
16
18
  @serializer_instance = serializer_instance
17
19
 
18
- relationships.select { |_, r| r.relationship_type == relationship_type }
19
- .has_key?(expected)
20
+ relationship_matches? && submatchers_match?
20
21
  end
21
22
 
22
- def description
23
- description = "#{association_message} #{expected}"
23
+ def serializer(value)
24
+ add_submatcher AssociationMatchers::SerializerMatcher.new(value, expected)
24
25
 
25
- [description, submatchers.map(&:description)].flatten.join(' ')
26
+ self
26
27
  end
27
28
 
28
- def failure_message
29
+ def main_failure_message
29
30
  [expected_message, actual_message].compact.join(", ")
30
31
  end
31
32
 
@@ -37,20 +38,28 @@ module RSpecJSONAPISerializer
37
38
  "expected #{serializer_name} to #{association_message} #{expected}"
38
39
  end
39
40
 
41
+ def relationship_matches?
42
+ actual.present? && actual.relationship_type == relationship_type
43
+ end
44
+
40
45
  def actual_message
41
- actual ? "got :#{actual} instead" : nil
46
+ actual ? "got :#{actual.relationship_type} instead" : nil
42
47
  end
43
48
 
44
49
  def association_message
45
- relationship_matcher.to_s.split("_").join(" ")
50
+ relationship_matcher.to_s.split('_')
46
51
  end
47
52
 
48
53
  def actual
49
- relationships.key(expected) || relationship.key(expected.to_s)
54
+ metadata.relationship(expected)
50
55
  end
51
56
 
52
57
  def relationships
53
- @relationships ||= serializer_instance.class&.relationships_to_serialize || {}
58
+ metadata.relationships
59
+ end
60
+
61
+ def metadata
62
+ Metadata::Relationships.new(serializer_instance)
54
63
  end
55
64
  end
56
65
  end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec_jsonapi_serializer/metadata/relationships"
4
+
5
+ module RSpecJSONAPISerializer
6
+ module Matchers
7
+ module AssociationMatchers
8
+ class SerializerMatcher < Base
9
+ def initialize(value, relationship_target)
10
+ super(value)
11
+
12
+ @relationship_target = relationship_target
13
+ end
14
+
15
+ def matches?(serializer_instance)
16
+ @serializer_instance = serializer_instance
17
+
18
+ actual == expected
19
+ end
20
+
21
+ def main_failure_message
22
+ [expected_message, actual_message].compact.join(", ")
23
+ end
24
+
25
+ private
26
+
27
+ attr_reader :relationship_target
28
+
29
+ def expected_message
30
+ "expected #{serializer_name} to use #{expected} as serializer for #{relationship_target}"
31
+ end
32
+
33
+ def actual_message
34
+ actual ? "got #{actual} instead" : nil
35
+ end
36
+
37
+ def actual
38
+ metadata.relationship(relationship_target).serializer
39
+ end
40
+
41
+ def metadata
42
+ Metadata::Relationships.new(serializer_instance)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -13,17 +13,27 @@ module RSpecJSONAPISerializer
13
13
  end
14
14
 
15
15
  def failure_message
16
- raise NotImplementedError
16
+ ([main_failure_message] + submatcher_failure_messages).compact.join("\n")
17
17
  end
18
18
 
19
19
  def failure_message_when_negated
20
- raise NotImplementedError
20
+ ([main_failure_message_when_negated] + submatcher_failure_messages_when_negated)
21
+ .compact
22
+ .join("\n")
21
23
  end
22
24
 
23
25
  protected
24
26
 
25
27
  attr_reader :expected, :serializer_instance, :submatchers
26
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
+
27
37
  def add_submatcher(submatcher)
28
38
  submatchers << submatcher
29
39
  end
@@ -40,6 +50,16 @@ module RSpecJSONAPISerializer
40
50
  serializer_instance.class.name
41
51
  end
42
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
+
43
63
  def failing_submatchers
44
64
  @failing_submatchers ||= submatchers.select do |submatcher|
45
65
  !submatcher.matches?(serializer_instance)
@@ -13,12 +13,12 @@ module RSpecJSONAPISerializer
13
13
  association_matcher.matches?(serializer_instance)
14
14
  end
15
15
 
16
- def description
17
- association_matcher.description
16
+ def serializer(value)
17
+ association_matcher.serializer(value)
18
18
  end
19
19
 
20
- def failure_message
21
- association_matcher.failure_message
20
+ def main_failure_message
21
+ association_matcher.main_failure_message
22
22
  end
23
23
 
24
24
  private
@@ -19,35 +19,17 @@ module RSpecJSONAPISerializer
19
19
  end
20
20
 
21
21
  def as_nil
22
- as(nil)
23
- end
24
-
25
- def description
26
- description = "have attribute #{expected}"
22
+ add_submatcher HaveAttributeMatchers::AsMatcher.new(expected, nil)
27
23
 
28
- [description, submatchers.map(&:description)].flatten.join(' ')
29
- end
30
-
31
- def failure_message
32
- "Expected #{expectation}."
24
+ self
33
25
  end
34
26
 
35
- def failure_message_when_negated
36
- "Did not expect #{expectation}."
27
+ def main_failure_message
28
+ "expected #{serializer_name} to have attribute #{expected}." unless has_attribute?
37
29
  end
38
30
 
39
31
  private
40
32
 
41
- def expectation
42
- expectation = "#{serializer_name} to have attribute #{expected}"
43
-
44
- submatchers_expectations = failing_submatchers.map do |submatcher|
45
- "(#{submatcher.expectation})"
46
- end.compact.join(", ")
47
-
48
- [expectation, submatchers_expectations].reject(&:nil?).reject(&:empty?).join(" ")
49
- end
50
-
51
33
  def attributes
52
34
  @attributes ||= serializer_instance.class.try(:attributes_to_serialize) || {}
53
35
  end
@@ -18,30 +18,20 @@ module RSpecJSONAPISerializer
18
18
  actual == expected
19
19
  end
20
20
 
21
- def description
22
- "as #{expected_to_string}"
23
- end
24
-
25
- def expectation
26
- [ "as #{expected_to_string}", actual_message ].compact.join(", ")
21
+ def failure_message
22
+ [expected_message, actual_message].compact.join(", ")
27
23
  end
28
24
 
29
25
  private
30
26
 
31
27
  attr_reader :attribute
32
28
 
33
- def expected_to_string
34
- value_to_string(expected)
29
+ def expected_message
30
+ "expected #{serializer_instance.class.name} to serialize #{attribute} as #{expected}"
35
31
  end
36
32
 
37
33
  def actual_message
38
- "got #{value_to_string(actual)} instead" if attributes.has_key?(attribute)
39
- end
40
-
41
- def value_to_string(value)
42
- return 'nil' if value.nil?
43
-
44
- value.to_s
34
+ "got #{actual.nil? ? 'nil' : actual} instead" if attributes.has_key?(attribute)
45
35
  end
46
36
 
47
37
  def actual
@@ -11,7 +11,7 @@ module RSpecJSONAPISerializer
11
11
  actual == expected
12
12
  end
13
13
 
14
- def failure_message
14
+ def main_failure_message
15
15
  "expected that #{serializer_name} to have id :#{expected}, got :#{actual} instead"
16
16
  end
17
17
 
@@ -19,35 +19,17 @@ module RSpecJSONAPISerializer
19
19
  end
20
20
 
21
21
  def as_nil
22
- as(nil)
23
- end
24
-
25
- def description
26
- description = "have link #{expected}"
22
+ add_submatcher HaveLinkMatchers::AsMatcher.new(expected, nil)
27
23
 
28
- [description, submatchers.map(&:description)].flatten.join(' ')
29
- end
30
-
31
- def failure_message
32
- "Expected #{expectation}."
24
+ self
33
25
  end
34
26
 
35
- def failure_message_when_negated
36
- "Did not expect #{expectation}."
27
+ def main_failure_message
28
+ "expected #{serializer_name} to have link #{expected}." unless has_link?
37
29
  end
38
30
 
39
31
  private
40
32
 
41
- def expectation
42
- expectation = "#{serializer_name} to have link #{expected}"
43
-
44
- submatchers_expectations = failing_submatchers.map do |submatcher|
45
- "(#{submatcher.expectation})"
46
- end.compact.join(", ")
47
-
48
- [expectation, submatchers_expectations].reject(&:nil?).reject(&:empty?).join(" ")
49
- end
50
-
51
33
  def has_link?
52
34
  links.has_key?(expected)
53
35
  end
@@ -18,30 +18,20 @@ module RSpecJSONAPISerializer
18
18
  actual == expected
19
19
  end
20
20
 
21
- def description
22
- "as #{expected_to_string}"
23
- end
24
-
25
- def expectation
26
- [ "as #{expected_to_string}", actual_message ].compact.join(", ")
21
+ def failure_message
22
+ [expected_message, actual_message].compact.join(", ")
27
23
  end
28
24
 
29
25
  private
30
26
 
31
27
  attr_reader :link
32
28
 
33
- def expected_to_string
34
- value_to_string(expected)
29
+ def expected_message
30
+ "expected #{serializer_instance.class.name} to serialize link #{link} as #{expected}"
35
31
  end
36
32
 
37
33
  def actual_message
38
- "got #{value_to_string(actual)} instead" if links.has_key?(link)
39
- end
40
-
41
- def value_to_string(value)
42
- return 'nil' if value.nil?
43
-
44
- value.to_s
34
+ "got #{actual.nil? ? 'nil' : actual} instead" if links.has_key?(link)
45
35
  end
46
36
 
47
37
  def actual
@@ -13,12 +13,12 @@ module RSpecJSONAPISerializer
13
13
  association_matcher.matches?(serializer_instance)
14
14
  end
15
15
 
16
- def description
17
- association_matcher.description
16
+ def serializer(value)
17
+ association_matcher.serializer(value)
18
18
  end
19
19
 
20
- def failure_message
21
- association_matcher.failure_message
20
+ def main_failure_message
21
+ association_matcher.main_failure_message
22
22
  end
23
23
 
24
24
  private
@@ -19,35 +19,17 @@ module RSpecJSONAPISerializer
19
19
  end
20
20
 
21
21
  def as_nil
22
- as(nil)
23
- end
24
-
25
- def description
26
- description = "serialize meta #{expected}"
22
+ add_submatcher HaveMetaMatchers::AsMatcher.new(expected, nil)
27
23
 
28
- [description, submatchers.map(&:description)].flatten.join(' ')
29
- end
30
-
31
- def failure_message
32
- "Expected #{expectation}."
24
+ self
33
25
  end
34
26
 
35
- def failure_message_when_negated
36
- "Did not expect #{expectation}."
27
+ def main_failure_message
28
+ "expected #{serializer_name} to serialize meta #{expected}." unless has_meta?
37
29
  end
38
30
 
39
31
  private
40
32
 
41
- def expectation
42
- expectation = "#{serializer_name} to serialize meta #{expected}"
43
-
44
- submatchers_expectations = failing_submatchers.map do |submatcher|
45
- "(#{submatcher.expectation})"
46
- end.compact.join(", ")
47
-
48
- [expectation, submatchers_expectations].reject(&:nil?).reject(&:empty?).join(" ")
49
- end
50
-
51
33
  def metas
52
34
  @metas ||= serializable_hash.dig(:data, :meta) || {}
53
35
  end
@@ -18,33 +18,22 @@ module RSpecJSONAPISerializer
18
18
  actual == expected
19
19
  end
20
20
 
21
- def description
22
- "as #{expected_to_string}"
23
- end
24
-
25
- def expectation
26
- [ "as #{expected_to_string}", actual_message ].compact.join(", ")
21
+ def failure_message
22
+ [expected_message, actual_message].compact.join(", ")
27
23
  end
28
24
 
29
25
  private
30
26
 
31
27
  attr_reader :meta
32
28
 
33
- def expected_to_string
34
- value_to_string(expected)
29
+ def expected_message
30
+ "expected #{serializer_instance.class.name} to serialize meta #{meta} as #{expected}"
35
31
  end
36
32
 
37
33
  def actual_message
38
34
  "got #{actual.nil? ? 'nil' : actual} instead" if metas.has_key?(meta)
39
35
  end
40
36
 
41
- def value_to_string(value)
42
- return 'nil' if value.nil?
43
-
44
- value.to_s
45
- end
46
-
47
-
48
37
  def actual
49
38
  metas[meta]
50
39
  end
@@ -13,12 +13,12 @@ module RSpecJSONAPISerializer
13
13
  association_matcher.matches?(serializer_instance)
14
14
  end
15
15
 
16
- def description
17
- association_matcher.description
16
+ def serializer(value)
17
+ association_matcher.serializer(value)
18
18
  end
19
19
 
20
- def failure_message
21
- association_matcher.failure_message
20
+ def main_failure_message
21
+ association_matcher.main_failure_message
22
22
  end
23
23
 
24
24
  private
@@ -11,7 +11,7 @@ module RSpecJSONAPISerializer
11
11
  actual == expected
12
12
  end
13
13
 
14
- def failure_message
14
+ def main_failure_message
15
15
  "expected that #{serializer_name} to have type :#{expected}, got :#{actual} instead"
16
16
  end
17
17
 
@@ -13,6 +13,8 @@ module RSpecJSONAPISerializer
13
13
  module Matchers
14
14
  # This allows us to assert attributes on a serializer, e.g.:
15
15
  # expect(serializer).to belong_to(:team)
16
+ # If you have a custom serializer, you can assert its value with the `serializer` submatcher
17
+ # expect(serializer).to belong_to(:team).serializer(TeamSerializer)
16
18
  def belong_to(expected)
17
19
  BelongToMatcher.new(expected)
18
20
  end
@@ -45,6 +47,8 @@ module RSpecJSONAPISerializer
45
47
 
46
48
  # This allows us to assert attributes on a serializer, e.g.:
47
49
  # expect(serializer).to have_many(:teams)
50
+ # If you have a custom serializer, you can assert its value with the `serializer` submatcher
51
+ # expect(serializer).to have_many(:teams).serializer(TeamSerializer)
48
52
  def have_many(expected)
49
53
  HaveManyMatcher.new(expected)
50
54
  end
@@ -61,6 +65,8 @@ module RSpecJSONAPISerializer
61
65
 
62
66
  # This allows us to assert attributes on a serializer, e.g.:
63
67
  # expect(serializer).to have_one(:team)
68
+ # If you have a custom serializer, you can assert its value with the `serializer` submatcher
69
+ # expect(serializer).to have_one(:team).serializer(TeamSerializer)
64
70
  def have_one(expected)
65
71
  HaveOneMatcher.new(expected)
66
72
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+
4
+ require "rspec_jsonapi_serializer/metadata/relationships"
5
+
6
+ module RSpecJSONAPISerializer
7
+ module Metadata
8
+ class Relationships
9
+ def initialize(serializer)
10
+ @serializer = serializer
11
+ end
12
+
13
+ def relationship(key)
14
+ relationships.values.find { |relationship| relationship.key.to_s == key.to_s }
15
+ end
16
+
17
+ def relationships
18
+ serializer.class&.relationships_to_serialize || {}
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :serializer
24
+ end
25
+ end
26
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RSpecJSONAPISerializer
4
- VERSION = "1.0.1"
4
+ VERSION = "1.1.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec_jsonapi_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mateus Cruz
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-21 00:00:00.000000000 Z
11
+ date: 2021-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsonapi-serializer
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.0'
27
- description:
27
+ description:
28
28
  email:
29
29
  - mateus@intricately.com
30
30
  executables: []
@@ -34,6 +34,7 @@ files:
34
34
  - lib/rspec_jsonapi_serializer.rb
35
35
  - lib/rspec_jsonapi_serializer/matchers.rb
36
36
  - lib/rspec_jsonapi_serializer/matchers/association_matcher.rb
37
+ - lib/rspec_jsonapi_serializer/matchers/association_matchers/serializer_matcher.rb
37
38
  - lib/rspec_jsonapi_serializer/matchers/base.rb
38
39
  - lib/rspec_jsonapi_serializer/matchers/belong_to_matcher.rb
39
40
  - lib/rspec_jsonapi_serializer/matchers/have_attribute_matcher.rb
@@ -46,6 +47,7 @@ files:
46
47
  - lib/rspec_jsonapi_serializer/matchers/have_meta_matchers/as_matcher.rb
47
48
  - lib/rspec_jsonapi_serializer/matchers/have_one_matcher.rb
48
49
  - lib/rspec_jsonapi_serializer/matchers/have_type_matcher.rb
50
+ - lib/rspec_jsonapi_serializer/metadata/relationships.rb
49
51
  - lib/rspec_jsonapi_serializer/version.rb
50
52
  homepage: https://github.com/teamintricately/rspec_jsonapi_serializer
51
53
  licenses:
@@ -54,7 +56,7 @@ metadata:
54
56
  homepage_uri: https://github.com/teamintricately/rspec_jsonapi_serializer
55
57
  source_code_uri: https://github.com/teamintricately/rspec_jsonapi_serializer
56
58
  changelog_uri: https://github.com/teamintricately/rspec_jsonapi_serializer/blob/main/CHANGELOG.md
57
- post_install_message:
59
+ post_install_message:
58
60
  rdoc_options: []
59
61
  require_paths:
60
62
  - lib
@@ -69,8 +71,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
71
  - !ruby/object:Gem::Version
70
72
  version: '0'
71
73
  requirements: []
72
- rubygems_version: 3.3.7
73
- signing_key:
74
+ rubygems_version: 3.2.16
75
+ signing_key:
74
76
  specification_version: 4
75
77
  summary: RSpec matchers for jsonapi-serializer.
76
78
  test_files: []