kalibro_client 3.0.1 → 4.0.0.alpha1

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.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/features/metric_collector_details/find_by_name.feature +12 -4
  3. data/features/step_definitions/metric_collector_details_steps.rb +11 -7
  4. data/features/step_definitions/metric_configuration_steps.rb +2 -2
  5. data/features/step_definitions/module_result_steps.rb +1 -1
  6. data/features/support/env.rb +0 -3
  7. data/kalibro_client.gemspec +1 -0
  8. data/lib/kalibro_client.rb +4 -39
  9. data/lib/kalibro_client/entities/base.rb +12 -207
  10. data/lib/kalibro_client/entities/configurations/base.rb +3 -3
  11. data/lib/kalibro_client/entities/configurations/kalibro_configuration.rb +0 -4
  12. data/lib/kalibro_client/entities/configurations/metric_configuration.rb +1 -19
  13. data/lib/kalibro_client/entities/configurations/reading_group.rb +1 -5
  14. data/lib/kalibro_client/entities/miscellaneous/base.rb +4 -2
  15. data/lib/kalibro_client/entities/miscellaneous/metric.rb +0 -2
  16. data/lib/kalibro_client/entities/processor/base.rb +3 -3
  17. data/lib/kalibro_client/entities/processor/hotspot_metric_result.rb +1 -1
  18. data/lib/kalibro_client/entities/processor/metric_collector_details.rb +19 -7
  19. data/lib/kalibro_client/entities/processor/module_result.rb +0 -13
  20. data/lib/kalibro_client/entities/processor/project.rb +0 -4
  21. data/lib/kalibro_client/entities/processor/repository.rb +0 -4
  22. data/lib/kalibro_client/entities/processor/tree_metric_result.rb +1 -1
  23. data/lib/kalibro_client/errors.rb +1 -4
  24. data/lib/kalibro_client/kalibro_cucumber_helpers.rb +7 -36
  25. data/lib/kalibro_client/kalibro_cucumber_helpers/cleaner.rb +27 -0
  26. data/lib/kalibro_client/version.rb +1 -1
  27. data/spec/entities/base_spec.rb +17 -449
  28. data/spec/entities/configurations/kalibro_configuration_spec.rb +0 -35
  29. data/spec/entities/configurations/kalibro_range_spec.rb +3 -3
  30. data/spec/entities/configurations/metric_configuration_spec.rb +1 -58
  31. data/spec/entities/configurations/reading_group_spec.rb +0 -32
  32. data/spec/entities/configurations/reading_spec.rb +3 -3
  33. data/spec/entities/processor/metric_collector_details_spec.rb +46 -25
  34. data/spec/entities/processor/module_result_spec.rb +0 -41
  35. data/spec/entities/processor/project_spec.rb +1 -35
  36. data/spec/entities/processor/repository_spec.rb +1 -14
  37. data/spec/{savon/fixtures → fixtures}/config.yml +0 -0
  38. data/spec/{savon/fixtures → fixtures}/invalid_config.yml +0 -0
  39. data/spec/kalibro_cucumber_helpers/cleaner_spec.rb +32 -0
  40. metadata +26 -32
  41. data/lib/kalibro_client/errors/record_invalid.rb +0 -19
  42. data/lib/kalibro_client/errors/record_not_found.rb +0 -22
  43. data/lib/kalibro_client/errors/request_error.rb +0 -27
  44. data/lib/kalibro_client/errors/standard.rb +0 -24
  45. data/lib/kalibro_client/helpers/date_attributes.rb +0 -11
  46. data/lib/kalibro_client/helpers/hash_converters.rb +0 -48
  47. data/lib/kalibro_client/helpers/request_methods.rb +0 -64
  48. data/lib/kalibro_client/helpers/xml_converters.rb +0 -20
  49. data/lib/kalibro_client/kalibro_cucumber_helpers/configuration.rb +0 -30
  50. data/spec/errors/record_invalid_spec.rb +0 -45
  51. data/spec/helpers/date_attributes_spec.rb +0 -30
  52. data/spec/helpers/hash_converters_spec.rb +0 -120
  53. data/spec/helpers/xml_converters_spec.rb +0 -64
  54. data/spec/kalibro_entities_spec.rb +0 -112
  55. data/spec/savon/fixtures/project/does_not_exists.xml +0 -1
  56. data/spec/savon/savon_test_helper.rb +0 -14
