ashikawa-core 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/Guardfile +18 -0
  2. data/README.md +10 -10
  3. data/Rakefile +5 -4
  4. data/ashikawa-core.gemspec +14 -6
  5. data/lib/ashikawa-core.rb +1 -0
  6. data/lib/ashikawa-core/collection.rb +189 -98
  7. data/lib/ashikawa-core/connection.rb +18 -20
  8. data/lib/ashikawa-core/cursor.rb +65 -0
  9. data/lib/ashikawa-core/database.rb +33 -46
  10. data/lib/ashikawa-core/document.rb +63 -22
  11. data/lib/ashikawa-core/exceptions/document_not_found.rb +8 -0
  12. data/lib/ashikawa-core/index.rb +47 -0
  13. data/lib/ashikawa-core/version.rb +1 -1
  14. data/spec/fixtures/cursor/26011191-2.json +19 -0
  15. data/spec/fixtures/cursor/26011191-3.json +13 -0
  16. data/spec/fixtures/cursor/26011191.json +19 -0
  17. data/spec/fixtures/cursor/query.json +18 -0
  18. data/spec/fixtures/documents/new-4590-333.json +5 -0
  19. data/spec/fixtures/indices/all.json +22 -0
  20. data/spec/fixtures/indices/hash-index.json +12 -0
  21. data/spec/fixtures/indices/new-hash-index.json +12 -0
  22. data/spec/fixtures/simple-queries/all.json +10 -4
  23. data/spec/fixtures/simple-queries/all_limit.json +9 -3
  24. data/spec/fixtures/simple-queries/all_skip.json +9 -3
  25. data/spec/fixtures/simple-queries/example.json +9 -3
  26. data/spec/fixtures/simple-queries/near.json +11 -5
  27. data/spec/fixtures/simple-queries/range.json +10 -0
  28. data/spec/fixtures/simple-queries/within.json +9 -3
  29. data/spec/integration/arango_helper.rb +27 -0
  30. data/spec/integration/basic_spec.rb +107 -28
  31. data/spec/integration/spec_helper.rb +0 -28
  32. data/spec/unit/collection_spec.rb +190 -83
  33. data/spec/unit/connection_spec.rb +17 -17
  34. data/spec/unit/cursor_spec.rb +75 -0
  35. data/spec/unit/database_spec.rb +34 -19
  36. data/spec/unit/document_spec.rb +77 -6
  37. data/spec/unit/index_spec.rb +39 -0
  38. metadata +98 -6
@@ -0,0 +1,75 @@
1
+ require 'unit/spec_helper'
2
+ require 'ashikawa-core/cursor'
3
+
4
+ describe Ashikawa::Core::Cursor do
5
+ subject { Ashikawa::Core::Cursor }
6
+
7
+ before :each do
8
+ @database = double()
9
+ mock Ashikawa::Core::Document
10
+ end
11
+
12
+ it "should create a cursor for a non-complete batch" do
13
+ my_cursor = subject.new @database, server_response("/cursor/26011191")
14
+ my_cursor.id.should == 26011191
15
+ my_cursor.length.should == 5
16
+ end
17
+
18
+ it "should create a cursor for the last batch" do
19
+ my_cursor = subject.new @database, server_response("/cursor/26011191-3")
20
+ my_cursor.id.should be_nil
21
+ my_cursor.length.should == 5
22
+ end
23
+
24
+ describe "existing cursor" do
25
+ subject { Ashikawa::Core::Cursor.new @database,
26
+ server_response("/cursor/26011191")
27
+ }
28
+
29
+ it "should iterate over all documents of a cursor" do
30
+ first = true
31
+
32
+ @database.stub(:send_request).with("/cursor/26011191", put: {}) do
33
+ if first
34
+ first = false
35
+ server_response("/cursor/26011191-2")
36
+ else
37
+ server_response("/cursor/26011191-3")
38
+ end
39
+ end
40
+ @database.should_receive(:send_request).twice
41
+
42
+ Ashikawa::Core::Document.stub(:new)
43
+ Ashikawa::Core::Document.should_receive(:new).exactly(5).times
44
+
45
+ subject.each { |document| }
46
+ end
47
+
48
+ it "should be deletable" do
49
+ @database.stub(:send_request)
50
+ @database.should_receive(:send_request).with("/cursor/26011191",
51
+ delete: {})
52
+
53
+ subject.delete
54
+ end
55
+
56
+ it "should be enumerable" do
57
+ first = true
58
+
59
+ @database.stub(:send_request).with("/cursor/26011191", put: {}) do
60
+ if first
61
+ first = false
62
+ server_response("/cursor/26011191-2")
63
+ else
64
+ server_response("/cursor/26011191-3")
65
+ end
66
+ end
67
+ @database.should_receive(:send_request).twice
68
+
69
+ Ashikawa::Core::Document.stub(:new).and_return { 1 }
70
+ Ashikawa::Core::Document.should_receive(:new).exactly(5).times
71
+
72
+ subject.map{|i| i}[0].should == 1
73
+ end
74
+ end
75
+ end
@@ -3,51 +3,52 @@ require 'ashikawa-core/database'
3
3
 
