sunstone 2.0.0 → 2.0.1

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.
@@ -1,8 +1,6 @@
1
1
  require 'active_record/connection_adapters/abstract_adapter'
2
2
  require 'active_record/connection_adapters/statement_pool'
3
3
 
4
- require 'arel/visitors/sunstone'
5
- require 'arel/collectors/sunstone'
6
4
 
7
5
  require 'active_record/connection_adapters/sunstone/database_statements'
8
6
  require 'active_record/connection_adapters/sunstone/schema_statements'
@@ -397,11 +397,15 @@ module Arel
397
397
  collector.columns << {:count => (o.expressions.first.is_a?(Arel::Attributes::Attribute) ? o.expressions.first.name : o.expressions.first) }
398
398
  # collector.columns = visit o.expressions.first, collector
399
399
  end
400
- #
401
- # def visit_Arel_Nodes_Sum o, collector
402
- # aggregate "SUM", o, collector
403
- # end
404
- #
400
+
401
+ def visit_Arel_Nodes_Sum o, collector
402
+ collector.operation = :calculate
403
+
404
+ collector.columns ||= []
405
+ collector.columns << {:sum => (o.expressions.first.is_a?(Arel::Attributes::Attribute) ? o.expressions.first.name : o.expressions.first) }
406
+ # collector.columns = visit o.expressions.first, collector
407
+ end
408
+
405
409
  def visit_Arel_Nodes_Max o, collector
406
410
  collector.operation = :calculate
407
411
 
@@ -6,11 +6,17 @@ require File.expand_path(File.join(__FILE__, '../sunstone/version'))
6
6
  require File.expand_path(File.join(__FILE__, '../sunstone/connection'))
7
7
  require File.expand_path(File.join(__FILE__, '../sunstone/exception'))
8
8
 
9
+ require File.expand_path(File.join(__FILE__, '../arel/visitors/sunstone'))
10
+ require File.expand_path(File.join(__FILE__, '../arel/collectors/sunstone'))
11
+
12
+ require File.expand_path(File.join(__FILE__, '../active_record/connection_adapters/sunstone_adapter'))
13
+
9
14
  require File.expand_path(File.join(__FILE__, '../../ext/active_record/statement_cache'))
10
15
  require File.expand_path(File.join(__FILE__, '../../ext/active_record/relation'))
11
- require File.expand_path(File.join(__FILE__, '../../ext/active_record/relation/predicate_builder'))
12
- require File.expand_path(File.join(__FILE__, '../../ext/active_record/calculations'))
13
- require File.expand_path(File.join(__FILE__, '../../ext/active_record/associations/builder/has_and_belongs_to_many'))
16
+ # require File.expand_path(File.join(__FILE__, '../../ext/active_record/relation/predicate_builder'))
17
+ # require File.expand_path(File.join(__FILE__, '../../ext/active_record/calculations'))
18
+ require File.expand_path(File.join(__FILE__, '../../ext/active_record/query_methods'))
19
+ # require File.expand_path(File.join(__FILE__, '../../ext/active_record/associations/builder/has_and_belongs_to_many'))
14
20
 
15
21
  require File.expand_path(File.join(__FILE__, '../../ext/arel/select_manager'))
16
22
  require File.expand_path(File.join(__FILE__, '../../ext/arel/nodes/eager_load'))
@@ -9,8 +9,7 @@ module Sunstone
9
9
  class Connection
10
10
 
11
11
  # Set the User-Agent of the client. Will be joined with other User-Agent info
12
- attr_writer :user_agent
13
- attr_accessor :api_key, :host, :port, :use_ssl
12
+ attr_reader :api_key, :host, :port, :use_ssl
14
13
 
15
14
  # Initialize a connection a Sunstone API server.
16
15
  #
@@ -32,7 +31,7 @@ module Sunstone
32
31
  end
33
32
 
34
33
  [:api_key, :host, :port, :use_ssl, :user_agent].each do |key|
35
- self.send(:"#{key}=", config[key])
34
+ self.instance_variable_set(:"@#{key}", config[key])
36
35
  end
37
36
 
38
37
  @connection = Net::HTTP.new(host, port)
@@ -1,3 +1,3 @@
1
1
  module Sunstone
2
- VERSION = '2.0.0'
2
+ VERSION = '2.0.1'
3
3
  end
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
31
31
  s.add_runtime_dependency 'wankel'
32
32
  s.add_runtime_dependency 'cookie_store'
33
33
  s.add_runtime_dependency 'arel', '~> 6.0'
34
- s.add_runtime_dependency 'activesupport', '>= 4.2.3'
35
- s.add_runtime_dependency 'activemodel', '>= 4.2.3'
36
- s.add_runtime_dependency 'activerecord', '>= 4.2.3'
34
+ s.add_runtime_dependency 'activesupport', '>= 4.2.4'
35
+ s.add_runtime_dependency 'activemodel', '>= 4.2.4'
36
+ s.add_runtime_dependency 'activerecord', '>= 4.2.4'
37
37
  end