@@ -5,7 +5,7 @@
5
5
  # it under the terms of the GNU General Public License as published by
6
6
  # the Free Software Foundation, either version 3 of the License, or
7
7
  # (at your option) any later version.
8
- #
8
+ #
9
9
  # This program is distributed in the hope that it will be useful,
10
10
  # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
11
  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@@ -25,10 +25,6 @@ module KalibroClient
25
25
  @id = value.to_i
26
26
  end
27
27
 
28
- def self.all
29
- create_objects_array_from_hash(request('', {}, :get))
30
- end
31
-
32
28
  def readings
33
29
  KalibroClient::Entities::Configurations::Reading.create_objects_array_from_hash(self.class.request(':id/readings', {id: @id}, :get))
34
30
  end
@@ -1,12 +1,14 @@
1
+ require 'likeno/helpers/hash_converters'
2
+
1
3
  module KalibroClient
2
4
  module Entities
3
5
  module Miscellaneous
4
6
  class Base
5
7
  def initialize(attributes={})
6
- attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) }
8
+ attributes.each { |field, value| send("#{field}=", value) if self.class.valid?(field) }
7
9
  end
8
10
 
9
- include HashConverters
11
+ include Likeno::HashConverters
10
12
  def to_hash(options={})
11
13
  hash = Hash.new
12
14
  excepts = options[:except].nil? ? [] : options[:except]
@@ -1,5 +1,3 @@
1
- require 'kalibro_client/helpers/hash_converters'
2
-
3
1
  module KalibroClient
4
2
  module Entities
5
3
  module Miscellaneous
@@ -1,14 +1,14 @@
1
1
  require 'json'
2
- require 'kalibro_client/helpers/date_attributes'
2
+ require 'likeno/helpers/date_attributes'
3
3
 
4
4
  module KalibroClient
5
5
  module Entities
6
6
  module Processor
7
7
  class Base < KalibroClient::Entities::Base
8
- include DateAttributes
8
+ include Likeno::DateAttributes
9
9
 
10
10
  def self.address
11
- :processor_address
11
+ Likeno.config[:processor_address]
12
12
  end
13
13
  end
14
14
  end
@@ -24,7 +24,7 @@ module KalibroClient
24
24
  def initialize(attributes={}, persisted=false)
25
25
  @line_number = attributes["line_number"].to_i
26
26
  attributes.each do |field, value|
27
- if field != "line_number" and self.class.is_valid?(field)
27
+ if field != "line_number" and self.class.valid?(field)
28
28
  send("#{field}=", value)
29
29
  end
30
30
  end
@@ -36,24 +36,36 @@ module KalibroClient
36
36
  metric.nil? ? nil : metric.last
37
37
  end
38
38
 
39
+ def find_metric_by_name!(name)
40
+ metric = self.find_metric_by_name(name)
41
+ raise Likeno::Errors::RecordNotFound if metric.nil?
42
+ metric
43
+ end
44
+
39
45
  def find_metric_by_code(metric_code)
40
46
  @supported_metrics[metric_code]
41
47
  end
42
48
 
49
+ def find_metric_by_code!(metric_code)
50
+ metric = self.find_metric_by_code(metric_code)
51
+ raise Likeno::Errors::RecordNotFound if metric.nil?
52
+ metric
53
+ end
54
+
43
55
  def self.find_by_name(metric_collector_name)
44
56
  begin
45
- new request(:find, {name: metric_collector_name})["metric_collector_details"]
46
- rescue
47
- raise KalibroClient::Errors::RecordNotFound
57
+ self.find_by_name!(metric_collector_name)
58
+ rescue Likeno::Errors::RecordNotFound
59
+ nil
48
60
  end
