mongo_mapper 0.11.0 → 0.11.1

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.
@@ -482,6 +482,30 @@ class QueryingTesting < Test::Unit::TestCase
482
482
  end
483
483
  end
484
484
 
485
+ context ".size" do
486
+ should "return 0 if no documents" do
487
+ @document.count.should == 0
488
+ end
489
+
490
+ should "return the number of documents" do
491
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
492
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
493
+ @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
494
+ @document.count.should == 3
495
+ end
496
+ end
497
+
498
+ context ".empty?" do
499
+ should "be true if no documents" do
500
+ @document.empty?.should be_true
501
+ end
502
+
503
+ should "be false if documents present" do
504
+ @doc = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
505
+ @document.empty?.should be_false
506
+ end
507
+ end
508
+
485
509
  context ".exists?" do
486
510
  setup do
487
511
  @doc = @document.create(:first_name => "James", :age => 27)
@@ -505,6 +529,19 @@ class QueryingTesting < Test::Unit::TestCase
505
529
  end
506
530
  end
507
531
 
532
+ context "to_a" do
533
+ should "return an array" do
534
+ @document.to_a.class.should == Array
535
+ end
536
+
537
+ should "return everything" do
538
+ @doc1 = @document.create({:first_name => 'John', :last_name => 'Nunemaker', :age => '27'})
539
+ @doc2 = @document.create({:first_name => 'Steve', :last_name => 'Smith', :age => '28'})
540
+ @doc3 = @document.create({:first_name => 'Steph', :last_name => 'Nunemaker', :age => '26'})
541
+ @document.to_a.size.should == 3
542
+ end
543
+ end
544
+
508
545
  context ".where" do
509
546
  setup do
510
547
  @doc1 = @document.create(:first_name => 'John', :last_name => 'Nunemaker', :age => '27')
@@ -47,16 +47,16 @@ class TimestampsTest < Test::Unit::TestCase
47
47
 
48
48
  should "set updated_at on document update but leave created_at alone" do
49
49
  doc = @klass.create(:first_name => 'John', :age => 27)
50
- old_created_at = doc.created_at
51
- old_updated_at = doc.updated_at
50
+ old_created_at = doc.created_at.to_f
52
51
 
53
- Timecop.freeze(Time.now + 5.seconds) do
52
+ new_updated_at = Time.now + 5.seconds
53
+ Timecop.freeze(new_updated_at) do
54
54
  @klass.update(doc._id, { :first_name => 'Johnny' })
55
55
  end
56
56
 
57
57
  doc = doc.reload
58
- doc.created_at.should == old_created_at
59
- doc.updated_at.should_not == old_updated_at
58
+ doc.created_at.to_f.should be_close(old_created_at, 0.001)
59
+ doc.updated_at.to_f.should be_close(new_updated_at.to_f, 0.001)
60
60
  end
61
61
  end
62
62
  end
