mongo_mapper-unstable 2009.10.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. data/.gitignore +8 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +50 -0
  4. data/Rakefile +87 -0
  5. data/VERSION +1 -0
  6. data/bin/mmconsole +55 -0
  7. data/lib/mongo_mapper/associations/base.rb +83 -0
  8. data/lib/mongo_mapper/associations/belongs_to_polymorphic_proxy.rb +34 -0
  9. data/lib/mongo_mapper/associations/belongs_to_proxy.rb +22 -0
  10. data/lib/mongo_mapper/associations/many_documents_as_proxy.rb +27 -0
  11. data/lib/mongo_mapper/associations/many_documents_proxy.rb +116 -0
  12. data/lib/mongo_mapper/associations/many_embedded_polymorphic_proxy.rb +33 -0
  13. data/lib/mongo_mapper/associations/many_embedded_proxy.rb +67 -0
  14. data/lib/mongo_mapper/associations/many_polymorphic_proxy.rb +11 -0
  15. data/lib/mongo_mapper/associations/many_proxy.rb +6 -0
  16. data/lib/mongo_mapper/associations/proxy.rb +74 -0
  17. data/lib/mongo_mapper/associations.rb +86 -0
  18. data/lib/mongo_mapper/callbacks.rb +106 -0
  19. data/lib/mongo_mapper/dirty.rb +137 -0
  20. data/lib/mongo_mapper/document.rb +340 -0
  21. data/lib/mongo_mapper/dynamic_finder.rb +35 -0
  22. data/lib/mongo_mapper/embedded_document.rb +355 -0
  23. data/lib/mongo_mapper/finder_options.rb +98 -0
  24. data/lib/mongo_mapper/key.rb +36 -0
  25. data/lib/mongo_mapper/observing.rb +50 -0
  26. data/lib/mongo_mapper/pagination.rb +51 -0
  27. data/lib/mongo_mapper/rails_compatibility/document.rb +15 -0
  28. data/lib/mongo_mapper/rails_compatibility/embedded_document.rb +27 -0
  29. data/lib/mongo_mapper/save_with_validation.rb +19 -0
  30. data/lib/mongo_mapper/serialization.rb +55 -0
  31. data/lib/mongo_mapper/serializers/json_serializer.rb +92 -0
  32. data/lib/mongo_mapper/support.rb +161 -0
  33. data/lib/mongo_mapper/validations.rb +69 -0
  34. data/lib/mongo_mapper.rb +111 -0
  35. data/mongo_mapper.gemspec +162 -0
  36. data/specs.watchr +32 -0
  37. data/test/NOTE_ON_TESTING +1 -0
  38. data/test/custom_matchers.rb +55 -0
  39. data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +55 -0
  40. data/test/functional/associations/test_belongs_to_proxy.rb +49 -0
  41. data/test/functional/associations/test_many_documents_as_proxy.rb +244 -0
  42. data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +132 -0
  43. data/test/functional/associations/test_many_embedded_proxy.rb +174 -0
  44. data/test/functional/associations/test_many_polymorphic_proxy.rb +297 -0
  45. data/test/functional/associations/test_many_proxy.rb +331 -0
  46. data/test/functional/test_associations.rb +44 -0
  47. data/test/functional/test_binary.rb +18 -0
  48. data/test/functional/test_callbacks.rb +85 -0
  49. data/test/functional/test_dirty.rb +138 -0
  50. data/test/functional/test_document.rb +1051 -0
  51. data/test/functional/test_embedded_document.rb +97 -0
  52. data/test/functional/test_logger.rb +20 -0
  53. data/test/functional/test_pagination.rb +87 -0
  54. data/test/functional/test_rails_compatibility.rb +30 -0
  55. data/test/functional/test_validations.rb +279 -0
  56. data/test/models.rb +195 -0
  57. data/test/test_helper.rb +30 -0
  58. data/test/unit/serializers/test_json_serializer.rb +189 -0
  59. data/test/unit/test_association_base.rb +144 -0
  60. data/test/unit/test_document.rb +184 -0
  61. data/test/unit/test_dynamic_finder.rb +125 -0
  62. data/test/unit/test_embedded_document.rb +656 -0
  63. data/test/unit/test_finder_options.rb +261 -0
  64. data/test/unit/test_key.rb +172 -0
  65. data/test/unit/test_mongomapper.rb +28 -0
  66. data/test/unit/test_observing.rb +101 -0
  67. data/test/unit/test_pagination.rb +109 -0
  68. data/test/unit/test_rails_compatibility.rb +39 -0
  69. data/test/unit/test_serializations.rb +52 -0
  70. data/test/unit/test_support.rb +291 -0
  71. data/test/unit/test_time_zones.rb +40 -0
  72. data/test/unit/test_validations.rb +503 -0
  73. metadata +210 -0
