jsonapi_expectations 0.0.2
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 +7 -0
- data/lib/jsonapi_expectations.rb +102 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e54a36f06982325f2f32397b6166e6825dc0cd82
|
4
|
+
data.tar.gz: 533ff06dd33dfbb49323405f5ab2a449ab762640
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 56243ec14d0edfe0b80808f8f534cefb1805dd78029de937039deb832c2ad529f9abfd08bd3a53c231ff2c7406b4496a13f78722ffb5e16a656265abdd566e60
|
7
|
+
data.tar.gz: 6f4279038148b436d332c7ae2578881c37a79a0121cdfa52da6bd25bc00b7750e02b833872d7b0160779ab16db2f37f820af7c7ed6c964ebe5ea12a9f619d7a1
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'airborne'
|
2
|
+
|
3
|
+
module JsonapiExpectations
|
4
|
+
def expect_attributes attrs
|
5
|
+
expect(json_body[:data]).to_not be_empty
|
6
|
+
expect_json 'data.attributes', dasherize_keys(attrs)
|
7
|
+
end
|
8
|
+
|
9
|
+
def expect_attributes_in_list attrs
|
10
|
+
expect(json_body[:data]).to_not be_empty
|
11
|
+
expect_json 'data.?.attributes', dasherize_keys(attrs)
|
12
|
+
end
|
13
|
+
|
14
|
+
def expect_relationship opts
|
15
|
+
# If looking for item in a list, need to change location string
|
16
|
+
location = if opts[:in_list]
|
17
|
+
"data.?.relationships.#{opts[:key]}"
|
18
|
+
else
|
19
|
+
"data.relationships.#{opts[:key]}"
|
20
|
+
end
|
21
|
+
|
22
|
+
if opts[:link]
|
23
|
+
begin
|
24
|
+
expect_json "#{location}.links.related", opts[:link]
|
25
|
+
rescue # check if it's in an array
|
26
|
+
expect_json "#{location}.?.links.related", opts[:link]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
if opts[:id]
|
31
|
+
location = "#{location}.data"
|
32
|
+
type = opts[:type] || opts[:key].pluralize
|
33
|
+
|
34
|
+
if opts[:id].respond_to? :each # if an array was passed in, look for each of them
|
35
|
+
opts[:id].each do |id|
|
36
|
+
expect_linkage_data "#{location}.?", { type: type, id: id }, opts[:included]
|
37
|
+
end
|
38
|
+
else # otherwise just look for it
|
39
|
+
expect_linkage_data location, { type: type, id: opts[:id] }, opts[:included]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def expect_relationship_in_list opts
|
45
|
+
opts[:in_list] = true
|
46
|
+
expect_relationship opts
|
47
|
+
end
|
48
|
+
|
49
|
+
def expect_item_count number
|
50
|
+
expect_json_sizes data: number
|
51
|
+
end
|
52
|
+
|
53
|
+
def expect_item_in_list find_me, opts = {}
|
54
|
+
opts[:type] ||= jsonapi_type find_me
|
55
|
+
expect(json_body[:data]).to_not be_empty
|
56
|
+
found = json_body[:data].detect do |item|
|
57
|
+
jsonapi_match? find_me, item, opts[:type]
|
58
|
+
end
|
59
|
+
expect(found).to be_truthy
|
60
|
+
end
|
61
|
+
|
62
|
+
def expect_item_to_not_be_in_list dont_find_me, opts = {}
|
63
|
+
opts[:type] ||= jsonapi_type dont_find_me
|
64
|
+
expect(json_body[:data]).to_not be_empty
|
65
|
+
json_body[:data].each do |item|
|
66
|
+
expect(jsonapi_match?(dont_find_me, item, opts[:type])).to be_falsey
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
## Finder helpers
|
71
|
+
|
72
|
+
def find_record_in_response record, opts = {}
|
73
|
+
opts[:type] ||= jsonapi_type(record)
|
74
|
+
json_body[:data].select do |item|
|
75
|
+
item[:id]&.to_s == record.id&.to_s && item[:type] == opts[:type]
|
76
|
+
end.first
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def expect_linkage_data location, relationship_data, included
|
82
|
+
begin
|
83
|
+
expect_json location, relationship_data
|
84
|
+
rescue # check if it's in an array
|
85
|
+
expect_json "#{location}.?", relationship_data
|
86
|
+
end
|
87
|
+
|
88
|
+
expect_json "included.?", relationship_data if included
|
89
|
+
end
|
90
|
+
|
91
|
+
def dasherize_keys hash
|
92
|
+
hash.deep_transform_keys { |key| key.to_s.tr('_', '-').to_sym }
|
93
|
+
end
|
94
|
+
|
95
|
+
def jsonapi_match? model, data, type
|
96
|
+
(data[:type] == type) && (data[:id] == model.id.to_s)
|
97
|
+
end
|
98
|
+
|
99
|
+
def jsonapi_type model
|
100
|
+
model.class.to_s.underscore.downcase.pluralize.tr('_', '-')
|
101
|
+
end
|
102
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsonapi_expectations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ross-Hunter
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: airborne
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.2.12
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.2.12
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.14'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.14'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- ross-hunter@ross-hunter.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- lib/jsonapi_expectations.rb
|
77
|
+
homepage: https://github.com/Ross-Hunter/jsonapi_expectations
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.6.8
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Semantic expectation helpers for JSON API testing using Airborne and RSpec.
|
101
|
+
test_files: []
|