49
61
  end
50
62
 
51
- def self.all_names
52
- request(:names, {}, :get)['metric_collector_names'].to_a
63
+ def self.find_by_name!(metric_collector_name)
64
+ new request(:find, {name: metric_collector_name})["metric_collector_details"]
53
65
  end
54
66
 
55
- def self.all
56
- create_objects_array_from_hash(request('', {}, :get))
67
+ def self.all_names
68
+ request(:names, {}, :get)['metric_collector_names'].to_a
57
69
  end
58
70
  end
59
71
  end
@@ -89,19 +89,6 @@ module KalibroClient
89
89
  HotspotMetricResult.create_objects_array_from_hash(self.class.request(":id/hotspot_metric_results",
90
90
  {id: self.id}, :get))
91
91
  end
92
-
93
- # FIXME: KalibroProcessor should return a 404 if the object does not exist instead of 422
94
- def self.find(id)
95
- begin
96
- super
97
- rescue KalibroClient::Errors::RequestError => e
98
- if(e.response.status == 422)
99
- raise KalibroClient::Errors::RecordNotFound.new(response: e.response)
100
- else
101
- raise e
102
- end
103
- end
104
- end
105
92
  end
106
93
  end
107
94
  end
@@ -25,10 +25,6 @@ module KalibroClient
25
25
  @id = value.to_i
26
26
  end
27
27
 
28
- def self.all
29
- create_objects_array_from_hash(request('', {}, :get))
30
- end
31
-
32
28
  def repositories
33
29
  Repository.create_objects_array_from_hash(self.class.request(':id/repositories', {id: id}, :get))
34
30
  end
@@ -112,10 +112,6 @@ module KalibroClient
112
112
  Processing.new(self.class.request("#{self.id}/last_processing/before", {:date => date})['processing'])
113
113
  end
114
114
 
115
- def self.all
116
- create_objects_array_from_hash(request("", {}, :get))
117
- end
118
-
119
115
  def self.branches(url, scm_type)
120
116
  request("/branches", {url: url, scm_type: scm_type})
121
117
  end
@@ -24,7 +24,7 @@ module KalibroClient
24
24
  value = attributes["value"]
25
25
  @value = (value == "NaN") ? attributes["aggregated_value"].to_f : value.to_f
26
26
  attributes.each do |field, value|
27
- if field!= "value" and field!= "aggregated_value" and self.class.is_valid?(field)
27
+ if field!= "value" and field!= "aggregated_value" and self.class.valid?(field)
28
28
  send("#{field}=", value)
29
29
  end
30
30
  end
@@ -14,7 +14,4 @@
14
14
  # You should have received a copy of the GNU General Public License
15
15
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
- require 'kalibro_client/errors/standard'
18
- require 'kalibro_client/errors/request_error'
19
- require 'kalibro_client/errors/record_not_found'
20
- require 'kalibro_client/errors/record_invalid'
17
+ require 'likeno/errors'
@@ -14,47 +14,18 @@
14
14
  # You should have received a copy of the GNU General Public License
15
15
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
- require 'yaml'
18
- require 'kalibro_client/kalibro_cucumber_helpers/configuration'
17
+ require 'kalibro_client/kalibro_cucumber_helpers/cleaner'
19
18
 
20
19
  module KalibroClient
21
20
  module KalibroCucumberHelpers
22
- @configuration = KalibroClient::KalibroCucumberHelpers::Configuration.new
23
-
24
- def KalibroCucumberHelpers.configure(&config_block)
25
- config_block.call(@configuration)
26
- end
27
-
28
- def KalibroCucumberHelpers.configure_from_yml(file_path)
29
- configuration = YAML.load(File.open("features/support/kalibro_cucumber_helpers.yml"))
30
-
31
- configuration["kalibro_cucumber_helpers"].each do |config, value|
32
- @configuration.send("#{config}=", value)
33
- end
34
- end
35
-
36
- def KalibroCucumberHelpers.configuration
37
- @configuration
21
+ def self.clean_processor
22
+ @processor_cleaner ||= KalibroClient::KalibroCucumberHelpers::Cleaner.new(:processor_address)
23
+ @processor_cleaner.clean_database
38
24
  end
