jsonapi-rspec 0.0.6 → 0.0.11

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fa7320e501ff0cc08655a6e935589d300bedef20bc70add21f64d91d2e053a36
4
- data.tar.gz: 1d31cafea53bb133cc8c62c274403bad9aaaa7dfcf4834679bd4cf2dbd246a8c
3
+ metadata.gz: 1cb9e74645ca848dc856552923c6092f1a50507b46e8a5d3d04c3f74ea8f3853
4
+ data.tar.gz: 99be94b04361649547d569632dfba13d6b4aff18f28125b0f5c2aef592e2a7f4
5
5
  SHA512:
6
- metadata.gz: 727c5d55cfa976e4961dfe9798d1767bde29b364d7e4ff33dd52541a12c719c45f67bc021fd179432bd37ae60d26bc8477dce288eadb4b6365d0575570d3c6f0
7
- data.tar.gz: bc74dd39b5b8186f7f6d945277f4fd8a091a20f9c33febf4ed52a788ac54778c6d176e20861261d5e7ef60a061cefdaf1fbefb4eaf63ff5163e661ee378ffde0
6
+ metadata.gz: 37d6572ad5001c856154da99ca102d801457674da382f931b339a501e05ff2e483b4f7bbadb43ae640ca7ff9d3a92e261915db96be9a50d9e3cb40706fc72070
7
+ data.tar.gz: 5284418148ab647f9b6bed37fc276f8ce97fba88499b564003b7f129238bf3fed2d1f8ec4fa34b491a790c7a0b86b52b343b8001abd149772dbf43a613bd0a9b
data/README.md CHANGED
@@ -37,6 +37,7 @@ Available matchers:
37
37
  * `expect(document['data']).to have_jsonapi_attributes(:name, :email, :country).exactly`
38
38
  * `expect(document['data']).to have_attribute(:name).with_value('Lucas')`
39
39
  * `expect(document['data']).to have_relationships(:posts, :comments)`
40
+ * `expect(document['data']).to have_relationships(:posts, :comments, :likes).exactly`
40
41
  * `expect(document['data']).to have_relationship(:posts).with_data([{ 'id' => '1', 'type' => 'posts' }])`
41
42
  * `expect(document['data']['relationships']['posts']).to have_links(:self, :related)`
42
43
  * `expect(document['data']).to have_link(:self).with_value('http://api.example.com/users/12')`
@@ -45,6 +46,33 @@ Available matchers:
45
46
  * `expect(document).to have_meta('foo' => 'bar', 'fum' => 'baz').exactly`
46
47
  * `expect(document).to have_jsonapi_object`
47
48
  * `expect(document).to have_jsonapi_object('version' => '1.0')`
49
+ * `expect(document).to have_error('status' => '422')`
50
+
51
+ ### On matcher arguments...
52
+
53
+ **Note**: JSON:API spec requires JSON documents, thus attribute, relationship
54
+ and link matcher arguments will always be converted into strings for
55
+ consistency!!!
56
+
57
+ Basically, the tests bellow are absolutely equal:
58
+
59
+ ```ruby
60
+ expect(document['data']).to have_id(12)
61
+ expect(document['data']).to have_id('12')
62
+
63
+ expect(document['data']).to have_type(:users)
64
+ expect(document['data']).to have_type('users')
65
+
66
+ expect(document['data']).to have_jsonapi_attributes(:name, :email)
67
+ expect(document['data']).to have_jsonapi_attributes('name', 'email')
68
+ ```
69
+
70
+ The JSON:API spec also requires the `id` and `type` to be strings, so any other
71
+ argument passed will also be converted into a string.
72
+
73
+ If the document you are trying to test has mixed string/symbol keys, just
74
+ configure matchers to be indifferent in that regard, using the
75
+ `jsonapi_indifferent_hash = true` configuration option.
48
76
 
49
77
  ## Advanced examples
50
78
 
@@ -8,6 +8,7 @@ require 'jsonapi/rspec/relationships'
8
8
  require 'jsonapi/rspec/links'
9
9
  require 'jsonapi/rspec/meta'
10
10
  require 'jsonapi/rspec/jsonapi_object'
11
+ require 'jsonapi/rspec/errors'
11
12
 
12
13
  RSpec.configure do |c|
13
14
  c.add_setting :jsonapi_indifferent_hash, default: false
@@ -1,17 +1,44 @@
1
1
  module JSONAPI
2
2
  module RSpec
3
3
  module Attributes
