ashikawa-core 0.4.1 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,6 @@ describe Ashikawa::Core::Collection do
6
6
 
7
7
  before :each do
8
8
  @database = double()
9
- mock { Ashikawa::Core::Cursor }
10
9
  end
11
10
 
12
11
  it "should have a name" do
@@ -19,6 +18,16 @@ describe Ashikawa::Core::Collection do
19
18
  my_collection.id.should == 4588
20
19
  end
21
20
 
21
+ it "should create a query" do
22
+ collection = subject.new @database, server_response("/collections/4588")
23
+
24
+ mock Ashikawa::Core::Query
25
+ Ashikawa::Core::Query.stub(:new)
26
+ Ashikawa::Core::Query.should_receive(:new).exactly(1).times.with(collection)
27
+
28
+ collection.query
29
+ end
30
+
22
31
  describe "the status code" do
23
32
  it "should know if the collection is new born" do
24
33
  my_collection = subject.new @database, { "status" => "1" }
@@ -133,79 +142,55 @@ describe Ashikawa::Core::Collection do
133
142
  subject.name = "my_new_name"
134
143
  end
135
144
 
136
- describe "working with documents" do
137
-
138
- describe "add and get single documents" do
139
- it "should receive a document by ID" do
140
- @database.stub(:send_request).with("/document/4590/333").and_return { server_response('documents/4590-333') }
141
- @database.should_receive(:send_request).with("/document/4590/333")
142
-
143
- # Documents need to get initialized:
144
- Ashikawa::Core::Document.should_receive(:new)
145
-
146
- subject[333]
147
- end
145
+ describe "add and get single documents" do
146
+ it "should receive a document by ID" do
147
+ @database.stub(:send_request).with("/document/4590/333").and_return { server_response('documents/4590-333') }
148
+ @database.should_receive(:send_request).with("/document/4590/333")
148
149
 
149
- it "should replace a document by ID" do
150
- @database.stub(:send_request).with("/document/4590/333", put: {"name" => "The Dude"})
151
- @database.should_receive(:send_request).with("/document/4590/333", put: {"name" => "The Dude"})
150
+ # Documents need to get initialized:
151
+ Ashikawa::Core::Document.should_receive(:new)
152
152
 
153
- subject[333] = {"name" => "The Dude"}
154
- end
153
+ subject[333]
154
+ end
155
155
 
156
- it "should create a new document" do
157
- @database.stub(:send_request).with("/document?collection=4590", post: { "name" => "The Dude" }).and_return do
158
- server_response('documents/new-4590-333')
159
- end
160
- @database.stub(:send_request).with("/document/4590/333", post: { "name" => "The Dude" }).and_return { server_response('documents/4590-333') }
156
+ it "should replace a document by ID" do
157
+ @database.stub(:send_request).with("/document/4590/333", put: {"name" => "The Dude"})
158
+ @database.should_receive(:send_request).with("/document/4590/333", put: {"name" => "The Dude"})
161
159
 
162
- # Documents need to get initialized:
163
- Ashikawa::Core::Document.should_receive(:new)
160
+ subject[333] = {"name" => "The Dude"}
161
+ end
164
162
 
165
- subject.create({"name" => "The Dude"})
163
+ it "should create a new document" do
164
+ @database.stub(:send_request).with("/document?collection=4590", post: { "name" => "The Dude" }).and_return do
165
+ server_response('documents/new-4590-333')
166
166
  end
167
+ @database.stub(:send_request).with("/document/4590/333", post: { "name" => "The Dude" }).and_return { server_response('documents/4590-333') }
167
168
 
168
- it "should create a new document with `<<`" do
169
- @database.stub(:send_request).with("/document?collection=4590", post: { "name" => "The Dude" }).and_return do
170
- server_response('documents/new-4590-333')
171
- end
172
- @database.stub(:send_request).with("/document/4590/333").and_return { server_response('documents/4590-333') }
169
+ # Documents need to get initialized:
170
+ Ashikawa::Core::Document.should_receive(:new)
173
171
 
174
- # Documents need to get initialized:
175
- Ashikawa::Core::Document.should_receive(:new)
176
-
177
- subject << {"name" => "The Dude"}
178
- end
172
+ subject.create({"name" => "The Dude"})
179
173
  end
180
174
 
