mongodbmodel 0.0.1

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.
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Model Query" do
4
+ with_mongo_model
5
+
6
+ before :all do
7
+ class Unit
8
+ inherit Mongo::Model
9
+ collection :units
10
+
11
+ attr_accessor :name
12
+ end
13
+ end
14
+ after(:all){remove_constants :Unit, :SpecialUnit}
15
+
16
+ before{@zeratul = Unit.build name: 'Zeratul'}
17
+
18
+ it 'exist?' do
19
+ Unit.should_not exist(name: 'Zeratul')
20
+ @zeratul.save!
21
+ Unit.should exist(name: 'Zeratul')
22
+ end
23
+
24
+ it 'first, first!' do
25
+ Unit.first.should be_nil
26
+ -> {Unit.first!}.should raise_error(Mongo::NotFound)
27
+ @zeratul.save
28
+ Unit.first.should_not be_nil
29
+ Unit.first!.should_not be_nil
30
+ end
31
+
32
+ it 'all, each' do
33
+ list = []; Unit.each{|o| list << o}
34
+ list.size.should == 0
35
+
36
+ @zeratul.save
37
+ list = []; Unit.each{|o| list << o}
38
+ list.size.should == 1
39
+ end
40
+
41
+ it 'dynamic finders integration' do
42
+ Unit.first_by_name('Zeratul').should be_nil
43
+ u = Unit.build(name: 'Zeratul')
44
+ u.save!
45
+ Unit.first_by_name('Zeratul').name.should == 'Zeratul'
46
+ Unit.by_id!(u._id).name.should == 'Zeratul'
47
+ end
48
+
49
+ it 'build, create, create!' do
50
+ class SpecialUnit < Unit
51
+ attr_accessor :age
52
+ end
53
+
54
+ u = SpecialUnit.query(name: 'Zeratul').build age: 500
55
+ [u.name, u.age].should == ['Zeratul', 500]
56
+
57
+ SpecialUnit.destroy_all
58
+ SpecialUnit.query(name: 'Zeratul').create age: 500
59
+ u = SpecialUnit.first
60
+ [u.name, u.age].should == ['Zeratul', 500]
61
+
62
+ SpecialUnit.destroy_all
63
+ SpecialUnit.query(name: 'Zeratul').create! age: 500
64
+ u = SpecialUnit.first
65
+ [u.name, u.age].should == ['Zeratul', 500]
66
+ end
67
+ end
@@ -0,0 +1,150 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Scope" do
4
+ with_mongo_model
5
+
6
+ before do
7
+ class Unit
8
+ inherit Mongo::Model
9
+ collection :units
10
+
11
+ attr_accessor :name, :status, :race
12
+ end
13
+ end
14
+
15
+ after{remove_constants :Unit, :Protoss}
16
+
17
+ describe 'current scope' do
18
+ it "should affect finders" do
19
+ Unit.build(name: 'Zeratul', status: 'alive').save!
20
+ Unit.build(name: 'Jim', status: 'alive').save!
21
+ Unit.build(name: 'Tassadar', status: 'dead').save!
22
+
23
+ Unit.count.should == 3
24
+ Unit.all.size.should == 3
25
+ Unit.first(name: 'Tassadar').should_not be_nil
26
+ Unit.first!(name: 'Tassadar').should_not be_nil
27
+
28
+ Unit.stub!(:current_scope).and_return(Unit.query(status: 'alive'))
29
+
30
+ Unit.count.should == 2
31
+ Unit.all.size.should == 2
32
+ Unit.first(name: 'Tassadar').should be_nil
33
+ -> {Unit.first!(name: 'Tassadar')}.should raise_error(Mongo::NotFound)
34
+
35
+ # should be merged with finders
36
+ Unit.count(status: 'dead').should == 1
37
+ Unit.all(status: 'dead').size.should == 1
38
+ Unit.first(name: 'Tassadar', status: 'dead').should_not be_nil
39
+ Unit.first!(name: 'Tassadar', status: 'dead').should_not be_nil
40
+ end
41
+ end
42
+
43
+ describe 'default scope' do
44
+ it "should not affect objects without default_scope" do
45
+ Unit.current_scope.should be_nil
46
+ end
47
+
48
+ it "definition" do
49
+ Unit.default_scope status: 'alive'
50
+
51
+ Unit.current_scope.should == Unit.query(status: 'alive')
52
+
53
+ Unit.default_scope do
54
+ {status: 'alive'}
55
+ end
56
+ Unit.current_scope.should == Unit.query(status: 'alive')
57
+ end
58
+
59
+ it "should be inherited" do
60
+ Unit.default_scope status: 'alive'
61
+
62
+ class Protoss < Unit; end
63
+ Protoss.current_scope.should == Unit.query(status: 'alive')
64
+
65
+ Protoss.default_scope status: 'dead'
66
+ Unit.current_scope.should == Unit.query(status: 'alive')
67
+ Protoss.current_scope.should == Protoss.query(status: 'dead')
68
+ end
69
+ end
70
+
71
+ describe 'scope' do
72
+ it "definition" do
73
+ Unit.scope :alive, status: 'alive'
74
+ Unit.alive.current_scope.should == Unit.query(status: 'alive')
75
+
76
+ Unit.scope :alive do
77
+ {status: 'alive'}
78
+ end
79
+ Unit.alive.current_scope.should == Unit.query(status: 'alive')
80
+ end
81
+
82
+ it 'scope should affect current scope' do
83
+ Unit.scope :alive, status: 'alive'
84
+
85
+ Unit.current_scope.should be_nil
86
+
87
+ Unit.alive.current_scope.should == Unit.query(status: 'alive')
88
+ Unit.alive.class.should == Mongo::Model::Query
89
+ end
90
+
91
+ it 'should be merged with default scope' do
92
+ Unit.default_scope race: 'Protoss'
93
+ Unit.scope :alive, status: 'alive'
94
+ Unit.alive.current_scope.should == Unit.query(race: 'Protoss', status: 'alive')
95
+ end
96
+
97
+ it 'should allow to chain scopes' do
98
+ Unit.scope :alive, status: 'alive'
99
+ Unit.scope :protosses, race: 'Protoss'
100
+ Unit.alive.protosses.current_scope.should == Unit.query(race: 'Protoss', status: 'alive')
101
+ end
102
+ end
103
+
104
+ describe 'with_scope' do
105
+ it "shouldn't allow to nest exclusive scope" do
106
+ -> {
107
+ Unit.with_exclusive_scope do
108
+ Unit.with_exclusive_scope{}
109
+ end
110
+ }.should raise_error(/exclusive scope already applied/)
111
+
112
+ -> {
113
+ Unit.with_exclusive_scope do
114
+ Unit.with_scope{}
115
+ end
116
+ }.should raise_error(/exclusive scope already applied/)
117
+ end
118
+
119
+ it "with_exclusive_scope should clear other scopes" do
120
+ Unit.default_scope status: 'alive'
121
+
122
+ Unit.with_scope race: 'Protoss' do
123
+ Unit.current_scope.should == Unit.query(status: 'alive', race: 'Protoss')
124
+
125
+ Unit.with_exclusive_scope do
126
+ Unit.current_scope.should == Unit.query({})
127
+ end
128
+
129
+ Unit.with_exclusive_scope race: 'Terran' do
130
+ Unit.current_scope.should == Unit.query(race: 'Terran')
131
+ end
132
+ end
133
+ end
134
+
135
+ it "usage" do
136
+ Unit.with_scope status: 'alive' do
137
+ Unit.current_scope.should == Unit.query(status: 'alive')
138
+ end
139
+ end
140
+
141
+ it "should merge scope" do
142
+ Unit.default_scope status: 'alive'
143
+ Unit.with_scope race: 'Protoss' do
144
+ Unit.with_scope name: 'Zeratul' do
145
+ Unit.current_scope.should == Unit.query(name: 'Zeratul', race: 'Protoss', status: 'alive')
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,13 @@
1
+ require 'mongo/model'
2
+
3
+ require 'rspec_ext'
4
+ require 'mongo/model/spec'
5
+
6
+ #
7
+ # Handy spec helpers
8
+ #
9
+ rspec do
10
+ def db
11
+ mongo.db
12
+ end
13
+ end
@@ -0,0 +1,127 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Validations" do
4
+ with_mongo_model
5
+
6
+ before :all do
7
+ class BaseUnit
8
+ inherit Mongo::Model
9
+ collection :units
10
+
11
+ attr_accessor :name
12
+ end
13
+ end
14
+ after{remove_constants :Unit}
15
+ after(:all){remove_constants :BaseUnit}
16
+
17
+ describe 'basics' do
18
+ it "before validation callback" do
19
+ class Unit < BaseUnit
20
+ before_validate :check_name
21
+ def check_name
22
+ errors.add :name, 'invalid name'
23
+ end
24
+ end
25
+
26
+ unit = Unit.new
27
+ unit.save.should be_false
28
+ end
29
+
30
+ it "should not save model with errors" do
31
+ unit = BaseUnit.build name: 'Zeratul'
32
+ unit.save.should be_true
33
+
34
+ unit.errors.clear
35
+ unit.save.should be_true
36
+
37
+ unit.errors[:name] = 'hairy error'
38
+ unit.save.should be_false
39
+
40
+ unit.errors.clear
41
+ unit.save.should be_true
42
+ end
43
+
44
+ it "should add custom validations" do
45
+ class Unit < BaseUnit
46
+ validate :check_name
47
+ def check_name
48
+ errors.add :name, 'invalid name'
49
+ end
50
+ end
51
+
52
+ unit = Unit.new
53
+ unit.save.should be_false
54
+ unit.errors[:name].should == ['invalid name']
55
+ end
56
+
57
+ # it "should check :errors only and ignore valid? method" do
58
+ # unit = BaseUnit.build name: 'Zeratul'
59
+ # unit.should_not_receive(:valid?)
60
+ # unit.save.should be_true
61
+ # end
62
+ end
63
+
64
+ describe "validatable2" do
65
+ it "smoke test" do
66
+ class Unit < BaseUnit
67
+ validates_presence_of :name
68
+ end
69
+
70
+ unit = Unit.new
71
+ unit.should_not be_valid
72
+ unit.errors.size.should == 1
73
+ unit.errors.first.first.should == :name
74
+ unit.save.should be_false
75
+
76
+ unit.errors.clear
77
+ unit.name = 'Zeratul'
78
+ unit.should be_valid
79
+ unit.errors.should be_empty
80
+ unit.save.should be_true
81
+ end
82
+
83
+ it "should validate before save" do
84
+ unit = BaseUnit.new
85
+ unit.should_receive(:run_validations)
86
+ unit.save
87
+ end
88
+
89
+ it "should not save errors as instance variables" do
90
+ unit = BaseUnit.new
91
+ unit.valid?
92
+ unit.instance_variables.select{|iv_name| iv_name !~ /^@_/}.should be_empty
93
+ end
94
+ end
95
+
96
+ describe "special" do
97
+ it 'validates_uniqueness_of' do
98
+ class Unit < BaseUnit
99
+ validates_uniqueness_of :name
100
+ end
101
+
102
+ unit = Unit.build name: 'Zeratul'
103
+ unit.save.should be_true
104
+
105
+ unit = Unit.build name: 'Zeratul'
106
+ unit.save.should be_false
107
+ unit.errors[:name].first.should =~ /unique/
108
+ end
109
+
110
+ it 'validates_uniqueness_of with scope' do
111
+ class Unit < BaseUnit
112
+ attr_accessor :account_id
113
+
114
+ validates_uniqueness_of :name, scope: :account_id
115
+ end
116
+
117
+ unit = Unit.build name: 'Zeratul', account_id: '10'
118
+ unit.save.should be_true
119
+
120
+ unit = Unit.build name: 'Zeratul', account_id: '10'
121
+ unit.save.should be_false
122
+
123
+ unit = Unit.build name: 'Zeratul', account_id: '20'
124
+ unit.save.should be_true
125
+ end
126
+ end
127
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongodbmodel
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexey Petrushin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-31 00:00:00.000000000Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Rakefile
21
+ - readme.md
22
+ - lib/mongo/model/assignment.rb
23
+ - lib/mongo/model/attribute_convertors.rb
24
+ - lib/mongo/model/callbacks.rb
25
+ - lib/mongo/model/crud.rb
26
+ - lib/mongo/model/db.rb
27
+ - lib/mongo/model/file_model.rb
28
+ - lib/mongo/model/misc.rb
29
+ - lib/mongo/model/model.rb
30
+ - lib/mongo/model/query.rb
31
+ - lib/mongo/model/query_mixin.rb
32
+ - lib/mongo/model/scope.rb
33
+ - lib/mongo/model/spec.rb
34
+ - lib/mongo/model/support/types.rb
35
+ - lib/mongo/model/validation/uniqueness_validator.rb
36
+ - lib/mongo/model/validation.rb
37
+ - lib/mongo/model.rb
38
+ - lib/mongodb_model/gems.rb
39
+ - spec/assignment_spec.rb
40
+ - spec/associations_spec.rb
41
+ - spec/attribute_convertors_spec.rb
42
+ - spec/callbacks_spec.rb
43
+ - spec/crud_spec.rb
44
+ - spec/db_spec.rb
45
+ - spec/file_model_spec.rb
46
+ - spec/misc_spec.rb
47
+ - spec/query_spec.rb
48
+ - spec/scope_spec.rb
49
+ - spec/spec_helper.rb
50
+ - spec/validation_spec.rb
51
+ homepage: http://github.com/alexeypetrushin/mongodb_model
52
+ licenses: []
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 1.8.6
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Object Model for MongoDB
75
+ test_files: []