@@ -0,0 +1,125 @@
1
+ require 'test_helper'
2
+
3
+ class TouchTest < Test::Unit::TestCase
4
+ context "touch" do
5
+ context "document" do
6
+ setup do
7
+ @document = Doc { timestamps! }
8
+ end
9
+
10
+ should "update the updated_at timestamp" do
11
+ doc = @document.create
12
+ old_updated_at = doc.updated_at
13
+
14
+ Timecop.freeze(Time.now + 1.day) do
15
+ doc.touch
16
+ end
17
+
18
+ doc.reload
19
+ doc.updated_at.should_not == old_updated_at
20
+ end
21
+ end
22
+
23
+ context "embedded document" do
24
+ should "update the updated_at timestamp" do
25
+ Doc = Doc("Document") { timestamps!}
26
+ Emdoc = EDoc("EmbeddedDocument") { timestamps! }
27
+ Doc.has_many :emdocs, :class => Emdoc
28
+
29
+ doc = Doc.create
30
+ emdoc = Emdoc.new
31
+ doc.emdocs << emdoc
32
+ doc.save
33
+
34
+ old_updated_at = emdoc.updated_at
35
+ document_old_updated_at = doc.updated_at
36
+
37
+ Timecop.freeze(Time.now + 1.day) do
38
+ emdoc.touch
39
+ end
40
+
41
+ doc.reload
42
+ emdoc.updated_at.should_not == old_updated_at
43
+ doc.updated_at.should_not == document_old_updated_at
44
+ end
45
+ end
46
+
47
+ context "association" do
48
+ setup do
49
+ @post_class = Doc("Post") do
50
+ key :touched_at, DateTime
51
+ timestamps!
52
+ end
53
+ @comment_class = Doc("Comment") do
54
+ key :post_id, ObjectId
55
+ key :text, String
56
+ timestamps!
57
+ end
58
+
59
+ @post_class.many :comments, :class => @comment_class
60
+ end
61
+
62
+ should 'not be true by default' do
63
+ @comment_class.belongs_to :post, :class => @post_class
64
+ @comment_class.associations[:post].touch?.should_not be_true
65
+ end
66
+
67
+ context 'touch the parent when true' do
68
+ setup do
69
+ @comment_class.belongs_to :post, :class => @post_class, :touch => true
70
+ @post = @post_class.create(:title => 'Hello, world!')
71
+ @comment = @post.comments.build
72
+ end
73
+
74
+ should "when the child is created" do
75
+ orig_updated_at = @post.updated_at
76
+ Timecop.freeze(Time.now + 1.day) do
77
+ @comment.save
78
+ end
79
+
80
+ @post.reload.updated_at.should_not == orig_updated_at
81
+ end
82
+
83
+ should "when the child is updated" do
84
+ @comment.save
85
+ old_updated_at = @post.updated_at
86
+ Timecop.freeze(Time.now + 2.day) do
87
+ @comment.update_attributes(:text => "Something")
88
+ end
89
+ @post.reload.updated_at.should_not == old_updated_at
90
+ end
91
+
92
+ should "when the child is touched" do
93
+ @comment.save
94
+ old_updated_at = @post.updated_at
95
+ Timecop.freeze(Time.now + 3.day) do
96
+ @comment.touch
97
+ end
98
+ @post.reload.updated_at.should_not == old_updated_at
99
+ end
100
+ end
101
+
102
+ context "when set to a symbol that is a key of parent" do
103
+ should "set that key on touch events" do
104
+ @comment_class.belongs_to :post, :class => @post_class, :touch => :touched_at
105
+ post = @post_class.create(:title => 'Hello, world!')
106
+ post.touched_at.should be_nil
107
+
108
+ comment = post.comments.build
109
+ comment.save
110
+ post.reload.touched_at.should_not be_nil
111
+ end
112
+ end
113
+
114
+ should 'not touch the parent when false' do
115
+ post = @post_class.create(:title => 'Hello, world!')
116
+ comment = post.comments.build
117
+ Timecop.freeze(Time.now + 1.day) do
118
+ comment.save
119
+ end
120
+
121
+ post.reload.updated_at.should == post.created_at
122
+ end
123
+ end
124
+ end
125
+ end
@@ -133,4 +133,14 @@ class AssociationBaseTest < Test::Unit::TestCase
133
133
  end
134
134
  end
135
135
 
136
+ context "touch?" do
137
+ should "be true if touch" do
138
+ BelongsToAssociation.new(:car, :touch => true).touch?.should be_true
139
+ end
140
+
141
+ should "be false if not touch" do
142
+ BelongsToAssociation.new(:car).touch?.should be_false
143
+ end
144
+ end
145
+
136
146
  end
@@ -282,7 +282,7 @@ class SupportTest < Test::Unit::TestCase
282
282
  end
283
283
 
284
284
  should "be time to milliseconds if string" do