181
- describe "list all" do
182
- it "should list all documents" do
183
- @database.stub(:send_request).with("/simple/all", put: {"collection" => "example_1"}).and_return { server_response('simple-queries/all') }
184
- @database.should_receive(:send_request).with("/simple/all", put: {"collection" => "example_1"})
185
-
186
- # Documents need to get initialized:
187
- Ashikawa::Core::Cursor.should_receive(:new)
188
-
189
- subject.all
175
+ it "should create a new document with `<<`" do
176
+ @database.stub(:send_request).with("/document?collection=4590", post: { "name" => "The Dude" }).and_return do
177
+ server_response('documents/new-4590-333')
190
178
  end
179
+ @database.stub(:send_request).with("/document/4590/333").and_return { server_response('documents/4590-333') }
191
180
 
192
- it "should limit to a certain amount" do
193
- @database.stub(:send_request).with("/simple/all", put: {"collection" => "example_1", "limit" => 1}).and_return { server_response('simple-queries/all_skip') }
194
- @database.should_receive(:send_request).with("/simple/all", put: {"collection" => "example_1", "limit" => 1})
181
+ # Documents need to get initialized:
182
+ Ashikawa::Core::Document.should_receive(:new)
195
183
 
196
- Ashikawa::Core::Cursor.should_receive(:new)
184
+ subject << {"name" => "The Dude"}
185
+ end
197
186
 
198
- subject.all :limit => 1
187
+ it "should raise an exception if a document is not found" do
188
+ @database.stub(:send_request).with("/document/4590/333") do
189
+ raise RestClient::ResourceNotFound
199
190
  end
191
+ @database.should_receive(:send_request).with("/document/4590/333")
200
192
 
201
- it "should skip documents" do
202
- @database.stub(:send_request).with("/simple/all", put: {"collection" => "example_1", "skip" => 1}).and_return { server_response('simple-queries/all_limit') }
203
- @database.should_receive(:send_request).with("/simple/all", put: {"collection" => "example_1", "skip" => 1})
204
-
205
- Ashikawa::Core::Cursor.should_receive(:new)
206
-
207
- subject.all :skip => 1
208
- end
193
+ expect { subject[333] }.to raise_error(Ashikawa::Core::DocumentNotFoundException)
209
194
  end
210
195
 
211
196
  describe "indices" do
@@ -245,91 +230,6 @@ describe Ashikawa::Core::Collection do
245
230
  indices.length.should == 1
246
231
  end
247
232
  end
248
-
249
- describe "by example" do
250
- before(:each) do
251
- @example = { :hello => "world" }
252
- end
253
-
254
- it "should find documents by example" do
255
- @database.stub(:send_request).with("/simple/by-example", put:
256
- {"collection" => "example_1", "example" => { :hello => "world"}}
257
- ).and_return { server_response('simple-queries/example') }
258
- @database.should_receive(:send_request).with("/simple/by-example", put:
259
- {"collection" => "example_1", "example" => { :hello => "world"}})
260
-
261
- Ashikawa::Core::Cursor.should_receive(:new)
262
-
263
- subject.by_example example: @example
264
- end
265
-
266
- it "should find one document by example" do
267
- @database.stub(:send_request).with("/simple/first-example", put:
268
- {"collection" => "example_1", "example" => { :hello => "world"}}
269
- ).and_return { server_response('simple-queries/example') }
270
- @database.should_receive(:send_request).with("/simple/first-example", put:
271
- {"collection" => "example_1", "example" => { :hello => "world"}})
272
-
273
- Ashikawa::Core::Document.should_receive(:new)
274
-
275
- subject.first_example @example
276
- end
277
-
278
- it "should skip documents" do
279
- @database.stub(:send_request).with("/simple/by-example", put:
280
- {"collection" => "example_1", "skip" => 1, "example" => { :hello => "world"}}
281
- ).and_return { server_response('simple-queries/example') }
282
- @database.should_receive(:send_request).with("/simple/by-example", put:
283
- {"collection" => "example_1", "skip" => 1, "example" => { :hello => "world"}})
284
-
285
- Ashikawa::Core::Cursor.should_receive(:new)
286
-
287
- subject.by_example example: @example, :skip => 1
288
- end
289
-
290
- it "should limit documents" do
291
- @database.stub(:send_request).with("/simple/by-example", put: {"collection" => "example_1", "limit" => 2, "example" => { :hello => "world"}}).and_return { server_response('simple-queries/example') }
292
- @database.should_receive(:send_request).with("/simple/by-example", put: {"collection" => "example_1", "limit" => 2, "example" => { :hello => "world"}})
293
-
294
- Ashikawa::Core::Cursor.should_receive(:new)
295
-
296
- subject.by_example example: @example, :limit => 2
297
- end
298
- end
299
-
300
- describe "near" do
301
- it "should look for documents based on latitude/longitude" do
302
- @database.stub(:send_request).with("/simple/near", put: { "collection" => "example_1", "latitude" => 0, "longitude" => 0 }).and_return { server_response('simple-queries/near') }
303
- @database.should_receive(:send_request).with("/simple/near", put: { "collection" => "example_1", "latitude" => 0, "longitude" => 0 })
304
-
305
- Ashikawa::Core::Cursor.should_receive(:new)
306
-
307
- subject.near :latitude => 0, :longitude => 0
308
- end
309
- end
310
-
311
- describe "within" do
312
- it "should look for documents within a certain radius" do
313
- @database.stub(:send_request).with("/simple/within", put: { "collection" => "example_1", "latitude" => 0, "longitude" => 0, "radius" => 2 }).and_return { server_response('simple-queries/within') }
314
- @database.should_receive(:send_request).with("/simple/within" , put: { "collection" => "example_1", "latitude" => 0, "longitude" => 0, "radius" => 2 })
315
-
316
- Ashikawa::Core::Cursor.should_receive(:new)
317
-
318
- subject.within :latitude => 0, :longitude => 0, :radius => 2
319
- end
320
- end
321
-
322
- describe "in range" do
323
- it "should look for documents with an attribute within a certain range" do
324
- arguments = { "collection" => "example_1", "attribute" => "age", "left" => 50, "right" => 60, "closed" => false}
325
- @database.stub(:send_request).with("/simple/range", put: arguments).and_return { server_response('simple-queries/range') }
326
- @database.should_receive(:send_request).with("/simple/range" , put: arguments)
327
-
328
- Ashikawa::Core::Cursor.should_receive(:new)
329
-
330
- subject.in_range attribute: "age", left: 50, right: 60, closed: false
331
- end
332
- end
333
233
  end
