mongodb_model 0.2.8 → 2.0.0

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.
data/spec/crud_spec.rb CHANGED
@@ -1,83 +1,81 @@
1
1
  require 'spec_helper'
2
- require 'mongo/object/spec/crud_shared'
2
+ require 'mongo/object/spec/shared_object_crud'
3
3
 
4
4
  describe "Model CRUD" do
5
5
  with_mongo_model
6
6
 
7
- describe 'simple' do
8
- before :all do
7
+ describe 'single model' do
8
+ it_should_behave_like "single object CRUD"
9
+
10
+ before do
9
11
  class Unit
10
12
  inherit Mongo::Model
11
13
  collection :units
12
14
 
13
15
  attr_accessor :name, :info
14
- def == o; [self.class, name, info] == [o.class, o.respond_to(:name), o.respond_to(:info)] end
16
+ def == o; [self.class, name, info] == [o.class, o.name, o.info] end
15
17
  end
16
- end
17
- after(:all){remove_constants :Unit}
18
18
 
19
- before do
20
- @zeratul = Unit.build name: 'Zeratul', info: 'Dark Templar'
19
+ @unit = Unit.build name: 'Zeratul', info: 'Dark Templar'
21
20
  end
21
+ after{remove_constants :Unit}
22
22
 
23
- it_should_behave_like "object CRUD"
24
-
25
- it 'model crud' do
26
- # read
23
+ it 'should perform CRUD' do
24
+ # Read.
27
25
  Unit.count.should == 0
28
26
  Unit.all.should == []
29
27
  Unit.first.should == nil
30
28
 
31
- # create
32
- @zeratul.save.should be_true
33
- @zeratul._id.should_not be_nil
29
+ # Create.
30
+ @unit.save.should be_true
31
+ @unit._id.should_not be_nil
34
32
 
35
- # read
33
+ # Read.
36
34
  Unit.count.should == 1
37
- Unit.all.should == [@zeratul]
38
- Unit.first.should == @zeratul
39
- Unit.first.object_id.should_not == @zeratul.object_id
35
+ Unit.all.should == [@unit]
36
+ Unit.first.should == @unit
37
+ Unit.first.object_id.should_not == @unit.object_id
40
38
 
41
- # update
42
- @zeratul.info = 'Killer of Cerebrates'
43
- @zeratul.save.should be_true
39
+ # Update.
40
+ @unit.info = 'Killer of Cerebrates'
41
+ @unit.save.should be_true
44
42
  Unit.count.should == 1
45
43
  Unit.first(name: 'Zeratul').info.should == 'Killer of Cerebrates'
46
44
 
47
- # destroy
48
- @zeratul.destroy.should be_true
45
+ # Delete.
46
+ @unit.delete.should be_true
49
47
  Unit.count.should == 0
50
48
  end
51
49
 
52
- it 'should be able to save to another collection' do
53
- # create
54
- @zeratul.save(collection: db.heroes).should be_true
55
- @zeratul._id.should_not be_nil
50
+ it 'should be able to save model to another collection' do
51
+ # Create.
52
+ @unit.save(collection: db.heroes).should be_true
53
+ @unit._id.should_not be_nil
56
54
 
57
- # read
55
+ # Read.
58
56
  Unit.count.should == 0
59
57
  db.heroes.count.should == 1
60
- db.heroes.first.should == @zeratul
61
- db.heroes.first.object_id.should_not == @zeratul.object_id
58
+ db.heroes.first.should == @unit
59
+ db.heroes.first.object_id.should_not == @unit.object_id
62
60
 
63
- # update
64
- @zeratul.info = 'Killer of Cerebrates'
65
- @zeratul.save(collection: db.heroes).should be_true
61
+ # Update.
62
+ @unit.info = 'Killer of Cerebrates'
63
+ @unit.save(collection: db.heroes).should be_true
66
64
  Unit.count.should == 0
67
65
  db.heroes.count.should == 1
68
66
  db.heroes.first(name: 'Zeratul').info.should == 'Killer of Cerebrates'
