jsonapi-rspec 0.0.4 → 0.0.9

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: b47e58f3f6448c75638d51bf9b1e71158cf0ba27e9ad2ef3de757eefef637cff
4
- data.tar.gz: cb6760bd5fbcfd5302f8b53cca011588fedbc591674859a08c4272b820297ba4
3
+ metadata.gz: 404100d143c51997a54c90beae1d40c2f30c38c62d83358c3b52147ca2528a9f
4
+ data.tar.gz: 8c2abd999070e75c839193c513e7c443b65c36c7d31b2e9bd8c27a193a8aa0e1
5
5
  SHA512:
6
- metadata.gz: bd3a469d0c1bdb3655f8d53b35a1dd4e65cb90d26ce7bd5dc8c3d9d8b391e352fa405fe76f9b49dc0943d263c6823abc6fa70904c3b7f4a420dc6ad53d62ea26
7
- data.tar.gz: 8f2965c6f53ad2c2e92de9c550390eb964b07f3dfad58e3ded6e0527da5f61eb8aab126eeb758d34a53df36cf3e773a8b76ec40301c8022906950a2707bccfac
6
+ metadata.gz: 2f69b0906838b7409d56adee6012bf83583b6f271394e8e3a4a4a171821df9b865551e58fea814f799afdf27084a01ca7d4be409a20610c528fc748dd61134b9
7
+ data.tar.gz: 3a9c8bc87b5e83db4279bd7b286892a3b4869dd3fbe6bf44b47dd172a25fb2349cdad018b7d6d54e3d4119e9121e2601910bb34d91552c795927ac137eaa86fe
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
 
@@ -40,14 +37,42 @@ Available matchers:
40
37
  * `expect(document['data']).to have_jsonapi_attributes(:name, :email, :country).exactly`
41
38
  * `expect(document['data']).to have_attribute(:name).with_value('Lucas')`
42
39
  * `expect(document['data']).to have_relationships(:posts, :comments)`
40
+ * `expect(document['data']).to have_relationships(:posts, :comments, :likes).exactly`
43
41
  * `expect(document['data']).to have_relationship(:posts).with_data([{ 'id' => '1', 'type' => 'posts' }])`
44
42
  * `expect(document['data']['relationships']['posts']).to have_links(:self, :related)`
45
43
  * `expect(document['data']).to have_link(:self).with_value('http://api.example.com/users/12')`
46
44
  * `expect(document).to have_meta`
47
45
  * `expect(document).to have_meta('foo' => 'bar')`
46
+ * `expect(document).to have_meta('foo' => 'bar', 'fum' => 'baz').exactly`
48
47
  * `expect(document).to have_jsonapi_object`
49
48
  * `expect(document).to have_jsonapi_object('version' => '1.0')`
50
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
+
51
76
  ## Advanced examples
52
77
 
53
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,20 +1,49 @@
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
8
18
  end
9
19
 
10
- chain :with_value do |val|
11
- @val_set = true
12
- @val = val
20
+ chain :with_value do |expected_value|
21
+ @should_match_value = true
22
+ @expected = expected_value
13
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
14
42
  end
15
43
 
16
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
49
  counted = (attrs.size == actual['attributes'].size) if @exactly
@@ -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
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,7 +11,7 @@ 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|
@@ -25,9 +26,17 @@ module JSONAPI
25
26
 
26
27
  ::RSpec::Matchers.define :have_relationships do |*rels|
27
28
  match do |actual|
29
+ actual = JSONAPI::RSpec.as_indifferent_hash(actual)
28
30
  return false unless actual.key?('relationships')
29
31
 
30
- 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
31
40
  end
32
41
  end
33
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.4
4
+ version: 0.0.9
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-08-07 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
@@ -115,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
129
  - !ruby/object:Gem::Version
116
130
  version: '0'
117
131
  requirements: []
118
- rubygems_version: 3.0.1
132
+ rubygems_version: 3.1.2
119
133
  signing_key:
120
134
  specification_version: 4
121
135
  summary: RSpec matchers for JSON API.