334
234
  end
335
235
  end
@@ -27,6 +27,16 @@ describe Ashikawa::Core::Database do
27
27
  database = subject.new "http://localhost:8529"
28
28
  end
29
29
 
30
+ it "should create a query" do
31
+ database = subject.new @connection
32
+
33
+ mock Ashikawa::Core::Query
34
+ Ashikawa::Core::Query.stub(:new)
35
+ Ashikawa::Core::Query.should_receive(:new).exactly(1).times.with(database)
36
+
37
+ database.query
38
+ end
39
+
30
40
  describe "initialized database" do
31
41
  subject { Ashikawa::Core::Database.new @connection }
32
42
 
@@ -76,19 +86,5 @@ describe Ashikawa::Core::Database do
76
86
 
77
87
  subject.send_request "/my/path", post: { data: "mydata" }
78
88
  end
79
-
80
- describe "handling queries" do
81
- it "should send a query to the server" do
82
- @connection.stub(:send_request).and_return { server_response("cursor/query") }
83
- @connection.should_receive(:send_request).with("/cursor", post: {
84
- query: "FOR u IN users LIMIT 2 RETURN u",
85
- count: true,
86
- batchSize: 2
87
- })
88
- Ashikawa::Core::Cursor.should_receive(:new).with(subject, server_response("cursor/query"))
89
-
90
- subject.query "FOR u IN users LIMIT 2 RETURN u", count: true, batch_size: 2
91
- end
92
- end
93
89
  end
94
90
  end
