mongomatic 0.6.2 → 0.6.3
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/test/helper.rb +16 -3
- data/test/test_exceptions.rb +12 -11
- data/test/test_find.rb +81 -0
- data/test/test_misc.rb +190 -0
- data/test/test_modifiers.rb +26 -25
- data/test/test_persistence.rb +43 -0
- data/test/test_validations.rb +340 -0
- metadata +15 -10
- data/test/test_mongomatic.rb +0 -656
data/test/helper.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
|
3
|
-
require 'shoulda'
|
2
|
+
gem 'minitest', "~> 2.0"
|
4
3
|
require 'pp'
|
5
4
|
|
6
5
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
@@ -88,5 +87,19 @@ class Person < Mongomatic::Base
|
|
88
87
|
|
89
88
|
end
|
90
89
|
|
91
|
-
class
|
90
|
+
class Thing < Mongomatic::Base
|
91
|
+
def before_insert
|
92
|
+
raise NoMethodError
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.before_drop
|
96
|
+
raise NoMethodError
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class Foobar < Mongomatic::Base
|
101
|
+
def validate
|
102
|
+
errors << ["color", "must not be blank"] if self["color"].blank?
|
103
|
+
errors << "missing style" if self["style"].blank?
|
104
|
+
end
|
92
105
|
end
|
data/test/test_exceptions.rb
CHANGED
@@ -1,36 +1,37 @@
|
|
1
1
|
require 'helper'
|
2
|
+
require 'minitest/autorun'
|
2
3
|
|
3
|
-
class TestExceptions <
|
4
|
+
class TestExceptions < MiniTest::Unit::TestCase
|
4
5
|
def setup
|
5
6
|
Person.collection.drop
|
6
7
|
end
|
7
8
|
|
8
|
-
|
9
|
+
def test_raise_on_update_of_new_doc
|
9
10
|
p1 = Person.new(:name => "Jordan")
|
10
11
|
assert_equal false, p1.update
|
11
|
-
|
12
|
+
assert_raises(Mongomatic::Exceptions::DocumentIsNew) { p1.update(:raise => true) }
|
12
13
|
end
|
13
14
|
|
14
|
-
|
15
|
+
def test_raise_on_insert_of_invalid_doc
|
15
16
|
p1 = Person.new
|
16
17
|
assert_equal false, p1.insert
|
17
|
-
|
18
|
+
assert_raises(Mongomatic::Exceptions::DocumentNotValid) { p1.insert(:raise => true) }
|
18
19
|
end
|
19
20
|
|
20
|
-
|
21
|
+
def test_raise_on_update_of_invalid_doc
|
21
22
|
p1 = Person.new(:name => "Ben")
|
22
23
|
assert p1.insert
|
23
24
|
p1["name"] = nil
|
24
25
|
assert_equal false, p1.update
|
25
|
-
|
26
|
+
assert_raises(Mongomatic::Exceptions::DocumentNotValid) { p1.update(:raise => true) }
|
26
27
|
end
|
27
28
|
|
28
|
-
|
29
|
+
def test_raise_on_update_or_insert_after_remove
|
29
30
|
p1 = Person.new(:name => "Ben")
|
30
31
|
assert p1.insert
|
31
32
|
assert p1.remove
|
32
|
-
|
33
|
-
|
34
|
-
|
33
|
+
assert_raises(Mongomatic::Exceptions::DocumentWasRemoved) { p1.remove(:raise => true) }
|
34
|
+
assert_raises(Mongomatic::Exceptions::DocumentWasRemoved) { p1.update(:raise => true) }
|
35
|
+
assert_raises(Mongomatic::Exceptions::DocumentWasRemoved) { p1.insert(:raise => true) }
|
35
36
|
end
|
36
37
|
end
|
data/test/test_find.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
class TestFind < MiniTest::Unit::TestCase
|
5
|
+
def test_find_one_with_query
|
6
|
+
Person.collection.drop
|
7
|
+
p1 = Person.new(:name => "Jordan")
|
8
|
+
p1.insert
|
9
|
+
|
10
|
+
assert_equal p1, Person.find_one(:name => "Jordan")
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_find_one_with_id
|
14
|
+
Person.collection.drop
|
15
|
+
p1 = Person.new(:name => "Jordan")
|
16
|
+
p1.insert
|
17
|
+
|
18
|
+
assert_equal p1, Person.find_one(p1['_id'])
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_find_one_with_id_or_hash
|
22
|
+
Person.collection.drop
|
23
|
+
Person.create_indexes
|
24
|
+
|
25
|
+
p = Person.new(:name => "Ben1", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
26
|
+
assert p.insert!.is_a?(BSON::ObjectId)
|
27
|
+
assert_equal 1, Person.count
|
28
|
+
|
29
|
+
found = Person.find({"_id" => BSON::ObjectId(p["_id"].to_s)}).next
|
30
|
+
assert_equal found, p
|
31
|
+
|
32
|
+
assert_raises(TypeError) { Person.find_one(p["_id"].to_s) }
|
33
|
+
|
34
|
+
found = Person.find_one({"_id" => p["_id"].to_s})
|
35
|
+
assert_equal found, nil
|
36
|
+
|
37
|
+
found = Person.find_one({"_id" => BSON::ObjectId(p["_id"].to_s)})
|
38
|
+
assert_equal found, p
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_limit_and_sort
|
42
|
+
Person.collection.drop
|
43
|
+
p = Person.new(:name => "Ben", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
44
|
+
assert p.insert.is_a?(BSON::ObjectId)
|
45
|
+
assert_equal 1, Person.collection.count
|
46
|
+
p2 = Person.new(:name => "Ben2", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
47
|
+
assert p2.insert.is_a?(BSON::ObjectId)
|
48
|
+
assert_equal 2, Person.collection.count
|
49
|
+
|
50
|
+
cursor = Person.find({"_id" => p["_id"]})
|
51
|
+
found = cursor.next
|
52
|
+
assert_equal p, found
|
53
|
+
|
54
|
+
cursor = Person.find()
|
55
|
+
assert_equal 0, cursor.current_limit
|
56
|
+
assert_equal 2, cursor.to_a.size
|
57
|
+
cursor = Person.find().limit(1)
|
58
|
+
assert_equal 1, cursor.current_limit
|
59
|
+
assert_equal 1, cursor.to_a.size
|
60
|
+
cursor = Person.find().limit(1)
|
61
|
+
assert_equal p, cursor.next
|
62
|
+
assert_equal nil, cursor.next
|
63
|
+
cursor = Person.find().limit(1).skip(1)
|
64
|
+
assert_equal p2, cursor.next
|
65
|
+
|
66
|
+
cursor = Person.find().sort("name", Mongo::ASCENDING)
|
67
|
+
assert_equal p, cursor.next
|
68
|
+
|
69
|
+
cursor = Person.find().sort("name", Mongo::DESCENDING)
|
70
|
+
assert_equal p2, cursor.next
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_instance_of_class_returned_on_find_one
|
74
|
+
Person.collection.drop
|
75
|
+
p1 = Person.new(:name => "Jordan")
|
76
|
+
p1.insert
|
77
|
+
|
78
|
+
assert_equal Person, Person.find_one(:name => "Jordan").class
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
data/test/test_misc.rb
ADDED
@@ -0,0 +1,190 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
class TestMisc < MiniTest::Unit::TestCase
|
5
|
+
def test_keys_can_be_strings_or_symbols
|
6
|
+
Person.collection.drop
|
7
|
+
p1 = Person.new(:name => "Jordan")
|
8
|
+
p1[:address] = { :city => "San Francisco" }
|
9
|
+
assert_equal "Jordan", p1["name"]
|
10
|
+
assert_equal "Jordan", p1[:name]
|
11
|
+
assert_equal "San Francisco", p1["address"]["city"]
|
12
|
+
assert_equal "San Francisco", p1[:address][:city]
|
13
|
+
p1.insert
|
14
|
+
|
15
|
+
p1 = Person.find_one(:name => "Jordan")
|
16
|
+
assert_equal "Jordan", p1["name"]
|
17
|
+
assert_equal "Jordan", p1[:name]
|
18
|
+
assert_equal "San Francisco", p1["address"]["city"]
|
19
|
+
assert_equal "San Francisco", p1[:address][:city]
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_empty_cursor
|
23
|
+
Person.collection.drop
|
24
|
+
assert Person.empty?
|
25
|
+
assert Person.find.empty?
|
26
|
+
p1 = Person.new(:name => "Jordan")
|
27
|
+
p1.insert
|
28
|
+
assert !Person.empty?
|
29
|
+
assert !Person.find.empty?
|
30
|
+
assert !Person.find({"name" => "Jordan"}).empty?
|
31
|
+
assert Person.find({"name" => "Ben"}).empty?
|
32
|
+
p1.remove!
|
33
|
+
assert Person.empty?
|
34
|
+
assert Person.find.empty?
|
35
|
+
assert Person.find({"name" => "Jordan"}).empty?
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_enumerable
|
39
|
+
Person.collection.drop
|
40
|
+
p1 = Person.new(:name => "Ben1", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
41
|
+
p2 = Person.new(:name => "Ben2", :birth_year => 1986, :created_at => Time.now.utc, :admin => true)
|
42
|
+
assert p1.insert.is_a?(BSON::ObjectId)
|
43
|
+
assert p2.insert.is_a?(BSON::ObjectId)
|
44
|
+
assert_equal 2, Person.collection.count
|
45
|
+
assert_equal 2, Person.find.inject(0) { |sum, p| assert p.is_a?(Person); sum += 1 }
|
46
|
+
assert_equal p2, Person.find.max { |p1,p2| p1["birth_year"] <=> p2["birth_year"] }
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_enumerable_cursor
|
50
|
+
Person.collection.drop
|
51
|
+
1000.upto(2000) do |i|
|
52
|
+
p = Person.new(:name => "Ben#{i}", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
53
|
+
assert p.insert.is_a?(BSON::ObjectId)
|
54
|
+
end
|
55
|
+
i = 1000
|
56
|
+
Person.find().sort(["name", :asc]).each { |p| assert_equal "Ben#{i}", p["name"]; i += 1 }
|
57
|
+
Person.find().sort(["name", :asc]).each_with_index { |p,i| assert_equal "Ben#{1000+i}", p["name"] }
|
58
|
+
|
59
|
+
p = Person.find().limit(1).next
|
60
|
+
assert Person.find().sort(["name", :asc]).include?(p)
|
61
|
+
|
62
|
+
assert_equal 10, Person.find().limit(10).to_a.size
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_merging_hashes
|
66
|
+
Person.collection.drop
|
67
|
+
p = Person.new(:name => "Ben", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
68
|
+
assert p.insert.is_a?(BSON::ObjectId)
|
69
|
+
assert_equal 1, Person.collection.count
|
70
|
+
p.merge(:birth_year => 1986)
|
71
|
+
p.update
|
72
|
+
p = Person.find({"_id" => p["_id"]}).next
|
73
|
+
assert_equal 1986, p["birth_year"]
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_callbacks
|
77
|
+
p = Person.new(:name => "Ben1", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
78
|
+
p.callback_tests = []
|
79
|
+
assert p.callback_tests.empty?
|
80
|
+
assert p.valid?
|
81
|
+
assert_equal [:before_validate, :after_validate], p.callback_tests
|
82
|
+
p.callback_tests = []
|
83
|
+
assert p.insert.is_a?(BSON::ObjectId)
|
84
|
+
assert_equal [:before_validate, :after_validate, :before_insert, :before_insert_or_update, :after_insert, :after_insert_or_update], p.callback_tests
|
85
|
+
p.callback_tests = []
|
86
|
+
p.update
|
87
|
+
assert_equal [:before_validate, :after_validate, :before_update, :before_insert_or_update, :after_update, :after_insert_or_update], p.callback_tests
|
88
|
+
p.callback_tests = []
|
89
|
+
p.remove
|
90
|
+
assert_equal [:before_remove, :after_remove], p.callback_tests
|
91
|
+
Person.class_callbacks = []
|
92
|
+
Person.drop
|
93
|
+
assert_equal [:before_drop, :after_drop], Person.class_callbacks
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_unique_index
|
97
|
+
Person.collection.drop
|
98
|
+
Person.create_indexes
|
99
|
+
|
100
|
+
p = Person.new(:name => "Ben1", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
101
|
+
assert p.insert!.is_a?(BSON::ObjectId)
|
102
|
+
assert_equal 1, Person.count
|
103
|
+
|
104
|
+
p = Person.new(:name => "Ben1", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
105
|
+
assert_raises(Mongo::OperationFailure) { p.insert! }
|
106
|
+
|
107
|
+
assert_equal 1, Person.count
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_is_new_flag
|
111
|
+
Person.collection.drop
|
112
|
+
p = Person.new
|
113
|
+
assert p.is_new?
|
114
|
+
assert !p.insert
|
115
|
+
assert p.is_new?
|
116
|
+
p["name"] = "Ben"
|
117
|
+
assert p.insert
|
118
|
+
assert !p.is_new?
|
119
|
+
p = Person.find_one(p["_id"])
|
120
|
+
assert !p.is_new?
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_custom_id
|
124
|
+
Person.collection.drop
|
125
|
+
p = Person.new(:name => "Ben")
|
126
|
+
assert p.is_new?
|
127
|
+
p["_id"] = "mycustomid"
|
128
|
+
assert p.insert
|
129
|
+
found = Person.find_one({"_id" => "mycustomid"})
|
130
|
+
assert_equal found, p
|
131
|
+
assert !p.is_new?
|
132
|
+
found["age"] = 26
|
133
|
+
assert found.update
|
134
|
+
found = Person.find_one({"_id" => "mycustomid"})
|
135
|
+
assert_equal found["_id"], "mycustomid"
|
136
|
+
assert_equal 26, found["age"]
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_be_able_to_drop_collection
|
140
|
+
p = Person.new
|
141
|
+
p['name'] = "Jordan"
|
142
|
+
p.insert
|
143
|
+
|
144
|
+
assert !Person.empty?
|
145
|
+
|
146
|
+
Person.drop
|
147
|
+
assert Person.empty?
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_has_key_with_symbols_and_strings
|
151
|
+
p = Person.new
|
152
|
+
|
153
|
+
assert !p.has_key?(:name)
|
154
|
+
|
155
|
+
p['name'] = 'Jordan'
|
156
|
+
|
157
|
+
assert p.has_key?(:name)
|
158
|
+
assert p.has_key?('name')
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_has_key
|
162
|
+
p = Person.new(:employer => {:name => 'Meta+Level Games',
|
163
|
+
:function => 'Makes things with code',
|
164
|
+
:something_else => {
|
165
|
+
:with_a_key => 'And Value'}
|
166
|
+
})
|
167
|
+
|
168
|
+
assert !p.has_key?('employer.started_at')
|
169
|
+
assert p.has_key?('employer.name')
|
170
|
+
assert !p.has_key?('non.existent')
|
171
|
+
assert !p.has_key?('employer.something_else.not_here')
|
172
|
+
assert p.has_key?('employer.something_else.with_a_key')
|
173
|
+
end
|
174
|
+
|
175
|
+
def test_value_for_key
|
176
|
+
p = Person.new(:employer => {:name => 'Meta+Level Games',
|
177
|
+
:function => 'Makes things with code',
|
178
|
+
:something_else => {
|
179
|
+
:with_a_key => 'And Value'}
|
180
|
+
})
|
181
|
+
assert_equal "And Value", p.value_for_key("employer.something_else.with_a_key")
|
182
|
+
assert_equal "Meta+Level Games", p.value_for_key("employer.name")
|
183
|
+
assert_nil p.value_for_key("some_key.that_does_not.exist")
|
184
|
+
end
|
185
|
+
|
186
|
+
def test_no_method_error_in_callback
|
187
|
+
assert_raises(NoMethodError) { Thing.new.insert }
|
188
|
+
assert_raises(NoMethodError) { Thing.drop }
|
189
|
+
end
|
190
|
+
end
|
data/test/test_modifiers.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
require 'helper'
|
2
|
+
require 'minitest/autorun'
|
2
3
|
|
3
|
-
class TestModifiers <
|
4
|
+
class TestModifiers < MiniTest::Unit::TestCase
|
4
5
|
def setup
|
5
6
|
Person.collection.drop
|
6
7
|
end
|
7
8
|
|
8
|
-
|
9
|
+
def test_push
|
9
10
|
p1 = Person.new(:name => "Jordan")
|
10
11
|
p1.insert
|
11
12
|
assert p1.push("interests", "skydiving")
|
@@ -18,10 +19,10 @@ class TestModifiers < Test::Unit::TestCase
|
|
18
19
|
assert_equal ["skydiving","coding"], p1["interests"]
|
19
20
|
|
20
21
|
p1["interests"] = "foo"
|
21
|
-
|
22
|
+
assert_raises(Mongomatic::Modifiers::UnexpectedFieldType) { p1.push("interests", "snowboarding") }
|
22
23
|
end
|
23
24
|
|
24
|
-
|
25
|
+
def test_push_into_embedded_hash
|
25
26
|
p1 = Person.new(:name => "Jordan")
|
26
27
|
p1.insert
|
27
28
|
assert p1.push!("personal.interests", "skydiving")
|
@@ -30,7 +31,7 @@ class TestModifiers < Test::Unit::TestCase
|
|
30
31
|
assert_equal ["skydiving"], p1["personal"]["interests"]
|
31
32
|
end
|
32
33
|
|
33
|
-
|
34
|
+
def test_push_all
|
34
35
|
p1 = Person.new(:name => "Jordan")
|
35
36
|
p1.insert
|
36
37
|
assert p1.push("interests", "skydiving")
|
@@ -44,10 +45,10 @@ class TestModifiers < Test::Unit::TestCase
|
|
44
45
|
assert_equal ["skydiving","coding","running","snowboarding","reading"], p1["interests"]
|
45
46
|
|
46
47
|
p1["interests"] = "foo"
|
47
|
-
|
48
|
+
assert_raises(Mongomatic::Modifiers::UnexpectedFieldType) { p1.push_all("interests", ["snowboarding"]) }
|
48
49
|
end
|
49
50
|
|
50
|
-
|
51
|
+
def test_push_all_in_embedded_hash
|
51
52
|
p1 = Person.new(:name => "Jordan")
|
52
53
|
p1.insert
|
53
54
|
p1.push_all!("contacts.coworkers", ["Chris","Keith","Jordan","Mike"])
|
@@ -56,7 +57,7 @@ class TestModifiers < Test::Unit::TestCase
|
|
56
57
|
assert_equal ["Chris","Keith","Jordan","Mike"], p1["contacts"]["coworkers"]
|
57
58
|
end
|
58
59
|
|
59
|
-
|
60
|
+
def test_pull
|
60
61
|
p1 = Person.new(:name => "Jordan")
|
61
62
|
p1.insert
|
62
63
|
assert p1.push("interests", "skydiving")
|
@@ -69,10 +70,10 @@ class TestModifiers < Test::Unit::TestCase
|
|
69
70
|
assert_equal ["skydiving","coding","snowboarding","reading"], p1["interests"]
|
70
71
|
|
71
72
|
p1["interests"] = "foo"
|
72
|
-
|
73
|
+
assert_raises(Mongomatic::Modifiers::UnexpectedFieldType) { p1.pull("interests", ["snowboarding"]) }
|
73
74
|
end
|
74
75
|
|
75
|
-
|
76
|
+
def test_pull_from_embedded_hash
|
76
77
|
p1 = Person.new(:name => "Jordan")
|
77
78
|
p1.insert!
|
78
79
|
p1.push_all!("contacts.coworkers", ["Chris","Keith","Jordan","Mike","Joe"])
|
@@ -83,7 +84,7 @@ class TestModifiers < Test::Unit::TestCase
|
|
83
84
|
assert_equal ["Chris","Keith","Jordan","Mike"], p1["contacts"]["coworkers"]
|
84
85
|
end
|
85
86
|
|
86
|
-
|
87
|
+
def test_pull_all
|
87
88
|
p1 = Person.new(:name => "Jordan")
|
88
89
|
p1.insert
|
89
90
|
assert p1.push_all!("interests", ["skydiving", "coding","running","snowboarding","reading"])
|
@@ -95,10 +96,10 @@ class TestModifiers < Test::Unit::TestCase
|
|
95
96
|
assert_equal ["skydiving", "coding","reading"], p1["interests"]
|
96
97
|
|
97
98
|
p1["interests"] = "foo"
|
98
|
-
|
99
|
+
assert_raises(Mongomatic::Modifiers::UnexpectedFieldType) { p1.pull_all("interests", ["snowboarding"]) }
|
99
100
|
end
|
100
101
|
|
101
|
-
|
102
|
+
def test_pull_all_from_embedded_hash
|
102
103
|
p1 = Person.new(:name => "Jordan")
|
103
104
|
p1.insert!
|
104
105
|
p1.push_all!("contacts.coworkers", ["Chris","Jim","Keith","Jordan","Mike","Joe"])
|
@@ -109,7 +110,7 @@ class TestModifiers < Test::Unit::TestCase
|
|
109
110
|
assert_equal ["Chris","Keith","Jordan","Mike"], p1["contacts"]["coworkers"]
|
110
111
|
end
|
111
112
|
|
112
|
-
|
113
|
+
def test_inc
|
113
114
|
p1 = Person.new(:name => "Jordan")
|
114
115
|
assert p1.insert!
|
115
116
|
p1["count1"] = 5
|
@@ -123,7 +124,7 @@ class TestModifiers < Test::Unit::TestCase
|
|
123
124
|
assert_equal -4, p1["count2"]
|
124
125
|
end
|
125
126
|
|
126
|
-
|
127
|
+
def test_inc_in_embedded_hash
|
127
128
|
p1 = Person.new(:name => "Jordan")
|
128
129
|
assert p1.insert!
|
129
130
|
p1.inc!("counters.visitors", 10)
|
@@ -135,7 +136,7 @@ class TestModifiers < Test::Unit::TestCase
|
|
135
136
|
assert_equal 20, p1["level1"]["level2"]["level3"]["counter"]
|
136
137
|
end
|
137
138
|
|
138
|
-
|
139
|
+
def test_set
|
139
140
|
p1 = Person.new(:name => "Jordan")
|
140
141
|
assert p1.insert!
|
141
142
|
assert p1.set!("foo", "bar")
|
@@ -144,7 +145,7 @@ class TestModifiers < Test::Unit::TestCase
|
|
144
145
|
assert_equal "bar", p1["foo"]
|
145
146
|
end
|
146
147
|
|
147
|
-
|
148
|
+
def test_set_in_embedded_hash
|
148
149
|
p1 = Person.new(:name => "Jordan")
|
149
150
|
assert p1.insert!
|
150
151
|
p1.set!("l1.l2.l3.l4.name", "Ben")
|
@@ -153,7 +154,7 @@ class TestModifiers < Test::Unit::TestCase
|
|
153
154
|
assert_equal "Ben", p1["l1"]["l2"]["l3"]["l4"]["name"]
|
154
155
|
end
|
155
156
|
|
156
|
-
|
157
|
+
def test_unset
|
157
158
|
p1 = Person.new(:name => "Jordan")
|
158
159
|
assert p1.insert!
|
159
160
|
assert p1.set!("foo", "bar")
|
@@ -167,7 +168,7 @@ class TestModifiers < Test::Unit::TestCase
|
|
167
168
|
assert p1["foo"].nil?
|
168
169
|
end
|
169
170
|
|
170
|
-
|
171
|
+
def test_unset_in_embedded_hash
|
171
172
|
p1 = Person.new(:name => "Jordan")
|
172
173
|
assert p1.insert!
|
173
174
|
p1.set!("l1.l2.l3.l4.name", "Ben")
|
@@ -185,7 +186,7 @@ class TestModifiers < Test::Unit::TestCase
|
|
185
186
|
assert !p1["l1"]["l2"]["l3"]["l4"].has_key?("name")
|
186
187
|
end
|
187
188
|
|
188
|
-
|
189
|
+
def test_add_to_set
|
189
190
|
p1 = Person.new(:name => "Jordan")
|
190
191
|
assert p1.insert!
|
191
192
|
|
@@ -201,7 +202,7 @@ class TestModifiers < Test::Unit::TestCase
|
|
201
202
|
assert_equal ["grey","blue"], p1["cold_colors"]
|
202
203
|
end
|
203
204
|
|
204
|
-
|
205
|
+
def test_add_to_set_in_embedded_hash
|
205
206
|
p1 = Person.new(:name => "Jordan")
|
206
207
|
assert p1.insert!
|
207
208
|
p1.add_to_set!("colors.hot", ["red", "pink", "orange"])
|
@@ -210,7 +211,7 @@ class TestModifiers < Test::Unit::TestCase
|
|
210
211
|
assert_equal ["red", "pink", "orange"], p1["colors"]["hot"]
|
211
212
|
end
|
212
213
|
|
213
|
-
|
214
|
+
def test_pop_last
|
214
215
|
p1 = Person.new(:name => "Jordan")
|
215
216
|
p1["numbers"] = [1,2,3,4,5]
|
216
217
|
assert p1.insert!
|
@@ -220,7 +221,7 @@ class TestModifiers < Test::Unit::TestCase
|
|
220
221
|
assert_equal [1,2,3,4], p1["numbers"]
|
221
222
|
end
|
222
223
|
|
223
|
-
|
224
|
+
def test_pop_last_in_embedded_hash
|
224
225
|
p1 = Person.new(:name => "Jordan")
|
225
226
|
p1["stats"] = { "numbers" => [1,2,3,4,5] }
|
226
227
|
assert p1.insert!
|
@@ -230,7 +231,7 @@ class TestModifiers < Test::Unit::TestCase
|
|
230
231
|
assert_equal [1,2,3,4], p1["stats"]["numbers"]
|
231
232
|
end
|
232
233
|
|
233
|
-
|
234
|
+
def test_pop_first
|
234
235
|
p1 = Person.new(:name => "Jordan")
|
235
236
|
p1["numbers"] = [1,2,3,4,5]
|
236
237
|
assert p1.insert!
|
@@ -240,7 +241,7 @@ class TestModifiers < Test::Unit::TestCase
|
|
240
241
|
assert_equal [2,3,4,5], p1["numbers"]
|
241
242
|
end
|
242
243
|
|
243
|
-
|
244
|
+
def test_pop_first_in_embedded_hash
|
244
245
|
p1 = Person.new(:name => "Jordan")
|
245
246
|
p1["stats"] = { "numbers" => [1,2,3,4,5] }
|
246
247
|
assert p1.insert!
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'helper'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
class TestPersistence < MiniTest::Unit::TestCase
|
5
|
+
def test_insert_update_remove
|
6
|
+
Person.collection.drop
|
7
|
+
|
8
|
+
p = Person.new
|
9
|
+
|
10
|
+
assert !p.valid?
|
11
|
+
assert_equal(["name can't be empty"], p.errors.full_messages)
|
12
|
+
|
13
|
+
p["name"] = "Ben Myles"
|
14
|
+
p["birth_year"] = 1984
|
15
|
+
p["created_at"] = Time.now.utc
|
16
|
+
p["admin"] = true
|
17
|
+
|
18
|
+
assert !p.update
|
19
|
+
|
20
|
+
assert p.insert.is_a?(BSON::ObjectId)
|
21
|
+
|
22
|
+
assert_equal 1, Person.collection.count
|
23
|
+
|
24
|
+
cursor = Person.find({"_id" => p["_id"]})
|
25
|
+
found = cursor.next
|
26
|
+
assert_equal p, found
|
27
|
+
assert_equal "Ben Myles", found["name"]
|
28
|
+
|
29
|
+
p["name"] = "Benjamin"
|
30
|
+
assert p.update
|
31
|
+
|
32
|
+
cursor = Person.find({"_id" => p["_id"]})
|
33
|
+
found = cursor.next
|
34
|
+
assert_equal p, found
|
35
|
+
assert_equal "Benjamin", found["name"]
|
36
|
+
|
37
|
+
assert p.remove
|
38
|
+
assert p.removed?
|
39
|
+
cursor = Person.find({"_id" => p["_id"]})
|
40
|
+
found = cursor.next
|
41
|
+
assert_nil found
|
42
|
+
end
|
43
|
+
end
|