@@ -0,0 +1,14 @@
1
+ class Fleet < ActiveRecord::Base
2
+ has_many :ships
3
+ end
4
+
5
+ class Ship < ActiveRecord::Base
6
+ belongs_to :fleet
7
+
8
+ has_and_belongs_to_many :sailors
9
+ end
10
+
11
+ class Sailor < ActiveRecord::Base
12
+ has_and_belongs_to_many :sailors
13
+ end
14
+
@@ -0,0 +1,128 @@
1
+ require 'test_helper'
2
+
3
+ class QueryTest < Minitest::Test
4
+
5
+ def setup
6
+ stub_request(:get, "malomalo.io/ping").to_return(:body => "pong")
7
+ stub_request(:get, "malomalo.io/tables").to_return(:body => ['ships'].to_json)
8
+
9
+ stub_request(:get, "malomalo.io/ships/schema").to_return(:body => {
10
+ id: {type: 'integer', primary_key: true, null: false, array: false},
11
+ fleet_id: {type: 'integer', primary_key: false, null: true, array: false},
12
+ name: {type: 'string', primary_key: false, null: true, array: false}
13
+ }.to_json)
14
+
15
+ stub_request(:get, "malomalo.io/fleets/schema").to_return(:body => {
16
+ id: {type: 'integer', primary_key: true, null: false, array: false},
17
+ name: {type: 'string', primary_key: false, null: true, array: false}
18
+ }.to_json)
19
+
20
+ stub_request(:get, "malomalo.io/sailors/schema").to_return(:body => {
21
+ id: {type: 'integer', primary_key: true, null: false, array: false},
22
+ name: {type: 'string', primary_key: false, null: true, array: false}
23
+ }.to_json)
24
+
25
+ ActiveRecord::Base.establish_connection(
26
+ :adapter => 'sunstone',
27
+ :site => 'http://malomalo.io'
28
+ )
29
+ end
30
+
31
+ test '::find' do
32
+ stub_request(:get, URI::escape('malomalo.io/ships?limit=1&where[id]=42')).to_return(body: [{id: 42}].to_json)
33
+
34
+ assert_equal 42, Ship.find(42).id
35
+ end
36
+
37
+ test '::first' do
38
+ stub_request(:get, URI::escape('malomalo.io/ships?limit=1&order[][id]=asc')).to_return(body: [].to_json)
39
+
40
+ assert_nil Ship.first
41
+ end
42
+
43
+ test '::last' do
44
+ stub_request(:get, URI::escape('malomalo.io/ships?limit=1&order[][id]=desc')).to_return(body: [].to_json)
45
+
46
+ assert_nil Ship.last
47
+ end
48
+
49
+ test '::find_each' do
50
+ stub_request(:get, URI::escape('malomalo.io/ships?limit=1000&order[][id]=asc')).to_return(body: [].to_json)
51
+
52
+ assert_nil Ship.find_each { |s| s }
53
+ end
54
+
55
+ test '::where on columns' do
56
+ stub_request(:get, URI::escape('malomalo.io/ships?where[id]=10')).to_return(body: [].to_json)
57
+
58
+ assert_equal [], Ship.where(:id => 10).to_a
59
+ end
60
+
61
+ test '::where on belongs_to relation' do
62
+ stub_request(:get, URI::escape('malomalo.io/ships?where[fleet][id]=1')).to_return(body: [].to_json)
63
+
64
+ assert_equal [], Ship.where(:fleet => {id: 1}).to_a
65
+ end
66
+
67
+ test '::where on has_many relation' do
68
+ stub_request(:get, URI::escape('malomalo.io/fleets?where[ships][id]=1')).to_return(body: [].to_json)
69
+
70
+ assert_equal [], Fleet.where(:ships => {id: 1}).to_a
71
+ end
72
+
73
+ test '::where on has_and_belongs_to_many relation' do
74
+ stub_request(:get, URI::escape('malomalo.io/ships?where[sailors][id]=1')).to_return(body: [].to_json)
75
+
76
+ assert_equal [], Ship.where(:sailors => {id: 1}).to_a
77
+ end
78
+
79
+ test '::count' do
80
+ stub_request(:get, URI::escape('malomalo.io/ships/calculate?select[][count]=*')).to_return(body: '[10]')
81
+
82
+ assert_equal 10, Ship.count
83
+ end
84
+
85
+ test '::count(:column)' do
86
+ stub_request(:get, URI::escape('malomalo.io/ships/calculate?select[][count]=id')).to_return(body: '[10]')
87
+
88
+ assert_equal 10, Ship.count(:id)
89
+ end
90
+
91
+ test '::sum(:column)' do
92
+ stub_request(:get, URI::escape('malomalo.io/ships/calculate?select[][sum]=weight')).to_return(body: '[10]')
93
+
94
+ assert_equal 10, Ship.sum(:weight)
95
+ end
96
+
97
+ # Relation test
98
+
99
+ test '#to_sql' do
100
+ stub_request(:get, URI::escape('malomalo.io/ships?where[id]=10')).to_return(body: [].to_json)
101
+
102
+ assert_equal "SELECT ships.* FROM ships WHERE ships.id = 10", Ship.where(:id => 10).to_sql
103
+ end
104
+
105
+ # Preload test
106
+
107
+ test '#preload' do
108
+ stub_request(:get, URI::escape('malomalo.io/fleets')).to_return(body: [{id: 1}].to_json)
109
+ stub_request(:get, URI::escape('malomalo.io/ships?where[fleet_id][0]=1')).to_return(body: [{id: 1, fleet_id: 1}].to_json)
110
+
111
+ fleets = Fleet.preload(:ships)
112
+ assert_equal [1], fleets.map(&:id)
113
+ assert_equal [1], fleets.first.ships.map(&:id)
114
+ end
115
+
116
+ # Eagerload test
117
+
118
+ test '#eager_load' do
119
+ stub_request(:get, URI::escape('malomalo.io/fleets?include[]=ships')).to_return(body: [{
120
+ id: 1, ships: [{id: 1, fleet_id: 1}]
121
+ }].to_json)
122
+
123
+ fleets = Fleet.eager_load(:ships)
124
+ assert_equal [1], fleets.map(&:id)
125
+ assert_equal [1], fleets.first.ships.map(&:id)
126
+ end
127
+
128
+ end
@@ -3,310 +3,301 @@ require 'test_helper'
3
3
  class SunstoneTest < Minitest::Test