39
25
 
40
- def KalibroCucumberHelpers.clean_processor
41
- client = Faraday.new(:url => @configuration.kalibro_processor_address) do |conn|
42
- conn.request :json
43
- conn.response :json, :content_type => /\bjson$/
44
- conn.adapter Faraday.default_adapter # make requests with Net::HTTP
45
- end
46
-
47
- client.send(:post, "/tests/clean_database", {})
48
- end
49
-
50
- def KalibroCucumberHelpers.clean_configurations
51
- client = Faraday.new(:url => @configuration.kalibro_configurations_address) do |conn|
52
- conn.request :json
53
- conn.response :json, :content_type => /\bjson$/
54
- conn.adapter Faraday.default_adapter # make requests with Net::HTTP
55
- end
56
-
57
- client.send(:post, "/tests/clean_database", {})
26
+ def self.clean_configurations
27
+ @configurations_cleaner ||= KalibroClient::KalibroCucumberHelpers::Cleaner.new(:configurations_address)
28
+ @configurations_cleaner.clean_database
58
29
  end
59
30
  end
60
31
  end
@@ -0,0 +1,27 @@
1
+ require 'likeno'
2
+
3
+ module KalibroClient
4
+ module KalibroCucumberHelpers
5
+ class Cleaner
6
+ include Likeno::RequestMethods
7
+
8
+ attr_reader :address_key
9
+
10
+ def initialize(address_key)
11
+ @address_key = address_key
12
+ end
13
+
14
+ def address
15
+ Likeno.config[address_key]
16
+ end
17
+
18
+ def endpoint
19
+ 'tests'
20
+ end
21
+
22
+ def clean_database
23
+ request('clean_database', {}, :post)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -15,5 +15,5 @@
15
15
  # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
17
  module KalibroClient
18
- VERSION = "3.0.1"
18
+ VERSION = "4.0.0.alpha1"
19
19
  end
@@ -19,467 +19,35 @@ require 'spec_helper'
19
19
  # Create a class that has the attribute assignment methods, since some methods expect they exist
20
20
  # (and usually the subclasses do that).
21
21
 
22
+ class MiscellaneousTest < KalibroClient::Entities::Miscellaneous::Base
23
+ attr_accessor :id
24
+ end
25
+
22
26
  class BaseTest < KalibroClient::Entities::Base
23
- attr_accessor :id, :created_at, :updated_at
27
+ attr_accessor :id, :miscellaneous
24
28
  end
25
29
 
26
30
  describe KalibroClient::Entities::Base do
27
31
  subject { BaseTest.new }
28
32
 