@@ -0,0 +1,85 @@
1
+ require 'test_helper'
2
+
3
+ class CallbacksTest < Test::Unit::TestCase
4
+ context "Defining and running callbacks" do
5
+ setup do
6
+ @document = Class.new do
7
+ include MongoMapper::Document
8
+ set_collection_name 'test'
9
+
10
+ key :name, String
11
+
12
+ [ :before_validation_on_create, :before_validation_on_update,
13
+ :before_validation, :after_validation,
14
+ :before_create, :after_create,
15
+ :before_update, :after_update,
16
+ :before_save, :after_save,
17
+ :before_destroy, :after_destroy].each do |callback|
18
+ callback_method = "#{callback}_callback"
19
+ send(callback, callback_method)
20
+ define_method(callback_method) do
21
+ history << callback.to_sym
22
+ end
23
+ end
24
+
25
+ def history
26
+ @history ||= []
27
+ end
28
+
29
+ def clear_history
30
+ @history = nil
31
+ end
32
+ end
33
+ @document.collection.clear
34
+ end
35
+
36
+ should "get the order right for creating documents" do
37
+ doc = @document.create(:name => 'John Nunemaker')
38
+ doc.history.should == [:before_validation, :before_validation_on_create, :after_validation, :before_save, :before_create, :after_create, :after_save]
39
+ end
40
+
41
+ should "get the order right for updating documents" do
42
+ doc = @document.create(:name => 'John Nunemaker')
43
+ doc.clear_history
44
+ doc.name = 'John'
45
+ doc.save
46
+ doc.history.should == [:before_validation, :before_validation_on_update, :after_validation, :before_save, :before_update, :after_update, :after_save]
47
+ end
48
+
49
+ should "work for before and after validation" do
50
+ doc = @document.new(:name => 'John Nunemaker')
51
+ doc.valid?
52
+ doc.history.should include(:before_validation)
53
+ doc.history.should include(:after_validation)
54
+ end
55
+
56
+ should "work for before and after create" do
57
+ doc = @document.create(:name => 'John Nunemaker')
58
+ doc.history.should include(:before_create)
59
+ doc.history.should include(:after_create)
60
+ end
61
+
62
+ should "work for before and after update" do
63
+ doc = @document.create(:name => 'John Nunemaker')
64
+ doc.name = 'John Doe'
65
+ doc.save
66
+ doc.history.should include(:before_update)
67
+ doc.history.should include(:after_update)
68
+ end
69
+
70
+ should "work for before and after save" do
71
+ doc = @document.new
72
+ doc.name = 'John Doe'
73
+ doc.save
74
+ doc.history.should include(:before_save)
75
+ doc.history.should include(:after_save)
76
+ end
77
+
78
+ should "work for before and after destroy" do
79
+ doc = @document.create(:name => 'John Nunemaker')
80
+ doc.destroy
81
+ doc.history.should include(:before_destroy)
82
+ doc.history.should include(:after_destroy)
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,138 @@
1
+ require 'test_helper'
2
+ require 'models'
3
+
4
+ class DirtyTest < Test::Unit::TestCase
5
+ def setup
6
+ @document = Class.new do
7
+ include MongoMapper::Document
8
+ set_collection_name 'test'
9
+ key :phrase, String
10
+ end
11
+ @document.collection.clear
12
+
13
+ Status.collection.clear
14
+ Project.collection.clear
15
+ end
16
+
17
+ context "marking changes" do
18
+ should "not happen if there are none" do
19
+ doc = @document.new
20
+ doc.phrase_changed?.should be_false
21
+ doc.phrase_change.should be_nil
22
+ end
23
+
24
+ should "happen when change happens" do
25
+ doc = @document.new
26
+ doc.phrase = 'Golly Gee Willikers Batman'
27
+ doc.phrase_changed?.should be_true
28
+ doc.phrase_was.should be_nil
29
+ doc.phrase_change.should == [nil, 'Golly Gee Willikers Batman']
30
+ end
31
+
32
+ should "clear changes on save" do
33
+ doc = @document.new
34
+ doc.phrase = 'Golly Gee Willikers Batman'
35
+ doc.phrase_changed?.should be_true
36
+ doc.save
37
+ doc.phrase_changed?.should_not be_true
38
+ doc.phrase_change.should be_nil
39
+ end
40
+
41
+ should "clear changes on save!" do
42
+ doc = @document.new
43
+ doc.phrase = 'Golly Gee Willikers Batman'
44
+ doc.phrase_changed?.should be_true
45
+ doc.save!
46
+ doc.phrase_changed?.should_not be_true
47
+ doc.phrase_change.should be_nil
48
+ end
49
+ end
50
+
51
+ context "blank new value and type integer" do
52
+ should "not mark changes" do
53
+ @document.key :age, Integer
54
+
55
+ [nil, ''].each do |value|
56
+ doc = @document.new
57
+ doc.age = value
58
+ doc.age_changed?.should be_false
59
+ doc.age_change.should be_nil
60
+ end
61
+ end
62
+ end
63
+
64
+ context "blank new value and type float" do
65
+ should "not mark changes" do
66
+ @document.key :amount, Float
67
+
68
+ [nil, ''].each do |value|
69
+ doc = @document.new
70
+ doc.amount = value
71
+ doc.amount_changed?.should be_false
72
+ doc.amount_change.should be_nil
73
+ end
74
+ end
75
+ end
76
+
77
+ context "changed?" do
78
+ should "be true if key changed" do
79
+ doc = @document.new
80
+ doc.phrase = 'A penny saved is a penny earned.'
81
+ doc.changed?.should be_true
82
+ end
83
+
84
+ should "be false if no keys changed" do
85
+ @document.new.changed?.should be_false
86
+ end
87
+ end
88
+
89
+ context "changes" do
90
+ should "be empty hash if no changes" do
91
+ @document.new.changes.should == {}
92
+ end
93
+
94
+ should "be hash of keys with values of changes if there are changes" do
95
+ doc = @document.new
96
+ doc.phrase = 'A penny saved is a penny earned.'
97
+ doc.changes.should == {'phrase' => [nil, 'A penny saved is a penny earned.']}
98
+ end
99
+ end
100
+
101
+ context "changed" do
102
+ should "be empty array if no changes" do
103
+ @document.new.changed.should == []
104
+ end
105
+
106
+ should "be array of keys that have changed if there are changes" do
107
+ doc = @document.new
108
+ doc.phrase = 'A penny saved is a penny earned.'
109
+ doc.changed.should == ['phrase']
110
+ end
111
+ end
112
+
113
+ context "will_change!" do
114
+ should "mark changes" do
115
+ doc = @document.create(:phrase => 'Foo')
116
+
117
+ doc.phrase << 'bar'
118
+ doc.phrase_changed?.should be_false
119
+
120
+ doc.phrase_will_change!
121
+ doc.phrase_changed?.should be_true
122
+ doc.phrase_change.should == ['Foobar', 'Foobar']
123
+
124
+ doc.phrase << '!'
125
+ doc.phrase_changed?.should be_true
126
+ doc.phrase_change.should == ['Foobar', 'Foobar!']
127
+ end
128
+ end
129
+
130
+ context "changing a foreign key through association" do
131
+ should "mark changes" do
132
+ status = Status.create(:name => 'Foo')
133
+ status.project = Project.create(:name => 'Bar')
134
+ status.changed?.should be_true
135
+ status.changed.should == %w(project_id)
136
+ end
137
+ end
138
+ end