mongo_db 0.1.5 → 0.1.6

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.
@@ -2,7 +2,7 @@ require 'driver/spec_helper'
2
2
 
3
3
  describe "Collection" do
4
4
  with_mongo
5
-
5
+
6
6
  it 'by default save must update all matched by criteria (not first as defautl 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'
@@ -10,12 +10,12 @@ describe "Collection" do
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
14
  # destroy
15
15
  db.units.destroy race: 'Protoss'
16
16
  db.units.count.should == 0
17
17
  end
18
-
18
+
19
19
  describe "symbolize" do
20
20
  it 'should always return symbolized hashes' do
21
21
  zeratul = {name: 'Zeratul'}
@@ -26,12 +26,12 @@ describe "Collection" do
26
26
  r[:name].should == 'Zeratul'
27
27
  r['name'].should be_nil
28
28
  end
29
-
29
+
30
30
  it "should be able to disable symbolization" do
31
31
  old = Mongo.defaults[:symbolize]
32
32
  begin
33
33
  Mongo.defaults[:symbolize] = false
34
-
34
+
35
35
  zeratul = {name: 'Zeratul'}
36
36
  db.units.save(zeratul).should be_mongo_id
37
37
  r = db.units.first(name: 'Zeratul')
@@ -44,24 +44,24 @@ describe "Collection" do
44
44
  end
45
45
  end
46
46
  end
47
-
47
+
48
48
  it "first" do
49
49
  db.units.first.should be_nil
50
50
  zeratul = {name: 'Zeratul'}
51
51
  db.units.save(zeratul).should be_mongo_id
52
52
  db.units.first(name: 'Zeratul')[:name].should == 'Zeratul'
53
53
  end
54
-
54
+
55
55
  it 'all' do
56
- db.units.all.should == []
57
-
56
+ db.units.all.should == []
57
+
58
58
  zeratul = {name: 'Zeratul'}
59
59
  db.units.save(zeratul).should be_mongo_id
60
-
60
+
61
61
  list = db.units.all(name: 'Zeratul')
62
62
  list.size.should == 1
63
63
  list.first[:name].should == 'Zeratul'
64
-
64
+
65
65
  # with block
66
66
  list = []; db.units.all{|o| list << o}
67
67
  list.size.should == 1
@@ -2,50 +2,50 @@ require 'driver/spec_helper'
2
2
 
3
3
  describe "Hash CRUD" do
4
4
  with_mongo
5
-
5
+
6
6
  describe 'simple' do
7
7
  before do
8
8
  @zeratul = {name: 'Zeratul', info: 'Dark Templar'}
9
9
  end
10
-
10
+
11
11
  it 'crud' do
12
12
  # read
13
13
  db.units.count.should == 0
14
14
  db.units.all.should == []
15
15
  db.units.first.should == nil
16
-
16
+
17
17
  # create
18
18
  db.units.save(@zeratul).should be_mongo_id
19
19
  @zeratul[:_id].should be_mongo_id
20
-
20
+
21
21
  # read
22
22
  db.units.all.should == [@zeratul]
23
23
  db.units.count.should == 1
24
24
  db.units.first.should == @zeratul
25
-
25
+
26
26
  # update
27
27
  @zeratul[:info] = 'Killer of Cerebrates'
28
28
  db.units.save @zeratul
29
29
  db.units.count.should == 1
30
30
  db.units.first(name: 'Zeratul')[:info].should == 'Killer of Cerebrates'
31
-
31
+
32
32
  # destroy
33
33
  db.units.destroy @zeratul
34
34
  db.units.count.should == 0
35
35
  end
36
36
  end
37
-
37
+
38
38
  describe 'embedded' do
39
- before do
39
+ before do
40
40
  @player = {
41
41
  name: 'Alex',
42
42
  missions: [
43
43
  {name: 'Wasteland', stats: {buildings: 5, units: 10}},
44
44
  {name: 'Backwater Station', stats: {buildings: 8, units: 25}}
45
45
  ]
46
- }
46
+ }
47
47
  end