69
67
 
70
- # destroy
71
- @zeratul.destroy(collection: db.heroes).should be_true
68
+ # Delete.
69
+ @unit.delete(collection: db.heroes).should be_true
72
70
  db.heroes.count.should == 0
73
71
  end
74
72
 
75
- it 'build' do
73
+ it 'should build model' do
76
74
  u = Unit.build name: 'Zeratul'
77
75
  u.name.should == 'Zeratul'
78
76
  end
79
77
 
80
- it 'create' do
78
+ it 'should create model' do
81
79
  u = Unit.create(name: 'Zeratul')
82
80
  u.new_record?.should be_false
83
81
 
@@ -85,16 +83,16 @@ describe "Model CRUD" do
85
83
  u.new_record?.should be_false
86
84
  end
87
85
 
88
- it 'destroy_all' do
86
+ it 'should delete all models' do
89
87
  Unit.create(name: 'Zeratul')
90
88
  Unit.count.should == 1
91
- Unit.destroy_all
89
+ Unit.delete_all
92
90
  Unit.count.should == 0
93
91
 
94
- Unit.destroy_all!
92
+ Unit.delete_all!
95
93
  end
96
94
 
97
- it 'modifiers' do
95
+ it 'should accept modifiers' do
98
96
  unit = Unit.create! name: 'Zeratul'
99
97
 
100
98
  Unit.update({_id: unit._id}, _set: {name: 'Tassadar'})
@@ -107,57 +105,57 @@ describe "Model CRUD" do
107
105
  end
108
106
  end
109
107
 
110
- describe 'embedded' do
111
- before :all do
112
- class Player
108
+ describe 'embedded model' do
109
+ it_should_behave_like 'embedded object CRUD'
110
+
111
+ before do
112
+ class Unit
113
113
  inherit Mongo::Model
114
- collection :players
114
+ collection :units
115
115
 
116
- attr_accessor :missions
117
- def == o; [self.class, self.missions] == [o.class, o.respond_to(:missions)] end
116
+ attr_accessor :items
117
+ def == o; [self.class, self.items] == [o.class, o.items] end
118
118
 
119
- class Mission
119
+ class Item
120
120
  inherit Mongo::Model
121
121
 
122
- attr_accessor :name, :stats
123
- def == o; [self.class, self.name, self.stats] == [o.class, o.respond_to(:name), o.respond_to(:stats)] end
122
+ attr_accessor :name
123
+ def == o; [self.class, self.name] == [o.class, o.name] end
124
124
  end
125
125
  end
126
- end
127
- after(:all){remove_constants :Player}
128
126
 