285
- Time.to_mongo('2000-01-01 01:01:01.123456').to_f.should == Time.local(2000, 1, 1, 1, 1, 1, 0).utc.to_f
285
+ Time.to_mongo('2000-01-01 01:01:01.123456').to_f.should be_close(Time.local(2000, 1, 1, 1, 1, 1, 123456).utc.to_f, 0.0000001)
286
286
  end
287
287
 
288
288
  should "be time in utc if time" do
@@ -301,18 +301,18 @@ class SupportTest < Test::Unit::TestCase
301
301
  context "Time.to_mongo with Time.zone" do
302
302
  should "be time to milliseconds if time" do
303
303
  Time.zone = 'Hawaii'
304
- Time.to_mongo(Time.zone.local(2009, 8, 15, 14, 0, 0, 123456)).to_f.should == Time.utc(2009, 8, 16, 0, 0, 0, 0).to_f
304
+ Time.to_mongo(Time.zone.local(2009, 8, 15, 14, 0, 0, 123456)).to_f.should be_close(Time.utc(2009, 8, 16, 0, 0, 0, 123456).to_f, 0.0000001)
305
305
  Time.zone = nil
306
306
  end
307
307
 
308
308
  should "be time to milliseconds if string" do
309
309
  Time.zone = 'Hawaii'
310
- Time.to_mongo('2009-08-15 14:00:00.123456').to_f.should == Time.utc(2009, 8, 16, 0, 0, 0, 0).to_f
310
+ Time.to_mongo('2009-08-15 14:00:00.123456').to_f.should be_close(Time.utc(2009, 8, 16, 0, 0, 0, 123456).to_f, 0.0000001)
311
311
  Time.zone = nil
312
312
  end
313
313
 
314
314
  should "not round up times at the end of the month" do
315
- Time.to_mongo(Time.now.end_of_month).to_i.should == Time.now.end_of_month.utc.to_i
315
+ Time.to_mongo(Time.now.end_of_month).to_f.should be_close(Time.now.end_of_month.utc.to_f, 0.0000001)
316
316
  end
317
317
 
318
318
  should "be nil if blank string" do
@@ -23,5 +23,25 @@ class InspectTest < Test::Unit::TestCase
23
23
  should "include class name" do
24
24
  @doc.inspect.should =~ /^#<User/
25
25
  end
26
+
27
+ should "include embedded documents" do
28
+ klass = Doc()
29
+ pets = EDoc()
30
+
31
+ klass.many :pets, :class => pets
32
+
33
+ doc = klass.new(:pets => [{:name => "Kitten"}])
34
+ doc.inspect.should =~ /_id:.*, pets: \[.*_id.*, name: "Kitten".*\]/
35
+ end
36
+
37
+ should "include embedded document" do
38
+ klass = Doc()
39
+ pet = EDoc()
40
+
41
+ klass.one :pet, :class => pet
42
+
43
+ doc = klass.new(:pet => {:name => "Kitten"})
44
+ doc.inspect.should =~ /_id:.*, pet: .*_id.*, name: "Kitten".*/
45
+ end
26
46
  end
27
47
  end
@@ -56,6 +56,16 @@ class MongoMapperTest < Test::Unit::TestCase
56
56
  MongoMapper.connect('development')
57
57
  end
58
58
 
59
+ should "work with sinatra environment symbol" do
60
+ MongoMapper.config = {
61
+ 'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
62
+ }
63
+ Mongo::Connection.expects(:new).with('127.0.0.1', 27017, {})
64
+ MongoMapper.expects(:database=).with('test')
65
+ Mongo::DB.any_instance.expects(:authenticate).never
66
+ MongoMapper.connect(:development)
67
+ end
68
+
59
69
  should "work with options" do
