jsonapi-rspec 0.0.1 → 0.0.6

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
- SHA1:
3
- metadata.gz: 2c18e0598e541bf12401287dfcaa86a8dac978bc
4
- data.tar.gz: 9043ba44228a6e7c2d655447baace7db637be81b
2
+ SHA256:
3
+ metadata.gz: fa7320e501ff0cc08655a6e935589d300bedef20bc70add21f64d91d2e053a36
4
+ data.tar.gz: 1d31cafea53bb133cc8c62c274403bad9aaaa7dfcf4834679bd4cf2dbd246a8c
5
5
  SHA512:
6
- metadata.gz: 137f12232b91ae146be8b7bf8ec73cf64c8259547cec667685010942da126054859a7045cea4dc0e4b07b5be14321ae2722705183be99aba7184864a2e0653cf
7
- data.tar.gz: 3b372af8e591982007a21bfa54dd7e96f2f4b577d8855ca870eca6832d1f9a0161d8d7b274d16497911a042f200c640080e9b71fd34d481f1c9d7b0023c432f9
6
+ metadata.gz: 727c5d55cfa976e4961dfe9798d1767bde29b364d7e4ff33dd52541a12c719c45f67bc021fd179432bd37ae60d26bc8477dce288eadb4b6365d0575570d3c6f0
7
+ data.tar.gz: bc74dd39b5b8186f7f6d945277f4fd8a091a20f9c33febf4ed52a788ac54778c6d176e20861261d5e7ef60a061cefdaf1fbefb4eaf63ff5163e661ee378ffde0
data/README.md CHANGED
@@ -2,17 +2,6 @@
2
2
 
