mongo_mapper_ign 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. data/.gitignore +10 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +35 -0
  4. data/Rakefile +37 -0
  5. data/bin/mmconsole +60 -0
  6. data/lib/mongo_mapper.rb +116 -0
  7. data/lib/mongo_mapper/document.rb +313 -0
  8. data/lib/mongo_mapper/embedded_document.rb +70 -0
  9. data/lib/mongo_mapper/plugins.rb +35 -0
  10. data/lib/mongo_mapper/plugins/associations.rb +114 -0
  11. data/lib/mongo_mapper/plugins/associations/base.rb +123 -0
  12. data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +30 -0
  13. data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +25 -0
  14. data/lib/mongo_mapper/plugins/associations/collection.rb +21 -0
  15. data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +39 -0
  16. data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +144 -0
  17. data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
  18. data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +129 -0
  19. data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +31 -0
  20. data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +23 -0
  21. data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +13 -0
  22. data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +41 -0
  23. data/lib/mongo_mapper/plugins/associations/one_proxy.rb +69 -0
  24. data/lib/mongo_mapper/plugins/associations/proxy.rb +124 -0
  25. data/lib/mongo_mapper/plugins/callbacks.rb +240 -0
  26. data/lib/mongo_mapper/plugins/clone.rb +13 -0
  27. data/lib/mongo_mapper/plugins/descendants.rb +16 -0
  28. data/lib/mongo_mapper/plugins/dirty.rb +119 -0
  29. data/lib/mongo_mapper/plugins/equality.rb +23 -0
  30. data/lib/mongo_mapper/plugins/identity_map.rb +122 -0
  31. data/lib/mongo_mapper/plugins/inspect.rb +14 -0
  32. data/lib/mongo_mapper/plugins/keys.rb +345 -0
  33. data/lib/mongo_mapper/plugins/logger.rb +17 -0
  34. data/lib/mongo_mapper/plugins/modifiers.rb +107 -0
  35. data/lib/mongo_mapper/plugins/pagination.rb +24 -0
  36. data/lib/mongo_mapper/plugins/pagination/proxy.rb +72 -0
  37. data/lib/mongo_mapper/plugins/persistence.rb +68 -0
  38. data/lib/mongo_mapper/plugins/protected.rb +45 -0
  39. data/lib/mongo_mapper/plugins/rails.rb +57 -0
  40. data/lib/mongo_mapper/plugins/serialization.rb +91 -0
  41. data/lib/mongo_mapper/plugins/serialization/array.rb +56 -0
  42. data/lib/mongo_mapper/plugins/serialization/xml_serializer.rb +240 -0
  43. data/lib/mongo_mapper/plugins/timestamps.rb +21 -0
  44. data/lib/mongo_mapper/plugins/userstamps.rb +14 -0
  45. data/lib/mongo_mapper/plugins/validations.rb +46 -0
  46. data/lib/mongo_mapper/query.rb +143 -0
  47. data/lib/mongo_mapper/support.rb +218 -0
  48. data/lib/mongo_mapper/support/descendant_appends.rb +46 -0
  49. data/lib/mongo_mapper/support/find.rb +77 -0
  50. data/lib/mongo_mapper/version.rb +3 -0
  51. data/mongo_mapper.gemspec +214 -0
  52. data/mongo_mapper_ign.gemspec +217 -0
  53. data/performance/read_write.rb +52 -0
  54. data/specs.watchr +51 -0
  55. data/test/NOTE_ON_TESTING +1 -0
  56. data/test/active_model_lint_test.rb +13 -0
  57. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
  58. data/test/functional/associations/test_belongs_to_proxy.rb +101 -0
  59. data/test/functional/associations/test_in_array_proxy.rb +325 -0
  60. data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
  61. data/test/functional/associations/test_many_documents_proxy.rb +536 -0
  62. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
  63. data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
  64. data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
  65. data/test/functional/associations/test_one_embedded_proxy.rb +68 -0
  66. data/test/functional/associations/test_one_proxy.rb +196 -0
  67. data/test/functional/test_associations.rb +44 -0
  68. data/test/functional/test_binary.rb +27 -0
  69. data/test/functional/test_callbacks.rb +151 -0
  70. data/test/functional/test_dirty.rb +163 -0
  71. data/test/functional/test_document.rb +1219 -0
  72. data/test/functional/test_embedded_document.rb +210 -0
  73. data/test/functional/test_identity_map.rb +507 -0
  74. data/test/functional/test_indexing.rb +44 -0
  75. data/test/functional/test_logger.rb +20 -0
  76. data/test/functional/test_modifiers.rb +394 -0
  77. data/test/functional/test_pagination.rb +93 -0
  78. data/test/functional/test_protected.rb +163 -0
  79. data/test/functional/test_string_id_compatibility.rb +67 -0
  80. data/test/functional/test_timestamps.rb +64 -0
  81. data/test/functional/test_userstamps.rb +28 -0
  82. data/test/functional/test_validations.rb +342 -0
  83. data/test/models.rb +227 -0
  84. data/test/support/custom_matchers.rb +37 -0
  85. data/test/support/timing.rb +16 -0
  86. data/test/test_helper.rb +64 -0
  87. data/test/unit/associations/test_base.rb +212 -0
  88. data/test/unit/associations/test_proxy.rb +105 -0
  89. data/test/unit/serializers/test_json_serializer.rb +202 -0
  90. data/test/unit/test_descendant_appends.rb +71 -0
  91. data/test/unit/test_document.rb +225 -0
  92. data/test/unit/test_dynamic_finder.rb +123 -0
  93. data/test/unit/test_embedded_document.rb +657 -0
  94. data/test/unit/test_keys.rb +185 -0
  95. data/test/unit/test_mongo_mapper.rb +118 -0
  96. data/test/unit/test_pagination.rb +160 -0
  97. data/test/unit/test_plugins.rb +50 -0
  98. data/test/unit/test_query.rb +374 -0
  99. data/test/unit/test_rails.rb +181 -0
  100. data/test/unit/test_rails_compatibility.rb +52 -0
  101. data/test/unit/test_serialization.rb +51 -0
  102. data/test/unit/test_support.rb +382 -0
  103. data/test/unit/test_time_zones.rb +39 -0
  104. data/test/unit/test_validations.rb +544 -0
  105. metadata +327 -0
