rspec-json_api 1.3.0 → 1.4.0

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.
data/README.md CHANGED
@@ -1,319 +1,319 @@
1
- # RSpec::JsonApi
2
-
3
- [RSpec:JsonAPI](https://github.com/nomtek/rspec-json_api)
4
- is an extension for [RSpec](https://github.com/rspec)
5
- to easily allow testing JSON API responses.
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'rspec-json_api'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle install
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install rspec-json_api
22
-
23
- Generate directory tree:
24
-
25
- rails generate rspec:json_api:install
26
-
27
- Require gem assets in your `rails_helper.rb`
28
- ```ruby
29
- Dir[File.join(__dir__, 'rspec', 'json_api', 'types', '*.rb')].each { |file| require file }
30
- Dir[File.join(__dir__, 'rspec', 'json_api', 'interfaces', '*.rb')].each { |file| require file }
31
- ```
32
-
33
- ## Generators
34
-
35
- Using build-in generators it's possible to create custom interface and type.
36
-
37
- Generate new template:
38
-
39
- rails generate rspec:json_api:interface interface-name
40
-
41
- Generate new type:
42
-
43
- rails generate rspec:json_api:type type-name
44
-
45
-
46
- ## Example usage
47
-
48
- ```ruby
49
- # spec/controllers/users_controller_spec.rb
50
-
51
- RSpec.describe UsersController, type: :controller do
52
- describe '#index' do
53
- let(:expected_schema) do
54
- Array[{
55
- id: RSpec::JsonApi::Types::UUID,
56
- name: String,
57
- age: Integer,
58
- favouriteColorHex: /^\#([a-fA-F]|[0-9]){3,6}$/,
59
- number: -> { { type: Integer, min: 10, max: 20, lambda: ->(actual) { actual.even? } } }
60
- }]
61
- end
62
-
63
- it 'matches API response' do
64
- get :index
65
-
66
- expect(response.body).to match_json_schema(expected_schema)
67
- end
68
- end
69
-
70
- describe '#update' do
71
- it 'matches API response' do
72
- put :update, params: { name: 'John', age: 35 }
73
-
74
- expect(response.body).to have_no_content
75
- end
76
- end
77
- end
78
- ```
79
-
80
- ## Built-in matchers
81
- - ### match_json_schema
82
- ```
83
- expect(response.body).to match_json_schema(expected_schema)
84
- ```
85
-
86
- - ### have_no_content
87
- ```
88
- expect(response.body).to have_no_content
89
- ```
90
-
91
- ## Interfaces
92
- The gem introduces interfaces to reuse them during test matches.
93
-
94
- ```ruby
95
- # spec/rspec/json_api/interfaces/example_interface.rb
96
-
97
- module RSpec
98
- module JsonApi
99
- module Interfaces
100
- EXAMPLE_INTERFACE = {
101
- id: Types::UUID,
102
- name: String,
103
- number: Integer,
104
- color: -> { { inclusion: %w[black red white], allow_blank: true } }
105
- }.freeze
106
- end
107
- end
108
- end
109
- ```
110
- _Note: You can either generate file on your own or use generator._
111
- ## Types
112
-
113
- The gem allow users either to user build-in types or define owns.
114
- ### Build-in types
115
- - #### EMAIL
116
- ```ruby
117
- RSpec::JsonApi::Types::EMAIL
118
- ```
119
- - #### URI
120
- ```ruby
121
- RSpec::JsonApi::Types::URI
122
- ```
123
- - #### UUID
124
- ```ruby
125
- RSpec::JsonApi::Types::UUID
126
- ```
127
-
128
-
129
- Custom type example:
130
- ```ruby
131
- # spec/rspec/json_api/types/color_hex.rb
132
-
133
- module RSpec
134
- module JsonApi
135
- module Types
136
- COLOR_HEX = /^#(?:[0-9a-fA-F]{3}){1,2}$/
137
- end
138
- end
139
- end
140
-
141
- RSpec::JsonApi::Types::COLOR_HEX
142
- ```
143
-
144
- _Note: You can either generate file on your own or use generator._
145
- ## Matching methods
146
- The gem offers variety of possible matching methods.
147
-
148
- ### Presumptions
149
- - `match_json_schema` always require full keys match.
150
-
151
- Failure Example:
152
- ```ruby
153
- let(:expected) do
154
- {
155
- id: RSpec::JsonApi::Types::UUID,
156
- name: String,
157
- age: Integer
158
- }
159
- end
160
-
161
- let(:actual) do
162
- {
163
- id: "0a2f911f-3767-4cc7-9c19-049f4350e38c",
164
- name: "Mikel",
165
- }
166
- end
167
- ```
168
-
169
- Success Example:
170
- ```ruby
171
- let(:expected) do
172
- {
173
- id: RSpec::JsonApi::Types::UUID,
174
- name: String,
175
- age: Integer
176
- }
177
- end
178
-
179
- let(:actual) do
180
- {
181
- id: "0a2f911f-3767-4cc7-9c19-049f4350e38c",
182
- name: "John",
183
- age: 24
184
- }
185
- end
186
- ```
187
-
188
- ### Value match
189
- ```ruby
190
- let(:expected_schema) do
191
- {
192
- id: "e0067346-4d24-4aa6-b303-f927a410a001",
193
- name: "John",
194
- age: 24,
195
- favouriteColorHex: "#FF5733"
196
- }
197
- end
198
- ```
199
-
200
- ### Class match
201
- ```ruby
202
- let(:expected_schema) do
203
- {
204
- id: Integer,
205
- name: String,
206
- age: Integer,
207
- notes: Array[String]
208
- }
209
- end
210
- ```
211
-
212
- ### Type match
213
- ```ruby
214
- let(:expected_schema) do
215
- {
216
- id: RSpec::JsonApi::Types::UUID,
217
- email: RSpec::JsonApi::Types::EMAIL,
218
- }
219
- end
220
- ```
221
-
222
- ### Regexp match
223
- ```ruby
224
- let(:expected_schema) do
225
- {
226
- color: /^\#([a-fA-F]|[0-9]){3,6}$/
227
- }
228
- end
229
- ```
230
-
231
- ### Interface match
232
- ```ruby
233
- let(:expected_schema) do
234
- Array[RSpec::JsonApi::Interfaces::PERSON]
235
- end
236
- ```
237
-
238
- ### Proc match
239
- Proc match allows to customize schema accoring needs using lambda shorthand notation `->`
240
-
241
- Supported options:
242
- - #### type
243
- ```ruby
244
- let(:expected_schema) do
245
- {
246
- name: -> { { type: String } }
247
- }
248
- end
249
- ```
250
- - #### value
251
- ```ruby
252
- let(:expected_schema) do
253
- {
254
- name: -> { { value: "John" } }
255
- }
256
- end
257
- ```
258
- - #### min
259
- ```ruby
260
- let(:expected_schema) do
261
- {
262
- age: -> { { min: 15 } }
263
- }
264
- end
265
- ```
266
- - #### max
267
- ```ruby
268
- let(:expected_schema) do
269
- {
270
- age: -> { { max: 25 } }
271
- }
272
- end
273
- ```
274
- - #### inclusion
275
- ```ruby
276
- let(:expected_schema) do
277
- {
278
- letter: -> { { inclusion: %w[A B C] } }
279
- }
280
- end
281
- ```
282
- - #### regex
283
- ```ruby
284
- let(:expected_schema) do
285
- {
286
- hex: -> { { regex: /^\#([a-fA-F]|[0-9]){3,6}$/ } }
287
- }
288
- end
289
- ```
290
- - #### lambda
291
- ```ruby
292
- let(:expected) do
293
- {
294
- number: -> { { lambda: ->(actual) { actual.even? } } }
295
- }
296
- end
297
- ```
298
- - #### allow_blank
299
-
300
- ```ruby
301
- let(:expected_schema) do
302
- {
303
- name: -> { { type: String, allow_blank: true } }
304
- }
305
- end
306
- ```
307
- _Note: Default value is `false`_
308
-
309
- ## Contributing
310
-
311
- Bug reports and pull requests are welcome on GitHub at https://github.com/nomtek/rspec-json_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/nomtek/rspec-json_api/blob/master/CODE_OF_CONDUCT.md).
312
-
313
- ## License
314
-
315
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
316
-
317
- ## Code of Conduct
318
-
319
- Everyone interacting in the RSpec::JsonApi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/nomtek/rspec-json_api/blob/master/CODE_OF_CONDUCT.md).
1
+ # RSpec::JsonApi
2
+
3
+ [RSpec:JsonAPI](https://github.com/nomtek/rspec-json_api)
4
+ is an extension for [RSpec](https://github.com/rspec)
5
+ to easily allow testing JSON API responses.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rspec-json_api'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rspec-json_api
22
+
23
+ Generate directory tree:
24
+
25
+ rails generate rspec:json_api:install
26
+
27
+ Require gem assets in your `rails_helper.rb`
28
+ ```ruby
29
+ Dir[File.join(__dir__, 'rspec', 'json_api', 'types', '*.rb')].each { |file| require file }
30
+ Dir[File.join(__dir__, 'rspec', 'json_api', 'interfaces', '*.rb')].each { |file| require file }
31
+ ```
32
+
33
+ ## Generators
34
+
35
+ Using build-in generators it's possible to create custom interface and type.
36
+
37
+ Generate new template:
38
+
39
+ rails generate rspec:json_api:interface interface-name
40
+
41
+ Generate new type:
42
+
43
+ rails generate rspec:json_api:type type-name
44
+
45
+
46
+ ## Example usage
47
+
48
+ ```ruby
49
+ # spec/controllers/users_controller_spec.rb
50
+
51
+ RSpec.describe UsersController, type: :controller do
52
+ describe '#index' do
53
+ let(:expected_schema) do
54
+ [{
55
+ id: RSpec::JsonApi::Types::UUID,
56
+ name: String,
57
+ age: Integer,
58
+ favoriteColorHex: /^\#([a-fA-F]|[0-9]){3,6}$/,
59
+ number: -> { { type: Integer, min: 10, max: 20, lambda: lambda(&:even?) } }
60
+ }]
61
+ end
62
+
63
+ it 'matches API response' do
64
+ get :index
65
+
66
+ expect(response.body).to match_json_schema(expected_schema)
67
+ end
68
+ end
69
+
70
+ describe '#update' do
71
+ it 'matches API response' do
72
+ put :update, params: { name: 'John', age: 35 }
73
+
74
+ expect(response.body).to have_no_content
75
+ end
76
+ end
77
+ end
78
+ ```
79
+
80
+ ## Built-in matchers
81
+ - ### match_json_schema
82
+ ```
83
+ expect(response.body).to match_json_schema(expected_schema)
84
+ ```
85
+
86
+ - ### have_no_content
87
+ ```
88
+ expect(response.body).to have_no_content
89
+ ```
90
+
91
+ ## Interfaces
92
+ The gem introduces interfaces to reuse them during test matches.
93
+
94
+ ```ruby
95
+ # spec/rspec/json_api/interfaces/example_interface.rb
96
+
97
+ module RSpec
98
+ module JsonApi
99
+ module Interfaces
100
+ EXAMPLE_INTERFACE = {
101
+ id: Types::UUID,
102
+ name: String,
103
+ number: Integer,
104
+ color: -> { { inclusion: %w[black red white], allow_blank: true } }
105
+ }.freeze
106
+ end
107
+ end
108
+ end
109
+ ```
110
+ _Note: You can either generate file on your own or use generator._
111
+ ## Types
112
+
113
+ The gem allow users either to user build-in types or define owns.
114
+ ### Build-in types
115
+ - #### EMAIL
116
+ ```ruby
117
+ RSpec::JsonApi::Types::EMAIL
118
+ ```
119
+ - #### URI
120
+ ```ruby
121
+ RSpec::JsonApi::Types::URI
122
+ ```
123
+ - #### UUID
124
+ ```ruby
125
+ RSpec::JsonApi::Types::UUID
126
+ ```
127
+
128
+
129
+ Custom type example:
130
+ ```ruby
131
+ # spec/rspec/json_api/types/color_hex.rb
132
+
133
+ module RSpec
134
+ module JsonApi
135
+ module Types
136
+ COLOR_HEX = /^#(?:[0-9a-fA-F]{3}){1,2}$/
137
+ end
138
+ end
139
+ end
140
+
141
+ RSpec::JsonApi::Types::COLOR_HEX
142
+ ```
143
+
144
+ _Note: You can either generate file on your own or use generator._
145
+ ## Matching methods
146
+ The gem offers variety of possible matching methods.
147
+
148
+ ### Presumptions
149
+ - `match_json_schema` always require full keys match.
150
+
151
+ Failure Example:
152
+ ```ruby
153
+ let(:expected_schema) do
154
+ {
155
+ id: RSpec::JsonApi::Types::UUID,
156
+ name: String,
157
+ age: Integer
158
+ }
159
+ end
160
+
161
+ let(:actual) do
162
+ {
163
+ id: "0a2f911f-3767-4cc7-9c19-049f4350e38c",
164
+ name: "Mikel",
165
+ }
166
+ end
167
+ ```
168
+
169
+ Success Example:
170
+ ```ruby
171
+ let(:expected_schema) do
172
+ {
173
+ id: RSpec::JsonApi::Types::UUID,
174
+ name: String,
175
+ age: Integer
176
+ }
177
+ end
178
+
179
+ let(:actual) do
180
+ {
181
+ id: "0a2f911f-3767-4cc7-9c19-049f4350e38c",
182
+ name: "John",
183
+ age: 24
184
+ }
185
+ end
186
+ ```
187
+
188
+ ### Value match
189
+ ```ruby
190
+ let(:expected_schema) do
191
+ {
192
+ id: "e0067346-4d24-4aa6-b303-f927a410a001",
193
+ name: "John",
194
+ age: 24,
195
+ favoriteColorHex: "#FF5733"
196
+ }
197
+ end
198
+ ```
199
+
200
+ ### Class match
201
+ ```ruby
202
+ let(:expected_schema) do
203
+ {
204
+ id: Integer,
205
+ name: String,
206
+ age: Integer,
207
+ notes: [String]
208
+ }
209
+ end
210
+ ```
211
+
212
+ ### Type match
213
+ ```ruby
214
+ let(:expected_schema) do
215
+ {
216
+ id: RSpec::JsonApi::Types::UUID,
217
+ email: RSpec::JsonApi::Types::EMAIL,
218
+ }
219
+ end
220
+ ```
221
+
222
+ ### Regexp match
223
+ ```ruby
224
+ let(:expected_schema) do
225
+ {
226
+ color: /^\#([a-fA-F]|[0-9]){3,6}$/
227
+ }
228
+ end
229
+ ```
230
+
231
+ ### Interface match
232
+ ```ruby
233
+ let(:expected_schema) do
234
+ [RSpec::JsonApi::Interfaces::PERSON]
235
+ end
236
+ ```
237
+
238
+ ### Proc match
239
+ Proc match allows to customize schema according needs using lambda shorthand notation `->`
240
+
241
+ Supported options:
242
+ - #### type
243
+ ```ruby
244
+ let(:expected_schema) do
245
+ {
246
+ name: -> { { type: String } }
247
+ }
248
+ end
249
+ ```
250
+ - #### value
251
+ ```ruby
252
+ let(:expected_schema) do
253
+ {
254
+ name: -> { { value: "John" } }
255
+ }
256
+ end
257
+ ```
258
+ - #### min
259
+ ```ruby
260
+ let(:expected_schema) do
261
+ {
262
+ age: -> { { min: 15 } }
263
+ }
264
+ end
265
+ ```
266
+ - #### max
267
+ ```ruby
268
+ let(:expected_schema) do
269
+ {
270
+ age: -> { { max: 25 } }
271
+ }
272
+ end
273
+ ```
274
+ - #### inclusion
275
+ ```ruby
276
+ let(:expected_schema) do
277
+ {
278
+ letter: -> { { inclusion: %w[A B C] } }
279
+ }
280
+ end
281
+ ```
282
+ - #### regex
283
+ ```ruby
284
+ let(:expected_schema) do
285
+ {
286
+ hex: -> { { regex: /^\#([a-fA-F]|[0-9]){3,6}$/ } }
287
+ }
288
+ end
289
+ ```
290
+ - #### lambda
291
+ ```ruby
292
+ let(:expected_schema) do
293
+ {
294
+ number: -> { { lambda: lambda(&:even?) } }
295
+ }
296
+ end
297
+ ```
298
+ - #### allow_blank
299
+
300
+ ```ruby
301
+ let(:expected_schema) do
302
+ {
303
+ name: -> { { type: String, allow_blank: true } }
304
+ }
305
+ end
306
+ ```
307
+ _Note: Default value is `false`_
308
+
309
+ ## Contributing
310
+
311
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nomtek/rspec-json_api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/nomtek/rspec-json_api/blob/master/CODE_OF_CONDUCT.md).
312
+
313
+ ## License
314
+
315
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
316
+
317
+ ## Code of Conduct
318
+
319
+ Everyone interacting in the RSpec::JsonApi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/nomtek/rspec-json_api/blob/master/CODE_OF_CONDUCT.md).
@@ -1,8 +1,8 @@
1
- # frozen_string_literal: true
2
-
3
- class Array
4
- def deep_sort
5
- map { |element| element.is_a?(Array) ? element.deep_sort : element }
6
- .sort_by { |el| el.is_a?(Array) ? el.first.to_s : el.to_s }
7
- end
8
- end
1
+ # frozen_string_literal: true
2
+
3
+ class Array
4
+ def deep_sort
5
+ map { |element| element.is_a?(Array) ? element.deep_sort : element }
6
+ .sort_by { |el| el.is_a?(Array) ? el.first.to_s : el.to_s }
7
+ end
8
+ end