langalex-couch_potato 0.1.1 → 0.2.0

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.
Files changed (61) hide show
  1. data/README.md +260 -0
  2. data/VERSION.yml +2 -2
  3. data/init.rb +3 -0
  4. data/lib/core_ext/date.rb +10 -0
  5. data/lib/core_ext/string.rb +15 -0
  6. data/lib/core_ext/time.rb +6 -9
  7. data/lib/couch_potato/database.rb +90 -0
  8. data/lib/couch_potato/persistence/belongs_to_property.rb +1 -1
  9. data/lib/couch_potato/persistence/callbacks.rb +14 -11
  10. data/lib/couch_potato/persistence/dirty_attributes.rb +13 -6
  11. data/lib/couch_potato/persistence/json.rb +9 -14
  12. data/lib/couch_potato/persistence/magic_timestamps.rb +13 -0
  13. data/lib/couch_potato/persistence/properties.rb +10 -8
  14. data/lib/couch_potato/persistence/simple_property.rb +14 -11
  15. data/lib/couch_potato/persistence.rb +11 -165
  16. data/lib/couch_potato/view/base_view_spec.rb +20 -0
  17. data/lib/couch_potato/view/custom_view_spec.rb +26 -0
  18. data/lib/couch_potato/view/custom_views.rb +30 -0
  19. data/lib/couch_potato/view/model_view_spec.rb +39 -0
  20. data/lib/couch_potato/view/properties_view_spec.rb +35 -0
  21. data/lib/couch_potato/view/raw_view_spec.rb +21 -0
  22. data/lib/couch_potato/view/view_query.rb +45 -0
  23. data/lib/couch_potato.rb +23 -6
  24. data/rails/init.rb +7 -0
  25. data/spec/callbacks_spec.rb +40 -43
  26. data/spec/create_spec.rb +9 -60
  27. data/spec/custom_view_spec.rb +93 -19
  28. data/spec/destroy_spec.rb +5 -4
  29. data/spec/property_spec.rb +22 -8
  30. data/spec/spec_helper.rb +12 -14
  31. data/spec/unit/attributes_spec.rb +26 -0
  32. data/spec/unit/create_spec.rb +58 -0
  33. data/spec/{dirty_attributes_spec.rb → unit/dirty_attributes_spec.rb} +31 -13
  34. data/spec/unit/string_spec.rb +13 -0
  35. data/spec/unit/view_query_spec.rb +2 -3
  36. data/spec/update_spec.rb +8 -7
  37. metadata +54 -32
  38. data/README.textile +0 -340
  39. data/lib/couch_potato/active_record/compatibility.rb +0 -9
  40. data/lib/couch_potato/ordering.rb +0 -84
  41. data/lib/couch_potato/persistence/bulk_save_queue.rb +0 -47
  42. data/lib/couch_potato/persistence/collection.rb +0 -51
  43. data/lib/couch_potato/persistence/custom_view.rb +0 -41
  44. data/lib/couch_potato/persistence/external_collection.rb +0 -83
  45. data/lib/couch_potato/persistence/external_has_many_property.rb +0 -72
  46. data/lib/couch_potato/persistence/find.rb +0 -21
  47. data/lib/couch_potato/persistence/finder.rb +0 -65
  48. data/lib/couch_potato/persistence/inline_has_many_property.rb +0 -43
  49. data/lib/couch_potato/persistence/view_query.rb +0 -81
  50. data/lib/couch_potato/versioning.rb +0 -46
  51. data/spec/attributes_spec.rb +0 -42
  52. data/spec/belongs_to_spec.rb +0 -55
  53. data/spec/find_spec.rb +0 -96
  54. data/spec/finder_spec.rb +0 -125
  55. data/spec/has_many_spec.rb +0 -241
  56. data/spec/inline_collection_spec.rb +0 -15
  57. data/spec/ordering_spec.rb +0 -95
  58. data/spec/reload_spec.rb +0 -50
  59. data/spec/unit/external_collection_spec.rb +0 -84
  60. data/spec/unit/finder_spec.rb +0 -10
  61. data/spec/versioning_spec.rb +0 -150