@@ -0,0 +1,217 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mongo_mapper_ign}
8
+ s.version = "0.7.4"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["John Nunemaker", "Chandra Patni", "Thomas Nguyen"]
12
+ s.date = %q{2010-04-29}
13
+ s.default_executable = %q{mmconsole}
14
+ s.email = %q{nunemaker@gmail.com,cpatni@ign.com,thnguyen@ign.com}
15
+ s.executables = ["mmconsole"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "bin/mmconsole",
26
+ "lib/mongo_mapper.rb",
27
+ "lib/mongo_mapper/document.rb",
28
+ "lib/mongo_mapper/embedded_document.rb",
29
+ "lib/mongo_mapper/plugins.rb",
30
+ "lib/mongo_mapper/plugins/associations.rb",
31
+ "lib/mongo_mapper/plugins/associations/base.rb",
32
+ "lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb",
33
+ "lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb",
34
+ "lib/mongo_mapper/plugins/associations/collection.rb",
35
+ "lib/mongo_mapper/plugins/associations/embedded_collection.rb",
36
+ "lib/mongo_mapper/plugins/associations/in_array_proxy.rb",
37
+ "lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb",
38
+ "lib/mongo_mapper/plugins/associations/many_documents_proxy.rb",
39
+ "lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb",
40
+ "lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb",
41
+ "lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb",
42
+ "lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb",
43
+ "lib/mongo_mapper/plugins/associations/one_proxy.rb",
44
+ "lib/mongo_mapper/plugins/associations/proxy.rb",
45
+ "lib/mongo_mapper/plugins/callbacks.rb",
46
+ "lib/mongo_mapper/plugins/clone.rb",
47
+ "lib/mongo_mapper/plugins/descendants.rb",
48
+ "lib/mongo_mapper/plugins/dirty.rb",
49
+ "lib/mongo_mapper/plugins/equality.rb",
50
+ "lib/mongo_mapper/plugins/identity_map.rb",
51
+ "lib/mongo_mapper/plugins/inspect.rb",
52
+ "lib/mongo_mapper/plugins/keys.rb",
53
+ "lib/mongo_mapper/plugins/logger.rb",
54
+ "lib/mongo_mapper/plugins/modifiers.rb",
55
+ "lib/mongo_mapper/plugins/pagination.rb",
56
+ "lib/mongo_mapper/plugins/pagination/proxy.rb",
57
+ "lib/mongo_mapper/plugins/persistence.rb",
58
+ "lib/mongo_mapper/plugins/protected.rb",
59
+ "lib/mongo_mapper/plugins/rails.rb",
60
+ "lib/mongo_mapper/plugins/serialization.rb",
61
+ "lib/mongo_mapper/plugins/serialization/array.rb",
62
+ "lib/mongo_mapper/plugins/serialization/xml_serializer.rb",
63
+ "lib/mongo_mapper/plugins/timestamps.rb",
64
+ "lib/mongo_mapper/plugins/userstamps.rb",
65
+ "lib/mongo_mapper/plugins/validations.rb",
66
+ "lib/mongo_mapper/query.rb",
67
+ "lib/mongo_mapper/support.rb",
68
+ "lib/mongo_mapper/support/descendant_appends.rb",
69
+ "lib/mongo_mapper/support/find.rb",
70
+ "lib/mongo_mapper/version.rb",
71
+ "mongo_mapper.gemspec",
72
+ "mongo_mapper_ign.gemspec",
73
+ "performance/read_write.rb",
74
+ "specs.watchr",
75
+ "test/NOTE_ON_TESTING",
76
+ "test/active_model_lint_test.rb",
77
+ "test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
78
+ "test/functional/associations/test_belongs_to_proxy.rb",
79
+ "test/functional/associations/test_in_array_proxy.rb",
80
+ "test/functional/associations/test_many_documents_as_proxy.rb",
81
+ "test/functional/associations/test_many_documents_proxy.rb",
82
+ "test/functional/associations/test_many_embedded_polymorphic_proxy.rb",
83
+ "test/functional/associations/test_many_embedded_proxy.rb",
84
+ "test/functional/associations/test_many_polymorphic_proxy.rb",
85
+ "test/functional/associations/test_one_embedded_proxy.rb",
86
+ "test/functional/associations/test_one_proxy.rb",
87
+ "test/functional/test_associations.rb",
88
+ "test/functional/test_binary.rb",
89
+ "test/functional/test_callbacks.rb",
90
+ "test/functional/test_dirty.rb",
91
+ "test/functional/test_document.rb",
92
+ "test/functional/test_embedded_document.rb",
93
+ "test/functional/test_identity_map.rb",
94
+ "test/functional/test_indexing.rb",
95
+ "test/functional/test_logger.rb",
96
+ "test/functional/test_modifiers.rb",
97
+ "test/functional/test_pagination.rb",
98
+ "test/functional/test_protected.rb",
99
+ "test/functional/test_string_id_compatibility.rb",
100
+ "test/functional/test_timestamps.rb",
101
+ "test/functional/test_userstamps.rb",
102
+ "test/functional/test_validations.rb",
103
+ "test/models.rb",
104
+ "test/support/custom_matchers.rb",
105
+ "test/support/timing.rb",
106
+ "test/test_helper.rb",
107
+ "test/unit/associations/test_base.rb",
108
+ "test/unit/associations/test_proxy.rb",
109
+ "test/unit/serializers/test_json_serializer.rb",
110
+ "test/unit/test_descendant_appends.rb",
111
+ "test/unit/test_document.rb",
112
+ "test/unit/test_dynamic_finder.rb",
113
+ "test/unit/test_embedded_document.rb",
114
+ "test/unit/test_keys.rb",
115
+ "test/unit/test_mongo_mapper.rb",
116
+ "test/unit/test_pagination.rb",
117
+ "test/unit/test_plugins.rb",
118
+ "test/unit/test_query.rb",
119
+ "test/unit/test_rails.rb",
120
+ "test/unit/test_rails_compatibility.rb",
121
+ "test/unit/test_serialization.rb",
122
+ "test/unit/test_support.rb",
123
+ "test/unit/test_time_zones.rb",
124
+ "test/unit/test_validations.rb"
125
+ ]
126
+ s.homepage = %q{http://github.com/rubyorchard/mongomapper}
127
+ s.rdoc_options = ["--charset=UTF-8"]
128
+ s.require_paths = ["lib"]
129
+ s.rubygems_version = %q{1.3.6}
130
+ s.summary = %q{A Ruby Object Mapper for Mongo including support for XML serialization}
131
+ s.test_files = [
132
+ "test/active_model_lint_test.rb",
133
+ "test/functional/associations/test_belongs_to_polymorphic_proxy.rb",
134
+ "test/functional/associations/test_belongs_to_proxy.rb",
135
+ "test/functional/associations/test_in_array_proxy.rb",
136
+ "test/functional/associations/test_many_documents_as_proxy.rb",
137
+ "test/functional/associations/test_many_documents_proxy.rb",
138
+ "test/functional/associations/test_many_embedded_polymorphic_proxy.rb",
139
+ "test/functional/associations/test_many_embedded_proxy.rb",
140
+ "test/functional/associations/test_many_polymorphic_proxy.rb",
141
+ "test/functional/associations/test_one_embedded_proxy.rb",
142
+ "test/functional/associations/test_one_proxy.rb",
143
+ "test/functional/test_associations.rb",
144
+ "test/functional/test_binary.rb",
145
+ "test/functional/test_callbacks.rb",
146
+ "test/functional/test_dirty.rb",
147
+ "test/functional/test_document.rb",
148
+ "test/functional/test_embedded_document.rb",
149
+ "test/functional/test_identity_map.rb",
150
+ "test/functional/test_indexing.rb",
151
+ "test/functional/test_logger.rb",
152
+ "test/functional/test_modifiers.rb",
153
+ "test/functional/test_pagination.rb",
154
+ "test/functional/test_protected.rb",
155
+ "test/functional/test_string_id_compatibility.rb",
156
+ "test/functional/test_timestamps.rb",
157
+ "test/functional/test_userstamps.rb",
158
+ "test/functional/test_validations.rb",
159
+ "test/models.rb",
160
+ "test/support/custom_matchers.rb",
161
+ "test/support/timing.rb",
162
+ "test/test_helper.rb",
163
+ "test/unit/associations/test_base.rb",
164
+ "test/unit/associations/test_proxy.rb",
165
+ "test/unit/serializers/test_json_serializer.rb",
166
+ "test/unit/test_descendant_appends.rb",
167
+ "test/unit/test_document.rb",
168
+ "test/unit/test_dynamic_finder.rb",
169
+ "test/unit/test_embedded_document.rb",
170
+ "test/unit/test_keys.rb",
171
+ "test/unit/test_mongo_mapper.rb",
172
+ "test/unit/test_pagination.rb",
173
+ "test/unit/test_plugins.rb",
174
+ "test/unit/test_query.rb",
175
+ "test/unit/test_rails.rb",
176
+ "test/unit/test_rails_compatibility.rb",
177
+ "test/unit/test_serialization.rb",
178
+ "test/unit/test_support.rb",
179
+ "test/unit/test_time_zones.rb",
180
+ "test/unit/test_validations.rb"
181
+ ]
182
+
183
+ if s.respond_to? :specification_version then
184
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
185
+ s.specification_version = 3
186
+
187
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
188
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.4"])
189
+ s.add_runtime_dependency(%q<mongo>, ["= 0.20.1"])
190
+ s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.8.4"])
191
+ s.add_development_dependency(%q<json>, [">= 1.2.3"])
192
+ s.add_development_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
193
+ s.add_development_dependency(%q<shoulda>, ["= 2.10.2"])
194
+ s.add_development_dependency(%q<timecop>, ["= 0.3.1"])
195
+ s.add_development_dependency(%q<mocha>, ["= 0.9.8"])
196
+ else
197
+ s.add_dependency(%q<activesupport>, [">= 2.3.4"])
198
+ s.add_dependency(%q<mongo>, ["= 0.20.1"])
199
+ s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.4"])
200
+ s.add_dependency(%q<json>, [">= 1.2.3"])
201
+ s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
202
+ s.add_dependency(%q<shoulda>, ["= 2.10.2"])
203
+ s.add_dependency(%q<timecop>, ["= 0.3.1"])
204
+ s.add_dependency(%q<mocha>, ["= 0.9.8"])
205
+ end
206
+ else
207
+ s.add_dependency(%q<activesupport>, [">= 2.3.4"])
208
+ s.add_dependency(%q<mongo>, ["= 0.20.1"])
209
+ s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.4"])
210
+ s.add_dependency(%q<json>, [">= 1.2.3"])
211
+ s.add_dependency(%q<jnunemaker-matchy>, ["= 0.4.0"])
212
+ s.add_dependency(%q<shoulda>, ["= 2.10.2"])
213
+ s.add_dependency(%q<timecop>, ["= 0.3.1"])
214
+ s.add_dependency(%q<mocha>, ["= 0.9.8"])
215
+ end
216
+ end
217
+
@@ -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)
@@ -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,13 @@
1
+ require 'test_helper'
2
+ # For testing against edge rails also.
3
+ # $:.unshift '/Users/jnunemaker/dev/ruby/rails/activemodel/lib'
4
+ require 'active_model'
5
+ require 'models'
6
+
7
+ class ActiveModelLintTest < ActiveModel::TestCase
8
+ include ActiveModel::Lint::Tests
9
+
10
+ def setup
11
+ @model = Post.new
12
+ end
13
+ end
@@ -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 = BSON::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