4
- ::RSpec::Matchers.define :have_attribute do |attr|
5
- match do |actual|
6
- actual = JSONAPI::RSpec.as_indifferent_hash(actual)
7
- (actual['attributes'] || {}).key?(attr.to_s) &&
8
- (!@val_set || actual['attributes'][attr.to_s] == @val)
4
+ ::RSpec::Matchers.define :have_attribute do |attr_name|
5
+ match do |doc|
6
+ doc = JSONAPI::RSpec.as_indifferent_hash(doc)
7
+ attributes_node = doc['attributes']
8
+
9
+ return false unless attributes_node
10
+
11
+ @existing_attributes = attributes_node.keys
12
+ @has_attribute = attributes_node.key?(attr_name.to_s)
13
+ @actual = attributes_node[attr_name.to_s]
14
+
15
+ return @actual == @expected if @has_attribute && @should_match_value
16
+
17
+ @has_attribute
9
18
  end
10
19
 
11
- chain :with_value do |val|
12
- @val_set = true
13
- @val = val
20
+ chain :with_value do |expected_value|
21
+ @should_match_value = true
22
+ @expected = expected_value
14
23
  end
24
+
25
+ description do
26
+ result = "have attribute #{attr_name.inspect}"
27
+ result << " with value #{@expected.inspect}" if @should_match_value
28
+ result
29
+ end
30
+
31
+ failure_message do |_doc|
32
+ if @has_attribute
33
+ "expected `#{attr_name}` attribute " \
34
+ "to have value `#{@expected}` but was `#{@actual}`"
35
+ else
36
+ "expected attributes to include `#{attr_name}`. " \
37
+ "Actual attributes were #{@existing_attributes}"
38
+ end
39
+ end
40
+
41
+ diffable
15
42
  end
16
43
 
17
44
  ::RSpec::Matchers.define :have_jsonapi_attributes do |*attrs|
@@ -0,0 +1,19 @@
1
+ module JSONAPI
2
+ module RSpec
3
+ module Meta
4
+ ::RSpec::Matchers.define :have_error do |error|
5
+ match do |actual|
6
+ actual = JSONAPI::RSpec.as_indifferent_hash(actual)
7
+ return false unless actual.key?('errors')
8
+ return false unless actual['errors'].is_a?(Array)
9
+
10
+ return true if actual['errors'].any? && error.nil?
11
+
12
+ error = JSONAPI::RSpec.as_indifferent_hash(error)
13
+
14
+ actual['errors'].any? { |actual_error| error <= actual_error }
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -15,11 +15,11 @@ module JSONAPI
15
15
  end
16
16
 
17
17
  failure_message do |actual|
18
- if !(actual['relationships'] || {}).key?(rel.to_s)
19
- "expected #{actual} to have relationship #{rel}"
20
- else
18
+ if (actual['relationships'] || {}).key?(rel.to_s)
21
19
  "expected #{actual['relationships'][rel.to_s]} " \
22
20
  "to have data #{@data_val}"
21
+ else
22
+ "expected #{actual} to have relationship #{rel}"
23
23
  end
24
24
  end
25
25
  end
@@ -29,7 +29,14 @@ module JSONAPI
29
29
  actual = JSONAPI::RSpec.as_indifferent_hash(actual)
30
30
  return false unless actual.key?('relationships')
31
31
 
32
- rels.map(&:to_s).all? { |rel| actual['relationships'].key?(rel) }
32
+ counted = (rels.size == actual['relationships'].keys.size) if @exactly
33
+
34
+ rels.map(&:to_s).all? { |rel| actual['relationships'].key?(rel) } \
35
+ && (counted == @exactly)
36
+ end
37
+
38
+ chain :exactly do
39
+ @exactly = true
33
40
  end
34
41
  end
35
42
  end
@@ -3,7 +3,7 @@ module JSONAPI
3
3
  module Type
4
4
  ::RSpec::Matchers.define :have_type do |expected|
5
5
  match do |actual|
6
- JSONAPI::RSpec.as_indifferent_hash(actual)['type'] == expected
6
+ JSONAPI::RSpec.as_indifferent_hash(actual)['type'] == expected.to_s
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Hosseini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-12 00:00:00.000000000 Z
11
+ date: 2020-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-core
@@ -104,6 +104,7 @@ files:
104
104
  - README.md
105
105
  - lib/jsonapi/rspec.rb
106
106
  - lib/jsonapi/rspec/attributes.rb
107
+ - lib/jsonapi/rspec/errors.rb
107
108
  - lib/jsonapi/rspec/id.rb
108
109
  - lib/jsonapi/rspec/jsonapi_object.rb
109
110
  - lib/jsonapi/rspec/links.rb
@@ -129,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
130
  - !ruby/object:Gem::Version
130
131
  version: '0'
131
132
  requirements: []
132
- rubygems_version: 3.1.2
133
+ rubygems_version: 3.2.0.rc.1
133
134
  signing_key:
134
135
  specification_version: 4
135
136
  summary: RSpec matchers for JSON API.