@@ -20,6 +20,9 @@ class CallbackRecorder
20
20
  self.send callback, callback
21
21
  end
22
22
 
23
+ attr_accessor :lambda_works
24
+ before_create lambda {|model| model.lambda_works = true}
25
+
23
26
  def callbacks
24
27
  @callbacks ||= []
25
28
  end
@@ -27,9 +30,7 @@ class CallbackRecorder
27
30
  end
28
31
 
29
32
  describe "multiple callbacks at once" do
30
- before(:all) do
31
- CouchPotato::Persistence.Db!
32
- end
33
+
33
34
  class Monkey
34
35
  include CouchPotato::Persistence
35
36
  attr_accessor :eaten_banana, :eaten_apple
@@ -47,19 +48,19 @@ describe "multiple callbacks at once" do
47
48
  end
48
49
  end
49
50
  it "should run all callback methods given to the callback method call" do
50
- monkey = Monkey.create!
51
+ monkey = Monkey.new
52
+ CouchPotato.database.save_document! monkey
51
53
  monkey.eaten_banana.should be_true
52
54
  monkey.eaten_apple.should be_true
53
55
  end
54
56
  end
55
57
 
56
58
  describe 'create callbacks' do
57
- before(:all) do
58
- CouchPotato::Persistence.Db!
59
- end
60
59
 
61
60
  before(:each) do
62
61
  @recorder = CallbackRecorder.new
62
+ couchrest_database = stub 'couchrest_database', :save_doc => {'id' => '1', 'rev' => '2'}
63
+ @db = CouchPotato::Database.new(couchrest_database)
63
64
  end
64
65
 
65
66
  describe "successful create" do
@@ -68,32 +69,32 @@ describe 'create callbacks' do
68
69
  end
69
70
 
70
71
  it "should call before_validation_on_create" do
71
- @recorder.save!
72
+ @db.save_document! @recorder
72
73
  @recorder.callbacks.should include(:before_validation_on_create)
73
74
  end
74
75
 
75
76
  it "should call before_validation_on_save" do
76
- @recorder.save!
77
+ @db.save_document! @recorder
77
78
  @recorder.callbacks.should include(:before_validation_on_save)
78
79
  end
79
80
 
80
81
  it "should call before_save" do
81
- @recorder.save!
82
+ @db.save_document! @recorder
82
83
  @recorder.callbacks.should include(:before_save)
83
84
  end
84
85
 
85
86
  it "should call after_save" do
86
- @recorder.save!
87
+ @db.save_document! @recorder
87
88
  @recorder.callbacks.should include(:after_save)
88
89
  end
89
90
 
90
91
  it "should call before_create" do
91
- @recorder.save!
92
+ @db.save_document! @recorder
92
93
  @recorder.callbacks.should include(:before_create)
93
94
  end
94
95
 
95
96
  it "should call after_create" do
96
- @recorder.save!
97
+ @db.save_document! @recorder
97
98
  @recorder.callbacks.should include(:after_create)
98
99
  end
99
100
 
@@ -102,47 +103,46 @@ describe 'create callbacks' do
102
103
  describe "failed create" do
103
104
 
104
105
  it "should call before_validation_on_create" do
105
- @recorder.save
106
+ @db.save_document @recorder
106
107
  @recorder.callbacks.should include(:before_validation_on_create)
107
108
  end
108
109
 
109
110
  it "should call before_validation_on_save" do
110
- @recorder.save
111
+ @db.save_document @recorder
111
112
  @recorder.callbacks.should include(:before_validation_on_save)
112
113
  end
113
114
 
114
115
  it "should not call before_save" do
115
- @recorder.save
116
+ @db.save_document @recorder
116
117
  @recorder.callbacks.should_not include(:before_save)
117
118
  end
118
119
 
119
120
  it "should not call after_save" do
120
- @recorder.save
121
+ @db.save_document @recorder
121
122
  @recorder.callbacks.should_not include(:after_save)