@@ -0,0 +1,14 @@
1
+ require "ashikawa-core/exceptions/document_not_found"
2
+ require "ashikawa-core/exceptions/no_collection_provided"
3
+
4
+ describe Ashikawa::Core::DocumentNotFoundException do
5
+ it "should have a good explanation" do
6
+ subject.to_s.should include "does not exist"
7
+ end
8
+ end
9
+
10
+ describe Ashikawa::Core::NoCollectionProvidedException do
11
+ it "should have a good explanation" do
12
+ subject.to_s.should include "without a collection"
13
+ end
14
+ end
@@ -0,0 +1,215 @@
1
+ require 'unit/spec_helper'
2
+ require 'ashikawa-core/query'
3
+
4
+ describe Ashikawa::Core::Query do
5
+ let(:collection) { double }
6
+ let(:database) { double }
7
+
8
+ describe "initialized with collection" do
9
+ subject { Ashikawa::Core::Query.new collection }
10
+
11
+ before do
12
+ collection.stub(:name).and_return "example_1"
13
+ collection.stub(:database).and_return double
14
+ end
15
+
16
+ describe "get all" do
17
+ it "should list all documents" do
18
+ collection.stub(:send_request).and_return { server_response('simple-queries/all') }
19
+ collection.should_receive(:send_request).with("/simple/all", put: {"collection" => "example_1"})
20
+
21
+ Ashikawa::Core::Cursor.should_receive(:new)
22
+
23
+ subject.all
24
+ end
25
+
26
+ it "should be able to limit the number of documents" do
27
+ collection.stub(:send_request).and_return { server_response('simple-queries/all_skip') }
28
+ collection.should_receive(:send_request).with("/simple/all", put: {"collection" => "example_1", "limit" => 1})
29
+
30
+ Ashikawa::Core::Cursor.should_receive(:new)
31
+
32
+ subject.all limit: 1
33
+ end
34
+
35
+ it "should be able to skip documents" do
36
+ collection.stub(:send_request).and_return { server_response('simple-queries/all_limit') }
37
+ collection.should_receive(:send_request).with("/simple/all", put: {"collection" => "example_1", "skip" => 1})
38
+
39
+ Ashikawa::Core::Cursor.should_receive(:new)
40
+
41
+ subject.all skip: 1
42
+ end
43
+ end
44
+
45
+ describe "first by example" do
46
+ let(:example) { {:hello => "world"} }
47
+
48
+ it "should find exactly one fitting document" do
49
+ collection.stub(:database).and_return { double }
50
+
51
+ collection.stub(:send_request).and_return { server_response('simple-queries/example') }
52
+ collection.should_receive(:send_request).with("/simple/first-example", put:
53
+ {"collection" => "example_1", "example" => { :hello => "world"}})
54
+
55
+ Ashikawa::Core::Document.should_receive(:new)
56
+
57
+ subject.first_example example
58
+ end
59
+ end
60
+
61
+ describe "all by example" do
62
+ let(:example) { {:hello => "world"} }
63
+
64
+ it "should find all fitting documents" do
65
+ collection.stub(:send_request).and_return { server_response('simple-queries/example') }
66
+ collection.should_receive(:send_request).with("/simple/by-example", put:
67
+ {"collection" => "example_1", "example" => { :hello => "world"}})
68
+
69
+ Ashikawa::Core::Cursor.should_receive(:new)
70
+
71
+ subject.by_example example
72
+ end
73
+
74
+ it "should be able to limit the number of documents" do
75
+ collection.stub(:send_request).and_return { server_response('simple-queries/example') }
76
+ collection.should_receive(:send_request).with("/simple/by-example", put: {"collection" => "example_1", "limit" => 2, "example" => { :hello => "world"}})
77
+
78
+ Ashikawa::Core::Cursor.should_receive(:new)
79
+
80
+ subject.by_example example, limit: 2
81
+ end
82
+
83
+ it "should be able to skip documents" do
84
+ collection.stub(:send_request).and_return { server_response('simple-queries/example') }
85
+ collection.should_receive(:send_request).with("/simple/by-example", put:
86
+ {"collection" => "example_1", "skip" => 1, "example" => { :hello => "world"}})
87
+
88
+ Ashikawa::Core::Cursor.should_receive(:new)
89
+
90
+ subject.by_example example, skip: 1
91
+ end
92
+ end
93
+
94
+ describe "near a geolocation" do
95
+ it "should find documents based on latitude/longitude" do
96
+ collection.stub(:send_request).and_return { server_response('simple-queries/near') }
97
+ collection.should_receive(:send_request).with("/simple/near", put: { "collection" => "example_1", "latitude" => 0, "longitude" => 0 })
98
+
99
+ Ashikawa::Core::Cursor.should_receive(:new)
100
+
101
+ subject.near latitude: 0, longitude: 0
102
+ end
103
+ end
104
+
105
+ describe "within a radious of a geolocation" do
106
+ it "should look for documents based on latidude/longitude" do
107
+ collection.stub(:send_request).and_return { server_response('simple-queries/within') }
108
+ collection.should_receive(:send_request).with("/simple/within" , put: { "collection" => "example_1", "latitude" => 0, "longitude" => 0, "radius" => 2 })
109
+
110
+ Ashikawa::Core::Cursor.should_receive(:new)
111
+
112
+ subject.within latitude: 0, longitude: 0, radius: 2
113
+ end
114
+ end
115
+
116
+ describe "in a certain range" do
117
+ it "should look for documents with an attribute in that range" do
118
+ arguments = { "collection" => "example_1", "attribute" => "age", "left" => 50, "right" => 60, "closed" => false}
119
+ collection.stub(:send_request).and_return { server_response('simple-queries/range') }
120
+ collection.should_receive(:send_request).with("/simple/range" , put: arguments)
121
+
122
+ Ashikawa::Core::Cursor.should_receive(:new)
123
+
124
+ subject.in_range attribute: "age", left: 50, right: 60, closed: false
125
+ end
126
+ end
127
+
128
+ describe "with an AQL query" do
129
+ it "should be able to execute it" do
130
+ collection.stub(:database).and_return double
131
+ collection.stub(:send_request).and_return { server_response("cursor/query") }
132
+ collection.should_receive(:send_request).with("/cursor", post: {
133
+ "query" => "FOR u IN users LIMIT 2 RETURN u",
134
+ "count" => true,
135
+ "batchSize" => 2
136
+ })
137
+ Ashikawa::Core::Cursor.should_receive(:new).with(collection.database, server_response("cursor/query"))
138
+
139
+ subject.execute "FOR u IN users LIMIT 2 RETURN u", count: true, batch_size: 2
140
+ end
141
+
142
+ it "should return true when asked if a valid query is valid" do
143
+ query = "FOR u IN users LIMIT 2 RETURN u"
144
+
145
+ collection.stub(:send_request).and_return { server_response("query/valid") }
146
+ collection.should_receive(:send_request).with("/query", post: {
147
+ "query" => query
148
+ })
149
+
150
+ subject.valid?(query).should be_true
151
+ end
152
+
153
+ it "should return false when asked if an invalid query is valid" do
154
+ query = "FOR u IN users LIMIT 2"
155
+
156
+ collection.stub(:send_request) do
157
+ raise RestClient::BadRequest
158
+ end
159
+ collection.should_receive(:send_request).with("/query", post: {
160
+ "query" => query
161
+ })
162
+
163
+ subject.valid?(query).should be_false
164
+ end
165
+ end
166
+ end
167
+
168
+ describe "initialized with database" do
169
+ subject { Ashikawa::Core::Query.new database}
170
+
171
+ it "should throw an exception when a simple query is executed" do
172
+ [:all, :by_example, :first_example, :near, :within, :in_range].each do |method|
173
+ expect { subject.send method }.to raise_error Ashikawa::Core::NoCollectionProvidedException
174
+ end
175
+ end
176
+
177
+ describe "with an AQL query" do
178
+ it "should be able to execute it" do
179
+ database.stub(:send_request).and_return { server_response("cursor/query") }
180
+ database.should_receive(:send_request).with("/cursor", post: {
181
+ "query" => "FOR u IN users LIMIT 2 RETURN u",
182
+ "count" => true,
183
+ "batchSize" => 2
184
+ })
185
+ Ashikawa::Core::Cursor.should_receive(:new).with(database, server_response("cursor/query"))
186
+
187
+ subject.execute "FOR u IN users LIMIT 2 RETURN u", count: true, batch_size: 2
188
+ end
189
+
190
+ it "should return true when asked if a valid query is valid" do
191
+ query = "FOR u IN users LIMIT 2 RETURN u"
192
+
193
+ database.stub(:send_request).and_return { server_response("query/valid") }
194
+ database.should_receive(:send_request).with("/query", post: {
195
+ "query" => query
196
+ })
197
+
198
+ subject.valid?(query).should be_true
199
+ end
200
+
201
+ it "should return false when asked if an invalid query is valid" do
202
+ query = "FOR u IN users LIMIT 2"
203
+
204
+ database.stub(:send_request) do
205
+ raise RestClient::BadRequest
206
+ end
207
+ database.should_receive(:send_request).with("/query", post: {
208
+ "query" => query
209
+ })
210
+
211
+ subject.valid?(query).should be_false
212
+ end
213
+ end
214
+ end
215
+ end
@@ -1,6 +1,12 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
3
 
4
+ require 'simplecov'
5
+ SimpleCov.start do
6
+ add_filter "spec/"
7
+ end
8
+ SimpleCov.minimum_coverage 100
9
+
4
10
  # For HTTP Testing
5
11
  require 'webmock/rspec'
6
12
  require 'json'
@@ -8,4 +14,4 @@ require 'json'
8
14
  # Helper to simulate Server Responses. Parses the fixtures in the spec folder
9
15
  def server_response(path)
10
16
  return JSON.parse(File.readlines("spec/fixtures/#{path}.json").join)
11
- end
17
+ end