mongodb 0.0.13 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,16 @@
1
1
  require 'mongo/object'
2
2
  require 'mongo/driver/spec'
3
3
 
4
- # RSpec adds some instance variables and we need to skip it.
5
- Mongo::Object.send :remove_const, :SKIP_IV_REGEXP
6
- Mongo::Object.send :const_set, :SKIP_IV_REGEXP, /^@_|^@mock_proxy/
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
@@ -0,0 +1,15 @@
1
+ Object.class_eval do
2
+ def to_mongo; self end
3
+ end
4
+
5
+ Array.class_eval do
6
+ def to_mongo
7
+ collect{|v| v.to_mongo}
8
+ end
9
+ end
10
+
11
+ Hash.class_eval do
12
+ def to_mongo
13
+ {}.tap{|h| each{|k, v| h[k] = v.to_mongo}}
14
+ end
15
+ end
@@ -1 +1 @@
1
- gem 'mongo', '~> 1.3'
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 'by default save must update all matched by criteria (not first as defautl in mongo)' do
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
- # update
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
- # destroy
15
- db.units.destroy race: 'Protoss'
14
+ # Delete.
15
+ db.units.delete race: 'Protoss'
16
16
  db.units.count.should == 0
17
17
  end
18
18
 
19
- # Discarded
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
- zeratul = {name: 'Zeratul'}
52
- db.units.save(zeratul).should be_mongo_id
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
- zeratul = {name: 'Zeratul'}
60
- db.units.save(zeratul).should be_mongo_id
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
- # with block
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,7 +3,7 @@ require 'driver/spec_helper'
3
3
  describe "Connection" do
4
4
  with_mongo
5
5
 
6
- it "should provide handy access to databases" do
6
+ it "should provide handy shortcuts to databases" do
7
7
  db.connection.some_db.name.should == 'some_db'
8
8
  end
9
9
  end
@@ -3,7 +3,7 @@ require 'driver/spec_helper'
3
3
  describe "Database" do
4
4
  with_mongo
5
5
 
6
- it "should provide handy access to collections" do
6
+ it "should provide handy shortcuts to collections" do
7
7
  db.some_collection.name.should == 'some_collection'
8
8
  end
9
9
  end
@@ -3,15 +3,15 @@ require 'driver/spec_helper'
3
3
  describe "Dynamic Finders" do
4
4
  with_mongo
5
5
 
6
- before :all do
6
+ before_all do
7
7
  class FindersStub
8
8
  include Mongo::DynamicFinders
9
9
  end
10
10
  end
11
11
 
12
- after(:all){remove_constants :FindersStub}
12
+ after_all{remove_constants :FindersStub}
13
13
 
14
- it "parse_finder" do
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 'integration with collection' do
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'
@@ -1,12 +1,12 @@
1
1
  require 'driver/spec_helper'
2
2
 
3
- describe "Driver fixes" do
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').class.should == BSON::ObjectId
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
@@ -10,9 +10,7 @@ require 'ruby_ext'
10
10
  require 'rspec_ext'
11
11
  require 'mongo/driver/spec'
12
12
 
13
- #
14
- # Handy spec helpers
15
- #
13
+ # Shortcuts.
16
14
  rspec do
17
15
  def db
18
16
  mongo.db
@@ -12,7 +12,7 @@ describe "Migration" do
12
12
  @migration.update(0).should be_false
13
13
  end
14
14
 
15
- it "migration should provide access to database" do
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 "increase_db_version" do
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 "decrease_db_version" do
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 automigrate to highest version" do
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