122
123
  end
123
124
 
124
125
  it "should not call before_create" do
125
- @recorder.save
126
+ @db.save_document @recorder
126
127
  @recorder.callbacks.should_not include(:before_create)
127
128
  end
128
129
 
129
130
  it "should not call after_create" do
130
- @recorder.save
131
+ @db.save_document @recorder
131
132
  @recorder.callbacks.should_not include(:after_create)
132
133
  end
133
-
134
134
  end
135
-
136
-
137
135
  end
138
136
 
139
137
  describe "update callbacks" do
140
- before(:all) do
141
- CouchPotato::Persistence.Db!
142
- end
143
138
 
144
139
  before(:each) do
145
- @recorder = CallbackRecorder.create! :required_property => 1
140
+ @recorder = CallbackRecorder.new :required_property => 1
141
+
142
+ couchrest_database = stub 'couchrest_database', :save_doc => {'id' => '1', 'rev' => '2'}
143
+ @db = CouchPotato::Database.new(couchrest_database)
144
+ @db.save_document! @recorder
145
+
146
146
  @recorder.required_property = 2
147
147
  @recorder.callbacks.clear
148
148
  end
@@ -150,7 +150,7 @@ describe "update callbacks" do
150
150
  describe "successful update" do
151
151
 
152
152
  before(:each) do
153
- @recorder.save!
153
+ @db.save_document! @recorder
154
154
  end
155
155
 
156
156
  it "should call before_validation_on_update" do
@@ -183,7 +183,7 @@ describe "update callbacks" do
183
183
 
184
184
  before(:each) do
185
185
  @recorder.required_property = nil
186
- @recorder.save
186
+ @db.save_document @recorder
187
187
  end
188
188
 
189
189
  it "should call before_validation_on_update" do
@@ -215,34 +215,31 @@ describe "update callbacks" do
215
215
  end
216
216
 
217
217
  describe "destroy callbacks" do
218
- before(:all) do
219
- CouchPotato::Persistence.Db!
220
- end
221
218
 
222
219
  before(:each) do
223
- @recorder = CallbackRecorder.create! :required_property => 1
220
+ @recorder = CallbackRecorder.new :required_property => 1
221
+ couchrest_database = stub 'couchrest_database', :save_doc => {'id' => '1', 'rev' => '2'}, :delete_doc => nil
222
+ @db = CouchPotato::Database.new(couchrest_database)
223
+ @db.save_document! @recorder
224
+
224
225
  @recorder.callbacks.clear
225
226
  end
226
227
 
227
228
  it "should call before_destroy" do
228
- @recorder.destroy
229
+ @db.destroy_document @recorder
229
230
  @recorder.callbacks.should include(:before_destroy)
230
231
  end
231
232
 
232
233
  it "should call after_destroy" do
233
- @recorder.destroy
234
+ @db.destroy_document @recorder
234
235
  @recorder.callbacks.should include(:after_destroy)
235
236
  end
236
237
  end
237
238
 
238
- describe 'save_without_callbacks' do
239
- before(:all) do
240
- CouchPotato::Persistence.Db!
241
- end
242
-
243
- it "should not run any callbacks" do
244
- @recorder = CallbackRecorder.new
245
- @recorder.save_without_callbacks
246
- @recorder.callbacks.should be_empty
239
+ describe "lambda callbacks" do
240
+ it "should run the lambda" do
241
+ recorder = CallbackRecorder.new
242
+ recorder.run_callbacks :before_create
243
+ recorder.lambda_works.should be_true
247
244
  end
248
245
  end
data/spec/create_spec.rb CHANGED
@@ -2,72 +2,21 @@ require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe "create" do
4
4
  before(:all) do
5
- CouchPotato::Persistence.Db!
5
+ recreate_db
6
6
  end
7
-
8
7
  describe "succeeds" do
9
- before(:each) do
10
- @comment = Comment.new :title => 'my_title'
11
- @comment.save!
12
- end
13
-
14
- it "should assign an id" do
15
- @comment._id.should_not be_nil
16
- end
17
-
18
- it "should assign a revision" do
19
- @comment._rev.should_not be_nil
20
- end
21
-
22
8
  it "should store the class" do
