rspec-api_helpers 0.0.2 → 0.2.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: 1267d02472c6b6dc9488167e1b26a94338cf8ac1
4
- data.tar.gz: b70848a92746fa4642d8a5723e802d134046ee53
3
+ metadata.gz: e39766df47a5e882fe90dabfe2fc4454891af1d6
4
+ data.tar.gz: 40e50c76772eb224404b81fb6106468bd610823c
5
5
  SHA512:
6
- metadata.gz: 80a8a72d5999a2f57de18831edbfba0349bb231a70aed3766529d96cdc3cb61b4b4cad47f1ba05e21d2917265fdb2f9a3a882bbcd010761a20221ca249f3f54d
7
- data.tar.gz: fbceba80d2355469c7be0560360b524519edca15f03d1a8ca7e8d8e0e3c3f399423ad3d3b7acd7f33d63aff19d14062e9b1ac108abe17d00174ef368e0a3865f
6
+ metadata.gz: 5a2376e96df8cd62d06b00ec44de1a70dc71139a9839c4f12b4488519aa572d6037e863b4bee42ad2ca5d06fa1c4350d6f88bf7be3ed83581fbf51b9ca2092b9
7
+ data.tar.gz: bda8bd3912b2f9fdebfb5bdebe38a04538eeb0dde5e3c1edd4d0b82689fa3e1566a4f2c5dd41de50cb2906104da42b7760355bc0b55a4dc999e9b34e0b992793
@@ -2,29 +2,39 @@ module Rspec
2
2
  module ApiHelpers
3
3
  module ExampleGroupMethods
4
4
  def it_returns_status(status)
5
- it 'returns the correct status' do
5
+ it "returns the correct HTTP status (status: #{status})" do
6
6
  expect(last_response.status).to eql(status)
7
7
  end
8
8
  end
9
9
 
10
- def it_returns_attributes(resource:, model:, only: [], modifier: nil)
11
- it "expects returned resource to have the following model's attributes" do
12
- @api_resource = objectize_resource(last_response.body, root: resource)
10
+ def it_returns_attribute_values(resource:, model:, only: [], modifier: {})
11
+ it "expects returned resource (#{resource}) to have model attribute values" do
12
+ api_resource = objectize_resource(last_response.body, root: resource)
13
13
 
14
- @model = eval(model)
15
- if @model.is_a? Hash
16
- @model = object_hash(@model)
14
+ modifier = HashWithIndifferentAccess.new(modifier)
15
+
16
+ model = eval(model)
17
+ if model.is_a? Hash
18
+ model = object_hash(model)
17
19
  end
18
20
 
19
21
  if only
20
22
  only.each do |attribute|
21
23
  begin
