jsonapi-rspec 0.0.6 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +28 -0
- data/lib/jsonapi/rspec.rb +1 -0
- data/lib/jsonapi/rspec/attributes.rb +35 -8
- data/lib/jsonapi/rspec/errors.rb +19 -0
- data/lib/jsonapi/rspec/relationships.rb +11 -4
- data/lib/jsonapi/rspec/type.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1cb9e74645ca848dc856552923c6092f1a50507b46e8a5d3d04c3f74ea8f3853
|
4
|
+
data.tar.gz: 99be94b04361649547d569632dfba13d6b4aff18f28125b0f5c2aef592e2a7f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 37d6572ad5001c856154da99ca102d801457674da382f931b339a501e05ff2e483b4f7bbadb43ae640ca7ff9d3a92e261915db96be9a50d9e3cb40706fc72070
|
7
|
+
data.tar.gz: 5284418148ab647f9b6bed37fc276f8ce97fba88499b564003b7f129238bf3fed2d1f8ec4fa34b491a790c7a0b86b52b343b8001abd149772dbf43a613bd0a9b
|
data/README.md
CHANGED
@@ -37,6 +37,7 @@ Available matchers:
|
|
37
37
|
* `expect(document['data']).to have_jsonapi_attributes(:name, :email, :country).exactly`
|
38
38
|
* `expect(document['data']).to have_attribute(:name).with_value('Lucas')`
|
39
39
|
* `expect(document['data']).to have_relationships(:posts, :comments)`
|
40
|
+
* `expect(document['data']).to have_relationships(:posts, :comments, :likes).exactly`
|
40
41
|
* `expect(document['data']).to have_relationship(:posts).with_data([{ 'id' => '1', 'type' => 'posts' }])`
|
41
42
|
* `expect(document['data']['relationships']['posts']).to have_links(:self, :related)`
|
42
43
|
* `expect(document['data']).to have_link(:self).with_value('http://api.example.com/users/12')`
|
@@ -45,6 +46,33 @@ Available matchers:
|
|
45
46
|
* `expect(document).to have_meta('foo' => 'bar', 'fum' => 'baz').exactly`
|
46
47
|
* `expect(document).to have_jsonapi_object`
|
47
48
|
* `expect(document).to have_jsonapi_object('version' => '1.0')`
|
49
|
+
* `expect(document).to have_error('status' => '422')`
|
50
|
+
|
51
|
+
### On matcher arguments...
|
52
|
+
|
53
|
+
**Note**: JSON:API spec requires JSON documents, thus attribute, relationship
|
54
|
+
and link matcher arguments will always be converted into strings for
|
55
|
+
consistency!!!
|
56
|
+
|
57
|
+
Basically, the tests bellow are absolutely equal:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
expect(document['data']).to have_id(12)
|
61
|
+
expect(document['data']).to have_id('12')
|
62
|
+
|
63
|
+
expect(document['data']).to have_type(:users)
|
64
|
+
expect(document['data']).to have_type('users')
|
65
|
+
|
66
|
+
expect(document['data']).to have_jsonapi_attributes(:name, :email)
|
67
|
+
expect(document['data']).to have_jsonapi_attributes('name', 'email')
|
68
|
+
```
|
69
|
+
|
70
|
+
The JSON:API spec also requires the `id` and `type` to be strings, so any other
|
71
|
+
argument passed will also be converted into a string.
|
72
|
+
|
73
|
+
If the document you are trying to test has mixed string/symbol keys, just
|
74
|
+
configure matchers to be indifferent in that regard, using the
|
75
|
+
`jsonapi_indifferent_hash = true` configuration option.
|
48
76
|
|
49
77
|
## Advanced examples
|
50
78
|
|
data/lib/jsonapi/rspec.rb
CHANGED
@@ -1,17 +1,44 @@
|
|
1
1
|
module JSONAPI
|
2
2
|
module RSpec
|
3
3
|
module Attributes
|
4
|
-
::RSpec::Matchers.define :have_attribute do |
|
5
|
-
match do |
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
9
18
|
end
|
10
19
|
|
11
|
-
chain :with_value do |
|
12
|
-
@
|
13
|
-
@
|
20
|
+
chain :with_value do |expected_value|
|
21
|
+
@should_match_value = true
|
22
|
+
@expected = expected_value
|
14
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 @has_attribute
|
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
|
15
42
|
end
|
16
43
|
|
17
44
|
::RSpec::Matchers.define :have_jsonapi_attributes do |*attrs|
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module JSONAPI
|
2
|
+
module RSpec
|
3
|
+
module Meta
|
4
|
+
::RSpec::Matchers.define :have_error do |error|
|
5
|
+
match do |actual|
|
6
|
+
actual = JSONAPI::RSpec.as_indifferent_hash(actual)
|
7
|
+
return false unless actual.key?('errors')
|
8
|
+
return false unless actual['errors'].is_a?(Array)
|
9
|
+
|
10
|
+
return true if actual['errors'].any? && error.nil?
|
11
|
+
|
12
|
+
error = JSONAPI::RSpec.as_indifferent_hash(error)
|
13
|
+
|
14
|
+
actual['errors'].any? { |actual_error| error <= actual_error }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -15,11 +15,11 @@ module JSONAPI
|
|
15
15
|
end
|
16
16
|
|
17
17
|
failure_message do |actual|
|
18
|
-
if
|
19
|
-
"expected #{actual} to have relationship #{rel}"
|
20
|
-
else
|
18
|
+
if (actual['relationships'] || {}).key?(rel.to_s)
|
21
19
|
"expected #{actual['relationships'][rel.to_s]} " \
|
22
20
|
"to have data #{@data_val}"
|
21
|
+
else
|
22
|
+
"expected #{actual} to have relationship #{rel}"
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
@@ -29,7 +29,14 @@ module JSONAPI
|
|
29
29
|
actual = JSONAPI::RSpec.as_indifferent_hash(actual)
|
30
30
|
return false unless actual.key?('relationships')
|
31
31
|
|
32
|
-
rels.
|
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
|
33
40
|
end
|
34
41
|
end
|
35
42
|
end
|
data/lib/jsonapi/rspec/type.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.11
|
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-
|
11
|
+
date: 2020-12-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec-core
|
@@ -104,6 +104,7 @@ files:
|
|
104
104
|
- README.md
|
105
105
|
- lib/jsonapi/rspec.rb
|
106
106
|
- lib/jsonapi/rspec/attributes.rb
|
107
|
+
- lib/jsonapi/rspec/errors.rb
|
107
108
|
- lib/jsonapi/rspec/id.rb
|
108
109
|
- lib/jsonapi/rspec/jsonapi_object.rb
|
109
110
|
- lib/jsonapi/rspec/links.rb
|
@@ -129,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
129
130
|
- !ruby/object:Gem::Version
|
130
131
|
version: '0'
|
131
132
|
requirements: []
|
132
|
-
rubygems_version: 3.1
|
133
|
+
rubygems_version: 3.2.0.rc.1
|
133
134
|
signing_key:
|
134
135
|
specification_version: 4
|
135
136
|
summary: RSpec matchers for JSON API.
|