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 +5 -5
- data/README.md +16 -13
- data/lib/jsonapi/rspec.rb +17 -0
- data/lib/jsonapi/rspec/attributes.rb +11 -2
- data/lib/jsonapi/rspec/id.rb +3 -1
- data/lib/jsonapi/rspec/jsonapi_object.rb +4 -2
- data/lib/jsonapi/rspec/links.rb +4 -2
- data/lib/jsonapi/rspec/meta.rb +12 -2
- data/lib/jsonapi/rspec/relationships.rb +6 -3
- data/lib/jsonapi/rspec/type.rb +3 -1
- metadata +60 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fa7320e501ff0cc08655a6e935589d300bedef20bc70add21f64d91d2e053a36
|
4
|
+
data.tar.gz: 1d31cafea53bb133cc8c62c274403bad9aaaa7dfcf4834679bd4cf2dbd246a8c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
|
data/lib/jsonapi/rspec.rb
CHANGED
@@ -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 :
|
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.
|
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
|
data/lib/jsonapi/rspec/id.rb
CHANGED
@@ -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.
|
7
|
-
|
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
|
data/lib/jsonapi/rspec/links.rb
CHANGED
@@ -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
|
data/lib/jsonapi/rspec/meta.rb
CHANGED
@@ -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.
|
7
|
-
|
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]}
|
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
|
data/lib/jsonapi/rspec/type.rb
CHANGED
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.
|
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:
|
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:
|
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
|
-
|
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.
|