22
- if modifier
23
- expect(@api_resource.send(attribute)).to(
24
- eql(@model.send(attribute).send(modifier.to_sym))
24
+ if modifier.has_key?(attribute)
25
+ modifier[attribute] = [modifier[attribute]].flatten
26
+
27
+ expect(api_resource.send(attribute)).to(
28
+ eql(
29
+ modifier[attribute].inject(
30
+ model.send(attribute), :send
31
+ )
32
+ )
25
33
  )
26
34
  else
27
- expect(@api_resource.send(attribute)).to eql(@model.send(attribute))
35
+ expect(api_resource.send(attribute)).to(
36
+ eql(model.send(attribute))
37
+ )
28
38
  end
29
39
  rescue RSpec::Expectations::ExpectationNotMetError => e
30
40
  e.message << "failed at model attribute: #{attribute}"
@@ -35,24 +45,151 @@ module Rspec
35
45
  end
36
46
 
37
47
  end
38
- alias_method :it_returns_db_model, :it_returns_attributes
39
- alias_method :it_returns_more_attributes, :it_returns_attributes
48
+ alias_method :it_returns_more_attribute_values, :it_returns_attribute_values
49
+
50
+
51
+ def it_returns_no_attributes(resource:, attributes: [])
52
+ it "expects returned resource (#{resource}) to NOT have the following attributes" do
53
+ api_resource = objectize_resource(
54
+ last_response.body, root: resource, existing: false
55
+ )
56
+
57
+ attributes.each do |attribute|
58
+ begin
59
+ expect(api_resource.send(attribute)).to eql(:attribute_not_found)
60
+ rescue RSpec::Expectations::ExpectationNotMetError => e
61
+ e.message << "failed at model attribute: #{attribute}"
62
+ raise e
63
+ end
64
+ end
65
+ end
66
+ end
40
67
 
41
68
  def it_includes_in_headers(headers = {})
42
- it 'returns the correct headers' do
43
- headers.each do |header, value|
69
+ headers.each do |header, value|
70
+ it "returns headers #{header} wih value: #{value}" do
44
71
  expect(last_response.headers[header.to_s]).to eq(eval(value))
45
72
  end
46
73
  end
47
74
  end
48
75
 
49
- def it_returns_resources(root:, number:)
50
- it 'returns the correct number of data in the body' do
51
- users = objectize_resources(last_response.body, root: root)
52
- expect(users.length).to eql(number)
76
+ def it_returns_collection_size(resource:, size:)
77
+ it "returns the correct number of resources in the #{resource} collection" do
78
+ resources = objectize_resources(last_response.body, root: resource)
79
+ expect(resources.length).to eql(size)
53
80
  end
54
81
  end
55
- alias_method :it_returns_the_resources, :it_returns_resources
82
+
83
+ def it_returns_collection_attributes(resource:, attributes: [], subset: true)
84
+ it "returns the correct attributes (no value checking) for each resource in the #{resource} collection" do
85
+ resources = objectize_resources(last_response.body, root: resource.pluralize)
86
+
87
+ resource_attributes_set = SortedSet.new(
88
+ resources.sample.hash.keys.map(&:to_s)
89
+ )
90
+ checking_attributes_set = SortedSet.new(attributes.map(&:to_s).to_set)
91
+
92
+ if subset
93
+ begin
94
+ expect(resource_attributes_set.superset?(checking_attributes_set)).to(
95
+ eq(true)
96
+ )
97
+ rescue RSpec::Expectations::ExpectationNotMetError => _
98
+ raise(
99
+ $!,
100
+ superset_mismatch_error(
101
+ resource_attributes_set, checking_attributes_set
102
+ ),
103
+ $!.backtrace
104
+ )
105
+ end
106
+ else
107
+ expect(resource_attributes_set).to eq(checking_attributes_set)
108
+ end
109
+ end
110
+ end
111
+ alias_method(
112
+ :it_returns_more_collection_attributes,
113
+ :it_returns_collection_attributes
114
+ )
115
+
116
+ def it_returns_collection_embedded_resource_attributes(
117
+ resource:, embeds:, attributes: [], subset: true
118
+ )
119
+ it "returns the correct attributes (no value checking) of #{embeds} resource inside #{resource} collection" do
120
+ resource = objectize_resources(last_response.body, root: resource)
121
+ embedded_resource = object_hash(resource.sample.send(embeds.to_sym))
122
+
123
+ embedded_resource_attributes_set = SortedSet.new(
124
+ embedded_resource.hash.keys.map(&:to_s)
125
+ )
126
+ checking_attributes_set = SortedSet.new(attributes.map(&:to_s).to_set)
127
+
128
+ if subset
129
+ begin
130
+ expect(
131
+ embedded_resource_attributes_set.superset?(
132
+ checking_attributes_set
133
+ )
134
+ ).to(
135
+ eq(true)
136
+ )
137
+ rescue RSpec::Expectations::ExpectationNotMetError => _
138
+ raise(
139
+ $!,
140
+ superset_mismatch_error(
141
+ embedded_resource_attributes_set, checking_attributes_set
142
+ ),
143
+ $!.backtrace
144
+ )
145
+ end
146
+ else
147
+ expect(resource_attributes_set).to eq(checking_attributes_set)
148
+ end
149
+ end
150
+
151
+ end
152
+
153
+ def it_returns_no_collection_attributes(resource:, attributes: [])
154
+ it "expects returned collection (#{resource}) to NOT have the following attributes" do
155
+ resources = objectize_resources(
156
+ last_response.body, root: resource.pluralize, existing: false
157
+ )
158
+
159
+ attributes.each do |attribute|
160
+ begin
161
+ expect(resources.sample.send(attribute)).to eql(:attribute_not_found)
162
+ rescue RSpec::Expectations::ExpectationNotMetError => e
163
+ e.message << "failed at model attribute: #{attribute}"
164
+ raise e
165
+ end
166
+ end
167
+ end
168
+ end
169
+
170
+ def it_returns_no_collection_embedded_resource_attributes(
171
+ resource:, embeds:, attributes: []
172
+ )
173
+ it "expects the embedded resource #{embeds} inside the returned collection (#{resource}) to NOT have the following attributes" do
174
+ resources = objectize_resources(
175
+ last_response.body, root: resource.pluralize
176
+ )
177
+
178
+ embedded_resource = object_hash(
179
+ resources.sample.send(embeds.to_sym), existing: false
180
+ )
181
+
182
+ attributes.each do |attribute|
183
+ begin
184
+ expect(embedded_resource.send(attribute)).to eql(:attribute_not_found)
185
+ rescue RSpec::Expectations::ExpectationNotMetError => e
186
+ e.message << "failed at model attribute: #{attribute}"
187
+ raise e
188
+ end
189
+ end
190
+ end
191
+ end
192
+
56
193
  end
57
194
  end
58
195
  end
@@ -3,46 +3,68 @@ require "rspec/api_helpers/version"
3
3
  module Rspec
4
4
  module ApiHelpers
5
5
  module ExampleMethods
6
- def objectize_resources(json, root:)
6
+ def objectize_resources(json, root:, existing: true)
7
7
  array = []
8
- array_hash = HashWithIndifferentAccess.new(JSON.load(json))
8
+ array_hash = HashWithIndifferentAccess.new(JSON.parse(json))
9
9
 
10
10
  if root
11
11
  array_hash = array_hash[root]
12
12
  end
13
13
 
14
14
  array_hash.each do |resource|
15
- array << object_hash(resource)
15
+ array << object_hash(resource, existing: existing)
16
16
  end
17
17
 
18
18
  return array
19
19
  end
20
20
 
21
- def objectize_resource(json, root:)
22
- hash = HashWithIndifferentAccess.new(JSON.load(json))
21
+ def objectize_resource(json, root:, existing: true)
22
+ hash = HashWithIndifferentAccess.new(JSON.parse(json))
23
23
  if root
24
- obj = object_hash(hash[root])
24
+ obj = object_hash(hash[root], existing: existing)
25
25
  else
26
- obj = object_hash(hash)
26
+ obj = object_hash(hash, existing: existing)
27
27
  end
28
28
 
29
29
  return obj
30
30
  end
31
31
 
32
- def object_hash(hash)
33
- ObjectHash.new(hash)
32
+ def object_hash(hash, existing: true)
33
+ ObjectHash.new(hash, existing: existing)
34
34
  end
35
35
 
36
36
  class ObjectHash
37
- attr_accessor :hash
38
- def initialize(hash)
37
+ #existing denotes whether we search for attributes that exist on the
38
+ #resource or attributes that shouldn't exist
39
+ attr_accessor :hash, :existing
40
+ def initialize(hash, existing: true)
39
41
  @hash = HashWithIndifferentAccess.new(hash)
42
+ @existing = existing
40
43
  end
41
44
  def method_missing(name)
42
- return hash[name] if hash.key? name
43
- raise KeyError.new("Attribute not found in resource: #{name}")
45
+ if existing
46
+ if hash.key?(name)
47
+ return hash[name]
48
+ else
49
+ return raise KeyError.new("Attribute not found in resource: #{name}")
50
+ end
51
+ else
52
+ if hash.key?(name)
53
+ return raise(
54
+ KeyError.new(
55
+ "Attribute found in resource when it shouldn't: #{name}"
56
+ )
57
+ )
58
+ else
59
+ return :attribute_not_found
60
+ end
61
+ end
44
62
  end
45
63
  end
64
+
65
+ def superset_mismatch_error(superset, subset)
66
+ "Expected \n #{subset.to_a.to_s} \n to be included in \n #{superset.to_a.to_s}"
67
+ end
46
68
  end
47
69
  end
48
70
  end
@@ -1,5 +1,5 @@
1
1
  module Rspec
2
2
  module Api
3
- VERSION = "0.0.2"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-api_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Filippos Vasilakis
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-10-06 00:00:00.000000000 Z
12
+ date: 2015-10-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler