jsonapi_spec_helpers 0.2.3 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 319b2470e387e56133c175300436a521422afd02
4
- data.tar.gz: 3afed12ece44c28fb3133fc238bd26a9ef9f0992
3
+ metadata.gz: 2a6f8ea375381fb21a7d8db9415ade8e1d7a8da6
4
+ data.tar.gz: 202e35d79f5d3798410d0b25fa35830ff6c58dd1
5
5
  SHA512:
6
- metadata.gz: 3f318974fe29ca4339a596c116b12cda815ba84044ca336555002190c094c67e504d4f55e15b5185ce17c0fff67dc45970c7f9adbb9fca96baa07f45ce93f7f8
7
- data.tar.gz: 52d3b95df9f5b7ebe4f52c7322ecd92ab5e8776dd3ce85676457944697194e199f01cc5611c9eebd00f3a438eb957ce390d3988b9872abca110573757a9a4bdd
6
+ metadata.gz: 8ad84e0cc89b526ccfa0bb1f9375e7c96ae371cf103a66ed4198a832f0cd1465d3a79e437c214f1a847a134681d830cb9e91fdef7e24d2f5457028e352ef14ae
7
+ data.tar.gz: 556a4ec9df1bfaa1b36b4286704965a9e62b76d078fa95cfaa65ddb90d6727863b72d6c95a5bc180858cfc8a88306a85056661789f018b920e47fd9daa627863
@@ -1,122 +1,22 @@
1
1
  require 'json'
2
2
  require 'jsonapi_spec_helpers/version'
3
+ require 'jsonapi_spec_helpers/matchers'
4
+ require 'jsonapi_spec_helpers/helpers'
3
5
  require 'jsonapi_spec_helpers/payload'
4
6
 
5
- RSpec::Matchers.define :match_payload do |attribute, expected|
6
- match do |actual|
7
- # cast as_json for things like Time
8
- # use as_json instead of to_json for things like hashes
9
- if expected.respond_to?(:as_json)
10
- actual.as_json == expected.as_json
11
- else
12
- actual.to_json == expected.to_json
13
- end
14
- end
15
-
16
- failure_message do |actual|
17
- "Expected JSON payload to have key '#{attribute}' == #{expected.inspect} but was #{actual.inspect}"
18
- end
19
- end
20
-
21
- RSpec::Matchers.define :have_payload_key do |expected, allow_nil|
22
- match do |json|
23
- @has_key = json.has_key?(expected.to_s)
24
- @has_value = !json[expected.to_s].nil?
25
-
26
- if allow_nil
27
- @has_key
28
- else
29
- @has_key && @has_value
30
- end
31
- end
32
-
33
- failure_message do |actual|
34
- msg = !allow_nil && @has_key ? "nil. Use 'key(:foo, allow_nil: true)' to allow nils" : "not present"
35
- "Expected JSON payload to have key '#{expected}' but was #{msg}"
36
- end
37
-
38
- failure_message_when_negated do |actual|
39
- "Expected JSON payload to NOT have key '#{expected}' but was present"
40
- end
41
- end
42
-
43
- RSpec::Matchers.define :be_not_in_payload do |expected|
44
- match do |json|
45
- false
46
- end
47
-
48
- failure_message do |actual|
49
- "JSON payload contained unexpected key '#{actual}'"
50
- end
51
- end
52
-
53
7
  module JsonapiSpecHelpers
54
8
  def self.included(klass)
9
+ klass.send(:include, Helpers)
55
10
  if defined?(Rails)
56
11
  Dir[Rails.root.join('spec/payloads/**/*.rb')].each { |f| require f }
57
12
  end
58
13
  end
59
14
 
60
- def json
61
- JSON.parse(response.body)
62
- end
63
-
64
- def json_item(from: nil)
65
- from = json if from.nil?
66
- data = from.has_key?('data') ? from['data'] : from
67
-
68
- {}.tap do |item|
69
- item['id'] = data['id']
70
- item['jsonapi_type'] = data['type']
71
- item.merge!(data['attributes']) if data.has_key?('attributes')
72
- end
73
- end
74
-
75
- def json_items(*indices)
76
- items = []
77
- json['data'].each_with_index do |datum, index|
78
- included = indices.empty? || indices.include?(index)
79
- items << json_item(from: datum) if included
80
- end
81
- indices.length == 1 ? items[0] : items
82
- end
83
-
84
- def json_related_link(payload, assn_name)
85
- link = payload['relationships'][assn_name]['links']['related']['href']
86
- fail "link for #{assn_name} not found" unless link
87
- URI.decode(link)
88
- end
89
-
90
- def json_included_types
91
- (json['included'] || []).map { |i| i['type'] }.uniq
92
- end
93
-
94
- def json_includes(type, *indices)
95
- included = (json['included'] || []).select { |data| data['type'] == type }
96
- indices = (0...included.length).to_a if indices.empty?
97
- includes = []
98
- indices.each do |index|
99
- includes << json_item(from: included.at(index))
100
- end
101
- includes
102
- end
103
-
104
- def json_include(type, index = 0)
105
- json_includes(type, index)[0]
106
- end
107
-
108
- def json_ids(integers = false)
109
- ids = json['data'].map { |d| d['id'] }
110
- ids.map!(&:to_i) if integers
111
- ids
112
- end
113
-
114
15
  def assert_payload(name, record, json, &blk)
115
16
  unless payload = JsonapiSpecHelpers::Payload.registry[name]
116
17
  raise "No payloads registered for '#{name}'"
117
18
  end
118
19
 
119
- # todo dup payload
120
20
  if blk
121
21
  payload = payload.fork
122
22
  payload.instance_eval(&blk)