29
- describe 'new' do
30
- subject { described_class.new({}) }
31
-
32
- it 'is expected to create a model from an empty hash' do
33
- expect(subject.kalibro_errors).to eq([])
34
- end
35
- end
36
-
37
- describe 'entity_name' do
38
- it 'is expected to be a String' do
39
- expect(subject.class.entity_name).to be_a(String)
40
- end
41
-
42
- it 'is expected to return Base' do
43
- expect(subject.class.entity_name).to eq('base')
44
- end
45
- end
46
-
47
- describe 'endpoint' do
48
- it 'is expected to return the entity_name' do
49
- endpoint = 'tests'
50
- described_class.expects(:entity_name).returns(endpoint)
51
- expect(described_class.endpoint).to eq(endpoint)
52
- end
53
- end
54
-
55
- describe 'client' do
56
- it 'returns a Faraday::Connection' do
57
- expect { described_class.client }.to raise_error(NotImplementedError)
58
- end
59
- end
60
-
61
- describe 'request' do
62
- context 'with successful responses' do
63
- let(:exists_response) { { 'exists' => false } }
64
- let(:bases_response) { { 'bases' => { 'id' => 1 } } }
65
- let(:prefix_bases_response) { { 'bases' => { 'id' => 2 } } }
66
- let(:faraday_stubs) { Faraday::Adapter::Test::Stubs.new }
67
- let(:connection) { Faraday.new { |builder| builder.adapter :test, faraday_stubs } }
68
-
69
- before :each do
70
- subject.class.expects(:client).at_least_once.returns(connection)
71
- subject.class.expects(:endpoint).at_least_once.returns('bases')
72
- end
73
-
74
- after :each do
75
- faraday_stubs.verify_stubbed_calls
76
- end
77
-
78
- context 'without an id parameter' do
79
- context 'without a prefix' do
80
- it 'is expected to make the request without the prefix' do
81
- # stub.get receives arguments: path, headers, block
82
- # The block should be a Array [status, headers, body]
83
- faraday_stubs.get('/bases/') { [200, {}, bases_response] }
84
- response = subject.class.request('', {}, :get)
85
- expect(response).to eq(bases_response)
86
- end
87
- end
88
-
89
- context 'with a prefix' do
90
- it 'is expected to make the request with the prefix' do
91
- # stub.get receives arguments: path, headers, block
92
- # The block should be a Array [status, headers, body]
93
- faraday_stubs.get('/prefix/bases/') { [200, {}, prefix_bases_response] }
94
- response = subject.class.request('', {}, :get, 'prefix')
95
- expect(response).to eq(prefix_bases_response)
96
- end
97
- end
98
- end
99
-
100
- context 'with an id parameter' do
101
- it 'is expected to make the request with the id included' do
102
- # stub.get receives arguments: path, headers, block
103
- # The block should be a Array [status, headers, body]
104
- faraday_stubs.get('/bases/1/exists') { [200, {}, exists_response] }
105
- response = subject.class.request(':id/exists', { id: 1 }, :get)
106
- expect(response).to eq(exists_response)
107
- end
108
- end
109
- end
110
-
111
- context 'when the record was not found' do
112
- context 'and or the status is 404' do
113
- let!(:faraday_stubs) do
114
- Faraday::Adapter::Test::Stubs.new do |stub|
115
- # stub.get receives arguments: path, headers, block
116
- # The block should be a Array [status, headers, body]
117
- stub.get('/bases/1') { [404, {}, {}] }
118
- end
119
- end
120
- let!(:connection) { Faraday.new { |builder| builder.adapter :test, faraday_stubs } }
121
-
122
- before :each do
123
- described_class.expects(:client).at_least_once.returns(connection)
124
- end
125
-
126
- it 'is expected to raise a RecordNotFound error' do
127
- expect { described_class.request(':id', { id: 1 }, :get) }.to raise_error(KalibroClient::Errors::RecordNotFound)
128
- faraday_stubs.verify_stubbed_calls
129
- end
130
- end
131
-
132
- context 'and or the response has a NotFound error message' do
133
- let!(:faraday_stubs) do
134
- Faraday::Adapter::Test::Stubs.new do |stub|
135
- # stub.get receives arguments: path, headers, block
136
- # The block should be a Array [status, headers, body]
137
- stub.get('/bases/1') { [422, {}, { 'errors' => 'RecordNotFound' }] }
138
- end
139
- end
140
- let!(:connection) { Faraday.new { |builder| builder.adapter :test, faraday_stubs } }
141
-
142
- before :each do
143
- described_class.expects(:client).at_least_once.returns(connection)
144
- end
145
-
146
- it 'is expected to raise a RecordNotFound error' do
147
- expect { described_class.request(':id', { id: 1 }, :get) }.to raise_error(KalibroClient::Errors::RecordNotFound)
148
- faraday_stubs.verify_stubbed_calls
149
- end
150
- end
151
- end
152
-
153
- context 'with an unsuccessful request' do
154
- let!(:stubs) { Faraday::Adapter::Test::Stubs.new { |stub| stub.get('/bases/1/exists') { [500, {}, {}] } } }
155
- let(:connection) { Faraday.new { |builder| builder.adapter :test, stubs } }
156
-
157
- before :each do
158
- subject.class.expects(:client).at_least_once.returns(connection)
159
- end
160
-
161
- it 'is expected to raise a RequestError with the response' do
162
- expect { subject.class.request(':id/exists', { id: 1 }, :get) }.to raise_error do |error|
163
- expect(error).to be_a(KalibroClient::Errors::RequestError)
164
- expect(error.response.status).to eq(500)
165
- expect(error.response.body).to eq({})
166
- end
167
- stubs.verify_stubbed_calls
168
- end
169
- end
170
- end
171
-
172
- describe 'to_hash' do
173
- it 'is expected to return an empty hash' do
174
- expect(subject.to_hash).to be_empty
175
- end
33
+ before do
34
+ subject.id = 24
35
+ subject.miscellaneous = MiscellaneousTest.new
36
+ subject.miscellaneous.id = 42
176
37
  end
