jsonapi-rspec 0.0.2 → 0.0.7

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: 2be5ed3e2b2ccdb4cee1028850cfc2c881ce15f6
4
- data.tar.gz: 96c0c548a215f89d66b65ad1335cec161743afe9
2
+ SHA256:
3
+ metadata.gz: 5cb345693bd692513446e88fa4d1713b7927afa9b3a3033270ac26daefe3c07f
4
+ data.tar.gz: 8bc079f20dc7cd85e29b2a51f2d44e511d09153809507dbc4f361b55d79caf8e
5
5
  SHA512:
6
- metadata.gz: 2f9da5bfd20dc32a38d928744640f3490bcf443cbfd1e0ed1e987884caf3a31ad5db75e3a8bddd1716002655cbbe05bd91a0ae30e0be7f22b61addce281a6857
7
- data.tar.gz: 1f3146f7d30fcd120722539a0e6fd778577bd9574fccf06d60fc8b5cfcb014a6161b33002576fddba31a6a761198e97f4c37c4c7b28c4a567bb34e4f51b4e1f9
6
+ metadata.gz: 15c13b39490b4a72ef66e1aeed229665aacf8b98a0b3f2dfc8063d4b9dfa18190edb2c9a783cf921f23c0f5553d408065626669f4c39b7fa4cbdfb991e2e559e
7
+ data.tar.gz: e82b0d326630160bf704058b38068de72521670715280fbdf1539d17b725cb1e721167ff5e2ed830f73baa45bf100ed1f999589ef7392a0b19254bbb500b87f3
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,14 +33,17 @@ 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)`
40
+ * `expect(document['data']).to have_relationships(:posts, :comments, :likes).exactly`
46
41
  * `expect(document['data']).to have_relationship(:posts).with_data([{ 'id' => '1', 'type' => 'posts' }])`
47
42
  * `expect(document['data']['relationships']['posts']).to have_links(:self, :related)`
48
43
  * `expect(document['data']).to have_link(:self).with_value('http://api.example.com/users/12')`
49
44
  * `expect(document).to have_meta`
50
45
  * `expect(document).to have_meta('foo' => 'bar')`
46
+ * `expect(document).to have_meta('foo' => 'bar', 'fum' => 'baz').exactly`
51
47
  * `expect(document).to have_jsonapi_object`
52
48
  * `expect(document).to have_jsonapi_object('version' => '1.0')`
53
49
 
@@ -59,6 +55,14 @@ Checking for an included resource:
59
55
  expect(response_body['included'])
60
56
  .to include(have_type('posts').and have_id('1'))
61
57
  ```
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at
61
+ https://github.com/jsonapi-rb/jsonapi-rspec
62
+
63
+ This project is intended to be a safe, welcoming space for collaboration, and
64
+ contributors are expected to adhere to the
65
+ [Contributor Covenant](http://contributor-covenant.org) code of conduct.
62
66
 
63
67
  ## License
64
68
 
@@ -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,21 +3,64 @@ module JSONAPI
3
3
  module Attributes
4
4
  ::RSpec::Matchers.define :have_attribute do |attr|
5
5
  match do |actual|
6
- (actual['attributes'] || {}).key?(attr.to_s) &&
7
- (!@val_set || actual['attributes'][attr.to_s] == @val)
6
+ actual = JSONAPI::RSpec.as_indifferent_hash(actual)
7
+ @attributes_node = actual['attributes']
8
+
9
+ return false unless @attributes_node
10
+
11
+ @has_attribute = @attributes_node.key?(attr.to_s)
12
+ if @has_attribute && @should_match_value
13
+ @actual_value = @attributes_node[attr.to_s]
14
+
15
+ # Work nicely with diffable
16
+ @actual = @actual_value
17
+ @expected = @expected_value
18
+
19
+ return @actual == @expected
20
+ end
21
+ @has_attribute
22
+ end
23
+
24
+ chain :with_value do |expected_value|
25
+ @should_match_value = true
26
+ @expected_value = expected_value
8
27
  end
9
28
 
10
- chain :with_value do |val|
11
- @val_set = true
12
- @val = val
29
+ description do
30
+ result = "have attribute #{attr.inspect}"
31
+ if @should_match_value
32
+ result << " with value #{@expected_value.inspect}"
33
+ end
34
+ result
13
35
  end
36
+
37
+ failure_message do |_actual|
38
+ if @has_attribute
39
+ "expected `#{attr}` attribute " \
40
+ "to have value `#{@expected}` but was `#{@actual}`"
41
+ else
42
+ "expected attributes to include `#{attr}`. " \
43
+ "Actual attributes were #{@attributes_node.keys}"
44
+ end
45
+ end
46
+
47
+ diffable
48
+ attr_reader :actual, :expected
14
49
  end
15
50
 
16
- ::RSpec::Matchers.define :have_attributes do |*attrs|
51
+ ::RSpec::Matchers.define :have_jsonapi_attributes do |*attrs|
17
52
  match do |actual|
53
+ actual = JSONAPI::RSpec.as_indifferent_hash(actual)
18
54
  return false unless actual.key?('attributes')
19
55
 
20
- attrs.all? { |attr| actual['attributes'].key?(attr.to_s) }
56
+ counted = (attrs.size == actual['attributes'].size) if @exactly
57
+
58
+ (attrs.map(&:to_s) - actual['attributes'].keys).empty? &&
59
+ (counted == @exactly)
60
+ end
61
+
62
+ chain :exactly do
63
+ @exactly = true
21
64
  end
22
65
  end
23
66
  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,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
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.2
4
+ version: 0.0.7
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-25 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.