@@ -128,6 +28,10 @@ module JsonapiSpecHelpers
128
28
  if (expect(json).to have_payload_key(attribute, options[:allow_nil])) == true
129
29
  unless options[:allow_nil]
130
30
  expect(json[attribute.to_s]).to match_payload(attribute, prc.call(record))
31
+
32
+ if options[:type]
33
+ expect(json[attribute.to_s]).to match_type(attribute, options[:type])
34
+ end
131
35
  end
132
36
  end
133
37
  end
@@ -0,0 +1,58 @@
1
+ module JsonapiSpecHelpers
2
+ module Helpers
3
+ def json
4
+ JSON.parse(response.body)
5
+ end
6
+
7
+ def json_item(from: nil)
8
+ from = json if from.nil?
9
+ data = from.has_key?('data') ? from['data'] : from
10
+
11
+ {}.tap do |item|
12
+ item['id'] = data['id']
13
+ item['jsonapi_type'] = data['type']
14
+ item.merge!(data['attributes']) if data.has_key?('attributes')
15
+ end
16
+ end
17
+
18
+ def json_items(*indices)
19
+ items = []
20
+ json['data'].each_with_index do |datum, index|
21
+ included = indices.empty? || indices.include?(index)
22
+ items << json_item(from: datum) if included
23
+ end
24
+ indices.length == 1 ? items[0] : items
25
+ end
26
+
27
+ def json_related_link(payload, assn_name)
28
+ link = payload['relationships'][assn_name]['links']['related']['href']
29
+ fail "link for #{assn_name} not found" unless link
30
+ URI.decode(link)
31
+ end
32
+
33
+ def json_included_types
34
+ (json['included'] || []).map { |i| i['type'] }.uniq
35
+ end
36
+
37
+ def json_includes(type, *indices)
38
+ included = (json['included'] || []).select { |data| data['type'] == type }
39
+ indices = (0...included.length).to_a if indices.empty?
40
+ includes = []
41
+ indices.each do |index|
42
+ includes << json_item(from: included.at(index))
43
+ end
44
+ includes
45
+ end
46
+
47
+ def json_include(type, index = 0)
48
+ json_includes(type, index)[0]
49
+ end
50
+
51
+ def json_ids(integers = false)
52
+ ids = json['data'].map { |d| d['id'] }
53
+ ids.map!(&:to_i) if integers
54
+ ids
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,51 @@
1
+ RSpec::Matchers.define :match_payload do |attribute, expected|
2
+ match do |actual|
3
+ actual == expected
4
+ end
5
+
6
+ failure_message do |actual|
7
+ "Expected JSON payload to have key '#{attribute}' == #{expected.inspect} but was #{actual.inspect}"
8
+ end
9
+ end
10
+
11
+ RSpec::Matchers.define :match_type do |attribute, type|
12
+ match do |actual|
13
+ actual.is_a?(type)
14
+ end
15
+
16
+ failure_message do |actual|
17
+ "Expected JSON payload key '#{attribute}' to have type #{type} but was #{actual.class}"
18
+ end
19
+ end
20
+
21
+ RSpec::Matchers.define :have_payload_key do |expected, allow_nil|
22
+ match do |json|
23
+ @has_key = json.has_key?(expected.to_s)
24
+ @has_value = !json[expected.to_s].nil?
25
+
26
+ if allow_nil
27
+ @has_key
28
+ else
29
+ @has_key && @has_value
30
+ end
31
+ end
32
+
33
+ failure_message do |actual|
34
+ msg = !allow_nil && @has_key ? "nil. Use 'key(:foo, allow_nil: true)' to allow nils" : "not present"
35
+ "Expected JSON payload to have key '#{expected}' but was #{msg}"
36
+ end
37
+
38
+ failure_message_when_negated do |actual|
39
+ "Expected JSON payload to NOT have key '#{expected}' but was present"
40
+ end
41
+ end
42
+
43
+ RSpec::Matchers.define :be_not_in_payload do |expected|
44
+ match do |json|
45
+ false
46
+ end
47
+
48
+ failure_message do |actual|
49
+ "JSON payload contained unexpected key '#{actual}'"
50
+ end
51
+ end
@@ -30,7 +30,9 @@ module JsonapiSpecHelpers
30
30
  @no_keys << name
31
31
  end
32
32
 
33
- def key(name, options = {}, &blk)
33
+ def key(name, *args, &blk)
34
+ options = args.last.is_a?(Hash) ? args.pop : {}
35
+ options[:type] = args.first
34
36
  options[:allow_nil] ||= false
35
37
  @no_keys.reject! { |k| k == name }
36
38
  prc = blk
@@ -1,3 +1,3 @@
1
1
  module JsonapiSpecHelpers
2
- VERSION = "0.2.3"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi_spec_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-27 00:00:00.000000000 Z
11
+ date: 2017-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -113,6 +113,8 @@ files:
113
113
  - bin/setup
114
114
  - jsonapi_spec_helpers.gemspec
115
115
  - lib/jsonapi_spec_helpers.rb
116
+ - lib/jsonapi_spec_helpers/helpers.rb
117
+ - lib/jsonapi_spec_helpers/matchers.rb
116
118
  - lib/jsonapi_spec_helpers/payload.rb
117
119
  - lib/jsonapi_spec_helpers/version.rb
118
120
  homepage:
@@ -135,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
137
  version: '0'
136
138
  requirements: []
137
139
  rubyforge_project:
138
- rubygems_version: 2.4.5.1
140
+ rubygems_version: 2.5.1
139
141
  signing_key:
140
142
  specification_version: 4
141
143
  summary: Spec helpers for jsonapi