177
38
 
178
- describe 'to_object' do
179
- it 'is expected to return an Object with an empty hash' do
180
- expect(described_class.to_object({})).to eq(FactoryGirl.build(:model))
181
- end
182
-
183
- it "is expected to remain an object if it isn't a Hash" do
184
- expect(described_class.to_object(Object.new)).to be_an(Object)
185
- end
186
- end
187
-
188
- describe 'to_objects_array' do
189
- it 'is expected to convert [{}] to [Model]' do
190
- expect(described_class.to_objects_array({})).to eq([FactoryGirl.build(:model)])
191
- end
192
-
193
- it 'is expected to remain an array if it already is one' do
194
- object = Object.new
195
- expect(described_class.to_objects_array([object])).to eq([object])
196
- end
197
- end
198
-
199
- shared_examples 'persistence method' do |method_name, http_method, has_id = true|
200
- before :each do
201
- subject.id = 42 if has_id
202
- end
203
-
204
- let(:url) { has_id ? ':id' : '' }
205
- let(:params) { has_id ? has_entry(id: 42) : anything }
206
-
207
- context 'when a record does not exist with given id' do
208
- before :each do
209
- subject.class.expects(:request).with(url, params, http_method, '').
210
- raises(KalibroClient::Errors::RecordNotFound)
211
- end
212
-
213
- it 'is expected to raise a RecordNotFound error' do
214
- expect { subject.send(method_name) }.to raise_error(KalibroClient::Errors::RecordNotFound)
215
- end
216
- end
217
-
218
- context 'when a server error is returned' do
219
- before :each do
220
- error = KalibroClient::Errors::RequestError.new(response: mock(status: 500))
221
-
222
- subject.class.expects(:request).with(url, params, http_method, '').raises(error)
223
- end
224
-
225
- it 'is expected to raise a RequestError error' do
226
- expect { subject.send(method_name) }.to raise_error(KalibroClient::Errors::RequestError)
227
- end
228
- end
229
-
230
- context 'when a regular kind of error is returned' do
231
- before :each do
232
- error = KalibroClient::Errors::RequestError.new(response: mock(status: 422, body: { 'errors' => errors }))
233
-
234
- subject.class.expects(:request).with(url, params, http_method, '').raises(error)
235
- end
236
-
237
- context 'with a single error' do
238
- let(:errors) { "error" }
239
-
240
- it 'is expected to set the kalibro_errors field' do
241
- expect(subject.send(method_name)).to eq(false)
242
- expect(subject.kalibro_errors).to eq([errors])
243
- end
244
- end
245
-
246
- context 'with an array of errors' do
247
- let(:errors) { ["error_1", "error_2"] }
248
-
249
- it 'is expected to set the kalibro_errors field' do
250
- expect(subject.send(method_name)).to eq(false)
251
- expect(subject.kalibro_errors).to eq(errors)
252
- end
253
- end
254
-
255
- context 'with no error message at all' do
256
- let(:errors) { nil }
257
-
258
- it 'is expected to set the kalibro_errors field' do
259
- expect(subject.send(method_name)).to eq(false)
260
- expect(subject.kalibro_errors.first).to be_a(KalibroClient::Errors::RequestError)
261
- end
39
+ describe 'class method' do
40
+ describe 'module_name' do
41
+ it 'is expected to return the camelized gem name' do
42
+ expect(described_class.module_name).to eq('KalibroClient')
262
43
  end
