jsonapi-rspec 0.0.3 → 0.0.8

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: b878b8a4ec8f73194ed8abe2b544e1495d19d0ccc5a430a5696993cf63c4f4a7
4
- data.tar.gz: 94be8756e49e6a50ce5723f9acd6982791b2a00cdfd7ad2c078189ec9190175a
3
+ metadata.gz: 52b2400bcac651bfc0b4c8421d716c79fbaf2e18041a2eef042fe610ed07d6e9
4
+ data.tar.gz: 4222824ea957fe826a5d2e6fc7cf2bccfbe05abd012047809ea1e88ad75b426c
5
5
  SHA512:
6
- metadata.gz: 214b267455042c80a12476e976d7c4567dc21e8aa6239b29e97e6f55b2aab9285caefe39ab7af84c42090fd9792f97b2044b0a941277dc5cbf60b7ce95c284d2
7
- data.tar.gz: 6a2926acda88bb36be7455fabbfea98013c545032156b4375d2ab09b419bc23e67e26a03478c35a16cfeafa2140281e1053f1c2f3ed77bf6029a1fd40421c8f5
6
+ metadata.gz: 7aaf222387bac2360894059c1dea6b82e8c954518d6ed36aad4381784b40d08f93c12dea33d56c6eb07efaf7b8c2da37a40ef3ac68d37bbf0400aab2f173cc7d
7
+ data.tar.gz: 8322947de5e6a2832e1fa0813ce26a4a1db01f9b1fe7fc0236e9f66913f597386144454363cb184f252f178fe9001be86e6220e6ea94cef95313307aa06e2949
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:
@@ -25,8 +20,10 @@ Add to your `spec/spec_helpers.rb`:
25
20
  require 'jsonapi/rspec'
26
21
 
27
22
  RSpec.configure do |config|
28
- # ...
29
23
  config.include JSONAPI::RSpec
24
+
25
+ # Support for documents with mixed string/symbol keys. Disabled by default.
26
+ config.jsonapi_indifferent_hash = true
30
27
  end
