jsonapi-rspec 0.0.5 → 0.0.10

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: 3c6811a07f15a3e68d69b7ba45fc6df97c649d21e3b2a8df38fb493c6a20b615
4
- data.tar.gz: da564714ee866647007b624ec7c871c2834949ec6a5d7987622f39df15e72458
3
+ metadata.gz: fa870b24a1c1b3fcd2f6fb783dce03bd6249ddfc4cf3de068213e63aa4ed966e
4
+ data.tar.gz: eba5c9887ceec4b26b538a4f20337976d4196b28e126052623f9572d1234a65d
5
5
  SHA512:
6
- metadata.gz: 25715dae1b5adfcfc2e2e327d7042ee035efbd7123dd3bb36be939ff1e412a16bda3765f304ecc6304f446eab52a3584954cc0f0334fff4501143f06f450f81d
7
- data.tar.gz: f95632ad22f0b9b74fd5315e39f2ea800c7c1062b53845357d7312f3d1d7831faae4e476c2981d59acef81c2bc510200ecbd33bbced0bdf21a9ccba18f1da8a7
6
+ metadata.gz: 7d8ce2a95ddd8b843c24ef98856b771ff33c71f351bbc4f848b19eb425924a3814df254e8b8a288c64089caeb175fe54ce7acac263b42e45b5aae8b0919145f0
7
+ data.tar.gz: 3abf2abeb28e42e6294c36ce3fbd6e5a050ed79d990932614e44166b7f89d71e10bdf59e0b2eec415c3f57e66ebf500e3c75f49e40158660a727bedfc9b0d1fa
data/README.md CHANGED
@@ -2,11 +2,6 @@
2
2
 
3
3
  RSpec matchers for [JSON API](http://jsonapi.org).
4
4
 
5
- ## Resources
6
-
7
- * Chat: [gitter](http://gitter.im/jsonapi-rb)
8
- * Twitter: [@jsonapirb](http://twitter.com/jsonapirb)
9
-
10
5
  ## Installation
11
6
 
12
7
  Add the following to your application's Gemfile:
@@ -30,6 +25,7 @@ RSpec.configure do |config|
30
25
  # Support for documents with mixed string/symbol keys. Disabled by default.
31
26
  config.jsonapi_indifferent_hash = true
32
27
  end
28
+ ```
33
29
 
34
30
  ## Usage and documentation
35
31
 
@@ -41,13 +37,42 @@ Available matchers:
41
37
  * `expect(document['data']).to have_jsonapi_attributes(:name, :email, :country).exactly`
42
38
  * `expect(document['data']).to have_attribute(:name).with_value('Lucas')`
43
39
  * `expect(document['data']).to have_relationships(:posts, :comments)`
40
+ * `expect(document['data']).to have_relationships(:posts, :comments, :likes).exactly`
44
41
  * `expect(document['data']).to have_relationship(:posts).with_data([{ 'id' => '1', 'type' => 'posts' }])`
45
42
  * `expect(document['data']['relationships']['posts']).to have_links(:self, :related)`
46
43
  * `expect(document['data']).to have_link(:self).with_value('http://api.example.com/users/12')`
47
44
  * `expect(document).to have_meta`
48
45
  * `expect(document).to have_meta('foo' => 'bar')`
46
+ * `expect(document).to have_meta('foo' => 'bar', 'fum' => 'baz').exactly`
49
47
  * `expect(document).to have_jsonapi_object`
50
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.
51
76
 
52
77
  ## Advanced examples
53
78
 
@@ -1,5 +1,6 @@
1
1
  require 'json'
2
2
  require 'rspec/matchers'
3
+ require 'rspec/core'
3
4
  require 'jsonapi/rspec/id'
4
5
  require 'jsonapi/rspec/type'
5
6
  require 'jsonapi/rspec/attributes'
@@ -7,6 +8,7 @@ require 'jsonapi/rspec/relationships'
7
8
  require 'jsonapi/rspec/links'
8
9
  require 'jsonapi/rspec/meta'
9
10
  require 'jsonapi/rspec/jsonapi_object'
11
+ require 'jsonapi/rspec/errors'
10
12
 
11
13
  RSpec.configure do |c|
12
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 @actual
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
@@ -4,9 +4,17 @@ module JSONAPI
4
4
  ::RSpec::Matchers.define :have_meta do |val|
5
5
  match do |actual|
6
6
  actual = JSONAPI::RSpec.as_indifferent_hash(actual)
7
+ return false unless actual.key?('meta')
8
+ return true unless val
9
+
7
10
  val = JSONAPI::RSpec.as_indifferent_hash(val)
11
+ return false unless val <= actual['meta']
12
+
13
+ !@exactly || (@exactly && val.size == actual['meta'].size)
14
+ end
8
15
 
9
- actual.key?('meta') && (!val || actual['meta'] == val)
16
+ chain :exactly do
17
+ @exactly = true
10
18
  end
11
19
  end
12
20
  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,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.10
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-06-29 00:00:00.000000000 Z
11
+ date: 2020-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rspec-expectations
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -90,6 +104,7 @@ files:
90
104
  - README.md
91
105
  - lib/jsonapi/rspec.rb
92
106
  - lib/jsonapi/rspec/attributes.rb
107
+ - lib/jsonapi/rspec/errors.rb
93
108
  - lib/jsonapi/rspec/id.rb
94
109
  - lib/jsonapi/rspec/jsonapi_object.rb
95
110
  - lib/jsonapi/rspec/links.rb