263
44
  end
264
45
  end
265
46
 
266
- describe 'save' do
267
- it_behaves_like 'persistence method', :save, :post, false # false means Don't use ids in URLs
268
-
269
- context 'with a successful response' do
270
- context 'when it is not persisted' do
271
- before :each do
272
- subject.class.expects(:request).at_least_once.with('', anything, :post, '').
273
- returns({ "base" => { 'id' => 42, 'errors' => [] } })
274
- end
275
-
276
- it 'is expected to make a request to save model with id and return true without errors' do
277
- expect(subject.save).to be(true)
278
- expect(subject.id).to eq(42)
279
- expect(subject.kalibro_errors).to be_empty
280
- end
281
- end
282
-
283
- context 'when it is persisted' do
284
- before :each do
285
- subject.expects(:persisted?).at_least_once.returns(true)
286
- end
287
-
288
- it 'is expected to call the update method' do
289
- subject.expects(:update).returns(true)
290
- expect(subject.save).to eq(true)
291
- end
292
- end
293
- end
294
- end
295
-
296
- describe 'update' do
297
- it_behaves_like 'persistence method', :update, :put
298
-
299
- context 'with valid parameters' do
300
- before :each do
301
- id = 42
302
-
303
- subject.expects(:id).at_least_once.returns(id)
304
- described_class.expects(:request).with(':id', has_entry(id: id), :put, '').
305
- returns({ "base" => { 'id' => id, 'errors' => [] }})
306
- end
307
-
308
- it 'is expected to return true' do
309
- expect(subject.update).to eq(true)
310
- end
311
- end
312
- end
313
-
314
- describe 'create' do
315
- before :each do
316
- subject.expects(:save)
317
- described_class.
318
- expects(:new).
319
- with({}).
320
- returns(subject)
321
- end
322
-
323
- it 'is expected to instantiate and save the model' do
324
- expect(described_class.create {}).to eq(subject)
325
- end
326
- end
327
-
328
- describe 'find' do
329
- context 'with an inexistent id' do
330
- before :each do
331
- subject.class.expects(:request).at_least_once.with(':id', has_entry(id: 0), :get).
332
- raises(KalibroClient::Errors::RecordNotFound)
333
- end
334
-
335
- it 'is expected to raise a RecordNotFound error' do
336
- expect { subject.class.find(0) }.to raise_error(KalibroClient::Errors::RecordNotFound)
337
- end
338
- end
339
-
340
- context 'with an existent id' do
341
- before :each do
342
- subject.class.expects(:request).with(':id', has_entry(id: 42), :get).
343
- returns("base" => { 'id' => 42 })
344
- end
345
-
346
- it 'is expected to return an empty model' do
347
- expect(subject.class.find(42).id).to eq(42)
348
- end
349
- end
350
- end
351
-
352
- describe 'destroy' do
353
- it_behaves_like 'persistence method', :destroy, :delete
354
-
355
- context 'when it gets successfully destroyed' do
356
- before :each do
357
- subject.expects(:id).at_least_once.returns(42)
358
- described_class.expects(:request).with(':id', { id: subject.id }, :delete, '').returns({})
359
- end
360
-
361
- it 'is expected to remain with the errors array empty and not persisted' do
362
- subject.destroy
363
- expect(subject.kalibro_errors).to be_empty
364
- expect(subject.persisted?).to eq(false)
365
- end
366
- end
367
- end
368
-
369
- describe 'save!' do
370
- it 'is expected to call save and not raise when saving works' do
371
- subject.expects(:save).returns(true)
372
- expect { subject.save! }.not_to raise_error
373
- end
374
-
375
- it 'is expected to call save and raise RecordInvalid when saving fails' do
376
- subject.expects(:kalibro_errors).returns(['test1', 'test2'])
377
- subject.expects(:save).returns(false)
378
-
379
- expect { subject.save! }.to raise_error { |error|
380
- expect(error).to be_a(KalibroClient::Errors::RecordInvalid)
381
- expect(error.record).to be(subject)
382
- expect(error.message).to eq('Record invalid: test1, test2')
383
- }
384
- end
385
- end
386
-
387
- describe '==' do
388
- subject { FactoryGirl.build(:model) }
389
-
390
- context 'comparing objects from different classes' do
391
- it 'is expected to return false' do
392
- expect(subject).not_to eq(Object.new)
393
- end
394
- end
395
-
396
- context 'with two models with different attribute values' do
397
- let(:another_model) { FactoryGirl.build(:model) }
398
-
399
- before :each do
400
- subject.expects(:variable_names).returns(["answer"])
401
- subject.expects(:send).with("answer").returns(42)
402
- another_model.expects(:send).with("answer").returns(41)
403
- end
404
-
405
- it 'is expected to return false' do
406
- expect(subject).not_to eq(another_model)
407
- end
408
- end
409
-
410
- context 'with two empty models' do
411
- it 'is expected to return true' do
412
- expect(subject).to eq(FactoryGirl.build(:model))
413
- end
414
- end
415
- end
416
-
417
- describe 'exists?' do
418
- context 'with an inexistent id' do
419
- before :each do
420
- described_class.
421
- expects(:request).
422
- with(':id/exists', { id: 0 }, :get).
423
- returns({ 'exists' => false })
424
- end
425
-
426
- it 'is expected to return false' do
427
- expect(described_class.exists?(0)).to eq(false)
428
- end
429
- end
430
-
431
- context 'with an existent id' do
432
- before :each do
433
- described_class.
434
- expects(:request).
435
- with(':id/exists', { id: 42 }, :get).
436
- returns({ 'exists' => true })
437
- end
438
-
439
- it 'is expected to return false' do
440
- expect(described_class.exists?(42)).to eq(true)
441
- end
442
- end
443
- end
444
-
445
- describe 'create_objects_array_from_hash' do
446
- subject { FactoryGirl.build(:model) }
447
-
448
- context 'with nil' do
449
- it 'is expected to return an empty array' do
450
- expect(described_class.create_objects_array_from_hash("bases" => [])).to eq([])
451
- end
452
- end
453
-
454
- context 'with a Hash' do
455
- it 'is expected to return the correspondent object to the given hash inside of an Array' do
456
- expect(described_class.create_objects_array_from_hash("bases" => {})).to eq([subject])
457
- end
458
- end
459
- end
460
-
461
- describe 'is_valid?' do
462
- context 'with a global var' do
463
- it 'is expected to return false' do
464
- expect(described_class.is_valid?('@test')).to be_falsey
465
- end
466
- end
467
-
468
- context 'with the attributes var' do
469
- it 'is expected to return false' do
470
- expect(described_class.is_valid?(:attributes!)).to be_falsey
471
- end
472
- end
473
-
474
- context 'with a xsi var' do
475
- it 'is expected to return false' do
476
- expect(described_class.is_valid?('test_xsi')).to be_falsey
477
- end
478
- end
479
-
480
- context 'with a valid var' do
481
- it 'is expected to return true' do
482
- expect(described_class.is_valid?('test')).to be_truthy
47
+ describe 'instance method' do
48
+ describe 'convert_to_hash' do
49
+ it 'is expected to convert miscellaneous nested objects' do
50
+ expect(subject.to_hash).to eq({'id' => "24", 'miscellaneous' => {'id' => "42"}})
483
51
  end
484
52
  end
485
53
  end