4
4
 
5
5
  def setup
6
- Sunstone.site = "http://test_api_key@testhost.com"
7
- end
8
-
9
- # Sunstone.site= ============================================================
10
-
11
- test "setting the site sets the api_key" do
12
- Sunstone.site = 'https://my_api_key@localhost'
13
- assert_equal('my_api_key', Sunstone.api_key)
14
- end
15
-
16
- test "setting the site sets the host" do
17
- Sunstone.site = 'https://my_api_key@example.com'
18
- assert_equal('example.com', Sunstone.host)
19
- end
20
-
21
- test "setting the site sets the port" do
22
- Sunstone.site = 'http://my_api_key@example.com'
23
- assert_equal(80, Sunstone.port)
24
-
25
- Sunstone.site = 'https://my_api_key@example.com'
26
- assert_equal(443, Sunstone.port)
27
-
28
- Sunstone.site = 'https://my_api_key@example.com:4321'
29
- assert_equal(4321, Sunstone.port)
30
- end
31
-
32
- test "setting the site sets the use_ssl option" do
33
- Sunstone.site = 'http://my_api_key@example.com'
34
- assert_equal(false, Sunstone.use_ssl)
35
-
36
- Sunstone.site = 'https://my_api_key@example.com'
37
- assert_equal(true, Sunstone.use_ssl)
38
- end
39
-
40
- # Sunstone.user_agent= ======================================================
41
- test "setting the user_agent appends it to the User-Agent" do
42
- assert_equal("Sunstone/#{Sunstone::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} #{RUBY_PLATFORM}", Sunstone.user_agent)
43
-
44
- Sunstone.user_agent = "MyGem/3.14"
45
- assert_equal("MyGem/3.14 Sunstone/#{Sunstone::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} #{RUBY_PLATFORM}", Sunstone.user_agent)
46
- Sunstone.user_agent = nil
47
- end
48
-
49
- # Sunstone.with_cookie_store ================================================
50
-
51
- test '#with_cookie_store(store, &block) sets the cookie-store' do
52
- assert_nil Thread.current[:sunstone_cookie_store]
53
- Sunstone.with_cookie_store('my_store') do
54
- assert_equal 'my_store', Thread.current[:sunstone_cookie_store]
55
- end
56
- assert_nil Thread.current[:sunstone_cookie_store]
57
- end
58
-
59
- # Sunstone.with_api_key ================================================
60
-
61
- test '#with_api_key(key, &block) sets the Api-Key' do
62
- assert_nil Thread.current[:sunstone_api_key]
63
- Sunstone.with_api_key('my_api_key') do
64
- assert_equal 'my_api_key', Thread.current[:sunstone_api_key]
65
- end
66
- assert_nil Thread.current[:sunstone_api_key]
67
- end
68
-
69
- # Sunstone.send_request =====================================================
70
-
71
- test '#send_request(#<Net::HTTPRequest>)' do
72
- stub_request(:get, "http://testhost.com/test").to_return(:body => "get")
73
-
74
- assert_equal('get', Sunstone.send_request(Net::HTTP::Get.new('/test')).body)
75
- end
76
-
77
- test '#send_request(#<Net::HTTPRequest>, body) with string body' do
78
- stub_request(:post, "http://testhost.com/test").with(:body => '{"key":"value"}').to_return(:body => "post")
79
-
80
- assert_equal('post', Sunstone.send_request(Net::HTTP::Post.new('/test'), '{"key":"value"}').body)
81
- end
82
-
83
- test '#send_request(#<Net::HTTPRequest>, body) with IO body' do
84
- stub_request(:post, "http://testhost.com/test").with { |request|
85
- request.headers['Transfer-Encoding'] == "chunked" && request.body == '{"key":"value"}'
86
- }.to_return(:body => "post")
87
-
88
- rd, wr = IO.pipe
89
- wr.write('{"key":"value"}')
90
- wr.close
91
-
92
- assert_equal('post', Sunstone.send_request(Net::HTTP::Post.new('/test'), rd).body)
93
- end
94
-
95
- test '#send_request(#<Net::HTTPRequest>, body) with Ruby Object body' do
96
- stub_request(:post, "http://testhost.com/test").with(:body => '{"key":"value"}').to_return(:body => "post")
97
-
98
- assert_equal('post', Sunstone.send_request(Net::HTTP::Post.new('/test'), {:key => 'value'}).body)
99
- end
100
-
101
- test '#send_request(#<Net::HTTPRequest>) raises Sunstone::Exceptions on non-200 responses' do
102
- stub_request(:get, "http://testhost.com/400").to_return(:status => 400)
103
- stub_request(:get, "http://testhost.com/401").to_return(:status => 401)
104
- stub_request(:get, "http://testhost.com/404").to_return(:status => 404)
105
- stub_request(:get, "http://testhost.com/410").to_return(:status => 410)
106
- stub_request(:get, "http://testhost.com/422").to_return(:status => 422)
107
- stub_request(:get, "http://testhost.com/450").to_return(:status => 450)
108
- stub_request(:get, "http://testhost.com/503").to_return(:status => 503)
109
- stub_request(:get, "http://testhost.com/550").to_return(:status => 550)
110
-
111
- assert_raises(Sunstone::Exception::BadRequest) { Sunstone.send_request(Net::HTTP::Get.new('/400')) }
112
- assert_raises(Sunstone::Exception::Unauthorized) { Sunstone.send_request(Net::HTTP::Get.new('/401')) }
113
- assert_raises(Sunstone::Exception::NotFound) { Sunstone.send_request(Net::HTTP::Get.new('/404')) }
114
- assert_raises(Sunstone::Exception::Gone) { Sunstone.send_request(Net::HTTP::Get.new('/410')) }
115
- assert_raises(Sunstone::Exception::ApiVersionUnsupported) { Sunstone.send_request(Net::HTTP::Get.new('/422')) }
116
- assert_raises(Sunstone::Exception) { Sunstone.send_request(Net::HTTP::Get.new('/450')) }
117
- assert_raises(Sunstone::Exception::ServiceUnavailable) { Sunstone.send_request(Net::HTTP::Get.new('/503')) }
118
- assert_raises(Sunstone::Exception) { Sunstone.send_request(Net::HTTP::Get.new('/550')) }
119
- end
120
-
121
- test '#send_request(#<Net::HTTPRequest>, &block) returns value returned from &block' do
122
- stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
123
-
124
- value = Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
125
- 3215
126
- end
127
-
128
- assert_equal 3215, value
129
- end
130
-
131
- test '#send_request(#<Net::HTTPRequest>, &block)' do
132
- stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
133
-
134
- Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
135
- assert_equal 'get', response.body
136
- end
137
-
138
- # make sure block is not called when not in valid_response_codes
139
- stub_request(:get, "http://testhost.com/test").to_return(:status => 401, :body => 'get')
140
-
141
- assert_raises(Sunstone::Exception::Unauthorized) {
142
- Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
143
- raise Sunstone::Exception, 'Should not get here'
144
- end
145
- }
146
- end
147
-
148
- test '#send_request(#<Net::HTTPRequest>, &block) with block reading chunks' do
149
- rd, wr = IO.pipe
150
- rd = Net::BufferedIO.new(rd)
151
- wr.write(<<-DATA.gsub(/^ +/, '').gsub(/\n/, "\r\n"))
152
- HTTP/1.1 200 OK
153
- Content-Length: 5
154
-
155
- hello
156
- DATA
157
-
158
- res = Net::HTTPResponse.read_new(rd)
159
- connection = mock('connection')
160
- connection.stubs(:request).yields(res)
161
- Sunstone.stubs(:with_connection).yields(connection)
162
-
163
- res.reading_body(rd, true) do
164
- Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
165
- response.read_body do |chunk|
166
- assert_equal('hello', chunk)
167
- end
168
- end
169
- end
170
- end
171
-
172
- test '#send_request(#<Net::HTTPRequest) adds cookies to the cookie store if present' do
173
- store = CookieStore::HashStore.new
174
- stub_request(:get, "http://testhost.com/test").to_return(:body => 'get', :headers => {'Set-Cookie' => 'foo=bar; Max-Age=3600'})
175
-
176
- Sunstone.with_cookie_store(store) do
177
- Sunstone.send_request(Net::HTTP::Get.new('/test'))
178
- end
179
-
180
- assert_equal 1, store.instance_variable_get(:@domains).size
181
- assert_equal 1, store.instance_variable_get(:@domains)['testhost.com'].size
182
- assert_equal 1, store.instance_variable_get(:@domains)['testhost.com']['/test'].size
183
- assert_equal 'bar', store.instance_variable_get(:@domains)['testhost.com']['/test']['foo'].value
184
- end
185
-
186
- test '#send_request(#<Net::HTTPRequest>) includes the headers' do
187
- stub_request(:get, "http://testhost.com/test").with(:headers => {
188
- 'Api-Key' => 'test_api_key',
189
- 'Content-Type' => 'application/json',
190
- 'User-Agent' => Sunstone.user_agent
191
- }).to_return(:body => "get")
192
-
193
- assert_equal('get', Sunstone.send_request(Net::HTTP::Get.new('/test')).body)
194
-
195
- # Test without api key
196
- Sunstone.site = "http://testhost.com"
197
- stub_request(:get, "http://testhost.com/test").with(:headers => {
198
- 'Content-Type'=>'application/json',
199
- 'User-Agent'=>'Sunstone/0.1 Ruby/2.1.1-p76 x86_64-darwin13.0'
200
- }).to_return(:body => "get")
201
-
202
- assert_equal('get', Sunstone.send_request(Net::HTTP::Get.new('/test')).body)
203
- end
204
-
205
- # Sunstone.get ==============================================================
206
-
207
- test '#get(path)' do
208
- stub_request(:get, "http://testhost.com/test").to_return(:body => "get")
209
-
210
- assert_equal('get', Sunstone.get('/test').body)
211
- end
212
-
213
- test '#get(path, params) with params as string' do
214
- stub_request(:get, "http://testhost.com/test").with(:query => {'key' => 'value'}).to_return(:body => "get")
215
-
216
- assert_equal 'get', Sunstone.get('/test', 'key=value').body
217
- end
218
-
219
- test '#get(path, params) with params as hash' do
220
- stub_request(:get, "http://testhost.com/test").with(:query => {'key' => 'value'}).to_return(:body => "get")
221
-
222
- assert_equal 'get', Sunstone.get('/test', {:key => 'value'}).body
223
- end
224
-
225
- test '#get(path, &block)' do
226
- stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
227
-
228
- Sunstone.get('/test') do |response|
229
- assert_equal 'get', response.body
230
- end
231
- end
232
-
233
- # Sunstone.post =============================================================
234
-
235
- test '#post(path)' do
236
- stub_request(:post, "http://testhost.com/test").to_return(:body => "post")
237
-
238
- assert_equal('post', Sunstone.post('/test').body)
239
- end
240
-
241
- test '#post(path, body)' do
242
- stub_request(:post, "http://testhost.com/test").with(:body => 'body').to_return(:body => "post")
243
-
244
- assert_equal('post', Sunstone.post('/test', 'body').body)
245
- end
246
-
247
- test '#post(path, &block)' do
248
- stub_request(:post, "http://testhost.com/test").to_return(:body => 'post')
249
-
250
- Sunstone.post('/test') do |response|
251
- assert_equal 'post', response.body
252
- end
253
- end
254
-
255
- # Sunstone.put ==============================================================
256
-
257
- test '#put(path)' do
258
- stub_request(:put, "http://testhost.com/test").to_return(:body => "put")
259
-
260
- assert_equal('put', Sunstone.put('/test').body)
261
- end
262
-
263
- test '#put(path, body)' do
264
- stub_request(:put, "http://testhost.com/test").with(:body => 'body').to_return(:body => "put")
265
-
266
- assert_equal('put', Sunstone.put('/test', 'body').body)
267
- end
268
-
269
- test '#put(path, &block)' do
270
- stub_request(:put, "http://testhost.com/test").to_return(:body => 'put')
271
-
272
- Sunstone.put('/test') do |response|
273
- assert_equal 'put', response.body
274
- end
275
- end
276
-
277
- # Sunstone.delete ===========================================================
278
-
279
- test '#delete' do
280
- stub_request(:delete, "http://testhost.com/test").to_return(:body => "delete")
281
-
282
- assert_equal('delete', Sunstone.delete('/test').body)
283
- end
284
-
285
- test '#delete(path, &block)' do
286
- stub_request(:delete, "http://testhost.com/test").to_return(:body => 'delete')
287
-
288
- Sunstone.delete('/test') do |response|
289
- assert_equal 'delete', response.body
290
- end
291
- end
292
-
293
- # Sunstone.ping =============================================================
294
-
295
- test '#ping' do
296
- stub_request(:get, "http://testhost.com/ping").to_return(:body => 'pong')
297
-
298
- assert_equal( 'pong', Sunstone.ping )
299
- end
300
-
301
- # Sunstone.config ===========================================================
302
-
303
- test '#config' do
304
- stub_request(:get, "http://testhost.com/config").to_return(:body => '{"server": "configs"}')
305
-
306
- assert_equal( {:server => "configs"}, Sunstone.config )
307
- end
308
-
309
-
310
-
6
+ stub_request(:get, "malomalo.io/ping").to_return(:body => "pong")
7
+ end
8
+
9
+ test "connection configuration" do
10
+ ActiveRecord::Base.establish_connection(
11
+ :adapter => 'sunstone',
12
+ :site => 'http://malomalo.io'
13
+ )
14
+
15
+ assert_equal ActiveRecord::ConnectionAdapters::SunstoneAPIAdapter, ActiveRecord::Base.connection.class
16
+
17
+
18
+ assert_equal 'malomalo.io', ActiveRecord::Base.connection.instance_variable_get(:@connection).host
19
+ assert_equal 80, ActiveRecord::Base.connection.instance_variable_get(:@connection).port
20
+ assert_equal false, ActiveRecord::Base.connection.instance_variable_get(:@connection).use_ssl
21
+ end
22
+
23
+ test "connection with user_agent" do
24
+ ActiveRecord::Base.establish_connection(
25
+ :adapter => 'sunstone',
26
+ :site => 'http://malomalo.io',
27
+ :user_agent => 'MyAgent'
28
+ )
29
+
30
+ assert ActiveRecord::Base.connection.instance_variable_get(:@connection).user_agent.start_with?('MyAgent')
31
+ end
32
+
33
+ test 'connection with api key' do
34
+ ActiveRecord::Base.establish_connection(
35
+ :adapter => 'sunstone',
36
+ :site => 'http://myapikey@malomalo.io'
37
+ )
38
+
39
+ assert_equal 'myapikey', ActiveRecord::Base.connection.instance_variable_get(:@connection).api_key
40
+ end
41
+
42
+
43
+ # # Sunstone.with_cookie_store ================================================
44
+ #
45
+ # test '#with_cookie_store(store, &block) sets the cookie-store' do
46
+ # assert_nil Thread.current[:sunstone_cookie_store]
47
+ # Sunstone.with_cookie_store('my_store') do
48
+ # assert_equal 'my_store', Thread.current[:sunstone_cookie_store]
49
+ # end
50
+ # assert_nil Thread.current[:sunstone_cookie_store]
51
+ # end
52
+ #
53
+ # # Sunstone.with_api_key ================================================
54
+ #
55
+ # test '#with_api_key(key, &block) sets the Api-Key' do
56
+ # assert_nil Thread.current[:sunstone_api_key]
57
+ # Sunstone.with_api_key('my_api_key') do
58
+ # assert_equal 'my_api_key', Thread.current[:sunstone_api_key]
59
+ # end
60
+ # assert_nil Thread.current[:sunstone_api_key]
61
+ # end
62
+ #
63
+ # # Sunstone.send_request =====================================================
64
+ #
65
+ # test '#send_request(#<Net::HTTPRequest>)' do
66
+ # stub_request(:get, "http://testhost.com/test").to_return(:body => "get")
67
+ #
68
+ # assert_equal('get', Sunstone.send_request(Net::HTTP::Get.new('/test')).body)
69
+ # end
70
+ #
71
+ # test '#send_request(#<Net::HTTPRequest>, body) with string body' do
72
+ # stub_request(:post, "http://testhost.com/test").with(:body => '{"key":"value"}').to_return(:body => "post")
73
+ #
74
+ # assert_equal('post', Sunstone.send_request(Net::HTTP::Post.new('/test'), '{"key":"value"}').body)
75
+ # end
76
+ #
77
+ # test '#send_request(#<Net::HTTPRequest>, body) with IO body' do
78
+ # stub_request(:post, "http://testhost.com/test").with { |request|
79
+ # request.headers['Transfer-Encoding'] == "chunked" && request.body == '{"key":"value"}'
80
+ # }.to_return(:body => "post")
81
+ #
82
+ # rd, wr = IO.pipe
83
+ # wr.write('{"key":"value"}')
84
+ # wr.close
85
+ #
86
+ # assert_equal('post', Sunstone.send_request(Net::HTTP::Post.new('/test'), rd).body)
87
+ # end
88
+ #
89
+ # test '#send_request(#<Net::HTTPRequest>, body) with Ruby Object body' do
90
+ # stub_request(:post, "http://testhost.com/test").with(:body => '{"key":"value"}').to_return(:body => "post")
91
+ #
92
+ # assert_equal('post', Sunstone.send_request(Net::HTTP::Post.new('/test'), {:key => 'value'}).body)
93
+ # end
94
+ #
95
+ # test '#send_request(#<Net::HTTPRequest>) raises Sunstone::Exceptions on non-200 responses' do
96
+ # stub_request(:get, "http://testhost.com/400").to_return(:status => 400)
97
+ # stub_request(:get, "http://testhost.com/401").to_return(:status => 401)
98
+ # stub_request(:get, "http://testhost.com/404").to_return(:status => 404)
99
+ # stub_request(:get, "http://testhost.com/410").to_return(:status => 410)
100
+ # stub_request(:get, "http://testhost.com/422").to_return(:status => 422)
101
+ # stub_request(:get, "http://testhost.com/450").to_return(:status => 450)
102
+ # stub_request(:get, "http://testhost.com/503").to_return(:status => 503)
103
+ # stub_request(:get, "http://testhost.com/550").to_return(:status => 550)
104
+ #
105
+ # assert_raises(Sunstone::Exception::BadRequest) { Sunstone.send_request(Net::HTTP::Get.new('/400')) }
106
+ # assert_raises(Sunstone::Exception::Unauthorized) { Sunstone.send_request(Net::HTTP::Get.new('/401')) }
107
+ # assert_raises(Sunstone::Exception::NotFound) { Sunstone.send_request(Net::HTTP::Get.new('/404')) }
108
+ # assert_raises(Sunstone::Exception::Gone) { Sunstone.send_request(Net::HTTP::Get.new('/410')) }
109
+ # assert_raises(Sunstone::Exception::ApiVersionUnsupported) { Sunstone.send_request(Net::HTTP::Get.new('/422')) }
110
+ # assert_raises(Sunstone::Exception) { Sunstone.send_request(Net::HTTP::Get.new('/450')) }
111
+ # assert_raises(Sunstone::Exception::ServiceUnavailable) { Sunstone.send_request(Net::HTTP::Get.new('/503')) }
112
+ # assert_raises(Sunstone::Exception) { Sunstone.send_request(Net::HTTP::Get.new('/550')) }
113
+ # end
114
+ #
115
+ # test '#send_request(#<Net::HTTPRequest>, &block) returns value returned from &block' do
116
+ # stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
117
+ #
118
+ # value = Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
119
+ # 3215
120
+ # end
121
+ #
122
+ # assert_equal 3215, value
123
+ # end
124
+ #
125
+ # test '#send_request(#<Net::HTTPRequest>, &block)' do
126
+ # stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
127
+ #
128
+ # Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
129
+ # assert_equal 'get', response.body
130
+ # end
131
+ #
132
+ # # make sure block is not called when not in valid_response_codes
133
+ # stub_request(:get, "http://testhost.com/test").to_return(:status => 401, :body => 'get')
134
+ #
135
+ # assert_raises(Sunstone::Exception::Unauthorized) {
136
+ # Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
137
+ # raise Sunstone::Exception, 'Should not get here'
138
+ # end
139
+ # }
140
+ # end
141
+ #
142
+ # test '#send_request(#<Net::HTTPRequest>, &block) with block reading chunks' do
143
+ # rd, wr = IO.pipe
144
+ # rd = Net::BufferedIO.new(rd)
145
+ # wr.write(<<-DATA.gsub(/^ +/, '').gsub(/\n/, "\r\n"))
146
+ # HTTP/1.1 200 OK
147
+ # Content-Length: 5
148
+ #
149
+ # hello
150
+ # DATA
151
+ #
152
+ # res = Net::HTTPResponse.read_new(rd)
153
+ # connection = mock('connection')
154
+ # connection.stubs(:request).yields(res)
155
+ # Sunstone.stubs(:with_connection).yields(connection)
156
+ #
157
+ # res.reading_body(rd, true) do
158
+ # Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
159
+ # response.read_body do |chunk|
160
+ # assert_equal('hello', chunk)
161
+ # end
162
+ # end
163
+ # end
164
+ # end
165
+ #
166
+ # test '#send_request(#<Net::HTTPRequest) adds cookies to the cookie store if present' do
167
+ # store = CookieStore::HashStore.new
168
+ # stub_request(:get, "http://testhost.com/test").to_return(:body => 'get', :headers => {'Set-Cookie' => 'foo=bar; Max-Age=3600'})
169
+ #
170
+ # Sunstone.with_cookie_store(store) do
171
+ # Sunstone.send_request(Net::HTTP::Get.new('/test'))
172
+ # end
173
+ #
174
+ # assert_equal 1, store.instance_variable_get(:@domains).size
175
+ # assert_equal 1, store.instance_variable_get(:@domains)['testhost.com'].size
176
+ # assert_equal 1, store.instance_variable_get(:@domains)['testhost.com']['/test'].size
177
+ # assert_equal 'bar', store.instance_variable_get(:@domains)['testhost.com']['/test']['foo'].value
178
+ # end
179
+ #
180
+ # test '#send_request(#<Net::HTTPRequest>) includes the headers' do
181
+ # stub_request(:get, "http://testhost.com/test").with(:headers => {
182
+ # 'Api-Key' => 'test_api_key',
183
+ # 'Content-Type' => 'application/json',
184
+ # 'User-Agent' => Sunstone.user_agent
185
+ # }).to_return(:body => "get")
186
+ #
187
+ # assert_equal('get', Sunstone.send_request(Net::HTTP::Get.new('/test')).body)
188
+ #
189
+ # # Test without api key
190
+ # Sunstone.site = "http://testhost.com"
191
+ # stub_request(:get, "http://testhost.com/test").with(:headers => {
192
+ # 'Content-Type'=>'application/json',
193
+ # 'User-Agent'=>'Sunstone/0.1 Ruby/2.1.1-p76 x86_64-darwin13.0'
194
+ # }).to_return(:body => "get")
195
+ #
196
+ # assert_equal('get', Sunstone.send_request(Net::HTTP::Get.new('/test')).body)
197
+ # end
198
+ #
199
+ # # Sunstone.get ==============================================================
200
+ #
201
+ # test '#get(path)' do
202
+ # stub_request(:get, "http://testhost.com/test").to_return(:body => "get")
203
+ #
204
+ # assert_equal('get', Sunstone.get('/test').body)
205
+ # end
206
+ #
207
+ # test '#get(path, params) with params as string' do
208
+ # stub_request(:get, "http://testhost.com/test").with(:query => {'key' => 'value'}).to_return(:body => "get")
209
+ #
210
+ # assert_equal 'get', Sunstone.get('/test', 'key=value').body
211
+ # end
212
+ #
213
+ # test '#get(path, params) with params as hash' do
214
+ # stub_request(:get, "http://testhost.com/test").with(:query => {'key' => 'value'}).to_return(:body => "get")
215
+ #
216
+ # assert_equal 'get', Sunstone.get('/test', {:key => 'value'}).body
217
+ # end
218
+ #
219
+ # test '#get(path, &block)' do
220
+ # stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
221
+ #
222
+ # Sunstone.get('/test') do |response|
223
+ # assert_equal 'get', response.body
224
+ # end
225
+ # end
226
+ #
227
+ # # Sunstone.post =============================================================
228
+ #
229
+ # test '#post(path)' do
230
+ # stub_request(:post, "http://testhost.com/test").to_return(:body => "post")
231
+ #
232
+ # assert_equal('post', Sunstone.post('/test').body)
233
+ # end
234
+ #
235
+ # test '#post(path, body)' do
236
+ # stub_request(:post, "http://testhost.com/test").with(:body => 'body').to_return(:body => "post")
237
+ #
238
+ # assert_equal('post', Sunstone.post('/test', 'body').body)
239
+ # end
240
+ #
241
+ # test '#post(path, &block)' do
242
+ # stub_request(:post, "http://testhost.com/test").to_return(:body => 'post')
243
+ #
244
+ # Sunstone.post('/test') do |response|
245
+ # assert_equal 'post', response.body
246
+ # end
247
+ # end
248
+ #
249
+ # # Sunstone.put ==============================================================
250
+ #
251
+ # test '#put(path)' do
252
+ # stub_request(:put, "http://testhost.com/test").to_return(:body => "put")
253
+ #
254
+ # assert_equal('put', Sunstone.put('/test').body)
255
+ # end
256
+ #
257
+ # test '#put(path, body)' do
258
+ # stub_request(:put, "http://testhost.com/test").with(:body => 'body').to_return(:body => "put")
259
+ #
260
+ # assert_equal('put', Sunstone.put('/test', 'body').body)
261
+ # end
262
+ #
263
+ # test '#put(path, &block)' do
264
+ # stub_request(:put, "http://testhost.com/test").to_return(:body => 'put')
265
+ #
266
+ # Sunstone.put('/test') do |response|
267
+ # assert_equal 'put', response.body
268
+ # end
269
+ # end
270
+ #
271
+ # # Sunstone.delete ===========================================================
272
+ #
273
+ # test '#delete' do
274
+ # stub_request(:delete, "http://testhost.com/test").to_return(:body => "delete")
275
+ #
276
+ # assert_equal('delete', Sunstone.delete('/test').body)
277
+ # end
278
+ #
279
+ # test '#delete(path, &block)' do
280
+ # stub_request(:delete, "http://testhost.com/test").to_return(:body => 'delete')
281
+ #
282
+ # Sunstone.delete('/test') do |response|
283
+ # assert_equal 'delete', response.body
284
+ # end
285
+ # end
286
+ #
287
+ # # Sunstone.ping =============================================================
288
+ #
289
+ # test '#ping' do
290
+ # stub_request(:get, "http://testhost.com/ping").to_return(:body => 'pong')
291
+ #
292
+ # assert_equal( 'pong', Sunstone.ping )
293
+ # end
294
+ #
295
+ # # Sunstone.config ===========================================================
296
+ #
297
+ # test '#config' do
298
+ # stub_request(:get, "http://testhost.com/config").to_return(:body => '{"server": "configs"}')
299
+ #
300
+ # assert_equal( {:server => "configs"}, Sunstone.config )
301
+ # end
311
302
 
312
303
  end