4
4
  describe Ashikawa::Core::Database do
5
5
  subject { Ashikawa::Core::Database }
6
-
6
+
7
7
  before :each do
8
8
  mock(Ashikawa::Core::Connection)
9
9
  mock(Ashikawa::Core::Collection)
10
+ mock(Ashikawa::Core::Cursor)
10
11
  @connection = double()
11
12
  end
12
-
13
+
13
14
  it "should initialize with a connection" do
14
15
  @connection.stub(:ip) { "http://localhost" }
15
16
  @connection.stub(:port) { 8529 }
16
-
17
+
17
18
  database = subject.new @connection
18
19
  database.ip.should == "http://localhost"
19
20
  database.port.should == 8529
20
21
  end
21
-
22
+
22
23
  it "should initialize with a connection string" do
23
24
  Ashikawa::Core::Connection.stub(:new).with("http://localhost:8529").and_return(double())
24
25
  Ashikawa::Core::Connection.should_receive(:new).with("http://localhost:8529")
25
-
26
+
26
27
  database = subject.new "http://localhost:8529"
27
28
  end
28
-
29
+
29
30
  describe "initialized database" do
30
31
  subject { Ashikawa::Core::Database.new @connection }
31
-
32
+
32
33
  it "should fetch all available collections" do
33
34
  @connection.stub(:send_request) {|path| server_response("collections/all") }
34
35
  @connection.should_receive(:send_request).with("/collection")
35
-
36
+
36
37
  Ashikawa::Core::Collection.should_receive(:new).with(subject, server_response("/collections/all")["collections"][0])
37
38
  Ashikawa::Core::Collection.should_receive(:new).with(subject, server_response("/collections/all")["collections"][1])
38
-
39
+
39
40
  subject.collections.length.should == 2
40
41
  end
41
-
42
+
42
43
  it "should fetch a single collection if it exists" do
43
44
  @connection.stub(:send_request) { |path| server_response("collections/4588") }
44
45
  @connection.should_receive(:send_request).with("/collection/4588")
45
-
46
+
46
47
  Ashikawa::Core::Collection.should_receive(:new).with(subject, server_response("/collections/4588"))
47
-
48
+
48
49
  subject[4588]
49
50
  end
50
-
51
+
51
52
  it "should create a single collection if it doesn't exist" do
52
53
  @connection.stub :send_request do |path, method = {}|
53
54
  if method.has_key? :post
@@ -58,16 +59,30 @@ describe Ashikawa::Core::Database do
58
59
  end
59
60
  @connection.should_receive(:send_request).with("/collection/new_collection")
60
61
  @connection.should_receive(:send_request).with("/collection", post: { name: "new_collection"} )
61
-
62
- Ashikawa::Core::Collection.should_receive(:new).with(subject, server_response("/collections/4590"))
63
-
62
+
63
+ Ashikawa::Core::Collection.should_receive(:new).with(subject, server_response("collections/4590"))
64
+
64
65
  subject['new_collection']
65
66
  end
66
-
67
+
67
68
  it "should send a request via the connection object" do
68
69
  @connection.should_receive(:send_request).with("/my/path", post: { data: "mydata" })
69
-
70
+
70
71
  subject.send_request "/my/path", post: { data: "mydata" }