3
3
  RSpec matchers for [JSON API](http://jsonapi.org).
4
4
 
5
- ## Status
6
-
7
- [![Gem Version](https://badge.fury.io/rb/jsonapi-rspec.svg)](https://badge.fury.io/rb/jsonapi-rspec)
8
- [![Build Status](https://secure.travis-ci.org/jsonapi-rb/jsonapi-rspec.svg?branch=master)](http://travis-ci.org/jsonapi-rb/jsonapi-rspec?branch=master)
9
- [![Gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/jsonapi-rb/Lobby)
10
-
11
- ## Resources
12
-
13
- * Chat: [gitter](http://gitter.im/jsonapi-rb)
14
- * Twitter: [@jsonapirb](http://twitter.com/jsonapirb)
15
-
16
5
  ## Installation
17
6
 
18
7
  Add the following to your application's Gemfile:
@@ -28,9 +17,13 @@ Add to your `spec/spec_helpers.rb`:
28
17
 
29
18
  ```ruby
30
19
  # spec/spec_helpers.rb
20
+ require 'jsonapi/rspec'
21
+
31
22
  RSpec.configure do |config|
32
- # ...
33
23
  config.include JSONAPI::RSpec
24
+
25
+ # Support for documents with mixed string/symbol keys. Disabled by default.
26
+ config.jsonapi_indifferent_hash = true
34
27
  end
35
28
  ```
36
29
 
@@ -40,7 +33,8 @@ Available matchers:
40
33
 
41
34
  * `expect(document['data']).to have_id('12')`
42
35
  * `expect(document['data']).to have_type('users')`
43
- * `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`
44
38
  * `expect(document['data']).to have_attribute(:name).with_value('Lucas')`
45
39
  * `expect(document['data']).to have_relationships(:posts, :comments)`
46
40
  * `expect(document['data']).to have_relationship(:posts).with_data([{ 'id' => '1', 'type' => 'posts' }])`
@@ -48,6 +42,7 @@ Available matchers:
48
42
  * `expect(document['data']).to have_link(:self).with_value('http://api.example.com/users/12')`
49
43
  * `expect(document).to have_meta`
50
44
  * `expect(document).to have_meta('foo' => 'bar')`
45
+ * `expect(document).to have_meta('foo' => 'bar', 'fum' => 'baz').exactly`
51
46
  * `expect(document).to have_jsonapi_object`
52
47
  * `expect(document).to have_jsonapi_object('version' => '1.0')`
53
48
 
@@ -59,6 +54,14 @@ Checking for an included resource:
59
54
  expect(response_body['included'])
60
55
  .to include(have_type('posts').and have_id('1'))
61
56
  ```
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at
60
+ https://github.com/jsonapi-rb/jsonapi-rspec
61
+
62
+ This project is intended to be a safe, welcoming space for collaboration, and
63
+ contributors are expected to adhere to the
64
+ [Contributor Covenant](http://contributor-covenant.org) code of conduct.
62
65
 
63
66
  ## License
64
67
 
@@ -1,3 +1,6 @@
1
+ require 'json'
2
+ require 'rspec/matchers'
3
+ require 'rspec/core'
1
4
  require 'jsonapi/rspec/id'
2
5
  require 'jsonapi/rspec/type'
3
6
  require 'jsonapi/rspec/attributes'
@@ -6,6 +9,10 @@ require 'jsonapi/rspec/links'
6
9
  require 'jsonapi/rspec/meta'
7
10
  require 'jsonapi/rspec/jsonapi_object'
8
11
 
12
+ RSpec.configure do |c|
13
+ c.add_setting :jsonapi_indifferent_hash, default: false
14
+ end
15
+
9
16
  module JSONAPI
10
17
  module RSpec
11
18
  include Id
@@ -15,5 +22,15 @@ module JSONAPI
15
22
  include Links
16
23
  include Meta
17
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
18
35
  end
19
36
  end
@@ -3,6 +3,7 @@ module JSONAPI
3
3
  module Attributes
4
4
  ::RSpec::Matchers.define :have_attribute do |attr|
5
5
  match do |actual|
6
+ actual = JSONAPI::RSpec.as_indifferent_hash(actual)
6
7
  (actual['attributes'] || {}).key?(attr.to_s) &&
7
8
  (!@val_set || actual['attributes'][attr.to_s] == @val)
8
9
  end
@@ -13,11 +14,19 @@ module JSONAPI
13
14
  end
14
15
  end
15
16
 
16
- ::RSpec::Matchers.define :have_attributes do |*attrs|
17
+ ::RSpec::Matchers.define :have_jsonapi_attributes do |*attrs|
17
18
  match do |actual|
19
+ actual = JSONAPI::RSpec.as_indifferent_hash(actual)
18
20
  return false unless actual.key?('attributes')
19
21
 
20
- attrs.all? { |attr| actual['attributes'].key?(attr.to_s) }
22
+ counted = (attrs.size == actual['attributes'].size) if @exactly
23
+
24
+ (attrs.map(&:to_s) - actual['attributes'].keys).empty? &&
25
+ (counted == @exactly)
26
+ end
27
+
28
+ chain :exactly do
29
+ @exactly = true
21
30
  end
22
31
  end
23
32
  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
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,8 +3,9 @@ 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
- (!@val_set || actual['links'] == @val)
8
+ (!@val_set || actual['links'][link.to_s] == @val)
8
9
  end
9
10
 
10
11
  chain :with_value do |val|
@@ -15,9 +16,10 @@ 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
- links.all? { |link| actual['links'].key?(link) }
22
+ links.all? { |link| actual['links'].key?(link.to_s) }
21
23
  end
22
24
  end
23
25
  end
@@ -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,25 @@ 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
+ rels.map(&:to_s).all? { |rel| actual['relationships'].key?(rel) }
30
33
  end
31
34
  end
32
35
  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
7
+ end
6
8
  end
7
9
  end
8
10
  end
metadata CHANGED
@@ -1,15 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Hosseini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-14 00:00:00.000000000 Z
11
+ date: 2020-07-12 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'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-expectations
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
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'
13
55
  - !ruby/object:Gem::Dependency
14
56
  name: rspec
15
57
  requirement: !ruby/object:Gem::Requirement
@@ -25,7 +67,21 @@ dependencies:
25
67
  - !ruby/object:Gem::Version
26
68
  version: '0'
27
69
  - !ruby/object:Gem::Dependency
28
- name: rake
70
+ name: rubocop-performance
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
29
85
  requirement: !ruby/object:Gem::Requirement
30
86
  requirements:
31
87
  - - ">="
@@ -73,8 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
129
  - !ruby/object:Gem::Version
74
130
  version: '0'
75
131
  requirements: []
76
- rubyforge_project:
77
- rubygems_version: 2.6.13
132
+ rubygems_version: 3.1.2
78
133
  signing_key:
79
134
  specification_version: 4
80
135
  summary: RSpec matchers for JSON API.