jsonapi_expectations 0.0.5 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/jsonapi_expectations.rb +85 -40
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2601f5db12cdcdd320710f13892bb3b0f2737bb0
|
4
|
+
data.tar.gz: b2c4050e0a5496d0c0c5d7f2fcf069dc060ec43c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71a8dff71c02084597c964cf4f2db4fae13cef3781e01baf4eecf78529d670ed5a976c5b26bd9510f2518d9da9fba90b6e652cb3fecc914767617b0f9e21c41b
|
7
|
+
data.tar.gz: 6a3ccc5f53ccb07267ef3000116b0cbf2ae3aa00003a2ce16c457109cd224672bef356de39aa748e830d14725198c4931cce2ecb0d0361437285c9ab50d62610
|
data/lib/jsonapi_expectations.rb
CHANGED
@@ -2,36 +2,45 @@ require 'airborne'
|
|
2
2
|
require 'active_support/inflector'
|
3
3
|
|
4
4
|
module JsonapiExpectations
|
5
|
+
module Exceptions
|
6
|
+
class ExpectationError < StandardError; end
|
7
|
+
end
|
8
|
+
|
5
9
|
def expect_attributes attrs
|
6
10
|
expect_valid_data
|
7
|
-
if
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
11
|
+
location = if array_response?
|
12
|
+
'data.?.attributes'
|
13
|
+
else
|
14
|
+
'data.attributes'
|
15
|
+
end
|
12
16
|
expect_json location, dasherize_keys(attrs)
|
17
|
+
rescue RSpec::Expectations::ExpectationNotMetError
|
18
|
+
msg = "Expected attributes #{attrs} to be present in json response"
|
19
|
+
raise Exceptions::ExpectationError, msg
|
13
20
|
end
|
14
|
-
alias_method :expect_attributes_in_list, :expect_attributes
|
15
21
|
|
16
22
|
def expect_attributes_absent *keys
|
17
23
|
expect_valid_data
|
18
|
-
if
|
24
|
+
if array_response?
|
19
25
|
json_body[:data].each do |data|
|
20
26
|
dasherize_array(keys).each do |key|
|
21
|
-
expect(data[
|
27
|
+
expect(data[:attributes].key?(key)).to be_falsey
|
22
28
|
end
|
23
29
|
end
|
24
30
|
else
|
25
31
|
dasherize_array(keys).each do |key|
|
26
|
-
expect(json_body[:data][:attributes]
|
32
|
+
expect(json_body[:data][:attributes].key?(key)).to be_falsey
|
27
33
|
end
|
28
34
|
end
|
35
|
+
rescue RSpec::Expectations::ExpectationNotMetError
|
36
|
+
msg = "Expected #{keys} not to be present in json response"
|
37
|
+
raise Exceptions::ExpectationError, msg
|
29
38
|
end
|
30
|
-
alias_method :expect_attributes_absent_in_list, :expect_attributes_absent
|
31
39
|
|
32
40
|
def expect_relationship opts
|
33
41
|
expect_valid_data
|
34
|
-
|
42
|
+
type = opts[:type] || opts[:key].pluralize
|
43
|
+
location = if array_response?
|
35
44
|
"data.?.relationships.#{opts[:key]}"
|
36
45
|
else
|
37
46
|
"data.relationships.#{opts[:key]}"
|
@@ -47,9 +56,8 @@ module JsonapiExpectations
|
|
47
56
|
|
48
57
|
if opts[:id]
|
49
58
|
location = "#{location}.data"
|
50
|
-
type = opts[:type] || opts[:key].pluralize
|
51
59
|
|
52
|
-
if opts[:id].
|
60
|
+
if opts[:id].is_a? Array # if array was passed in, check each
|
53
61
|
opts[:id].each do |id|
|
54
62
|
expect_linkage_data "#{location}.?", { type: type, id: id }, opts[:included]
|
55
63
|
end
|
@@ -57,8 +65,10 @@ module JsonapiExpectations
|
|
57
65
|
expect_linkage_data location, { type: type, id: opts[:id] }, opts[:included]
|
58
66
|
end
|
59
67
|
end
|
68
|
+
rescue RSpec::Expectations::ExpectationNotMetError
|
69
|
+
msg = "Expected relationship to #{type} in response"
|
70
|
+
raise Exceptions::ExpectationError, msg
|
60
71
|
end
|
61
|
-
alias_method :expect_relationship_in_list, :expect_relationship
|
62
72
|
|
63
73
|
def expect_item_count number
|
64
74
|
expect_valid_data
|
@@ -67,60 +77,95 @@ module JsonapiExpectations
|
|
67
77
|
|
68
78
|
def expect_record find_me, opts = {}
|
69
79
|
opts[:type] ||= jsonapi_type find_me
|
70
|
-
if opts[:included]
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
80
|
+
location = if opts[:included]
|
81
|
+
json_body[:included]
|
82
|
+
else
|
83
|
+
json_body[:data]
|
84
|
+
end
|
75
85
|
expect_valid_data location
|
76
86
|
found = location.detect do |item|
|
77
87
|
jsonapi_match? find_me, item, opts[:type]
|
78
88
|
end
|
79
89
|
expect(found).to be_truthy
|
90
|
+
rescue RSpec::Expectations::ExpectationNotMetError
|
91
|
+
msg = "Expected #{find_me} to be present in json response"
|
92
|
+
raise Exceptions::ExpectationError, msg
|
80
93
|
end
|
81
|
-
alias_method :expect_item_in_list, :expect_record
|
82
94
|
|
83
95
|
def expect_record_absent dont_find_me, opts = {}
|
84
96
|
opts[:type] ||= jsonapi_type dont_find_me
|
85
|
-
if opts[:included]
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
97
|
+
location = if opts[:included]
|
98
|
+
json_body[:included]
|
99
|
+
else
|
100
|
+
json_body[:data]
|
101
|
+
end
|
90
102
|
expect_valid_data location
|
91
103
|
location.each do |item|
|
92
104
|
expect(jsonapi_match?(dont_find_me, item, opts[:type])).to be_falsey
|
93
105
|
end
|
106
|
+
rescue RSpec::Expectations::ExpectationNotMetError
|
107
|
+
msg = "Expected #{dont_find_me} to not be present in json response"
|
108
|
+
raise Exceptions::ExpectationError, msg
|
109
|
+
end
|
110
|
+
|
111
|
+
def expect_records_sorted_by attr, direction = :asc
|
112
|
+
json_body[:data].each_with_index do |item, index|
|
113
|
+
return if json_body[:data].last == item
|
114
|
+
|
115
|
+
this_one = item[:attributes][attr]
|
116
|
+
next_one = json_body[:data][index + 1][:attributes][attr]
|
117
|
+
|
118
|
+
if direction == :asc
|
119
|
+
expect(this_one).to be <= next_one
|
120
|
+
elsif direction == :desc
|
121
|
+
expect(next_one).to be <= this_one
|
122
|
+
else
|
123
|
+
raise "2nd argument needs to be :asc or :desc"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
rescue RSpec::Expectations::ExpectationNotMetError
|
127
|
+
msg = "Expected response to be sorted by #{attr} #{direction}"
|
128
|
+
raise Exceptions::ExpectationError, msg
|
94
129
|
end
|
95
|
-
alias_method :expect_item_not_in_list, :expect_record_absent
|
96
|
-
alias_method :expect_item_not_to_be_in_list, :expect_record_absent
|
97
|
-
alias_method :expect_item_to_not_be_in_list, :expect_record_absent
|
98
130
|
|
99
131
|
def find_record record, opts = {}
|
100
132
|
opts[:type] ||= jsonapi_type(record)
|
101
|
-
if opts[:included]
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
133
|
+
location = if opts[:included]
|
134
|
+
json_body[:included]
|
135
|
+
else
|
136
|
+
json_body[:data]
|
137
|
+
end
|
106
138
|
expect_valid_data location
|
107
139
|
location.select do |item|
|
108
140
|
jsonapi_match? record, item, opts[:type]
|
109
141
|
end.first
|
110
142
|
end
|
111
143
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
144
|
+
# This will go away in the 0.1.0 release
|
145
|
+
# TODO: deprecate these
|
146
|
+
alias expect_attributes_in_list expect_attributes
|
147
|
+
alias expect_attributes_absent_in_list expect_attributes_absent
|
148
|
+
alias expect_relationship_in_list expect_relationship
|
149
|
+
alias expect_item_in_list expect_record
|
150
|
+
alias expect_item_not_in_list expect_record_absent
|
151
|
+
alias expect_item_not_to_be_in_list expect_record_absent
|
152
|
+
alias expect_item_to_not_be_in_list expect_record_absent
|
117
153
|
|
118
154
|
private
|
119
155
|
|
120
|
-
def
|
156
|
+
def array_response?
|
121
157
|
json_body[:data].is_a? Array
|
122
158
|
end
|
123
159
|
|
160
|
+
def expect_valid_data location = nil
|
161
|
+
location ||= json_body[:data]
|
162
|
+
expect(location).to_not be_nil
|
163
|
+
expect(location).to_not be_empty
|
164
|
+
rescue RSpec::Expectations::ExpectationNotMetError
|
165
|
+
msg = "#{location} is does not contain data"
|
166
|
+
raise Exceptions::ExpectationError, msg
|
167
|
+
end
|
168
|
+
|
124
169
|
def expect_linkage_data location, relationship_data, included
|
125
170
|
begin
|
126
171
|
expect_json location, relationship_data
|
@@ -128,7 +173,7 @@ module JsonapiExpectations
|
|
128
173
|
expect_json "#{location}.?", relationship_data
|
129
174
|
end
|
130
175
|
|
131
|
-
expect_json
|
176
|
+
expect_json 'included.?', relationship_data if included
|
132
177
|
end
|
133
178
|
|
134
179
|
def dasherize_array array
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi_expectations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ross-Hunter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05-
|
11
|
+
date: 2017-05-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: airborne
|