71
72
  end
73
+
74
+ describe "handling queries" do
75
+ it "should send a query to the server" do
76
+ @connection.stub(:send_request).and_return { server_response("cursor/query") }
77
+ @connection.should_receive(:send_request).with("/cursor", post: {
78
+ query: "FOR u IN users LIMIT 2 RETURN u",
79
+ count: true,
80
+ batchSize: 2
81
+ })
82
+ Ashikawa::Core::Cursor.should_receive(:new).with(subject, server_response("cursor/query"))
83
+
84
+ subject.query "FOR u IN users LIMIT 2 RETURN u", count: true, batch_size: 2
85
+ end
86
+ end
72
87
  end
73
- end
88
+ end
@@ -2,12 +2,83 @@ require 'unit/spec_helper'
2
2
  require 'ashikawa-core/document'
3
3
 
4
4
  describe Ashikawa::Core::Document do
5
+ let(:database) { double() }
6
+ let(:raw_data) {
7
+ {
8
+ "_id" => "1234567/2345678",
9
+ "_rev" => "3456789",
10
+ "first_name" => "The",
11
+ "last_name" => "Dude"
12
+ }
13
+ }
14
+ let(:raw_data_without_id) {
15
+ {
16
+ "first_name" => "The",
17
+ "last_name" => "Dude"
18
+ }
19
+ }
5
20
  subject { Ashikawa::Core::Document }
6
-
7
- it "should initialize with an id and revision" do
8
- document = subject.new "189990/1631782", 1631782
9
- document.id.should == "189990/1631782"
10
- document.revision.should == 1631782
21
+
22
+ it "should initialize data with ID" do
23
+ document = subject.new database, raw_data
24
+ document.id.should == 2345678
25
+ document.revision.should == 3456789
26
+ end
27
+
28
+ it "should initialize data without ID" do
29
+ document = subject.new database, raw_data_without_id
30
+ document.id.should == nil
31
+ document.revision.should == nil
32
+ end
33
+
34
+ describe "initialized document with ID" do
35
+ subject { Ashikawa::Core::Document.new database, raw_data }
36
+
37
+ it "should return the correct value for an existing attribute" do
38
+ subject["first_name"].should be(raw_data["first_name"])
39
+ end
40
+
41
+ it "should return nil for an non-existing attribute" do
42
+ subject["no_name"].should be_nil
43
+ end
44
+
45
+ it "should be deletable" do
46
+ database.should_receive(:send_request).with("document/#{raw_data['_id']}",
47
+ { delete: {} }
48
+ )
49
+
50
+ subject.delete
51
+ end
52
+
53
+ it "should store changes to the database" do
54
+ database.should_receive(:send_request).with("document/#{raw_data['_id']}",
55
+ { put: { "first_name" => "The", "last_name" => "Other" } }
56
+ )
57
+
58
+ subject["last_name"] = "Other"
59
+ subject.save
60
+ end
61
+ end
62
+
63
+ describe "initialized document without ID" do
64
+ subject { Ashikawa::Core::Document.new database, raw_data_without_id }
65
+
66
+ it "should return the correct value for an existing attribute" do
67
+ subject["first_name"].should be(raw_data_without_id["first_name"])
68
+ end
69
+
70
+ it "should return nil for an non-existing attribute" do
71
+ subject["no_name"].should be_nil
72
+ end
73
+
74
+ it "should not be deletable" do
75
+ database.should_not_receive :send_request
76
+ expect { subject.delete }.to raise_error Ashikawa::Core::DocumentNotFoundException
77
+ end
78
+
79
+ it "should not store changes to the database" do
80
+ database.should_not_receive :send_request
81
+ expect { subject.save }.to raise_error Ashikawa::Core::DocumentNotFoundException
82
+ end
11
83
  end
12
-
13
84
  end