60
70
  MongoMapper.config = {
61
71
  'development' => {'host' => '127.0.0.1', 'port' => 27017, 'database' => 'test'}
@@ -9,6 +9,11 @@ class TimeZonesTest < Test::Unit::TestCase
9
9
  end
10
10
  end
11
11
 
12
+ should "preserve milliseconds" do
13
+ doc = @document.new(:created_at => '2011-02-12 16:01:02.543Z')
14
+ doc.created_at.should be_close(Time.parse('2011-02-12 16:01:02.543Z'), 0.0000001)
15
+ end
16
+
12
17
  should "work without Time.zone" do
13
18
  Time.zone = nil
14
19
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_mapper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 51
4
+ hash: 49
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 11
9
- - 0
10
- version: 0.11.0
9
+ - 1
10
+ version: 0.11.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - John Nunemaker
@@ -15,13 +15,9 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-26 00:00:00 -05:00
19
- default_executable:
18
+ date: 2012-03-30 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
- type: :runtime
23
- name: activemodel
24
- prerelease: false
25
21
  version_requirements: &id001 !ruby/object:Gem::Requirement
26
22
  none: false
27
23
  requirements:
@@ -32,11 +28,11 @@ dependencies:
32
28
  - 3
33
29
  - 0
34
30
  version: "3.0"
31
+ prerelease: false
35
32
  requirement: *id001
36
- - !ruby/object:Gem::Dependency
33
+ name: activemodel
37
34
  type: :runtime
38
- name: activesupport
39
- prerelease: false
35
+ - !ruby/object:Gem::Dependency
40
36
  version_requirements: &id002 !ruby/object:Gem::Requirement
41
37
  none: false
42
38
  requirements:
@@ -47,11 +43,11 @@ dependencies:
47
43
  - 3
48
44
  - 0
49
45
  version: "3.0"
46
+ prerelease: false
50
47
  requirement: *id002
51
- - !ruby/object:Gem::Dependency
48
+ name: activesupport
52
49
  type: :runtime
53
- name: plucky
54
- prerelease: false
50
+ - !ruby/object:Gem::Dependency
55
51
  version_requirements: &id003 !ruby/object:Gem::Requirement
56
52
  none: false
57
53
  requirements:
@@ -63,7 +59,10 @@ dependencies:
63
59
  - 4
64
60
  - 0
65
61
  version: 0.4.0
62
+ prerelease: false
66
63
  requirement: *id003
64
+ name: plucky
65
+ type: :runtime
67
66
  description:
68
67
  email:
69
68
  - nunemaker@gmail.com
@@ -161,6 +160,7 @@ files:
161
160
  - lib/mongo_mapper/plugins/scopes.rb
162
161
  - lib/mongo_mapper/plugins/serialization.rb
163
162
  - lib/mongo_mapper/plugins/timestamps.rb
163
+ - lib/mongo_mapper/plugins/touch.rb
164
164
  - lib/mongo_mapper/plugins/userstamps.rb
165
165
  - lib/mongo_mapper/plugins/validations.rb
166
166
  - lib/mongo_mapper/plugins.rb
@@ -207,6 +207,7 @@ files:
207
207
  - test/functional/test_sci.rb
208
208
  - test/functional/test_scopes.rb
209
209
  - test/functional/test_timestamps.rb
210
+ - test/functional/test_touch.rb
210
211
  - test/functional/test_userstamps.rb
211
212
  - test/functional/test_validations.rb
212
213
  - test/models.rb
@@ -247,7 +248,6 @@ files:
247
248
  - LICENSE
248
249
  - UPGRADES
249
250
  - README.rdoc
250
- has_rdoc: true
251
251
  homepage: http://github.com/jnunemaker/mongomapper
252
252
  licenses: []
253
253
 
@@ -277,7 +277,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
277
277
  requirements: []
278
278
 
279
279
  rubyforge_project:
280
- rubygems_version: 1.6.1
280
+ rubygems_version: 1.8.15
281
281
  signing_key:
282
282
  specification_version: 3
283
283
  summary: A Ruby Object Mapper for Mongo