31
28
  ```
32
29
 
@@ -36,17 +33,46 @@ Available matchers:
36
33
 
37
34
  * `expect(document['data']).to have_id('12')`
38
35
  * `expect(document['data']).to have_type('users')`
39
- * `expect(document['data']).to have_attributes(:name, :email)`
36
+ * `expect(document['data']).to have_jsonapi_attributes(:name, :email)`
37
+ * `expect(document['data']).to have_jsonapi_attributes(:name, :email, :country).exactly`
40
38
  * `expect(document['data']).to have_attribute(:name).with_value('Lucas')`
41
39
  * `expect(document['data']).to have_relationships(:posts, :comments)`
40
+ * `expect(document['data']).to have_relationships(:posts, :comments, :likes).exactly`
42
41
  * `expect(document['data']).to have_relationship(:posts).with_data([{ 'id' => '1', 'type' => 'posts' }])`
43
42
  * `expect(document['data']['relationships']['posts']).to have_links(:self, :related)`
44
43
  * `expect(document['data']).to have_link(:self).with_value('http://api.example.com/users/12')`
45
44
  * `expect(document).to have_meta`
46
45
  * `expect(document).to have_meta('foo' => 'bar')`
46
+ * `expect(document).to have_meta('foo' => 'bar', 'fum' => 'baz').exactly`
47
47
  * `expect(document).to have_jsonapi_object`
48
48
  * `expect(document).to have_jsonapi_object('version' => '1.0')`
49
49
 
50
+ ### On matcher arguments...
51
+
52
+ **Note**: JSON:API spec requires JSON documents, thus attribute, relationship
53
+ and link matcher arguments will always be converted into strings for
54
+ consistency!!!
55
+
56
+ Basically, the tests bellow are absolutely equal:
57
+
58
+ ```ruby
59
+ expect(document['data']).to have_id(12)
60
+ expect(document['data']).to have_id('12')
61
+
62
+ expect(document['data']).to have_type(:users)
63
+ expect(document['data']).to have_type('users')
64
+
65
+ expect(document['data']).to have_jsonapi_attributes(:name, :email)
66
+ expect(document['data']).to have_jsonapi_attributes('name', 'email')
67
+ ```
68
+
69
+ The JSON:API spec also requires the `id` and `type` to be strings, so any other
70
+ argument passed will also be converted into a string.
71
+
72
+ If the document you are trying to test has mixed string/symbol keys, just
73
+ configure matchers to be indifferent in that regard, using the
74
+ `jsonapi_indifferent_hash = true` configuration option.
75
+
50
76
  ## Advanced examples
51
77
 
52
78
  Checking for an included resource:
@@ -1,4 +1,6 @@
1
+ require 'json'
1
2
  require 'rspec/matchers'
3
+ require 'rspec/core'
2
4
  require 'jsonapi/rspec/id'
3
5
  require 'jsonapi/rspec/type'
4
6
  require 'jsonapi/rspec/attributes'
@@ -7,6 +9,10 @@ require 'jsonapi/rspec/links'
7
9
  require 'jsonapi/rspec/meta'
8
10
  require 'jsonapi/rspec/jsonapi_object'
9
11
 
12
+ RSpec.configure do |c|
13
+ c.add_setting :jsonapi_indifferent_hash, default: false
14
+ end
15
+
10
16
  module JSONAPI
11
17
  module RSpec
12
18
  include Id
@@ -16,5 +22,15 @@ module JSONAPI
16
22
  include Links
17
23
  include Meta
18
24
  include JsonapiObject
25
+
26
+ def self.as_indifferent_hash(doc)
27
+ return doc unless ::RSpec.configuration.jsonapi_indifferent_hash
28
+
29
+ if doc.respond_to?(:with_indifferent_access)
30
+ return doc.with_indifferent_access
31
+ end
32
+
33
+ JSON.parse(JSON.generate(doc))
34
+ end
19
35
  end
20
36
  end
@@ -1,23 +1,59 @@
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['attributes'] || {}).key?(attr.to_s) &&
7
- (!@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
18
+ end
19
+
20
+ chain :with_value do |expected_value|
21
+ @should_match_value = true
22
+ @expected = expected_value
8
23
  end
9
24
 
10
- chain :with_value do |val|
11
- @val_set = true
12
- @val = val
25
+ description do
26
+ result = "have attribute #{attr_name.inspect}"
27
+ result << " with value #{@expected.inspect}" if @should_match_value
28
+ result
13
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
14
42
  end
15
43
 
16
- ::RSpec::Matchers.define :have_attributes do |*attrs|
44
+ ::RSpec::Matchers.define :have_jsonapi_attributes do |*attrs|
17
45
  match do |actual|
46
+ actual = JSONAPI::RSpec.as_indifferent_hash(actual)
18
47
  return false unless actual.key?('attributes')
19
48
 
20
- attrs.all? { |attr| actual['attributes'].key?(attr.to_s) }
49
+ counted = (attrs.size == actual['attributes'].size) if @exactly
50
+
51
+ (attrs.map(&:to_s) - actual['attributes'].keys).empty? &&
52
+ (counted == @exactly)
53
+ end
54
+
55
+ chain :exactly do
56
+ @exactly = true
21
57
  end
22
58
  end
23
59
  end
@@ -2,7 +2,9 @@ module JSONAPI
2
2
  module RSpec
3
3
  module Id
4
4
  ::RSpec::Matchers.define :have_id do |expected|
5
- match { |actual| actual['id'] == expected }
5
+ match do |actual|
6
+ JSONAPI::RSpec.as_indifferent_hash(actual)['id'] == expected.to_s
7
+ end
6
8
  end
7
9
  end
8
10
  end
@@ -3,8 +3,10 @@ module JSONAPI
3
3
  module JsonapiObject
4
4
  ::RSpec::Matchers.define :have_jsonapi_object do |val|
5
5
  match do |actual|
6
- actual.key?('jsonapi') &&
7
- (!val || actual['jsonapi'] == val)
6
+ actual = JSONAPI::RSpec.as_indifferent_hash(actual)
7
+ val = JSONAPI::RSpec.as_indifferent_hash(val)
8
+
9
+ actual.key?('jsonapi') && (!val || actual['jsonapi'] == val)
8
10
  end
9
11
  end
10
12
  end
@@ -3,6 +3,7 @@ module JSONAPI
3
3
  module Links
4
4
  ::RSpec::Matchers.define :have_link do |link|
5
5
  match do |actual|
6
+ actual = JSONAPI::RSpec.as_indifferent_hash(actual)
6
7
  actual.key?('links') && actual['links'].key?(link.to_s) &&
7
8
  (!@val_set || actual['links'][link.to_s] == @val)
8
9
  end
@@ -15,6 +16,7 @@ module JSONAPI
15
16
 
16
17
  ::RSpec::Matchers.define :have_links do |*links|
17
18
  match do |actual|
19
+ actual = JSONAPI::RSpec.as_indifferent_hash(actual)
18
20
  return false unless actual.key?('links')
19
21
 
20
22
  links.all? { |link| actual['links'].key?(link.to_s) }
@@ -3,8 +3,18 @@ module JSONAPI
3
3
  module Meta
4
4
  ::RSpec::Matchers.define :have_meta do |val|
5
5
  match do |actual|
6
- actual.key?('meta') &&
7
- (!val || actual['meta'] == val)
6
+ actual = JSONAPI::RSpec.as_indifferent_hash(actual)
7
+ return false unless actual.key?('meta')
8
+ return true unless val
9
+
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
15
+
16
+ chain :exactly do
17
+ @exactly = true
8
18
  end
9
19
  end
10
20
  end
@@ -3,6 +3,7 @@ module JSONAPI
3
3
  module Relationships
4
4
  ::RSpec::Matchers.define :have_relationship do |rel|
5
5
  match do |actual|
6
+ actual = JSONAPI::RSpec.as_indifferent_hash(actual)
6
7
  return false unless (actual['relationships'] || {}).key?(rel.to_s)
7
8
 
8
9
  !@data_set || actual['relationships'][rel.to_s]['data'] == @data_val
@@ -10,23 +11,32 @@ module JSONAPI
10
11
 
11
12
  chain :with_data do |val|
12
13
  @data_set = true
13
- @data_val = val
14
+ @data_val = JSONAPI::RSpec.as_indifferent_hash(val)
14
15
  end
15
16
 
16
17
  failure_message do |actual|
17
18
  if !(actual['relationships'] || {}).key?(rel.to_s)
18
19
  "expected #{actual} to have relationship #{rel}"
19
20
  else
20
- "expected #{actual['relationships'][rel.to_s]} to have data #{@data_val}"
21
+ "expected #{actual['relationships'][rel.to_s]} " \
22
+ "to have data #{@data_val}"
21
23
  end
22
24
  end
23
25
  end
24
26
 
25
27
  ::RSpec::Matchers.define :have_relationships do |*rels|
26
28
  match do |actual|
29
+ actual = JSONAPI::RSpec.as_indifferent_hash(actual)
27
30
  return false unless actual.key?('relationships')
28
31
 
29
- rels.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
30
40
  end
31
41
  end
32
42
  end
@@ -2,7 +2,9 @@ module JSONAPI
2
2
  module RSpec
3
3
  module Type
4
4
  ::RSpec::Matchers.define :have_type do |expected|
5
- match { |actual| actual['type'] == expected }
5
+ match do |actual|
6
+ JSONAPI::RSpec.as_indifferent_hash(actual)['type'] == expected.to_s
7
+ end
6
8
  end
7
9
  end
8
10
  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.3
4
+ version: 0.0.8
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-03-26 00:00:00.000000000 Z
11
+ date: 2020-07-30 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
@@ -24,6 +38,20 @@ dependencies:
24
38
  - - ">="
25
39
  - !ruby/object:Gem::Version
26
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: rspec
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -39,7 +67,7 @@ dependencies:
39
67
  - !ruby/object:Gem::Version
40
68
  version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
- name: rake
70
+ name: rubocop-performance
43
71
  requirement: !ruby/object:Gem::Requirement
44
72
  requirements:
45
73
  - - ">="
@@ -101,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
129
  - !ruby/object:Gem::Version
102
130
  version: '0'
103
131
  requirements: []
104
- rubygems_version: 3.0.1
132
+ rubygems_version: 3.1.2
105
133
  signing_key:
106
134
  specification_version: 4
107
135
  summary: RSpec matchers for JSON API.