48
-
48
+
49
49
  it 'crud' do
50
50
  # create
51
51
  db.players.save(@player).should be_mongo_id
@@ -66,5 +66,5 @@ describe "Hash CRUD" do
66
66
  db.players.destroy @player
67
67
  db.players.count.should == 0
68
68
  end
69
- end
69
+ end
70
70
  end
@@ -1,8 +1,13 @@
1
1
  require 'driver/spec_helper'
2
2
 
3
- describe "HashHelper" do
3
+ describe "Collection" do
4
+ before do
5
+ @helper = Object.new
6
+ @helper.send :extend, Mongo::Ext::Collection
7
+ end
8
+
4
9
  it "symbolize" do
5
- Mongo::Ext::HashHelper.symbolize({
10
+ @helper.send(:symbolize_doc, {
6
11
  'a' => 1,
7
12
  'b' => {
8
13
  'c' => 2,
@@ -1,58 +1,55 @@
1
- require 'driver/spec_helper'
1
+ require 'mongo_db/driver/core'
2
+ require 'rspec'
2
3
 
3
- describe "Example" do
4
- with_mongo
5
-
4
+ describe "Driver example" do
6
5
  defaults = nil
7
6
  before(:all){defaults = Mongo.defaults.clone}
8
7
  after(:all){Mongo.defaults = defaults}
9
-
10
- it "core" do
11
- require 'mongo_db/driver'
12
-
8
+
9
+ it "core" do
10
+ require 'mongo_db/driver/core'
11
+
13
12
  # changing some defaults
14
13
  Mongo.defaults.merge! symbolize: true, multi: true, safe: true
15
-
14
+
16
15
  # connection & db
17
16
  connection = Mongo::Connection.new
18
17
  db = connection.db 'default_test'
19
-
18
+
20
19
  # collection shortcuts
21
20
  db.some_collection
22
-
21
+
23
22
  # create
24
23
  zeratul = {name: 'Zeratul', stats: {attack: 85, life: 300, shield: 100}}
25
24
  tassadar = {name: 'Tassadar', stats: {attack: 0, life: 80, shield: 300}}
26
-
25
+
27
26
  db.units.save zeratul
28
27
  db.units.save tassadar
29
-
28
+
30
29
  # udate (we made error - mistakenly set Tassadar's attack as zero, let's fix it)
31
30
  tassadar[:stats][:attack] = 20
32
31
  db.units.save tassadar
33
-
32
+
34
33
  # querying first & all, there's also :each, the same as :all
35
34
  db.units.first name: 'Zeratul' # => zeratul
36
35
  db.units.all name: 'Zeratul' # => [zeratul]
37
- db.units.all name: 'Zeratul' do |hero|
38
- hero # => zeratul
36
+ db.units.all name: 'Zeratul' do |unit|
37
+ unit # => zeratul
39
38
  end
40
- end
41
-
42
- it "optional" do
39
+
40
+
41
+ #
42
+ # optional
43
+ #
44
+ require 'mongo_db/driver/more'
45
+
43
46
  # simple finders (bang versions also availiable)
44
47
  db.units.by_name 'Zeratul' # => zeratul
45
48
  db.units.first_by_name 'Zeratul' # => zeratul
46
49
  db.units.all_by_name 'Zeratul' # => [zeratul]
47
-
50
+
48
51
  # query sugar, use {life: {_lt: 100}} instead of {life: {:$lt => 100}}
49
- # it will affect olny small set of keywords (:_lt, :_inc),
50
- # other underscored keys will be intact.
51
- Mongo.defaults.merge! convert_underscore_to_dollar: true
52
- db.units.all life: {_lt: 100} # => [tassadar]
53
-
54
- # it's also trivial to add support for {:life.lt => 100} notion, but
55
- # it uses ugly '=>' hash notation instead of ':' and it differs from
56
- # how it looks in native MongoDB JSON query.
52
+ Mongo.defaults.merge! convert_underscore_to_dollar: true
53
+ db.units.all 'stats.life' => {_lt: 100} # => [tassadar]
57
54
  end
58
55
  end
@@ -0,0 +1,12 @@
1
+ require 'driver/spec_helper'
2
+
3
+ describe "Driver fixes" do
4
+ with_mongo
5
+
6
+ it "should always return array if input is array" do
7
+ db.units.insert([{name: 'Zeratul'}]).class.should == Array
8
+
9
+ db.units.insert(name: 'Zeratul').class.should == BSON::ObjectId
10
+ db.units.insert([{name: 'Zeratul'}, {name: 'Tassadar'}]).class.should == Array
11
+ end
12
+ end
@@ -2,23 +2,23 @@ require 'driver/spec_helper'
2
2
 
3
3
  describe "Querying" do
4
4
  with_mongo
5
-
5
+
6
6
  before do
7
7
  @jim = {name: 'Jim', age: 34}
8
8
  @units = db.units
9
9
  end
10
-
10
+
11
11
  describe "dynamic finders" do
12
12
  it "find, first, by" do
13
- @units.first_by_name('Jim').should be_nil
13
+ @units.first_by_name('Jim').should be_nil
14
14
  -> {@units.first_by_name!('Jim')}.should raise_error(Mongo::NotFound)
15
- @units.by_name('Jim').should be_nil
15
+ @units.by_name('Jim').should be_nil
16
16
  -> {@units.by_name!('Jim')}.should raise_error(Mongo::NotFound)
17
- @units.first_by_name('Jim').should be_nil
17
+ @units.first_by_name('Jim').should be_nil
18
18
  -> {@units.first_by_name!('Jim')}.should raise_error(Mongo::NotFound)
19
-
19
+
20
20
  @units.save @jim
21
-
21
+
22
22
  @units.first_by_name('Jim').should == @jim
23
23
  @units.first_by_name!('Jim').should == @jim
24
24
  @units.by_name('Jim').should == @jim
@@ -26,31 +26,31 @@ describe "Querying" do
26
26
  @units.first_by_name('Jim').should == @jim
27
27
  @units.first_by_name!('Jim').should == @jim
28
28
  end
29
-
29
+
30
30
  it "all" do
31
- @units.all_by_name('Jim').should == []
31
+ @units.all_by_name('Jim').should == []
32
32
  @units.save @jim
33
33
  @units.all_by_name('Jim').should == [@jim]
34
34
  end
35
-
35
+
36
36
  it "should allow to use bang version only with :first" do
37
37
  -> {@units.all_by_name!('Jim')}.should raise_error(/can't use bang/)
38
38
  end
39
-
39
+
40
40
  it "by_id (special case)" do
41
41
  @units.method(:by_id).should == @units.method(:first_by_id)
42
42
  @units.method(:by_id!).should == @units.method(:first_by_id!)
43
-
44
- @units.by_id('4de81858cf26bde569000009').should be_nil
43
+
44
+ @units.by_id('4de81858cf26bde569000009').should be_nil
45
45
  -> {@units.by_id!('4de81858cf26bde569000009')}.should raise_error(Mongo::NotFound)
46
-
46
+
47
47
  @units.save @jim
48
-
48
+
49
49
  @units.by_id(@jim[:_id]).should == @jim
50
50
  @units.by_id!(@jim[:_id]).should == @jim
51
51
  end
52
52
  end
53
-
53
+
54
54
  it "underscore to dollar" do
55
55
  @units.save @jim
56
56
  @units.save name: 'Zeratul', age: 600
@@ -11,9 +11,9 @@ require 'ruby_ext'
11
11
  require 'rspec_ext'
12
12
  require 'mongo_db/driver/spec'
13
13
 
14
- #
14
+ #
15
15
  # Handy spec helpers
16
- #
16
+ #
17
17
  rspec do
18
18
  def db
19
19
  mongo.db
@@ -1,16 +1,16 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'Object callbacks' do
4
- with_mongo_model
5
-
4
+ with_mongo
5
+
6
6
  before do
7
7
  class Player
8
8
  attr_accessor :missions
9
-
9
+
10
10
  class Mission
11
- end
11
+ end
12
12
  end
13
-
13
+
14
14
  @mission = Player::Mission.new
15
15
  @player = Player.new
16
16
  @player.missions = [@mission]
@@ -29,13 +29,13 @@ describe 'Object callbacks' do
29
29
  @player.should_receive(name).ordered
30
30
  @misson.should_receive(name).ordered
31
31
  end
32
-
32
+
33
33
  db.players.save @player
34
34
  end
35
-
35
+
36
36
  it 'update' do
37
37
  db.players.save @player
38
-
38
+
39
39
  [
40
40
  :before_validation,
41
41
  :validate,
@@ -50,10 +50,10 @@ describe 'Object callbacks' do
50
50
  end
51
51
  db.players.save @player
52
52
  end
53
-
53
+
54
54
  it 'destroy' do
55
55
  db.players.save @player
56
-
56
+
57
57
  [
58
58
  :before_validation,
59
59
  :validate,
@@ -66,13 +66,13 @@ describe 'Object callbacks' do
66
66
  end
67
67
  db.players.destroy @player
68
68
  end
69
-
69
+
70
70
  it 'should be able interrupt CRUD' do
71
71
  @misson.stub!(:before_save).and_return(false)
72
72
  db.players.save(@player).should be_false
73
73
  db.players.count.should == 0
74
74
  end
75
-
75
+
76
76
  it 'should be able skip callbacks' do
77
77
  [
78
78
  :before_validation,
@@ -89,7 +89,7 @@ describe 'Object callbacks' do
89
89
  @player.should_not_receive(name)
90
90
  @misson.should_receive(name)
91
91
  end
92
-
92
+
93
93
  db.players.save @player, callbacks: false
94
94
  db.players.count.should == 1
95
95
  db.players.save @player, callbacks: false
File without changes
@@ -2,25 +2,25 @@ require 'spec_helper'
2
2
  require 'object/crud_shared'
3
3
 
4
4
  describe "Model CRUD" do
5
- with_mongo_model
6
-
5
+ with_mongo
6
+
7
7
  describe 'simple' do
8
8
  before do
9
9
  class Person
10
10
  inherit Mongo::Model
11
11
  collection{db.units}
12
-
13
- def initialize name, info; @name, @info = name, info end
12
+
13
+ def initialize name = nil, info = nil; @name, @info = name, info end
14
14
  attr_accessor :name, :info
15
15
  def == o; [self.class, name, info] == [o.class, o.respond_to(:name), o.respond_to(:info)] end
16
16
  end
17
-
18
- @zeratul = User.new 'Zeratul', 'Dark Templar'
17
+
18
+ @zeratul = Person.new 'Zeratul', 'Dark Templar'
19
19
  end
20
20
  after{remove_constants :Person}
21
-
21
+
22
22
  it_should_behave_like "object CRUD"
23
-
23
+
24
24
  it 'model crud' do
25
25
  # read
26
26
  Person.count.should == 0
@@ -29,14 +29,14 @@ describe "Model CRUD" do
29
29
 
30
30
  # create
31
31
  @zeratul.save.should be_true
32
- @zeratul._id.should be_present
32
+ @zeratul._id.should_not be_nil
33
33
 
34
34
  # read
35
35
  Person.count.should == 1
36
36
  Person.all.should == [@zeratul]
37
37
  Person.first.should == @zeratul
38
38
  Person.first.object_id.should_not == @zeratul.object_id
39
-
39
+
40
40
  # update
41
41
  @zeratul.info = 'Killer of Cerebrates'
42
42
  @zeratul.save.should be_true
@@ -47,18 +47,18 @@ describe "Model CRUD" do
47
47
  @zeratul.destroy.should be_true
48
48
  Person.count.should == 0
49
49
  end
50
-
50
+
51
51
  it 'should be able to save to another collection' do
52
52
  # create
53
53
  @zeratul.save(collection: db.protosses).should be_true
54
- @zeratul._id.should be_present
54
+ @zeratul._id.should_not be_nil
55
55
 
56
56
  # read
57
57
  Person.count.should == 0
58
58
  db.protosses.count.should == 1
59
59
  db.protosses.should == @zeratul
60
60
  db.protosses.object_id.should_not == @zeratul.object_id
61
-
61
+
62
62
  # update
63
63
  @zeratul.info = 'Killer of Cerebrates'
64
64
  @zeratul.save(collection: db.protosses).should be_true
@@ -71,22 +71,22 @@ describe "Model CRUD" do
71
71
  db.protosses.count.should == 0
72
72
  end
73
73
  end
74
-
74
+
75
75
  describe 'embedded' do
76
- before do
76
+ before do
77
77
  class Player
78
78
  inherit Mongo::Model
79
79
  attr_accessor :missions
80
- def == o; [self.class, self.missions] = [o.class, o.respond_to(:missions)] end
81
-
80
+ def == o; [self.class, self.missions] == [o.class, o.respond_to(:missions)] end
81
+
82
82
  class Mission
83
83
  inherit Mongo::Model
84
- def initialize name, stats; @name, @stats = name, stats end
84
+ def initialize name = nil, stats = nil; @name, @stats = name, stats end
85
85
  attr_accessor :name, :stats
86
- def == o; [self.class, self.name, self.stats] = [o.class, o.respond_to(:name), o.respond_to(:stats)] end
87
- end
86
+ def == o; [self.class, self.name, self.stats] == [o.class, o.respond_to(:name), o.respond_to(:stats)] end
87
+ end
88
88
  end
89
-
89
+
90
90
  @player = Player.new
91
91
  @player.missions = [
92
92
  Player::Mission.new('Wasteland', {buildings: 5, units: 10}),
@@ -94,13 +94,13 @@ describe "Model CRUD" do
94
94
  ]
95
95
  end
96
96
  after{remove_constants :Player}
97
-
97
+
98
98
  it_should_behave_like 'shared object CRUD'
99
-
99
+
100
100
  it 'crud' do
101
101
  # create
102
102
  @player.save.should be_true
103
- @player._id.should be_present
103
+ @player._id.should_not be_nil
104
104
 
105
105
  # read
106
106
  Player.count.should == 1
@@ -119,5 +119,5 @@ describe "Model CRUD" do
119
119
  @player.destroy.should be_true
120
120
  Player.count.should == 0
121
121
  end
122
- end
122
+ end
123
123
  end
@@ -1,16 +1,16 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'Object validation' do
4
- with_mongo_model
5
-
4
+ with_mongo
5
+
6
6
  before do
7
7
  class Player
8
8
  attr_accessor :missions
9
-
9
+
10
10
  class Mission
11
- end
11
+ end
12
12
  end
13
-
13
+
14
14
  @mission = Player::Mission.new
15
15
  @player = Player.new
16
16
  @player.missions = [@mission]
@@ -21,52 +21,52 @@ describe 'Object validation' do
21
21
  # create
22
22
  @player.stub!(:valid?).and_return(false)
23
23
  db.players.save(@player).should be_false
24
-
24
+
25
25
  @player.stub!(:valid?).and_return(true)
26
26
  db.players.save(@player).should be_true
27
-
27
+
28
28
  # update
29
29
  @player.stub!(:valid?).and_return(false)
30
30
  db.players.save(@player).should be_false
31
-
31
+
32
32
  @player.stub!(:valid?).and_return(true)
33
33
  db.players.save(@player).should be_true
34
-
34
+
35
35
  # destroy
36
36
  @player.stub!(:valid?).and_return(false)
37
37
  db.players.destroy(@player).should be_false
38
-
38
+
39
39
  @player.stub!(:valid?).and_return(true)
40
40
  db.players.destroy(@player).should be_true
41
41
  end
42
-
42
+
43
43
  it 'should not save/update/destroy invalid embedded objects' do
44
44
  # create
45
45
  @mission.stub!(:valid?).and_return(false)
46
46
  db.players.save(@player).should be_false
47
-
47
+
48
48
  @mission.stub!(:valid?).and_return(true)
49
49
  db.players.save(@player).should be_true
50
-
50
+
51
51
  # update
52
52
  @mission.stub!(:valid?).and_return(false)
53
53
  db.players.save(@player).should be_false
54
-
54
+
55
55
  @mission.stub!(:valid?).and_return(true)
56
56
  db.players.save(@player).should be_true
57
-
57
+
58
58
  # destroy
59
59
  @mission.stub!(:valid?).and_return(false)
60
60
  db.players.destroy(@player).should be_false
61
-
61
+
62
62
  @mission.stub!(:valid?).and_return(true)
63
63
  db.players.destroy(@player).should be_true
64
64
  end
65
-
65
+
66
66
  it "should be able skip validation" do
67
67
  @player.stub!(:valid?).and_return(false)
68
68
  db.players.save(@player, validate: false).should be_true
69
-
69
+
70
70
  @player.stub!(:valid?).and_return(true)
71
71
  @mission.stub!(:valid?).and_return(false)
72
72
  db.players.save(@player, validate: false).should be_true
@@ -1,4 +1,4 @@
1
- shared_examples_for 'object CRUD' do
1
+ shared_examples_for 'object CRUD' do
2
2
  it 'crud' do
3
3
  # read
4
4
  db.units.count.should == 0
@@ -7,31 +7,31 @@ shared_examples_for 'object CRUD' do
7
7
 
8
8
  # create
9
9
  db.units.save(@zeratul).should be_true
10
- @zeratul.instance_variable_get(:@_id).should be_present
11
-
10
+ @zeratul.instance_variable_get(:@_id).should_not be_nil
11
+
12
12
  # read
13
13
  db.units.count.should == 1
14
14
  db.units.all.should == [@zeratul]
15
15
  db.units.first.should == @zeratul
16
16
  db.units.first.object_id.should_not == @zeratul.object_id
17
-
17
+
18
18
  # update
19
19
  @zeratul.info = 'Killer of Cerebrates'
20
20
  db.units.save(@zeratul).should be_true
21
21
  db.units.count.should == 1
22
22
  db.units.first(name: 'Zeratul').info.should == 'Killer of Cerebrates'
23
-
23
+
24
24
  # destroy
25
25
  db.units.destroy(@zeratul).should be_true
26
26
  db.units.count.should == 0
27
27
  end
28
28
  end
29
29
 
30
- shared_examples_for 'embedded object CRUD' do
30
+ shared_examples_for 'embedded object CRUD' do
31
31
  it 'crud' do
32
32
  # create
33
33
  db.players.save(@player)
34
- @player.instance_variable_get(:@_id).should be_present
34
+ @player.instance_variable_get(:@_id).should_not be_nil
35
35
 
36
36
  # read
37
37
  db.players.count.should == 1
@@ -40,7 +40,7 @@ shared_examples_for 'embedded object CRUD' do
40
40
 
41
41
  # update
42
42
  @player.missions.first.stats[:units] = 9
43
- @player.missions << Player::Mission.new('Desperate Alliance', {buildings: 11, units: 40}),
43
+ @player.missions << Player::Mission.new('Desperate Alliance', {buildings: 11, units: 40})
44
44
  db.players.save @player
45
45
  db.players.count.should == 1
46
46
  db.players.first.should == @player