129
- before do
130
- @mission_class = Player::Mission
131
- @player = Player.new
132
- @player.missions = [
133
- Player::Mission.build(name: 'Wasteland', stats: {'buildings' => 5, 'units' => 10}),
134
- Player::Mission.build(name: 'Backwater Station', stats: {'buildings' => 8, 'units' => 25}),
127
+ @item_class = Unit::Item
128
+ @unit = Unit.new
129
+ @unit.items = [
130
+ Unit::Item.build(name: 'Psionic blade'),
131
+ Unit::Item.build(name: 'Plasma shield'),
135
132
  ]
136
133
  end
134
+ after{remove_constants :Unit}
137
135
 
138
- it_should_behave_like 'embedded object CRUD'
136
+ it 'should perform CRUD' do
137
+ # Create.
138
+ @unit.save.should be_true
139
+ @unit._id.should_not be_nil
139
140
 
140
- it 'crud' do
141
- # create
142
- @player.save.should be_true
143
- @player._id.should_not be_nil
144
-
145
- # read
146
- Player.count.should == 1
147
- Player.first.should == @player
148
- Player.first.object_id.should_not == @players.object_id
149
-
150
- # update
151
- @player.missions.first.stats['units'] = 9
152
- @player.missions << Player::Mission.build(name: 'Desperate Alliance', stats: {'buildings' => 11, 'units' => 40})
153
- @player.save.should be_true
154
- Player.count.should == 1
155
- Player.first.should == @player
156
- Player.first.object_id.should_not == @player.object_id
157
-
158
- # destroy
159
- @player.destroy.should be_true
160
- Player.count.should == 0
141
+ # Read.
142
+ Unit.count.should == 1
143
+ unit = Unit.first
144
+ unit.should == @unit
145
+ unit.object_id.should_not == @unit.object_id
146
+
147
+ # Update.
148
+ @unit.items.first.name = "Psionic blade level 3"
149
+ @unit.items << Unit::Item.build(name: 'Power suit')
150
+ @unit.save.should be_true
151
+ Unit.count.should == 1
152
+ unit = Unit.first
153
+ unit.should == @unit
154
+ unit.object_id.should_not == @unit.object_id
155
+
156
+ # Delete.
157
+ @unit.delete.should be_true
158
+ Unit.count.should == 0
161
159
  end
162
160
  end
163
161
  end
@@ -3,34 +3,44 @@ require 'spec_helper'
3
3
  describe 'Model equality' do
4
4
  with_mongo_model
5
5
 
6
- after{remove_constants :Player, :Tags}
6
+ after{remove_constants :Unit, :Tags}
7
7
 
8
- it "integration smoke test" do
9
- class Player
8
+ it "should check for equality based on model attributes" do
9
+ class Unit
10
10
  inherit Mongo::Model
11
11
 
12
- attr_accessor :name, :mission
12
+ attr_accessor :name, :items
13
13
 
14
- class Mission
14
+ class Item
15
15
  inherit Mongo::Model
16
16
 
17
17
  attr_accessor :name
18
18
  end
19
19
  end
20
20
 
21
- player1 = Player.new name: 'Alex'
22
- player1.mission = Player::Mission.new name: 'First Strike'
21
+ unit1 = Unit.new name: 'Zeratul'
22
+ unit1.items = [Unit::Item.new(name: 'Psionic blade')]
23
23
 
24
- player2 = Player.new name: 'Alex'
25
- player2.mission = Player::Mission.new name: 'First Strike'
24
+ unit2 = Unit.new name: 'Zeratul'
25
+ unit2.items = [Unit::Item.new(name: 'Psionic blade')]
26
26
 
27
- player1.should == player2
27
+ unit1.should == unit2
28
28
 
29
- player1.mission.name = 'Into the Flames'
30
- player1.should_not == player2
29
+ unit1.items.first.name = 'Power suit'
30
+ unit1.should_not == unit2
31
31
  end
32
32
 
33
- it "should correct compare Array/Hash models (from error)" do
33
+ it "should compare with non models (from errors)" do
34
+ class Unit
35
+ inherit Mongo::Model
36
+ end
37
+
38
+ unit = Unit.new
39
+ unit.should_not == 1
40
+ unit.should_not == nil
41
+ end
42
+
43
+ it "should correctly compare Array/Hash models (from error)" do
34
44
  class Tags < Array
35
45
  inherit Mongo::Model
36
46
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
  require 'file_model/spec/shared_crud'
3
3
 
4
- describe 'File Model' do
4
+ describe 'Integration with File Model' do
5
5
  with_mongo_model
6
6
  it_should_behave_like "file model crud"
7
7
 
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Integration with Validatable" do
4
+ with_mongo_model
5
+
6
+ describe "basics" do
7
+ before do
8
+ class Unit
9
+ inherit Mongo::Model
10
+ collection :units
11
+
12
+ attr_accessor :name
13
+ end
14
+ end
15
+ after{remove_constants :Unit}
16
+
17
+ it "should perform basic validation" do
18
+ class Unit
19
+ validates_presence_of :name
20
+ end
21
+
22
+ unit = Unit.new
23
+ unit.should_not be_valid
24
+ unit.errors.size.should == 1
25
+ unit.errors.first.first.should == :name
26
+ unit.save.should be_false
27
+
28
+ unit.errors.clear
29
+ unit.name = 'Zeratul'
30
+ unit.should be_valid
31
+ unit.errors.should be_empty
32
+ unit.save.should be_true
33
+ end
34
+
35
+ it "should validate before save" do
36
+ unit = Unit.new
37
+ unit.should_receive(:run_validations)
38
+ unit.save
39
+ end
40
+
41
+ it "should not save errors as instance variables" do
42
+ unit = Unit.new
43
+ unit.valid?
44
+ unit.instance_variables.select{|iv_name| iv_name !~ /^@_/}.should be_empty
45
+ end
46
+ end
47
+
48
+ describe "Special validators" do
49
+ before do
50
+ class Unit
51
+ inherit Mongo::Model
52
+ collection :units
53
+
54
+ attr_accessor :name
55
+ end
56
+ end
57
+ after{remove_constants :Unit}
58
+
59
+ it 'should validate uniqueness' do
60
+ class Unit
61
+ validates_uniqueness_of :name
62
+ end
63
+
64
+ unit = Unit.build name: 'Zeratul'
65
+ unit.save.should be_true
66
+
67
+ unit = Unit.build name: 'Zeratul'
68
+ unit.save.should be_false
69
+ unit.errors[:name].first.should =~ /unique/
70
+ end
71
+
72
+ it 'should validate uniqueness within scope' do
73
+ class Unit
74
+ attr_accessor :account_id
75
+
76
+ validates_uniqueness_of :name, scope: :account_id
77
+ end
78
+
79
+ unit = Unit.build name: 'Zeratul', account_id: '10'
80
+ unit.save.should be_true
81
+
82
+ unit = Unit.build name: 'Zeratul', account_id: '10'
83
+ unit.save.should be_false
84
+
85
+ unit = Unit.build name: 'Zeratul', account_id: '20'
86
+ unit.save.should be_true
87
+ end
88
+ end
89
+ end
data/spec/misc_spec.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe 'Model Miscellaneous' do
3
+ describe 'Miscellaneous' do
4
4
  with_mongo_model
5
5
 
6
6
  before do
@@ -13,7 +13,7 @@ describe 'Model Miscellaneous' do
13
13
  end
14
14
  after{remove_constants :Unit3, :User}
15
15
 
16
- it "timestamps" do
16
+ it "should create timestamps" do
17
17
  class Unit3
18
18
  inherit Mongo::Model
19
19
  collection :units
@@ -36,7 +36,7 @@ describe 'Model Miscellaneous' do
36
36
  unit.updated_at.should > updated_at
37
37
  end
38
38
 
39
- it 'cache' do
39
+ it 'should have cache' do
40
40
  class Unit3
41
41
  inherit Mongo::Model
42
42
  end
@@ -44,21 +44,21 @@ describe 'Model Miscellaneous' do
44
44
  u._cache.should == {}
45
45
  end
46
46
 
47
- it "to_param" do
47
+ it "should convert model to param" do
48
48
  u = User.new
49
49
  u.to_param.should be_nil
50
50
  u.save!
51
51
  u.to_param.should_not be_empty
52
52
  end
53
53
 
54
- it "dom_id" do
54
+ it "should have dom_id" do
55
55
  u = User.new
56
56
  u.dom_id.should be_nil
57
57
  u.save!
58
58
  u.dom_id.should_not be_empty
59
59
  end
60
60
 
61
- it 'reload' do
61
+ it 'should reload model' do
62
62
  u = User.create! name: 'Zeratul'
63
63
  u.name = 'Jim'
64
64
  u.reload
data/spec/query_spec.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "Model Query" do
3
+ describe "Query" do
4
4
  with_mongo_model
5
5
 
6
6
  before :all do
@@ -13,32 +13,32 @@ describe "Model Query" do
13
13
  end
14
14
  after(:all){remove_constants :Unit, :SpecialUnit}
15
15
 
16
- before{@zeratul = Unit.build name: 'Zeratul'}
16
+ before{@unit = Unit.build name: 'Zeratul'}
17
17
 
18
- it 'exist?' do
18
+ it 'should check existence' do
19
19
  Unit.should_not exist(name: 'Zeratul')
20
- @zeratul.save!
20
+ @unit.save!
21
21
  Unit.should exist(name: 'Zeratul')
22
22
  end
23
23
 
24
- it 'first, first!' do
24
+ it 'should return first model (also with bang version)' do
25
25
  Unit.first.should be_nil
26
26
  -> {Unit.first!}.should raise_error(Mongo::NotFound)
27
- @zeratul.save
27
+ @unit.save
28
28
  Unit.first.should_not be_nil
29
29
  Unit.first!.should_not be_nil
30
30
  end
31
31
 
32
- it 'all, each' do
32
+ it 'should return all and iterate over each models' do
33
33
  list = []; Unit.each{|o| list << o}
34
34
  list.size.should == 0
35
35
 
36
- @zeratul.save
36
+ @unit.save
37
37
  list = []; Unit.each{|o| list << o}
38
38
  list.size.should == 1
39
39
  end
40
40
 
41
- it 'dynamic finders integration' do
41
+ it 'should have dynamic finders' do
42
42
  Unit.first_by_name('Zeratul').should be_nil
43
43
  u = Unit.build(name: 'Zeratul')
44
44
  u.save!
@@ -46,7 +46,7 @@ describe "Model Query" do
46
46
  Unit.by_id!(u._id).name.should == 'Zeratul'
47
47
  end
48
48
 
49
- it 'build, create, create!' do
49
+ it 'should be integrated with build, create and create!' do
50
50
  class SpecialUnit < Unit
51
51
  attr_accessor :age
52
52
  end
@@ -54,27 +54,18 @@ describe "Model Query" do
54
54
  u = SpecialUnit.query(name: 'Zeratul').build age: 500
55
55
  [u.name, u.age].should == ['Zeratul', 500]
56
56
 
57
- SpecialUnit.destroy_all
57
+ SpecialUnit.delete_all
58
58
  SpecialUnit.query(name: 'Zeratul').create age: 500
59
59
  u = SpecialUnit.first
60
60
  [u.name, u.age].should == ['Zeratul', 500]
61
61
 
62
- SpecialUnit.destroy_all
62
+ SpecialUnit.delete_all
63
63
  SpecialUnit.query(name: 'Zeratul').create! age: 500
64
64
  u = SpecialUnit.first
65
65
  [u.name, u.age].should == ['Zeratul', 500]
66
66
  end
67
67
 
68
- it 'exist?' do
69
- u = Unit.new
70
- u.exist?.should be_false
71
- u.save!
72
- u.exist?.should be_true
73
- u.destroy!
74
- u.exist?.should be_false
75
- end
76
-
77
- it "build should assign protected attributes" do
68
+ it "should not allow to mass-assign protected attributes" do
78
69
  class SpecialUnit < Unit
79
70
  attr_accessor :age, :status
80
71
  assign do
@@ -88,9 +79,7 @@ describe "Model Query" do
88
79
  u.status.should == 'active'
89
80
  end
90
81
 
91
- it "where" do
92
- # Unit.where(name: 'Zeratul').should == Mongo::Model::Query.new(Unit, {name: 'Zeratul'}, {})
93
-
82
+ it "should support where clause" do
94
83
  query = Unit.where(name: 'Zeratul')
95
84
  query = query.where(race: 'Protoss')
96
85
  query.should == Mongo::Model::Query.new(Unit, {name: 'Zeratul', race: 'Protoss'}, {})
data/spec/scope_spec.rb CHANGED
@@ -45,7 +45,7 @@ describe "Scope" do
45
45
  Unit.current_scope.should be_nil
46
46
  end
47
47
 
48
- it "definition" do
48
+ it "should allow to define default scope" do
49
49
  Unit.default_scope status: 'alive'
50
50
 
51
51
  Unit.current_scope.should == Unit.query(status: 'alive')
@@ -56,7 +56,7 @@ describe "Scope" do
56
56
  Unit.current_scope.should == Unit.query(status: 'alive')
57
57
  end
58
58
 
59
- it "should be inherited" do
59
+ it "should inherit default scope" do
60
60
  Unit.default_scope status: 'alive'
61
61
 
62
62
  class Protoss < Unit; end
@@ -69,7 +69,7 @@ describe "Scope" do
69
69
  end
70
70
 
71
71
  describe 'scope' do
72
- it "definition" do
72
+ it "should allow to define scope" do
73
73
  Unit.scope :alive, status: 'alive'
74
74
  Unit.alive.current_scope.should == Unit.query(status: 'alive')
75
75
 
@@ -101,7 +101,7 @@ describe "Scope" do
101
101
  end
102
102
  end
103
103
 
104
- describe 'with_scope' do
104
+ describe 'querying with scope' do
105
105
  it "shouldn't allow to nest exclusive scope" do
106
106
  -> {
107
107
  Unit.with_exclusive_scope do
@@ -116,7 +116,7 @@ describe "Scope" do
116
116
  }.should raise_error(/exclusive scope already applied/)
117
117
  end
118
118
 
119
- it "with_exclusive_scope should clear other scopes" do
119
+ it "should clear other scopes when exclusive scope used" do
120
120
  Unit.default_scope status: 'alive'
121
121
 
122
122
  Unit.with_scope race: 'Protoss' do
@@ -132,13 +132,13 @@ describe "Scope" do
132
132
  end
133
133
  end
134
134
 
135
- it "usage" do
135
+ it "should access current scope" do
136
136
  Unit.with_scope status: 'alive' do
137
137
  Unit.current_scope.should == Unit.query(status: 'alive')
138
138
  end
139
139
  end
140
140
 
141
- it "should merge scope" do
141
+ it "should merge scopes" do
142
142
  Unit.default_scope status: 'alive'
143
143
  Unit.with_scope race: 'Protoss' do
144
144
  Unit.with_scope name: 'Zeratul' do
@@ -149,13 +149,13 @@ describe "Scope" do
149
149
  end
150
150
 
151
151
  describe 'handy scopes' do
152
- it "limit, skip, sort" do
152
+ it "should provide limit, skip and sort scopes" do
153
153
  query = Unit.skip(30).limit(10).sort([:name, 1]).snapshot
154
154
  query.selector.should == {}
155
155
  query.options.should == {skip: 30, limit: 10, sort: [[:name, 1]], snapshot: true}
156
156
  end
157
157
 
158
- it "sort should understand simplified form" do
158
+ it "should understand simplified form of sort scope" do
159
159
  query = Unit.sort(:name)
160
160
  query.options.should == {sort: [[:name, 1]]}
161
161
 
@@ -163,7 +163,7 @@ describe "Scope" do
163
163
  query.options.should == {sort: [[:race, 1], [:name, 1]]}
164
164
  end
165
165
 
166
- it 'paginate' do
166
+ it 'should provide paginate' do
167
167
  query = Unit.paginate(4, 10)
168
168
  query.selector.should == {}
169
169
  query.options.should == {skip: 30, limit: 10}
data/spec/spec_helper.rb CHANGED
@@ -3,11 +3,27 @@ require 'mongo/model'
3
3
  require 'rspec_ext'
4
4
  require 'mongo/model/spec'
5
5
 
6
- #
7
- # Handy spec helpers
8
- #
6
+ # Shortcuts.
9
7
  rspec do
10
8
  def db
11
9
  mongo.db
12
10
  end
11
+ end
12
+
13
+ # Helper to simplify callback expectations.
14
+ module RSpec::CallbackHelper
15
+ def run_before_callbacks name, method, options = {}
16
+ $_dont_watch_callbacks ? true : send(:"before_#{name}")
17
+ end
18
+
19
+ def run_after_callbacks name, method, options = {}
20
+ $_dont_watch_callbacks ? true : send(:"after_#{name}")
21
+ end
22
+
23
+ def dont_watch_callbacks &block
24
+ $_dont_watch_callbacks = true
25
+ block.call
26
+ ensure
27
+ $_dont_watch_callbacks = false
28
+ end
13
29
  end