23
- CouchPotato::Persistence.Db.get(@comment.id)['ruby_class'].should == 'Comment'
24
- end
25
-
26
- it "should set created at" do
27
- DateTime.parse(CouchPotato::Persistence.Db.get(@comment.id)['created_at']).should >= 1.second.ago
28
- @comment.created_at.should >= 10.seconds.ago
29
- end
30
-
31
- it "should set updated at" do
32
- DateTime.parse(CouchPotato::Persistence.Db.get(@comment.id)['updated_at']).should >= 1.second.ago
33
- @comment.updated_at.should >= 10.seconds.ago
9
+ @comment = Comment.new :title => 'my_title'
10
+ CouchPotato.database.save_document! @comment
11
+ CouchPotato.couchrest_database.get(@comment.id)['ruby_class'].should == 'Comment'
34
12
  end
35
13
  end
36
-
37
14
  describe "fails" do
38
- before(:each) do
39
- CouchPotato::Persistence.Db.delete!
40
- CouchPotato::Persistence.Db!
41
- @comment = Comment.new
42
- @comment.save
43
- end
44
-
45
- it "should not assign an id" do
46
- @comment._id.should be_nil
47
- end
48
-
49
- it "should not assign a revision" do
50
- @comment._rev.should be_nil
51
- end
52
-
53
15
  it "should not store anything" do
54
- CouchPotato::Persistence.Db.documents['rows'].should be_empty
55
- end
56
-
57
- it "should not set created at" do
58
- @comment.created_at.should be_nil
59
- end
60
-
61
- it "should set updated at" do
62
- @comment.updated_at.should be_nil
63
- end
64
-
65
- describe "with bank" do
66
- it "should raise an exception" do
67
- lambda {
68
- @comment.save!
69
- }.should raise_error
70
- end
16
+ @comment = Comment.new
17
+ CouchPotato.database.save_document @comment
18
+ CouchPotato.couchrest_database.documents['rows'].should be_empty
71
19
  end
72
20
  end
73
- end
21
+ end
22
+
@@ -7,38 +7,112 @@ class Build
7
7
  property :time
8
8
 
9
9
  view :timeline, :key => :time
10
- view :minimal_timeline, :key => :time, :properties => [:state]
10
+ view :minimal_timeline, :key => :time, :properties => [:state], :type => :properties
11
+ view :key_array_timeline, :key => [:time, :state]
12
+ view :custom_timeline, :map => "function(doc) { emit(doc._id, {state: 'custom_' + doc.state}); }", :type => :custom
13
+ view :custom_timeline_returns_docs, :map => "function(doc) { emit(doc._id, null); }", :include_docs => true, :type => :custom
14
+ view :raw, :type => :raw, :map => "function(doc) {emit(doc._id, doc.state)}"
15
+ view :filtered_raw, :type => :raw, :map => "function(doc) {emit(doc._id, doc.state)}", :results_filter => lambda{|res| res['rows'].map{|row| row['value']}}
16
+ view :with_view_options, :group => true, :key => :time
11
17
  end
12
18
 
13
- describe 'custom view' do
19
+ describe 'view' do
14
20
  before(:each) do
15
- CouchPotato::Persistence.Db.delete!
16
- CouchPotato::Persistence.Db!
21
+ recreate_db
17
22
  end
18
23
 
19
24
  it "should return instances of the class" do
20
- Build.db.save({:state => 'success', :time => '2008-01-01'})
21
- Build.timeline.map(&:class).should == [Build]
25
+ CouchPotato.database.save_document Build.new(:state => 'success', :time => '2008-01-01')
26
+ CouchPotato.database.view(Build.timeline).map(&:class).should == [Build]
22
27
  end
23
28
 
