mongodb 0.0.13 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +3 -4
- data/lib/mongo/driver.rb +3 -3
- data/lib/mongo/driver/collection.rb +10 -22
- data/lib/mongo/driver/dynamic_finders.rb +1 -3
- data/lib/mongo/object.rb +2 -16
- data/lib/mongo/object/{object_helper.rb → collection_helper.rb} +12 -15
- data/lib/mongo/object/load.rb +22 -0
- data/lib/mongo/object/object.rb +60 -254
- data/lib/mongo/object/spec.rb +13 -3
- data/lib/mongo/object/spec/shared_object_crud.rb +124 -0
- data/lib/mongo/object/support.rb +15 -0
- data/lib/mongodb/gems.rb +1 -1
- data/spec/driver/collection_spec.rb +13 -43
- data/spec/driver/connection_spec.rb +1 -1
- data/spec/driver/database_spec.rb +1 -1
- data/spec/driver/dynamic_finders_spec.rb +4 -4
- data/spec/driver/fixes_spec.rb +2 -2
- data/spec/driver/hash_crud_spec.rb +69 -0
- data/spec/driver/spec_helper.rb +1 -3
- data/spec/migration/migration_spec.rb +4 -4
- data/spec/object/miscellaneous_spec.rb +23 -0
- data/spec/object/object_crud_spec.rb +57 -0
- data/spec/object/spec_helper.rb +1 -14
- metadata +11 -12
- data/lib/mongo/object/spec/crud_shared.rb +0 -63
- data/spec/driver/crud_spec.rb +0 -70
- data/spec/driver/hash_helper_spec.rb +0 -25
- data/spec/object/callbacks_spec.rb +0 -115
- data/spec/object/crud_spec.rb +0 -61
- data/spec/object/validation_spec.rb +0 -79
data/lib/mongo/object/spec.rb
CHANGED
@@ -1,6 +1,16 @@
|
|
1
1
|
require 'mongo/object'
|
2
2
|
require 'mongo/driver/spec'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
Mongo::Object.class_eval do
|
5
|
+
# RSpec adds some instance variables and we need to skip it.
|
6
|
+
def persistent_instance_variable_names
|
7
|
+
instance_variables.select{|n| n !~ /^@_|^@mock_proxy/}
|
8
|
+
end
|
9
|
+
|
10
|
+
class << self
|
11
|
+
# Disabling cache.
|
12
|
+
def constantize class_name
|
13
|
+
eval class_name, TOPLEVEL_BINDING, __FILE__, __LINE__
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
shared_examples_for 'single object CRUD' do
|
2
|
+
it 'should convert object to mongo hash format' do
|
3
|
+
expected = {
|
4
|
+
"_class" => "Unit",
|
5
|
+
"name" => "Zeratul",
|
6
|
+
"info" => "Dark Templar"
|
7
|
+
}
|
8
|
+
@unit.to_mongo.should == expected
|
9
|
+
|
10
|
+
@unit._id = 'some id'
|
11
|
+
expected['_id'] = 'some id'
|
12
|
+
@unit.to_mongo.should == expected
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should perform CRUD' do
|
16
|
+
# Read.
|
17
|
+
db.units.count.should == 0
|
18
|
+
db.units.all.should == []
|
19
|
+
db.units.first.should == nil
|
20
|
+
|
21
|
+
# Create.
|
22
|
+
db.units.save(@unit).should be_true
|
23
|
+
@unit._id.should_not be_nil
|
24
|
+
|
25
|
+
# Read.
|
26
|
+
db.units.count.should == 1
|
27
|
+
obj = db.units.first
|
28
|
+
obj.should == @unit
|
29
|
+
obj.class.should == @unit.class
|
30
|
+
obj.object_id.should_not == @unit.object_id
|
31
|
+
|
32
|
+
# Update.
|
33
|
+
@unit.info = 'Killer of Cerebrates'
|
34
|
+
db.units.save(@unit).should be_true
|
35
|
+
db.units.count.should == 1
|
36
|
+
db.units.first(name: 'Zeratul').info.should == 'Killer of Cerebrates'
|
37
|
+
|
38
|
+
# Delete.
|
39
|
+
db.units.delete(@unit).should be_true
|
40
|
+
db.units.count.should == 0
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
shared_examples_for 'embedded object CRUD' do
|
45
|
+
it 'should convert object to mongo hash format' do
|
46
|
+
expected = {
|
47
|
+
"_class" => "Unit",
|
48
|
+
"items" => [
|
49
|
+
{"name" => "Psionic blade", "_class" => "Unit::Item"},
|
50
|
+
{"name" => "Plasma shield", "_class" => "Unit::Item"}
|
51
|
+
]
|
52
|
+
}
|
53
|
+
@unit.to_mongo.should == expected
|
54
|
+
|
55
|
+
@unit._id = 'some id'
|
56
|
+
expected['_id'] = 'some id'
|
57
|
+
@unit.to_mongo.should == expected
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should perform CRUD' do
|
61
|
+
# Create.
|
62
|
+
db.units.save @unit
|
63
|
+
@unit._id.should_not be_nil
|
64
|
+
|
65
|
+
item = @unit.items.first
|
66
|
+
item._id.should be_nil
|
67
|
+
|
68
|
+
# Read.
|
69
|
+
db.units.count.should == 1
|
70
|
+
unit = db.units.first
|
71
|
+
unit.should == @unit
|
72
|
+
unit.object_id.should_not == @unit.object_id
|
73
|
+
|
74
|
+
item = unit.items.first
|
75
|
+
item._id.should be_nil
|
76
|
+
|
77
|
+
# Update.
|
78
|
+
@unit.items.first.name = 'Psionic blade level 3'
|
79
|
+
item = @item_class.new.tap{|o| o.name = 'Power suit'}
|
80
|
+
@unit.items << item
|
81
|
+
db.units.save @unit
|
82
|
+
db.units.count.should == 1
|
83
|
+
unit = db.units.first
|
84
|
+
unit.should == @unit
|
85
|
+
unit.object_id.should_not == @unit.object_id
|
86
|
+
|
87
|
+
# Delete.
|
88
|
+
db.units.delete @unit
|
89
|
+
db.units.count.should == 0
|
90
|
+
end
|
91
|
+
|
92
|
+
it "embedded object should have :_parent reference to the main object" do
|
93
|
+
db.units.save @unit
|
94
|
+
unit = db.units.first
|
95
|
+
unit.items.first._parent.should == unit
|
96
|
+
end
|
97
|
+
|
98
|
+
# Discarded.
|
99
|
+
# describe "id for embedded objects" do
|
100
|
+
# old = nil
|
101
|
+
# before{old = Mongo.defaults[:generate_id]}
|
102
|
+
# after{Mongo.defaults[:generate_id] = old}
|
103
|
+
#
|
104
|
+
# it "should not be generated if not specified" do
|
105
|
+
# Mongo.defaults[:generate_id] = false
|
106
|
+
#
|
107
|
+
# db.units.save @unit
|
108
|
+
# unit = db.units.first
|
109
|
+
#
|
110
|
+
# @unit.items.first._id.should be_nil
|
111
|
+
# unit.items.first._id.should be_nil
|
112
|
+
# end
|
113
|
+
#
|
114
|
+
# it "should be generated if specified" do
|
115
|
+
# Mongo.defaults[:generate_id] = true
|
116
|
+
#
|
117
|
+
# db.units.save @unit
|
118
|
+
# unit = db.units.first
|
119
|
+
#
|
120
|
+
# @unit.items.first._id.should be_present
|
121
|
+
# unit.items.first._id.should be_present
|
122
|
+
# end
|
123
|
+
# end
|
124
|
+
end
|
data/lib/mongo/object/support.rb
CHANGED
data/lib/mongodb/gems.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
gem 'mongo', '~> 1.
|
1
|
+
gem 'mongo', '~> 1.4'
|
@@ -3,80 +3,50 @@ require 'driver/spec_helper'
|
|
3
3
|
describe "Collection" do
|
4
4
|
with_mongo
|
5
5
|
|
6
|
-
it
|
6
|
+
it "should by default update all matched by criteria (not first as default in mongo)" do
|
7
7
|
db.units.save name: 'Probe', race: 'Protoss', status: 'alive'
|
8
8
|
db.units.save name: 'Zealot', race: 'Protoss', status: 'alive'
|
9
9
|
|
10
|
-
#
|
10
|
+
# Update.
|
11
11
|
db.units.update({race: 'Protoss'}, :$set => {status: 'dead'})
|
12
12
|
db.units.all.collect{|u| u['status']}.should == %w(dead dead)
|
13
13
|
|
14
|
-
#
|
15
|
-
db.units.
|
14
|
+
# Delete.
|
15
|
+
db.units.delete race: 'Protoss'
|
16
16
|
db.units.count.should == 0
|
17
17
|
end
|
18
18
|
|
19
|
-
|
20
|
-
# describe "symbolize" do
|
21
|
-
# it 'should always return symbolized hashes' do
|
22
|
-
# zeratul = {name: 'Zeratul'}
|
23
|
-
# db.units.save(zeratul).should be_mongo_id
|
24
|
-
# r = db.units.first(name: 'Zeratul')
|
25
|
-
# r[:_id].should be_mongo_id
|
26
|
-
# r['_id'].should be_nil
|
27
|
-
# r[:name].should == 'Zeratul'
|
28
|
-
# r['name'].should be_nil
|
29
|
-
# end
|
30
|
-
#
|
31
|
-
# it "should be able to disable symbolization" do
|
32
|
-
# old = Mongo.defaults[:symbolize]
|
33
|
-
# begin
|
34
|
-
# Mongo.defaults[:symbolize] = false
|
35
|
-
#
|
36
|
-
# zeratul = {name: 'Zeratul'}
|
37
|
-
# db.units.save(zeratul).should be_mongo_id
|
38
|
-
# r = db.units.first(name: 'Zeratul')
|
39
|
-
# r[:_id].should be_nil
|
40
|
-
# r['_id'].should be_mongo_id
|
41
|
-
# r[:name].should be_nil
|
42
|
-
# r['name'].should == 'Zeratul'
|
43
|
-
# ensure
|
44
|
-
# Mongo.defaults[:symbolize] = old
|
45
|
-
# end
|
46
|
-
# end
|
47
|
-
# end
|
48
|
-
|
49
|
-
it "first" do
|
19
|
+
it "should return first element of collection" do
|
50
20
|
db.units.first.should be_nil
|
51
|
-
|
52
|
-
db.units.save(
|
21
|
+
unit = {name: 'Zeratul'}
|
22
|
+
db.units.save(unit).should be_mongo_id
|
53
23
|
db.units.first(name: 'Zeratul')['name'].should == 'Zeratul'
|
54
24
|
end
|
55
25
|
|
56
|
-
it 'all' do
|
26
|
+
it 'should return all elements of collection' do
|
57
27
|
db.units.all.should == []
|
58
28
|
|
59
|
-
|
60
|
-
db.units.save(
|
29
|
+
unit = {name: 'Zeratul'}
|
30
|
+
db.units.save(unit).should be_mongo_id
|
61
31
|
|
62
32
|
list = db.units.all(name: 'Zeratul')
|
63
33
|
list.size.should == 1
|
64
34
|
list.first['name'].should == 'Zeratul'
|
65
35
|
|
66
|
-
#
|
36
|
+
# With block.
|
67
37
|
list = []; db.units.all{|o| list << o}
|
68
38
|
list.size.should == 1
|
69
39
|
list.first['name'].should == 'Zeratul'
|
70
40
|
end
|
71
41
|
|
72
|
-
it 'count' do
|
42
|
+
it 'should return count of elements in collection' do
|
73
43
|
db.units.count(name: 'Zeratul').should == 0
|
74
44
|
db.units.save name: 'Zeratul'
|
75
45
|
db.units.save name: 'Tassadar'
|
76
46
|
db.units.count(name: 'Zeratul').should == 1
|
77
47
|
end
|
78
48
|
|
79
|
-
it "underscore to dollar" do
|
49
|
+
it "should rewrite underscore symbol to dollar in query" do
|
80
50
|
db.units.save name: 'Jim', age: 34
|
81
51
|
db.units.save name: 'Zeratul', age: 600
|
82
52
|
db.units.all(age: {_lt: 100}).count.should == 1
|
@@ -3,15 +3,15 @@ require 'driver/spec_helper'
|
|
3
3
|
describe "Dynamic Finders" do
|
4
4
|
with_mongo
|
5
5
|
|
6
|
-
|
6
|
+
before_all do
|
7
7
|
class FindersStub
|
8
8
|
include Mongo::DynamicFinders
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
-
|
12
|
+
after_all{remove_constants :FindersStub}
|
13
13
|
|
14
|
-
it "
|
14
|
+
it "should parse finders" do
|
15
15
|
[
|
16
16
|
[:first_by_name, 'Jim'], [:first, {name: 'Jim'}],
|
17
17
|
[:first_by_name!, 'Jim'], [:first!, {name: 'Jim'}],
|
@@ -42,7 +42,7 @@ describe "Dynamic Finders" do
|
|
42
42
|
-> {stub.all_by_name!('Jim')}.should raise_error(/can't use bang/)
|
43
43
|
end
|
44
44
|
|
45
|
-
it '
|
45
|
+
it 'should work with collection' do
|
46
46
|
db.units.first_by_name('Jim').should be_nil
|
47
47
|
db.units.save name: 'Jim'
|
48
48
|
db.units.first_by_name('Jim')['name'].should == 'Jim'
|
data/spec/driver/fixes_spec.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
require 'driver/spec_helper'
|
2
2
|
|
3
|
-
describe "
|
3
|
+
describe "Fixes" do
|
4
4
|
with_mongo
|
5
5
|
|
6
6
|
it "should always return array if input is array" do
|
7
7
|
db.units.insert([{name: 'Zeratul'}]).class.should == Array
|
8
8
|
|
9
|
-
db.units.insert(name: 'Zeratul').
|
9
|
+
db.units.insert(name: 'Zeratul').should be_mongo_id
|
10
10
|
db.units.insert([{name: 'Zeratul'}, {name: 'Tassadar'}]).class.should == Array
|
11
11
|
end
|
12
12
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'driver/spec_helper'
|
2
|
+
|
3
|
+
describe "Hash CRUD" do
|
4
|
+
with_mongo
|
5
|
+
|
6
|
+
describe 'single hash' do
|
7
|
+
before do
|
8
|
+
@unit = {'name' => 'Zeratul', 'info' => 'Dark Templar'}
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should perform CRUD' do
|
12
|
+
# Read.
|
13
|
+
db.units.count.should == 0
|
14
|
+
db.units.all.should == []
|
15
|
+
db.units.first.should == nil
|
16
|
+
|
17
|
+
# Create.
|
18
|
+
db.units.save(@unit).should be_mongo_id
|
19
|
+
@unit['_id'].should be_mongo_id
|
20
|
+
|
21
|
+
# Read.
|
22
|
+
db.units.all.should == [@unit]
|
23
|
+
db.units.count.should == 1
|
24
|
+
db.units.first.should == @unit
|
25
|
+
|
26
|
+
# Update.
|
27
|
+
@unit['info'] = 'Killer of Cerebrates'
|
28
|
+
db.units.save @unit
|
29
|
+
db.units.count.should == 1
|
30
|
+
db.units.first(name: 'Zeratul')['info'].should == 'Killer of Cerebrates'
|
31
|
+
|
32
|
+
# Delete.
|
33
|
+
db.units.delete @unit
|
34
|
+
db.units.count.should == 0
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe 'embedded hash' do
|
39
|
+
before do
|
40
|
+
@unit = {
|
41
|
+
'items' => [
|
42
|
+
{'name' => 'Psionic blade'},
|
43
|
+
{'name' => 'Plasma shield'}
|
44
|
+
]
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'should perform CRUD' do
|
49
|
+
# Create.
|
50
|
+
db.units.save(@unit).should be_mongo_id
|
51
|
+
|
52
|
+
# Read.
|
53
|
+
db.units.count.should == 1
|
54
|
+
db.units.first.should == @unit
|
55
|
+
|
56
|
+
# Update.
|
57
|
+
@unit['items'].first['name'] = 'Psionic blade level 3'
|
58
|
+
@unit['items'].push 'name' => 'Power suit'
|
59
|
+
db.units.save(@unit).should_not be_nil
|
60
|
+
db.units.count.should == 1
|
61
|
+
db.units.first.should == @unit
|
62
|
+
db.units.first.object_id.should_not == @unit.object_id
|
63
|
+
|
64
|
+
# Delete.
|
65
|
+
db.units.delete @unit
|
66
|
+
db.units.count.should == 0
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/spec/driver/spec_helper.rb
CHANGED
@@ -12,7 +12,7 @@ describe "Migration" do
|
|
12
12
|
@migration.update(0).should be_false
|
13
13
|
end
|
14
14
|
|
15
|
-
it "
|
15
|
+
it "should provide access to database" do
|
16
16
|
@migration.add 1 do |m|
|
17
17
|
m.up do |db|
|
18
18
|
db.users.save name: 'Bob'
|
@@ -22,7 +22,7 @@ describe "Migration" do
|
|
22
22
|
db.users.count.should == 1
|
23
23
|
end
|
24
24
|
|
25
|
-
it "
|
25
|
+
it "should increase db version" do
|
26
26
|
@migration.current_version.should == 0
|
27
27
|
|
28
28
|
check = mock
|
@@ -35,7 +35,7 @@ describe "Migration" do
|
|
35
35
|
@migration.current_version.should == 1
|
36
36
|
end
|
37
37
|
|
38
|
-
it "
|
38
|
+
it "should decrease db version" do
|
39
39
|
check = mock
|
40
40
|
@migration.add 1 do |m|
|
41
41
|
m.up{check.up}
|
@@ -50,7 +50,7 @@ describe "Migration" do
|
|
50
50
|
@migration.current_version.should == 0
|
51
51
|
end
|
52
52
|
|
53
|
-
it "should
|
53
|
+
it "should migrate to the highest version if version not explicitly specified" do
|
54
54
|
@migration.add 1 do |m|
|
55
55
|
m.up{}
|
56
56
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'object/spec_helper'
|
2
|
+
|
3
|
+
describe "Miscellaneous" do
|
4
|
+
with_mongo
|
5
|
+
old = Mongo.defaults[:generate_id] = false
|
6
|
+
before do
|
7
|
+
Mongo.defaults[:generate_id] = true
|
8
|
+
class Tmp
|
9
|
+
include Mongo::Object
|
10
|
+
end
|
11
|
+
end
|
12
|
+
after do
|
13
|
+
Mongo.defaults[:generate_id] = old
|
14
|
+
remove_constants :Tmp
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should use autogenerated random string id (if specified, instead of default BSON::ObjectId)" do
|
18
|
+
o = Tmp.new
|
19
|
+
db.objects.save o
|
20
|
+
o._id.should be_a(String)
|
21
|
+
o._id.size.should == Mongo.defaults[:random_string_id_size]
|
22
|
+
end
|
23
|
+
end
|