mongodb_model 0.2.8 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,22 +1,99 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "Validations" do
3
+ describe "Validation" do
4
4
  with_mongo_model
5
5
 
6
- before :all do
7
- class BaseUnit
8
- inherit Mongo::Model
9
- collection :units
6
+ describe 'CRUD' do
7
+ before do
8
+ class Unit
9
+ inherit Mongo::Model
10
+ collection :units
10
11
 
11
- attr_accessor :name
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 'basics' do
18
- it "before validation callback" do
19
- class Unit < BaseUnit
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 = BaseUnit.build name: 'Zeratul'
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 < BaseUnit
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
- # 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
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.2.8
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-10-11 00:00:00.000000000Z
12
+ date: 2011-11-18 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongodb
16
- requirement: &2829180 !ruby/object:Gem::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: *2829180
24
+ version_requirements: *2848940
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: validatable2
27
- requirement: &2828940 !ruby/object:Gem::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: *2828940
35
+ version_requirements: *2848700
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: ruby_ext
38
- requirement: &2828700 !ruby/object:Gem::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: *2828700
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/types.rb
70
- - lib/mongo/model/validation/uniqueness_validator.rb
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