24
- it "should assign all properties to the objects by default" do
25
- Build.db.save({:state => 'success', :time => '2008-01-01'})
26
- Build.timeline.first.state.should == 'success'
27
- Build.timeline.first.time.should == '2008-01-01'
29
+ it "should pass the view options to the viw query" do
30
+ query = mock 'query'
31
+ CouchPotato::View::ViewQuery.stub!(:new).and_return(query)
32
+ query.should_receive(:query_view!).with(hash_including(:key => 1)).and_return('rows' => [])
33
+ CouchPotato.database.view Build.timeline(:key => 1)
28
34
  end
29
35
 
30
- it "should assign the configured properties" do
31
- Build.db.save({:state => 'success', :time => '2008-01-01'})
32
- Build.minimal_timeline.first.state.should == 'success'
36
+ describe "properties defined" do
37
+ it "should assign the configured properties" do
38
+ CouchPotato.couchrest_database.save_doc({:state => 'success', :time => '2008-01-01'})
39
+ CouchPotato.database.view(Build.minimal_timeline).first.state.should == 'success'
40
+ end
41
+
42
+ it "should not assign the properties not configured" do
43
+ CouchPotato.couchrest_database.save_doc({:state => 'success', :time => '2008-01-01'})
44
+ CouchPotato.database.view(Build.minimal_timeline).first.time.should be_nil
45
+ end
46
+
47
+ it "should assign the id even if it is not configured" do
48
+ id = CouchPotato.couchrest_database.save_doc({:state => 'success', :time => '2008-01-01'})['id']
49
+ CouchPotato.database.view(Build.minimal_timeline).first._id.should == id
50
+ end
33
51
  end
34
52
 
35
- it "should assign the id when properties are configured" do
36
- id = Build.db.save({:state => 'success', :time => '2008-01-01'})['id']
37
- Build.minimal_timeline.first._id.should == id
53
+ describe "no properties defined" do
54
+ it "should assign all properties to the objects by default" do
55
+ id = CouchPotato.couchrest_database.save_doc({:state => 'success', :time => '2008-01-01'})['id']
56
+ result = CouchPotato.database.view(Build.timeline).first
57
+ result.state.should == 'success'
58
+ result.time.should == '2008-01-01'
59
+ result._id.should == id
60
+ end
38
61
  end
39
62
 
40
- it "should not assign the properties not configured" do
41
- Build.db.save({:state => 'success', :time => '2008-01-01'})
42
- Build.minimal_timeline.first.time.should be_nil
63
+ describe "map function given" do
64
+ it "should still return instances of the class" do
65
+ CouchPotato.couchrest_database.save_doc({:state => 'success', :time => '2008-01-01'})
66
+ CouchPotato.database.view(Build.custom_timeline).map(&:class).should == [Build]
67
+ end
68
+
69
+ it "should assign the properties from the value" do
70
+ CouchPotato.couchrest_database.save_doc({:state => 'success', :time => '2008-01-01'})
71
+ CouchPotato.database.view(Build.custom_timeline).map(&:state).should == ['custom_success']
72
+ end
73
+
74
+ it "should leave the other properties blank" do
75
+ CouchPotato.couchrest_database.save_doc({:state => 'success', :time => '2008-01-01'})
76
+ CouchPotato.database.view(Build.custom_timeline).map(&:time).should == [nil]
77
+ end
78
+
79
+ describe "that returns null documents" do
80
+ it "should return instances of the class" do
81
+ CouchPotato.couchrest_database.save_doc({:state => 'success', :time => '2008-01-01'})
82
+ CouchPotato.database.view(Build.custom_timeline_returns_docs).map(&:class).should == [Build]
83
+ end
84
+
85
+ it "should assign the properties from the value" do
86
+ CouchPotato.couchrest_database.save_doc({:state => 'success', :time => '2008-01-01'})
87
+ CouchPotato.database.view(Build.custom_timeline_returns_docs).map(&:state).should == ['success']
88
+ end
89
+ end
43
90
  end