@@ -0,0 +1,39 @@
1
+ require 'unit/spec_helper'
2
+ require 'ashikawa-core/index'
3
+
4
+ describe Ashikawa::Core::Index do
5
+ let(:collection) { double() }
6
+ let(:raw_data) {
7
+ {
8
+ "code" => 201,
9
+ "fields" => [
10
+ "something"
11
+ ],
12
+ "id" => "167137465/168054969",
13
+ "type" => "hash",
14
+ "isNewlyCreated" => true,
15
+ "unique" => true,
16
+ "error" => false
17
+ }
18
+ }
19
+ subject { Ashikawa::Core::Index}
20
+
21
+ it "should initialize an Index" do
22
+ index = subject.new collection, raw_data
23
+ index.type.should == :hash
24
+ index.on.should == [:something]
25
+ index.unique.should == true
26
+ end
27
+
28
+ describe "initialized index" do
29
+ subject { Ashikawa::Core::Index.new collection, raw_data }
30
+
31
+ it "should be deletable" do
32
+ collection.should_receive(:send_request).with("index/167137465/168054969",
33
+ delete: {})
34
+ collection.should_receive(:id).and_return(167137465)
35
+
36
+ subject.delete
37
+ end
38
+ end
39
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ashikawa-core
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-08-21 00:00:00.000000000 Z
13
+ date: 2012-08-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rest-client
@@ -28,6 +28,22 @@ dependencies:
28
28
  - - ~>
29
29
  - !ruby/object:Gem::Version
30
30
  version: 1.6.7
31
+ - !ruby/object:Gem::Dependency
32
+ name: redcarpet
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: 2.1.1
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 2.1.1
31
47
  - !ruby/object:Gem::Dependency
32
48
  name: rake
33
49
  requirement: !ruby/object:Gem::Requirement
@@ -93,13 +109,13 @@ dependencies:
93
109
  - !ruby/object:Gem::Version
94
110
  version: 1.8.9
95
111
  - !ruby/object:Gem::Dependency
96
- name: redcarpet
112
+ name: guard
97
113
  requirement: !ruby/object:Gem::Requirement
98
114
  none: false
99
115
  requirements:
100
116
  - - ~>
101
117
  - !ruby/object:Gem::Version
102
- version: 2.1.1
118
+ version: 1.3.2
103
119
  type: :development
104
120
  prerelease: false
105
121
  version_requirements: !ruby/object:Gem::Requirement
@@ -107,7 +123,55 @@ dependencies:
107
123
  requirements:
108
124
  - - ~>
109
125
  - !ruby/object:Gem::Version
110
- version: 2.1.1
126
+ version: 1.3.2
127
+ - !ruby/object:Gem::Dependency
128
+ name: guard-rspec
129
+ requirement: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ~>
133
+ - !ruby/object:Gem::Version
134
+ version: 1.2.1
135
+ type: :development
136
+ prerelease: false
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ~>
141
+ - !ruby/object:Gem::Version
142
+ version: 1.2.1
143
+ - !ruby/object:Gem::Dependency
144
+ name: guard-bundler
145
+ requirement: !ruby/object:Gem::Requirement
146
+ none: false
147
+ requirements:
148
+ - - ~>
149
+ - !ruby/object:Gem::Version
150
+ version: 1.0.0
151
+ type: :development
152
+ prerelease: false
153
+ version_requirements: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ~>
157
+ - !ruby/object:Gem::Version
158
+ version: 1.0.0
159
+ - !ruby/object:Gem::Dependency
160
+ name: guard-yard
161
+ requirement: !ruby/object:Gem::Requirement
162
+ none: false
163
+ requirements:
164
+ - - ~>
165
+ - !ruby/object:Gem::Version
166
+ version: 2.0.0
167
+ type: :development
168
+ prerelease: false
169
+ version_requirements: !ruby/object:Gem::Requirement
170
+ none: false
171
+ requirements:
172
+ - - ~>
173
+ - !ruby/object:Gem::Version
174
+ version: 2.0.0
111
175
  description: Ashikawa Core is a Wrapper around the ArangoDB Rest API. It provides
112
176
  low level access and will be used in different ArangoDB ODMs.
113
177
  email:
@@ -121,14 +185,18 @@ files:
121
185
  - .rvmrc
122
186
  - .travis.yml
123
187
  - Gemfile
188
+ - Guardfile
124
189
  - README.md
125
190
  - Rakefile
126
191
  - ashikawa-core.gemspec
127
192
  - lib/ashikawa-core.rb
128
193
  - lib/ashikawa-core/collection.rb
