mongo_mapper-rails3 0.7.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/Gemfile +15 -0
- data/LICENSE +20 -0
- data/README.rdoc +60 -0
- data/Rakefile +58 -0
- data/VERSION +1 -0
- data/bin/mmconsole +60 -0
- data/lib/mongo_mapper.rb +131 -0
- data/lib/mongo_mapper/document.rb +439 -0
- data/lib/mongo_mapper/embedded_document.rb +68 -0
- data/lib/mongo_mapper/plugins.rb +30 -0
- data/lib/mongo_mapper/plugins/associations.rb +106 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +123 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +30 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +25 -0
- data/lib/mongo_mapper/plugins/associations/collection.rb +21 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +50 -0
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +141 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +120 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +31 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +23 -0
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +13 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
- data/lib/mongo_mapper/plugins/associations/proxy.rb +119 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +87 -0
- data/lib/mongo_mapper/plugins/clone.rb +14 -0
- data/lib/mongo_mapper/plugins/descendants.rb +17 -0
- data/lib/mongo_mapper/plugins/dirty.rb +120 -0
- data/lib/mongo_mapper/plugins/equality.rb +24 -0
- data/lib/mongo_mapper/plugins/identity_map.rb +124 -0
- data/lib/mongo_mapper/plugins/inspect.rb +15 -0
- data/lib/mongo_mapper/plugins/keys.rb +310 -0
- data/lib/mongo_mapper/plugins/logger.rb +19 -0
- data/lib/mongo_mapper/plugins/pagination.rb +26 -0
- data/lib/mongo_mapper/plugins/pagination/proxy.rb +72 -0
- data/lib/mongo_mapper/plugins/protected.rb +46 -0
- data/lib/mongo_mapper/plugins/rails.rb +46 -0
- data/lib/mongo_mapper/plugins/serialization.rb +50 -0
- data/lib/mongo_mapper/plugins/validations.rb +88 -0
- data/lib/mongo_mapper/query.rb +130 -0
- data/lib/mongo_mapper/support.rb +217 -0
- data/lib/mongo_mapper/support/descendant_appends.rb +46 -0
- data/lib/mongo_mapper/support/find.rb +77 -0
- data/mongo_mapper-rails3.gemspec +208 -0
- data/performance/read_write.rb +52 -0
- data/specs.watchr +51 -0
- data/test/NOTE_ON_TESTING +1 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +101 -0
- data/test/functional/associations/test_in_array_proxy.rb +321 -0
- data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
- data/test/functional/associations/test_many_documents_proxy.rb +453 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
- data/test/functional/associations/test_one_proxy.rb +161 -0
- data/test/functional/test_associations.rb +44 -0
- data/test/functional/test_binary.rb +27 -0
- data/test/functional/test_callbacks.rb +81 -0
- data/test/functional/test_dirty.rb +163 -0
- data/test/functional/test_document.rb +1244 -0
- data/test/functional/test_embedded_document.rb +125 -0
- data/test/functional/test_identity_map.rb +508 -0
- data/test/functional/test_logger.rb +20 -0
- data/test/functional/test_modifiers.rb +252 -0
- data/test/functional/test_pagination.rb +93 -0
- data/test/functional/test_protected.rb +161 -0
- data/test/functional/test_string_id_compatibility.rb +67 -0
- data/test/functional/test_validations.rb +329 -0
- data/test/models.rb +232 -0
- data/test/support/custom_matchers.rb +55 -0
- data/test/support/timing.rb +16 -0
- data/test/test_helper.rb +59 -0
- data/test/unit/associations/test_base.rb +207 -0
- data/test/unit/associations/test_proxy.rb +105 -0
- data/test/unit/serializers/test_json_serializer.rb +189 -0
- data/test/unit/test_descendant_appends.rb +71 -0
- data/test/unit/test_document.rb +231 -0
- data/test/unit/test_dynamic_finder.rb +123 -0
- data/test/unit/test_embedded_document.rb +663 -0
- data/test/unit/test_keys.rb +169 -0
- data/test/unit/test_lint.rb +8 -0
- data/test/unit/test_mongo_mapper.rb +125 -0
- data/test/unit/test_pagination.rb +160 -0
- data/test/unit/test_plugins.rb +51 -0
- data/test/unit/test_query.rb +334 -0
- data/test/unit/test_rails.rb +123 -0
- data/test/unit/test_rails_compatibility.rb +57 -0
- data/test/unit/test_serialization.rb +51 -0
- data/test/unit/test_support.rb +362 -0
- data/test/unit/test_time_zones.rb +39 -0
- data/test/unit/test_validations.rb +557 -0
- metadata +344 -0
@@ -0,0 +1,52 @@
|
|
1
|
+
# The purpose of this is to check finding, initializing,
|
2
|
+
# and creating objects (typecasting times/dates and booleans).
|
3
|
+
|
4
|
+
require 'pp'
|
5
|
+
require 'benchmark'
|
6
|
+
require 'rubygems'
|
7
|
+
|
8
|
+
# to test with slow version just do this:
|
9
|
+
# gem 'mongo_mapper', '0.6.10'
|
10
|
+
# and comment out this:
|
11
|
+
$:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
|
12
|
+
|
13
|
+
require 'mongo_mapper'
|
14
|
+
|
15
|
+
MongoMapper.database = 'testing'
|
16
|
+
|
17
|
+
class Foo
|
18
|
+
include MongoMapper::Document
|
19
|
+
key :approved, Boolean
|
20
|
+
key :count, Integer
|
21
|
+
key :approved_at, Time
|
22
|
+
key :expire_on, Date
|
23
|
+
timestamps!
|
24
|
+
end
|
25
|
+
Foo.collection.remove
|
26
|
+
|
27
|
+
Benchmark.bm(5) do |x|
|
28
|
+
ids = []
|
29
|
+
x.report("write") do
|
30
|
+
1000.times { |i| ids << Foo.create(:count => 0, :approved => true, :approved_at => Time.now, :expire_on => Date.today).id }
|
31
|
+
end
|
32
|
+
|
33
|
+
x.report("read ") do
|
34
|
+
ids.each { |id| Foo.first(:id => id) }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# I was get something like this on my puny macbook air:
|
39
|
+
# user system total real
|
40
|
+
# write 4.810000 0.090000 4.900000 ( 5.039949)
|
41
|
+
# read 2.730000 0.070000 2.800000 ( 2.990749)
|
42
|
+
#
|
43
|
+
#
|
44
|
+
# After these commits:
|
45
|
+
#
|
46
|
+
# * http://github.com/jnunemaker/mongomapper/commit/e5091fa140d5fae2721017b53027092233694ee5
|
47
|
+
# * http://github.com/jnunemaker/mongomapper/commit/c22bbde4fa1cfbc310d79cb0e50203310ffb03d1
|
48
|
+
#
|
49
|
+
# I'm now getting something like this:
|
50
|
+
# user system total real
|
51
|
+
# write 1.660000 0.050000 1.710000 ( 1.752676)
|
52
|
+
# read 1.060000 0.050000 1.110000 ( 1.263429)
|
data/specs.watchr
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
def growl(title, msg, img)
|
2
|
+
%x{growlnotify -m #{ msg.inspect} -t #{title.inspect} --image ~/.watchr/#{img}.png}
|
3
|
+
end
|
4
|
+
|
5
|
+
def form_growl_message(str)
|
6
|
+
results = str.split("\n").last
|
7
|
+
if results =~ /[1-9]\s(failure|error)s?/
|
8
|
+
growl "Test Results", "#{results}", "fail"
|
9
|
+
elsif results != ""
|
10
|
+
growl "Test Results", "#{results}", "pass"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def run(cmd)
|
15
|
+
puts(cmd)
|
16
|
+
output = ""
|
17
|
+
IO.popen(cmd) do |com|
|
18
|
+
com.each_char do |c|
|
19
|
+
print c
|
20
|
+
output << c
|
21
|
+
$stdout.flush
|
22
|
+
end
|
23
|
+
end
|
24
|
+
form_growl_message output
|
25
|
+
end
|
26
|
+
|
27
|
+
def run_test_file(file)
|
28
|
+
run %Q(ruby -I"lib:test" -rubygems #{file})
|
29
|
+
end
|
30
|
+
|
31
|
+
def run_all_tests
|
32
|
+
run "rake test"
|
33
|
+
end
|
34
|
+
|
35
|
+
def related_test_files(path)
|
36
|
+
Dir['test/**/*.rb'].select { |file| file =~ /test_#{File.basename(path)}/ }
|
37
|
+
end
|
38
|
+
|
39
|
+
watch('test/test_helper\.rb') { system('clear'); run_all_tests }
|
40
|
+
watch('test/.*/test_.*\.rb') { |m| system('clear'); run_test_file(m[0]) }
|
41
|
+
watch('lib/.*') { |m| related_test_files(m[0]).each { |file| run_test_file(file) } }
|
42
|
+
|
43
|
+
# Ctrl-\
|
44
|
+
Signal.trap('QUIT') do
|
45
|
+
puts " --- Running all tests ---\n\n"
|
46
|
+
run_all_tests
|
47
|
+
end
|
48
|
+
|
49
|
+
# Ctrl-C
|
50
|
+
Signal.trap('INT') { abort("\n") }
|
51
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
I am doing my best to keep unit and functional tests separate. As I see them, functional tests hit the database and should never care about internals. Unit tests do not hit the database.
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class BelongsToPolymorphicProxyTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
Status.collection.remove
|
7
|
+
Project.collection.remove
|
8
|
+
end
|
9
|
+
|
10
|
+
should "default to nil" do
|
11
|
+
status = Status.new
|
12
|
+
status.target.nil?.should be_true
|
13
|
+
status.target.inspect.should == "nil"
|
14
|
+
end
|
15
|
+
|
16
|
+
should "have boolean presence method" do
|
17
|
+
status = Status.new
|
18
|
+
status.target?.should be_false
|
19
|
+
|
20
|
+
status.target = Project.new(:name => 'mongomapper')
|
21
|
+
status.target?.should be_true
|
22
|
+
end
|
23
|
+
|
24
|
+
should "be able to replace the association" do
|
25
|
+
status = Status.new(:name => 'Foo!')
|
26
|
+
project = Project.new(:name => "mongomapper")
|
27
|
+
status.target = project
|
28
|
+
status.save.should be_true
|
29
|
+
|
30
|
+
status = status.reload
|
31
|
+
status.target.nil?.should be_false
|
32
|
+
status.target_id.should == project._id
|
33
|
+
status.target_type.should == "Project"
|
34
|
+
status.target.name.should == "mongomapper"
|
35
|
+
end
|
36
|
+
|
37
|
+
should "unset the association" do
|
38
|
+
status = Status.new(:name => 'Foo!')
|
39
|
+
project = Project.new(:name => "mongomapper")
|
40
|
+
status.target = project
|
41
|
+
status.save.should be_true
|
42
|
+
|
43
|
+
status = status.reload
|
44
|
+
status.target = nil
|
45
|
+
status.target_type.nil?.should be_true
|
46
|
+
status.target_id.nil?.should be_true
|
47
|
+
status.target.nil?.should be_true
|
48
|
+
end
|
49
|
+
|
50
|
+
context "association id set but document not found" do
|
51
|
+
setup do
|
52
|
+
@status = Status.new(:name => 'Foo!')
|
53
|
+
project = Project.new(:name => "mongomapper")
|
54
|
+
@status.target = project
|
55
|
+
@status.save.should be_true
|
56
|
+
project.destroy
|
57
|
+
end
|
58
|
+
|
59
|
+
should "return nil instead of raising error" do
|
60
|
+
@status.target.nil?.should be_true
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class BelongsToProxyTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@post_class = Doc()
|
7
|
+
@comment_class = Doc do
|
8
|
+
key :post_id, String
|
9
|
+
end
|
10
|
+
|
11
|
+
@comment_class.belongs_to :post, :class => @post_class
|
12
|
+
end
|
13
|
+
|
14
|
+
should "default to nil" do
|
15
|
+
@comment_class.new.post.nil?.should be_true
|
16
|
+
end
|
17
|
+
|
18
|
+
should "send object id to target" do
|
19
|
+
post = @post_class.new(:name => 'mongomapper')
|
20
|
+
comment = @comment_class.new(:name => 'Foo!', :post => post)
|
21
|
+
comment.save
|
22
|
+
|
23
|
+
comment.post.object_id.should == comment.post.target.object_id
|
24
|
+
end
|
25
|
+
|
26
|
+
should "have boolean presence method" do
|
27
|
+
comment = @comment_class.new(:name => 'Foo!')
|
28
|
+
comment.post?.should be_false
|
29
|
+
|
30
|
+
comment.post = @post_class.new(:name => 'mongomapper')
|
31
|
+
comment.post?.should be_true
|
32
|
+
end
|
33
|
+
|
34
|
+
should "be able to replace the association" do
|
35
|
+
post = @post_class.new(:name => 'mongomapper')
|
36
|
+
comment = @comment_class.new(:name => 'Foo!', :post => post)
|
37
|
+
comment.save.should be_true
|
38
|
+
|
39
|
+
comment = comment.reload
|
40
|
+
comment.post.should == post
|
41
|
+
comment.post.nil?.should be_false
|
42
|
+
end
|
43
|
+
|
44
|
+
should "unset the association" do
|
45
|
+
post = @post_class.new(:name => 'mongomapper')
|
46
|
+
comment = @comment_class.new(:name => 'Foo!', :post => post)
|
47
|
+
comment.save.should be_true
|
48
|
+
|
49
|
+
comment = comment.reload
|
50
|
+
comment.post = nil
|
51
|
+
comment.post.nil?.should be_true
|
52
|
+
end
|
53
|
+
|
54
|
+
should "return nil if id set but document not found" do
|
55
|
+
id = Mongo::ObjectID.new
|
56
|
+
@comment_class.new(:name => 'Foo', :post_id => id).post.nil?.should be_true
|
57
|
+
end
|
58
|
+
|
59
|
+
context ":dependent" do
|
60
|
+
setup do
|
61
|
+
# FIXME: make use of already defined models
|
62
|
+
class ::Property
|
63
|
+
include MongoMapper::Document
|
64
|
+
end
|
65
|
+
Property.collection.remove
|
66
|
+
|
67
|
+
class ::Thing
|
68
|
+
include MongoMapper::Document
|
69
|
+
key :name, String
|
70
|
+
end
|
71
|
+
Thing.collection.remove
|
72
|
+
end
|
73
|
+
|
74
|
+
teardown do
|
75
|
+
Object.send :remove_const, 'Property' if defined?(::Property)
|
76
|
+
Object.send :remove_const, 'Thing' if defined?(::Thing)
|
77
|
+
end
|
78
|
+
|
79
|
+
context "=> destroy" do
|
80
|
+
setup do
|
81
|
+
Property.key :thing_id, ObjectId
|
82
|
+
Property.belongs_to :thing, :dependent => :destroy
|
83
|
+
Thing.many :properties
|
84
|
+
|
85
|
+
@thing = Thing.create(:name => "Tree")
|
86
|
+
@property1 = Property.create
|
87
|
+
@property2 = Property.create
|
88
|
+
@property3 = Property.create
|
89
|
+
@thing.properties << @property1
|
90
|
+
@thing.properties << @property2
|
91
|
+
@thing.properties << @property3
|
92
|
+
end
|
93
|
+
|
94
|
+
should "not execute on a belongs_to association" do
|
95
|
+
Thing.count.should == 1
|
96
|
+
@property1.destroy
|
97
|
+
Thing.count.should == 1
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,321 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class InArrayProxyTest < Test::Unit::TestCase
|
4
|
+
context "description" do
|
5
|
+
setup do
|
6
|
+
class ::List
|
7
|
+
include MongoMapper::Document
|
8
|
+
key :name, String, :required => true
|
9
|
+
end
|
10
|
+
|
11
|
+
class ::User
|
12
|
+
include MongoMapper::Document
|
13
|
+
key :name, String, :required => true
|
14
|
+
key :list_ids, Array
|
15
|
+
many :lists, :in => :list_ids
|
16
|
+
end
|
17
|
+
User.collection.remove
|
18
|
+
List.collection.remove
|
19
|
+
end
|
20
|
+
|
21
|
+
teardown do
|
22
|
+
Object.send :remove_const, 'List' if defined?(::List)
|
23
|
+
Object.send :remove_const, 'User' if defined?(::User)
|
24
|
+
end
|
25
|
+
|
26
|
+
should "default reader to empty array" do
|
27
|
+
User.new.lists.should == []
|
28
|
+
end
|
29
|
+
|
30
|
+
should "allow adding to association like it was an array" do
|
31
|
+
user = User.new(:name => 'John')
|
32
|
+
user.lists << List.new(:name => 'Foo1!')
|
33
|
+
user.lists.push List.new(:name => 'Foo2!')
|
34
|
+
user.lists.concat List.new(:name => 'Foo3!')
|
35
|
+
user.lists.size.should == 3
|
36
|
+
end
|
37
|
+
|
38
|
+
should "ignore adding duplicate ids" do
|
39
|
+
user = User.create(:name => 'John')
|
40
|
+
list = List.create(:name => 'Foo')
|
41
|
+
user.lists << list
|
42
|
+
user.lists << list
|
43
|
+
user.lists << list
|
44
|
+
|
45
|
+
user.list_ids.should == [list.id]
|
46
|
+
user.lists.count.should == 1
|
47
|
+
end
|
48
|
+
|
49
|
+
should "be able to replace the association" do
|
50
|
+
user = User.new(:name => 'John')
|
51
|
+
list = List.new(:name => 'Foo')
|
52
|
+
user.lists = [list]
|
53
|
+
user.save.should be_true
|
54
|
+
|
55
|
+
user.reload
|
56
|
+
user.list_ids.should == [list.id]
|
57
|
+
user.lists.size.should == 1
|
58
|
+
user.lists[0].name.should == 'Foo'
|
59
|
+
end
|
60
|
+
|
61
|
+
context "create" do
|
62
|
+
setup do
|
63
|
+
@user = User.create(:name => 'John')
|
64
|
+
@list = @user.lists.create(:name => 'Foo!')
|
65
|
+
end
|
66
|
+
|
67
|
+
should "add id to key" do
|
68
|
+
@user.list_ids.should include(@list.id)
|
69
|
+
end
|
70
|
+
|
71
|
+
should "persist id addition to key in database" do
|
72
|
+
@user.reload
|
73
|
+
@user.list_ids.should include(@list.id)
|
74
|
+
end
|
75
|
+
|
76
|
+
should "add doc to association" do
|
77
|
+
@user.lists.should include(@list)
|
78
|
+
end
|
79
|
+
|
80
|
+
should "save doc" do
|
81
|
+
@list.should_not be_new
|
82
|
+
end
|
83
|
+
|
84
|
+
should "reset cache" do
|
85
|
+
@user.lists.size.should == 1
|
86
|
+
@user.lists.create(:name => 'Moo!')
|
87
|
+
@user.lists.size.should == 2
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context "create!" do
|
92
|
+
setup do
|
93
|
+
@user = User.create(:name => 'John')
|
94
|
+
@list = @user.lists.create!(:name => 'Foo!')
|
95
|
+
end
|
96
|
+
|
97
|
+
should "add id to key" do
|
98
|
+
@user.list_ids.should include(@list.id)
|
99
|
+
end
|
100
|
+
|
101
|
+
should "persist id addition to key in database" do
|
102
|
+
@user.reload
|
103
|
+
@user.list_ids.should include(@list.id)
|
104
|
+
end
|
105
|
+
|
106
|
+
should "add doc to association" do
|
107
|
+
@user.lists.should include(@list)
|
108
|
+
end
|
109
|
+
|
110
|
+
should "save doc" do
|
111
|
+
@list.should_not be_new
|
112
|
+
end
|
113
|
+
|
114
|
+
should "raise exception if invalid" do
|
115
|
+
assert_raises(MongoMapper::DocumentNotValid) do
|
116
|
+
@user.lists.create!
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
should "reset cache" do
|
121
|
+
@user.lists.size.should == 1
|
122
|
+
@user.lists.create!(:name => 'Moo!')
|
123
|
+
@user.lists.size.should == 2
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
context "Finding scoped to association" do
|
128
|
+
setup do
|
129
|
+
@user = User.create(:name => 'John')
|
130
|
+
@user2 = User.create(:name => 'Brandon')
|
131
|
+
@list1 = @user.lists.create!(:name => 'Foo 1', :position => 1)
|
132
|
+
@list2 = @user.lists.create!(:name => 'Foo 2', :position => 2)
|
133
|
+
@list3 = @user2.lists.create!(:name => 'Foo 3', :position => 1)
|
134
|
+
end
|
135
|
+
|
136
|
+
context "all" do
|
137
|
+
should "work" do
|
138
|
+
@user.lists.find(:all, :order => :position.asc).should == [@list1, @list2]
|
139
|
+
@user.lists.all(:order => :position.asc).should == [@list1, @list2]
|
140
|
+
end
|
141
|
+
|
142
|
+
should "work with conditions" do
|
143
|
+
@user.lists.find(:all, :name => 'Foo 1').should == [@list1]
|
144
|
+
@user.lists.all(:name => 'Foo 1').should == [@list1]
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
context "first" do
|
149
|
+
should "work" do
|
150
|
+
@user.lists.find(:first, :order => 'position').should == @list1
|
151
|
+
@user.lists.first(:order => 'position').should == @list1
|
152
|
+
end
|
153
|
+
|
154
|
+
should "work with conditions" do
|
155
|
+
@user.lists.find(:first, :position => 2).should == @list2
|
156
|
+
@user.lists.first(:position => 2).should == @list2
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
context "last" do
|
161
|
+
should "work" do
|
162
|
+
@user.lists.find(:last, :order => 'position').should == @list2
|
163
|
+
@user.lists.last(:order => 'position').should == @list2
|
164
|
+
end
|
165
|
+
|
166
|
+
should "work with conditions" do
|
167
|
+
@user.lists.find(:last, :position => 2, :order => 'position').should == @list2
|
168
|
+
@user.lists.last(:position => 2, :order => 'position').should == @list2
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
context "with one id" do
|
173
|
+
should "work for id in association" do
|
174
|
+
@user.lists.find(@list1.id).should == @list1
|
175
|
+
end
|
176
|
+
|
177
|
+
should "not work for id not in association" do
|
178
|
+
@user.lists.find(@list3.id).should be_nil
|
179
|
+
end
|
180
|
+
|
181
|
+
should "raise error when using ! and not found" do
|
182
|
+
assert_raises MongoMapper::DocumentNotFound do
|
183
|
+
@user.lists.find!(@list3.id)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
context "with multiple ids" do
|
189
|
+
should "work for ids in association" do
|
190
|
+
@user.lists.find(@list1.id, @list2.id).should == [@list1, @list2]
|
191
|
+
end
|
192
|
+
|
193
|
+
should "not work for ids not in association" do
|
194
|
+
@user.lists.find(@list1.id, @list2.id, @list3.id).should == [@list1, @list2]
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
context "with #paginate" do
|
199
|
+
setup do
|
200
|
+
@lists = @user.lists.paginate(:per_page => 1, :page => 1, :order => 'position')
|
201
|
+
end
|
202
|
+
|
203
|
+
should "return total pages" do
|
204
|
+
@lists.total_pages.should == 2
|
205
|
+
end
|
206
|
+
|
207
|
+
should "return total entries" do
|
208
|
+
@lists.total_entries.should == 2
|
209
|
+
end
|
210
|
+
|
211
|
+
should "return the subject" do
|
212
|
+
@lists.collect(&:name).should == ['Foo 1']
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
context "dynamic finders" do
|
217
|
+
should "work with single key" do
|
218
|
+
@user.lists.find_by_name('Foo 1').should == @list1
|
219
|
+
@user.lists.find_by_name!('Foo 1').should == @list1
|
220
|
+
@user.lists.find_by_name('Foo 3').should be_nil
|
221
|
+
end
|
222
|
+
|
223
|
+
should "work with multiple keys" do
|
224
|
+
@user.lists.find_by_name_and_position('Foo 1', 1).should == @list1
|
225
|
+
@user.lists.find_by_name_and_position!('Foo 1', 1).should == @list1
|
226
|
+
@user.lists.find_by_name_and_position('Foo 3', 1).should be_nil
|
227
|
+
end
|
228
|
+
|
229
|
+
should "raise error when using ! and not found" do
|
230
|
+
assert_raises(MongoMapper::DocumentNotFound) do
|
231
|
+
@user.lists.find_by_name!('Foo 3')
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
context "find_or_create_by" do
|
236
|
+
should "not create document if found" do
|
237
|
+
lambda {
|
238
|
+
list = @user.lists.find_or_create_by_name('Foo 1')
|
239
|
+
list.should == @list1
|
240
|
+
}.should_not change { List.count }
|
241
|
+
end
|
242
|
+
|
243
|
+
should "create document if not found" do
|
244
|
+
lambda {
|
245
|
+
list = @user.lists.find_or_create_by_name('Home')
|
246
|
+
@user.lists.should include(list)
|
247
|
+
}.should change { List.count }
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
context "count" do
|
254
|
+
setup do
|
255
|
+
@user = User.create(:name => 'John')
|
256
|
+
@user2 = User.create(:name => 'Brandon')
|
257
|
+
@list1 = @user.lists.create!(:name => 'Foo 1')
|
258
|
+
@list2 = @user.lists.create!(:name => 'Foo 2')
|
259
|
+
@list3 = @user2.lists.create!(:name => 'Foo 3')
|
260
|
+
end
|
261
|
+
|
262
|
+
should "return number of ids" do
|
263
|
+
@user.lists.count.should == 2
|
264
|
+
@user2.lists.count.should == 1
|
265
|
+
end
|
266
|
+
|
267
|
+
should "return correct count when given criteria" do
|
268
|
+
@user.lists.count(:name => 'Foo 1').should == 1
|
269
|
+
@user2.lists.count(:name => 'Foo 1').should == 0
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
context "Removing documents" do
|
274
|
+
setup do
|
275
|
+
@user = User.create(:name => 'John')
|
276
|
+
@user2 = User.create(:name => 'Brandon')
|
277
|
+
@list1 = @user.lists.create!(:name => 'Foo 1', :position => 1)
|
278
|
+
@list2 = @user.lists.create!(:name => 'Foo 2', :position => 2)
|
279
|
+
@list3 = @user2.lists.create!(:name => 'Foo 3', :position => 1)
|
280
|
+
end
|
281
|
+
|
282
|
+
context "destroy_all" do
|
283
|
+
should "work" do
|
284
|
+
@user.lists.count.should == 2
|
285
|
+
@user.lists.destroy_all
|
286
|
+
@user.lists.count.should == 0
|
287
|
+
end
|
288
|
+
|
289
|
+
should "work with conditions" do
|
290
|
+
@user.lists.count.should == 2
|
291
|
+
@user.lists.destroy_all(:name => 'Foo 1')
|
292
|
+
@user.lists.count.should == 1
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
296
|
+
context "delete_all" do
|
297
|
+
should "work" do
|
298
|
+
@user.lists.count.should == 2
|
299
|
+
@user.lists.delete_all
|
300
|
+
@user.lists.count.should == 0
|
301
|
+
end
|
302
|
+
|
303
|
+
should "work with conditions" do
|
304
|
+
@user.lists.count.should == 2
|
305
|
+
@user.lists.delete_all(:name => 'Foo 1')
|
306
|
+
@user.lists.count.should == 1
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
should "work with nullify" do
|
311
|
+
@user.lists.count.should == 2
|
312
|
+
|
313
|
+
lambda {
|
314
|
+
@user.lists.nullify
|
315
|
+
}.should_not change { List.count }
|
316
|
+
|
317
|
+
@user.lists.count.should == 0
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|
321
|
+
end
|