91
+
92
+ describe "with array as key" do
93
+ it "should create a map function with the composite key" do
94
+ CouchPotato::View::ViewQuery.should_receive(:new).with(anything, anything, anything, string_matching(/emit\(\[doc\['time'\], doc\['state'\]\]/), anything).and_return(stub('view query').as_null_object)
95
+ CouchPotato.database.view Build.key_array_timeline
96
+ end
97
+ end
98
+
99
+ describe "raw view" do
100
+ it "should return the raw data" do
101
+ CouchPotato.database.save_document Build.new(:state => 'success', :time => '2008-01-01')
102
+ CouchPotato.database.view(Build.raw)['rows'][0]['value'].should == 'success'
103
+ end
104
+
105
+ it "should return filtred raw data" do
106
+ CouchPotato.database.save_document Build.new(:state => 'success', :time => '2008-01-01')
107
+ CouchPotato.database.view(Build.filtered_raw).should == ['success']
108
+ end
109
+
110
+ it "should pass view options declared in the view declaration to the query" do
111
+ view_query = mock 'view_query'
112
+ CouchPotato::View::ViewQuery.stub!(:new).and_return(view_query)
113
+ view_query.should_receive(:query_view!).with(hash_including(:group => true)).and_return({'rows' => []})
114
+ CouchPotato.database.view(Build.with_view_options)
115
+ end
116
+ end
117
+
44
118
  end
data/spec/destroy_spec.rb CHANGED
@@ -2,13 +2,14 @@ require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe 'destroy' do
4
4
  before(:all) do
5
- CouchPotato::Persistence.Db!
5
+ recreate_db
6
6
  end
7
7
 
8
8
  before(:each) do
9
- @comment = Comment.create! :title => 'title'
9
+ @comment = Comment.new :title => 'title'
10
+ CouchPotato.database.save_document! @comment
10
11
  @comment_id = @comment.id
11
- @comment.destroy
12
+ CouchPotato.database.destroy_document @comment
12
13
  end
13
14
 
14
15
  it "should unset the id" do
@@ -21,7 +22,7 @@ describe 'destroy' do
21
22
 
22
23
  it "should remove the document from the database" do
23
24
  lambda {
24
- CouchPotato::Persistence.Db.get(@comment_id).should
25
+ CouchPotato.couchrest_database.get(@comment_id).should
25
26
  }.should raise_error(RestClient::ResourceNotFound)
26
27
  end
27
28
 
@@ -1,35 +1,49 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
+ class Watch
4
+ include CouchPotato::Persistence
5
+
6
+ property :time, :type => Time
7
+ end
8
+
9
+
3
10
  describe 'properties' do
4
11
  before(:all) do
5
- CouchPotato::Persistence.Db!
12
+ recreate_db
6
13
  end
7
14
 
8
15
  it "should return the property names" do
9
- Comment.property_names.should == [:title, :commenter]
16
+ Comment.property_names.should == [:created_at, :updated_at, :title, :commenter]
10
17
  end
11
18
 
12
19
  it "should persist a string" do
13
20
  c = Comment.new :title => 'my title'
14
- c.save!
15
- c = Comment.find c.id
21
+ CouchPotato.database.save_document! c
22
+ c = CouchPotato.database.load_document c.id
16
23
  c.title.should == 'my title'
17
24
  end
18
25
 
19
26
  it "should persist a number" do
20
27
  c = Comment.new :title => 3
21
- c.save!
22
- c = Comment.find c.id
28
+ CouchPotato.database.save_document! c
29
+ c = CouchPotato.database.load_document c.id
23
30
  c.title.should == 3
24
31
  end
25
32
 
26
33
  it "should persist a hash" do
27
34
  c = Comment.new :title => {'key' => 'value'}
28
- c.save!
29
- c = Comment.find c.id
35
+ CouchPotato.database.save_document! c
36
+ c = CouchPotato.database.load_document c.id
30
37
  c.title.should == {'key' => 'value'}
31
38
  end
32
39
 
40
+ it "should persist a Time object" do
41
+ w = Watch.new :time => Time.now
42
+ CouchPotato.database.save_document! w
43
+ w = CouchPotato.database.load_document w.id
44
+ w.time.year.should == Time.now.year
45
+ end
46
+
33
47
  describe "predicate" do
34
48
  it "should return true if property set" do
35
49
  Comment.new(:title => 'title').title?.should be_true
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'rubygems'
2
- gem 'rspec'
3
2
  require 'spec'
4
3
 
5
4
  $:.unshift(File.dirname(__FILE__) + '/../lib')
@@ -7,20 +6,7 @@ $:.unshift(File.dirname(__FILE__) + '/../lib')
7
6
  require 'couch_potato'
8
7
 
9
8
  CouchPotato::Config.database_name = 'couch_potato_test'
10
- CouchPotato::Persistence.Db.delete! rescue nil
11
- CouchPotato::Persistence.Db!
12
9
 
13
- class User
14
- include CouchPotato::Persistence
15
-
16
- has_many :comments, :stored => :inline
17
- end
18
-
19
- class Commenter
20
- include CouchPotato::Persistence
21
-
22
- has_many :comments, :stored => :separately
23
- end
24
10
 
25
11
  class Comment
26
12
  include CouchPotato::Persistence
@@ -30,3 +16,15 @@ class Comment
30
16
  property :title
31
17
  belongs_to :commenter
32
18
  end
19
+
20
+ def recreate_db
21
+ CouchPotato.couchrest_database.delete! rescue nil
22
+ CouchPotato.couchrest_database.server.create_db CouchPotato::Config.database_name
23
+ end
24
+ recreate_db
25
+
26
+ Spec::Matchers.define :string_matching do |regex|
27
+ match do |string|
28
+ string =~ regex
29
+ end
30
+ end
@@ -0,0 +1,26 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ class Plant
4
+ include CouchPotato::Persistence
5
+ property :leaf_count
6
+ end
7
+
8
+ describe "attributes" do
9
+
10
+ describe 'attributes=' do
11
+ it "should assign the attributes" do
12
+ plant = Plant.new
13
+ plant.attributes = {:leaf_count => 1}
14
+ plant.leaf_count.should == 1
15
+ end
16
+ end
17
+
18
+ describe "attributes" do
19
+ it "should return the attributes" do
20
+ plant = Plant.new(:leaf_count => 1)
21
+ plant.attributes.should == {:leaf_count => 1, :created_at => nil, :updated_at => nil}
22
+ end
23
+ end
24
+
25
+ end
26
+
@@ -0,0 +1,58 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "create" do
4
+
5
+ describe "succeeds" do
6
+ before(:each) do
7
+ @comment = Comment.new :title => 'my_title'
8
+ CouchPotato::Database.new(stub('database', :save_doc => {'rev' => '123', 'id' => '456'})).save_document!(@comment)
9
+ end
10
+
11
+ it "should assign the id" do
12
+ @comment._id.should == '456'
13
+ end
14
+
15
+ it "should assign the revision" do
16
+ @comment._rev.should == '123'
17
+ end
18
+
19
+ it "should set created at" do
20
+ @comment.created_at.should >= Time.now - 10
21
+ end
22
+
23
+ it "should set updated at" do
24
+ @comment.updated_at.should >= Time.now - 10
25
+ end
26
+ end
27
+
28
+ describe "fails" do
29
+ before(:each) do
30
+ @comment = Comment.new
31
+ CouchPotato::Database.new(stub('database')).save_document(@comment)
32
+ end
33
+
34
+ it "should not assign an id" do
35
+ @comment._id.should be_nil
36
+ end
37
+
38
+ it "should not assign a revision" do
39
+ @comment._rev.should be_nil
40
+ end
41
+
42
+ it "should not set created at" do
43
+ @comment.created_at.should be_nil
44
+ end
45
+
46
+ it "should set updated at" do
47
+ @comment.updated_at.should be_nil
48
+ end
49
+
50
+ describe "with bank" do
51
+ it "should raise an exception" do
52
+ lambda {
53
+ @comment.save!
54
+ }.should raise_error
55
+ end
56
+ end
57
+ end
58
+ end