129
194
  - lib/ashikawa-core/connection.rb
195
+ - lib/ashikawa-core/cursor.rb
130
196
  - lib/ashikawa-core/database.rb
131
197
  - lib/ashikawa-core/document.rb
198
+ - lib/ashikawa-core/exceptions/document_not_found.rb
199
+ - lib/ashikawa-core/index.rb
132
200
  - lib/ashikawa-core/version.rb
133
201
  - spec/fixtures/collections/4588.json
134
202
  - spec/fixtures/collections/4590-properties.json
@@ -136,21 +204,33 @@ files:
136
204
  - spec/fixtures/collections/73482-figures.json
137
205
  - spec/fixtures/collections/all.json
138
206
  - spec/fixtures/collections/not_found.json
207
+ - spec/fixtures/cursor/26011191-2.json
208
+ - spec/fixtures/cursor/26011191-3.json
209
+ - spec/fixtures/cursor/26011191.json
210
+ - spec/fixtures/cursor/query.json
139
211
  - spec/fixtures/documents/4590-333.json
212
+ - spec/fixtures/documents/new-4590-333.json
213
+ - spec/fixtures/indices/all.json
214
+ - spec/fixtures/indices/hash-index.json
215
+ - spec/fixtures/indices/new-hash-index.json
140
216
  - spec/fixtures/simple-queries/all.json
141
217
  - spec/fixtures/simple-queries/all_limit.json
142
218
  - spec/fixtures/simple-queries/all_skip.json
143
219
  - spec/fixtures/simple-queries/example.json
144
220
  - spec/fixtures/simple-queries/near.json
221
+ - spec/fixtures/simple-queries/range.json
145
222
  - spec/fixtures/simple-queries/within.json
223
+ - spec/integration/arango_helper.rb
146
224
  - spec/integration/basic_spec.rb
147
225
  - spec/integration/spec_helper.rb
148
226
  - spec/unit/collection_spec.rb
149
227
  - spec/unit/connection_spec.rb
228
+ - spec/unit/cursor_spec.rb
150
229
  - spec/unit/database_spec.rb
151
230
  - spec/unit/document_spec.rb
231
+ - spec/unit/index_spec.rb
152
232
  - spec/unit/spec_helper.rb
153
- homepage: ''
233
+ homepage: https://github.com/triAGENS/ashikawa-core
154
234
  licenses: []
155
235
  post_install_message:
156
236
  rdoc_options: []
@@ -182,18 +262,30 @@ test_files:
182
262
  - spec/fixtures/collections/73482-figures.json
183
263
  - spec/fixtures/collections/all.json
184
264
  - spec/fixtures/collections/not_found.json
265
+ - spec/fixtures/cursor/26011191-2.json
266
+ - spec/fixtures/cursor/26011191-3.json
267
+ - spec/fixtures/cursor/26011191.json
268
+ - spec/fixtures/cursor/query.json
185
269
  - spec/fixtures/documents/4590-333.json
270
+ - spec/fixtures/documents/new-4590-333.json
271
+ - spec/fixtures/indices/all.json
272
+ - spec/fixtures/indices/hash-index.json
273
+ - spec/fixtures/indices/new-hash-index.json
186
274
  - spec/fixtures/simple-queries/all.json
187
275
  - spec/fixtures/simple-queries/all_limit.json
188
276
  - spec/fixtures/simple-queries/all_skip.json
189
277
  - spec/fixtures/simple-queries/example.json
190
278
  - spec/fixtures/simple-queries/near.json
279
+ - spec/fixtures/simple-queries/range.json
191
280
  - spec/fixtures/simple-queries/within.json
281
+ - spec/integration/arango_helper.rb
192
282
  - spec/integration/basic_spec.rb
193
283
  - spec/integration/spec_helper.rb
194
284
  - spec/unit/collection_spec.rb
195
285
  - spec/unit/connection_spec.rb
286
+ - spec/unit/cursor_spec.rb
196
287
  - spec/unit/database_spec.rb
197
288
  - spec/unit/document_spec.rb
289
+ - spec/unit/index_spec.rb
198
290
  - spec/unit/spec_helper.rb
199
291
  has_rdoc: