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/Rakefile +3 -4
- data/lib/mongo/model/assignment.rb +4 -2
- data/lib/mongo/model/callbacks.rb +27 -2
- data/lib/mongo/model/conversion.rb +26 -11
- data/lib/mongo/model/crud.rb +52 -9
- data/lib/mongo/model/{file_model.rb → integration/file_model.rb} +1 -1
- data/lib/mongo/model/{validation → integration/validatable}/uniqueness_validator.rb +2 -2
- data/lib/mongo/model/integration/validatable.rb +41 -0
- data/lib/mongo/model/load.rb +59 -0
- data/lib/mongo/model/model.rb +51 -19
- data/lib/mongo/model/query.rb +1 -1
- data/lib/mongo/model/scope.rb +2 -6
- data/lib/mongo/model/spec.rb +0 -3
- data/lib/mongo/model/support/{types.rb → conversions.rb} +9 -23
- data/lib/mongo/model/support.rb +11 -0
- data/lib/mongo/model/validation.rb +14 -34
- data/lib/mongo/model.rb +1 -46
- data/spec/assignment_spec.rb +2 -2
- data/spec/associations_spec.rb +2 -4
- data/spec/attribute_convertors_spec.rb +9 -9
- data/spec/callbacks_spec.rb +161 -34
- data/spec/conversion_spec.rb +18 -18
- data/spec/crud_spec.rb +79 -81
- data/spec/equality_spec.rb +23 -13
- data/spec/{file_model_spec.rb → integration/file_model_spec.rb} +1 -1
- data/spec/integration/validatable_spec.rb +89 -0
- data/spec/misc_spec.rb +6 -6
- data/spec/query_spec.rb +14 -25
- data/spec/scope_spec.rb +10 -10
- data/spec/spec_helper.rb +19 -3
- data/spec/validation_spec.rb +92 -85
- metadata +16 -12
data/spec/validation_spec.rb
CHANGED
@@ -1,22 +1,99 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "
|
3
|
+
describe "Validation" do
|
4
4
|
with_mongo_model
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
describe 'CRUD' do
|
7
|
+
before do
|
8
|
+
class Unit
|
9
|
+
inherit Mongo::Model
|
10
|
+
collection :units
|
10
11
|
|
11
|
-
|
12
|
+
attr_accessor :items
|
13
|
+
embedded :items
|
14
|
+
|
15
|
+
class Item
|
16
|
+
inherit Mongo::Model
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
after{remove_constants :Unit}
|
21
|
+
|
22
|
+
before do
|
23
|
+
@item = Unit::Item.new
|
24
|
+
@unit = Unit.new
|
25
|
+
@unit.items = [@item]
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should not save, update or delete invalid objects' do
|
29
|
+
# create
|
30
|
+
@unit.stub!(:valid?).and_return(false)
|
31
|
+
db.units.save(@unit).should be_false
|
32
|
+
|
33
|
+
@unit.stub!(:valid?).and_return(true)
|
34
|
+
db.units.save(@unit).should be_true
|
35
|
+
|
36
|
+
# update
|
37
|
+
@unit.stub!(:valid?).and_return(false)
|
38
|
+
db.units.save(@unit).should be_false
|
39
|
+
|
40
|
+
@unit.stub!(:valid?).and_return(true)
|
41
|
+
db.units.save(@unit).should be_true
|
42
|
+
|
43
|
+
# delete
|
44
|
+
@unit.stub!(:valid?).and_return(false)
|
45
|
+
db.units.delete(@unit).should be_false
|
46
|
+
|
47
|
+
@unit.stub!(:valid?).and_return(true)
|
48
|
+
db.units.delete(@unit).should be_true
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should not save, update or delete invalid embedded objects' do
|
52
|
+
# create
|
53
|
+
@item.stub!(:valid?).and_return(false)
|
54
|
+
db.units.save(@unit).should be_false
|
55
|
+
|
56
|
+
@item.stub!(:valid?).and_return(true)
|
57
|
+
db.units.save(@unit).should be_true
|
58
|
+
|
59
|
+
# update
|
60
|
+
@item.stub!(:valid?).and_return(false)
|
61
|
+
db.units.save(@unit).should be_false
|
62
|
+
|
63
|
+
@item.stub!(:valid?).and_return(true)
|
64
|
+
db.units.save(@unit).should be_true
|
65
|
+
|
66
|
+
# delete
|
67
|
+
@item.stub!(:valid?).and_return(false)
|
68
|
+
db.units.delete(@unit).should be_false
|
69
|
+
|
70
|
+
@item.stub!(:valid?).and_return(true)
|
71
|
+
db.units.delete(@unit).should be_true
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should be able to skip validation" do
|
75
|
+
@unit.stub!(:valid?).and_return(false)
|
76
|
+
db.units.save(@unit, validate: false).should be_true
|
77
|
+
|
78
|
+
@unit.stub!(:valid?).and_return(true)
|
79
|
+
@item.stub!(:valid?).and_return(false)
|
80
|
+
db.units.save(@unit, validate: false).should be_true
|
12
81
|
end
|
13
82
|
end
|
14
|
-
after{remove_constants :Unit}
|
15
|
-
after(:all){remove_constants :BaseUnit}
|
16
83
|
|
17
|
-
describe
|
18
|
-
|
19
|
-
class Unit
|
84
|
+
describe "basics" do
|
85
|
+
before do
|
86
|
+
class Unit
|
87
|
+
inherit Mongo::Model
|
88
|
+
collection :units
|
89
|
+
|
90
|
+
attr_accessor :name
|
91
|
+
end
|
92
|
+
end
|
93
|
+
after{remove_constants :Unit}
|
94
|
+
|
95
|
+
it "should provide validation callback" do
|
96
|
+
class Unit
|
20
97
|
before_validate :check_name
|
21
98
|
def check_name
|
22
99
|
errors.add :name, 'invalid name'
|
@@ -28,7 +105,7 @@ describe "Validations" do
|
|
28
105
|
end
|
29
106
|
|
30
107
|
it "should not save model with errors" do
|
31
|
-
unit =
|
108
|
+
unit = Unit.build name: 'Zeratul'
|
32
109
|
unit.save.should be_true
|
33
110
|
|
34
111
|
unit.errors.clear
|
@@ -42,7 +119,7 @@ describe "Validations" do
|
|
42
119
|
end
|
43
120
|
|
44
121
|
it "should add custom validations" do
|
45
|
-
class Unit
|
122
|
+
class Unit
|
46
123
|
validate :check_name
|
47
124
|
def check_name
|
48
125
|
errors.add :name, 'invalid name'
|
@@ -54,85 +131,15 @@ describe "Validations" do
|
|
54
131
|
unit.errors[:name].should == ['invalid name']
|
55
132
|
end
|
56
133
|
|
57
|
-
|
58
|
-
|
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
|
134
|
+
it "should clear errors before validation" do
|
135
|
+
class Unit
|
67
136
|
validates_presence_of :name
|
68
137
|
end
|
69
138
|
|
70
139
|
unit = Unit.new
|
71
140
|
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
141
|
unit.name = 'Zeratul'
|
78
142
|
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
|
-
it "should clear errors before validation" do
|
97
|
-
class Unit < BaseUnit
|
98
|
-
validates_presence_of :name
|
99
|
-
end
|
100
|
-
|
101
|
-
unit = Unit.new
|
102
|
-
unit.should_not be_valid
|
103
|
-
unit.name = 'Zeratul'
|
104
|
-
unit.should be_valid
|
105
|
-
end
|
106
|
-
|
107
|
-
describe "special" do
|
108
|
-
it 'validates_uniqueness_of' do
|
109
|
-
class Unit < BaseUnit
|
110
|
-
validates_uniqueness_of :name
|
111
|
-
end
|
112
|
-
|
113
|
-
unit = Unit.build name: 'Zeratul'
|
114
|
-
unit.save.should be_true
|
115
|
-
|
116
|
-
unit = Unit.build name: 'Zeratul'
|
117
|
-
unit.save.should be_false
|
118
|
-
unit.errors[:name].first.should =~ /unique/
|
119
|
-
end
|
120
|
-
|
121
|
-
it 'validates_uniqueness_of with scope' do
|
122
|
-
class Unit < BaseUnit
|
123
|
-
attr_accessor :account_id
|
124
|
-
|
125
|
-
validates_uniqueness_of :name, scope: :account_id
|
126
|
-
end
|
127
|
-
|
128
|
-
unit = Unit.build name: 'Zeratul', account_id: '10'
|
129
|
-
unit.save.should be_true
|
130
|
-
|
131
|
-
unit = Unit.build name: 'Zeratul', account_id: '10'
|
132
|
-
unit.save.should be_false
|
133
|
-
|
134
|
-
unit = Unit.build name: 'Zeratul', account_id: '20'
|
135
|
-
unit.save.should be_true
|
136
143
|
end
|
137
144
|
end
|
138
145
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongodb_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 2.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
12
|
+
date: 2011-11-18 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongodb
|
16
|
-
requirement: &
|
16
|
+
requirement: &2848940 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2848940
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: validatable2
|
27
|
-
requirement: &
|
27
|
+
requirement: &2848700 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2848700
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: ruby_ext
|
38
|
-
requirement: &
|
38
|
+
requirement: &2848460 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2848460
|
47
47
|
description:
|
48
48
|
email:
|
49
49
|
executables: []
|
@@ -58,16 +58,19 @@ files:
|
|
58
58
|
- lib/mongo/model/conversion.rb
|
59
59
|
- lib/mongo/model/crud.rb
|
60
60
|
- lib/mongo/model/db.rb
|
61
|
-
- lib/mongo/model/file_model.rb
|
61
|
+
- lib/mongo/model/integration/file_model.rb
|
62
62
|
- lib/mongo/model/integration/rails.rb
|
63
|
+
- lib/mongo/model/integration/validatable/uniqueness_validator.rb
|
64
|
+
- lib/mongo/model/integration/validatable.rb
|
65
|
+
- lib/mongo/model/load.rb
|
63
66
|
- lib/mongo/model/misc.rb
|
64
67
|
- lib/mongo/model/model.rb
|
65
68
|
- lib/mongo/model/query.rb
|
66
69
|
- lib/mongo/model/query_mixin.rb
|
67
70
|
- lib/mongo/model/scope.rb
|
68
71
|
- lib/mongo/model/spec.rb
|
69
|
-
- lib/mongo/model/support/
|
70
|
-
- lib/mongo/model/
|
72
|
+
- lib/mongo/model/support/conversions.rb
|
73
|
+
- lib/mongo/model/support.rb
|
71
74
|
- lib/mongo/model/validation.rb
|
72
75
|
- lib/mongo/model.rb
|
73
76
|
- lib/mongodb_model/gems.rb
|
@@ -79,7 +82,8 @@ files:
|
|
79
82
|
- spec/crud_spec.rb
|
80
83
|
- spec/db_spec.rb
|
81
84
|
- spec/equality_spec.rb
|
82
|
-
- spec/file_model_spec.rb
|
85
|
+
- spec/integration/file_model_spec.rb
|
86
|
+
- spec/integration/validatable_spec.rb
|
83
87
|
- spec/misc_spec.rb
|
84
88
|
- spec/query_spec.rb
|
85
89
|
- spec/scope_spec.rb
|