peleteiro-activecouch 0.2.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.
Files changed (58) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README +28 -0
  3. data/Rakefile +52 -0
  4. data/VERSION +1 -0
  5. data/lib/active_couch.rb +12 -0
  6. data/lib/active_couch/base.rb +608 -0
  7. data/lib/active_couch/callbacks.rb +89 -0
  8. data/lib/active_couch/connection.rb +164 -0
  9. data/lib/active_couch/errors.rb +13 -0
  10. data/lib/active_couch/support.rb +3 -0
  11. data/lib/active_couch/support/exporter.rb +97 -0
  12. data/lib/active_couch/support/extensions.rb +86 -0
  13. data/lib/active_couch/support/inflections.rb +52 -0
  14. data/lib/active_couch/support/inflector.rb +279 -0
  15. data/lib/active_couch/views.rb +3 -0
  16. data/lib/active_couch/views/errors.rb +4 -0
  17. data/lib/active_couch/views/raw_view.rb +40 -0
  18. data/lib/active_couch/views/view.rb +85 -0
  19. data/lib/activecouch.rb +1 -0
  20. data/spec/base/after_delete_spec.rb +110 -0
  21. data/spec/base/after_save_spec.rb +102 -0
  22. data/spec/base/before_delete_spec.rb +109 -0
  23. data/spec/base/before_save_spec.rb +101 -0
  24. data/spec/base/count_all_spec.rb +29 -0
  25. data/spec/base/count_spec.rb +77 -0
  26. data/spec/base/create_spec.rb +28 -0
  27. data/spec/base/database_spec.rb +70 -0
  28. data/spec/base/delete_spec.rb +97 -0
  29. data/spec/base/find_from_url_spec.rb +55 -0
  30. data/spec/base/find_spec.rb +383 -0
  31. data/spec/base/from_json_spec.rb +54 -0
  32. data/spec/base/has_many_spec.rb +89 -0
  33. data/spec/base/has_spec.rb +88 -0
  34. data/spec/base/id_spec.rb +25 -0
  35. data/spec/base/initialize_spec.rb +91 -0
  36. data/spec/base/marshal_dump_spec.rb +64 -0
  37. data/spec/base/marshal_load_spec.rb +58 -0
  38. data/spec/base/module_spec.rb +18 -0
  39. data/spec/base/nested_class_spec.rb +19 -0
  40. data/spec/base/rev_spec.rb +20 -0
  41. data/spec/base/save_spec.rb +130 -0
  42. data/spec/base/site_spec.rb +62 -0
  43. data/spec/base/to_json_spec.rb +73 -0
  44. data/spec/connection/initialize_spec.rb +28 -0
  45. data/spec/exporter/all_databases_spec.rb +24 -0
  46. data/spec/exporter/create_database_spec.rb +47 -0
  47. data/spec/exporter/delete_database_spec.rb +45 -0
  48. data/spec/exporter/delete_spec.rb +36 -0
  49. data/spec/exporter/export_spec.rb +62 -0
  50. data/spec/exporter/export_with_raw_views_spec.rb +66 -0
  51. data/spec/spec_helper.rb +9 -0
  52. data/spec/views/define_spec.rb +34 -0
  53. data/spec/views/include_attributes_spec.rb +30 -0
  54. data/spec/views/raw_view_spec.rb +49 -0
  55. data/spec/views/to_json_spec.rb +58 -0
  56. data/spec/views/with_filter_spec.rb +13 -0
  57. data/spec/views/with_key_spec.rb +19 -0
  58. metadata +117 -0
@@ -0,0 +1,77 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "ActiveCouch::Base #count method with just simple attributes" do
4
+ before(:each) do
5
+ # Define the model
6
+ class Person < ActiveCouch::Base
7
+ site 'http://localhost:5984'
8
+ has :name
9
+ end
10
+ # Define the view
11
+ class ByName < ActiveCouch::View
12
+ define :for_db => 'people' do
13
+ with_key 'name'
14
+ end
15
+ end
16
+ # Create the database first
17
+ ActiveCouch::Exporter.create_database('http://localhost:5984', 'people')
18
+ # Create a view
19
+ ActiveCouch::Exporter.export('http://localhost:5984', ByName)
20
+ # Save an object
21
+ Person.new(:name => 'McLovin').save
22
+ end
23
+
24
+ after(:each) do
25
+ # Delete the database last
26
+ ActiveCouch::Exporter.delete_database('http://localhost:5984', 'people')
27
+ Object.send(:remove_const, :Person)
28
+ Object.send(:remove_const, :ByName)
29
+ end
30
+
31
+ it "should respond to the count method" do
32
+ Person.should respond_to(:count)
33
+ end
34
+
35
+ it "should return an array with one Person object in it, when sent method count with search parameters" do
36
+ count = Person.count(:params => {:name => 'McLovin'})
37
+ count.should == 1
38
+ end
39
+ end
40
+
41
+
42
+ describe "ActiveCouch::Base #count method with multiple documents in the CouchDB database" do
43
+ before(:each) do
44
+ class Person < ActiveCouch::Base
45
+ site 'http://localhost:5984'
46
+
47
+ has :first_name
48
+ has :last_name
49
+ end
50
+
51
+ # Define the view
52
+ class ByLastName < ActiveCouch::View
53
+ define :for_db => 'people' do
54
+ with_key 'last_name'
55
+ end
56
+ end
57
+ # Create the database first
58
+ ActiveCouch::Exporter.create_database('http://localhost:5984', 'people')
59
+ # Create a view
60
+ ActiveCouch::Exporter.export('http://localhost:5984', ByLastName)
61
+ # Save two objects
62
+ Person.create(:last_name => 'McLovin', :first_name => 'Seth')
63
+ Person.create(:last_name => 'McLovin', :first_name => 'Bob')
64
+ end
65
+
66
+ after(:each) do
67
+ # Delete the database last
68
+ ActiveCouch::Exporter.delete_database('http://localhost:5984', 'people')
69
+ Object.send(:remove_const, :Person)
70
+ Object.send(:remove_const, :ByLastName)
71
+ end
72
+
73
+ it "should count all objects in the database when count method is sent with valid search parameters" do
74
+ count = Person.count(:params => {:last_name => 'McLovin'})
75
+ count.should == 2
76
+ end
77
+ end
@@ -0,0 +1,28 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "ActiveCouch::Base #create method" do
4
+ before(:each) do
5
+ class Person < ActiveCouch::Base
6
+ site 'http://localhost:5984/'
7
+ has :name
8
+ end
9
+ ActiveCouch::Exporter.create_database('http://localhost:5984/', 'people')
10
+ end
11
+
12
+ after(:each) do
13
+ ActiveCouch::Exporter.delete_database('http://localhost:5984/', 'people')
14
+ Object.send(:remove_const, :Person)
15
+ end
16
+
17
+ it "should have a class method called create" do
18
+ Person.methods.include?('create').should == true
19
+ end
20
+
21
+ it "should be able to persist itself in the CouchDB database" do
22
+ person = Person.create(:name => 'McLovin')
23
+ person.name.should == 'McLovin'
24
+ # Check whether document is persisted correctly in the database
25
+ response = Net::HTTP.get_response URI.parse("http://localhost:5984/people/_all_docs/")
26
+ response.body.index('"total_rows":1').should_not == nil
27
+ end
28
+ end
@@ -0,0 +1,70 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "A direct subclass of ActiveCouch::Base" do
4
+
5
+ before(:each) do
6
+ class Foo < ActiveCouch::Base
7
+ end
8
+ end
9
+
10
+ after(:each) do
11
+ # Remove class definition so we can start fresh in each spec.
12
+ Object.send(:remove_const, :Foo)
13
+ end
14
+
15
+ it "should have a base_class of itself" do
16
+ Foo.base_class.should == Foo
17
+ end
18
+
19
+ it "should infer the database name from the class name" do
20
+ Foo.database_name.should == 'foos'
21
+ end
22
+
23
+ it "should use the database name set with the set_database_name macro" do
24
+ Foo.set_database_name('legacy_foo')
25
+
26
+ Foo.database_name.should == 'legacy_foo'
27
+ end
28
+
29
+ it "should have an database_name= alias for set_database_name" do
30
+ Foo.database_name = 'fubar'
31
+
32
+ Foo.database_name.should == 'fubar'
33
+ end
34
+
35
+ it "should set an attribute from a value with define_attr_method" do
36
+ Foo.define_attr_method(:database_name, 'defined_foo')
37
+
38
+ Foo.database_name.should == 'defined_foo'
39
+ end
40
+
41
+ it "should set an attribute from a block with define_attr_method" do
42
+ Foo.send(:define_attr_method, :database_name) { 'legacy_' + original_database_name }
43
+
44
+ Foo.database_name.should == 'legacy_foos'
45
+ end
46
+ end
47
+
48
+ describe "A subclass of ActiveCouch::Base that's a subclass of an ActiveCouch::Base subclass" do
49
+
50
+ before(:all) do
51
+ class Parent < ActiveCouch::Base
52
+ end
53
+
54
+ class Child < Parent
55
+ end
56
+ end
57
+
58
+ after(:all) do
59
+ Object.send(:remove_const, :Parent)
60
+ Object.send(:remove_const, :Child)
61
+ end
62
+
63
+ it "should have a base_class of the parent" do
64
+ Child.base_class.should == Parent
65
+ end
66
+
67
+ it "should have a database_name of the parent's" do
68
+ Child.database_name.should == 'parents'
69
+ end
70
+ end
@@ -0,0 +1,97 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "ActiveCouch::Base #delete instance-level method" do
4
+ before(:each) do
5
+ class Person < ActiveCouch::Base
6
+ site 'http://localhost:5984/'
7
+ has :name
8
+ end
9
+ ActiveCouch::Exporter.create_database('http://localhost:5984/', 'people')
10
+ ActiveCouch::Exporter.create_database('http://localhost:5984/', 'good_people')
11
+
12
+ @person = Person.create(:name => 'McLovin')
13
+ end
14
+
15
+ after(:each) do
16
+ ActiveCouch::Exporter.delete_database('http://localhost:5984/', 'people')
17
+ ActiveCouch::Exporter.delete_database('http://localhost:5984/', 'good_people')
18
+
19
+ Object.send(:remove_const, :Person)
20
+ end
21
+
22
+ it "should have an instance method called delete" do
23
+ @person.methods.include?('delete').should == true
24
+ end
25
+
26
+ it "should be able to delete itself from the CouchDB database and return true" do
27
+ @person.delete.should == true
28
+ @person.id.should == nil
29
+ @person.rev.should == nil
30
+ # Check whether document has actually been deleted
31
+ response = Net::HTTP.get_response URI.parse("http://localhost:5984/people/_all_docs/")
32
+ response.body.index('"total_rows":0').should_not == nil
33
+ end
34
+
35
+ it "should raise an error if the revision for the object is not set and is attempted to be deleted" do
36
+ p = Person.new(:name => 'McLovin')
37
+ lambda { p.delete }.should raise_error(ArgumentError, "You must specify a revision for the document to be deleted")
38
+ end
39
+
40
+ it "should raise an error if the id for the object is not set, but the revision is set and is attempted to be deleted" do
41
+ p = Person.new(:name => 'McLovin')
42
+ p.rev = '123'
43
+ lambda { p.delete }.should raise_error(ArgumentError, "You must specify an ID for the document to be deleted")
44
+ end
45
+
46
+ it "should be able to accept a from_database option which will delete it from the right database" do
47
+ p = Person.new(:name => 'McLovin')
48
+ p.save(:to_database => 'good_people')
49
+ p.delete(:from_database => 'good_people').should == true
50
+
51
+ p.id.should == nil
52
+ p.rev.should == nil
53
+
54
+ # Check whether document has actually been deleted
55
+ response = Net::HTTP.get_response URI.parse("http://localhost:5984/good_people/_all_docs/")
56
+ response.body.index('"total_rows":0').should_not == nil
57
+ end
58
+ end
59
+
60
+ describe "ActiveCouch::Base #delete class-level method" do
61
+ before(:each) do
62
+ class Person < ActiveCouch::Base
63
+ site 'http://localhost:5984/'
64
+ has :name
65
+ end
66
+ ActiveCouch::Exporter.create_database('http://localhost:5984/', 'people')
67
+ @person = Person.create(:name => 'McLovin')
68
+ end
69
+
70
+ after(:each) do
71
+ ActiveCouch::Exporter.delete_database('http://localhost:5984/', 'people')
72
+ Object.send(:remove_const, :Person)
73
+ end
74
+
75
+ it "should have a class method called delete" do
76
+ Person.methods.include?('delete').should == true
77
+ end
78
+
79
+ it "should be able to delete a CouchDB document and return true after successful deletion" do
80
+ Person.delete(:id => @person.id, :rev => @person.rev).should == true
81
+ # Check whether document has actually been deleted
82
+ response = Net::HTTP.get_response URI.parse("http://localhost:5984/people/_all_docs/")
83
+ response.body.index('"total_rows":0').should_not == nil
84
+ end
85
+
86
+ it "should raise an error if the id is not specified in the options hash" do
87
+ lambda { Person.delete(:rev => 'abc') }.should raise_error(ArgumentError, "You must specify both an id and a rev for the document to be deleted")
88
+ end
89
+
90
+ it "should raise an error if the rev is not specified in the options hash" do
91
+ lambda { Person.delete(:id => 'abc') }.should raise_error(ArgumentError, "You must specify both an id and a rev for the document to be deleted")
92
+ end
93
+
94
+ it "should raise an error if a nil object is passed as a param to delete" do
95
+ lambda { Person.delete(nil) }.should raise_error(ArgumentError, "You must specify both an id and a rev for the document to be deleted")
96
+ end
97
+ end
@@ -0,0 +1,55 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "ActiveCouch::Base #find method with multiple documents in the CouchDB database" do
4
+ before(:each) do
5
+ class Person < ActiveCouch::Base
6
+ site 'http://localhost:5984'
7
+
8
+ has :first_name
9
+ has :last_name
10
+ end
11
+
12
+ # Define the migration
13
+ class ByLastName < ActiveCouch::View
14
+ define :for_db => 'people' do
15
+ with_key 'last_name'
16
+ end
17
+ end
18
+ # Create the database first
19
+ ActiveCouch::Exporter.create_database('http://localhost:5984', 'people')
20
+ # Create a view
21
+ ActiveCouch::Exporter.export('http://localhost:5984', ByLastName)
22
+ # Save two objects
23
+ Person.create(:last_name => 'McLovin', :first_name => 'Seth')
24
+ Person.create(:last_name => 'McLovin', :first_name => 'Bob')
25
+ Person.create(:id => '123', :last_name => 'McLovin', :first_name => 'Portnoy')
26
+ end
27
+
28
+ after(:each) do
29
+ # Delete the database last
30
+ ActiveCouch::Exporter.delete_database('http://localhost:5984', 'people')
31
+ Object.send(:remove_const, :Person)
32
+ end
33
+
34
+ it "should find all objects in the database when find_from_url method is used" do
35
+ people = Person.find_from_url("/people/_view/by_last_name/by_last_name?key=%22McLovin%22")
36
+ # Check if it is an array and if the size is 2
37
+ people.class.should == Array
38
+ people.size.should == 3
39
+ # The id's and rev's for all the objects must not be nil
40
+ people.each do |p|
41
+ p.id.should_not == nil
42
+ p.rev.should_not == nil
43
+ end
44
+ end
45
+
46
+ it "should fetch a single object from a URL if the URL specified is not accessing a view" do
47
+ person = Person.find_from_url("/people/123")
48
+ person.class.should == Person
49
+ person.last_name.should == 'McLovin'
50
+ person.first_name.should == 'Portnoy'
51
+
52
+ Person.find_from_url("/people/345").should == nil
53
+ end
54
+
55
+ end
@@ -0,0 +1,383 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper.rb'
2
+
3
+ describe "ActiveCouch::Base #find method with an object which has associations" do
4
+ before(:each) do
5
+ class Comment < ActiveCouch::Base
6
+ site 'http://localhost:5984'
7
+ has :body
8
+ end
9
+
10
+ class Blog < ActiveCouch::Base
11
+ site 'http://localhost:5984'
12
+ has :title
13
+ has_many :comments
14
+ end
15
+
16
+ # Define the migration
17
+ class ByTitle < ActiveCouch::View
18
+ define :for_db => 'blogs' do
19
+ with_key 'title'
20
+ end
21
+ end
22
+
23
+ # Create the database first
24
+ ActiveCouch::Exporter.create_database('http://localhost:5984', 'blogs')
25
+ # Create a view
26
+ ActiveCouch::Exporter.export('http://localhost:5984', ByTitle)
27
+ blog = Blog.new(:title => 'iPhone in Singapore')
28
+ # Associations
29
+ blog.add_comment(Comment.new(:body => 'soon plz'))
30
+ blog.add_comment(Comment.new(:body => 'ya rly!'))
31
+ # Save the blog
32
+ blog.save
33
+ end
34
+
35
+ after(:each) do
36
+ # Create the database first
37
+ ActiveCouch::Exporter.delete_database('http://localhost:5984', 'blogs')
38
+ Object.send(:remove_const, :Blog)
39
+ Object.send(:remove_const, :Comment)
40
+ Object.send(:remove_const, :ByTitle)
41
+ end
42
+
43
+ it "should be able to retrieve the simple attributes" do
44
+ blog = Blog.find(:first, :params => {:title => 'iPhone in Singapore'})
45
+ blog.title.should == 'iPhone in Singapore'
46
+ end
47
+
48
+ it "should be able to retrieve associations" do
49
+ blog = Blog.find(:first, :params => {:title => 'iPhone in Singapore'})
50
+ blog.comments.size.should == 2
51
+ # Check whether the bodies of the comments exist
52
+ (blog.comments.inspect =~ /soon plz/).should_not == nil
53
+ (blog.comments.inspect =~ /ya rly!/).should_not == nil
54
+ end
55
+
56
+ end
57
+
58
+
59
+ describe "ActiveCouch::Base #find method with no params passed" do
60
+ before(:each) do
61
+ class Person < ActiveCouch::Base
62
+ site 'http://localhost:5984/'
63
+ has :name
64
+ end
65
+ # Define the migration
66
+ class ByName < ActiveCouch::View
67
+ define :for_db => 'people' do
68
+ with_key 'name'
69
+ end
70
+ end
71
+ # Create the database first
72
+ ActiveCouch::Exporter.create_database('http://localhost:5984', 'people')
73
+ # Create a view
74
+ ActiveCouch::Exporter.export('http://localhost:5984', ByName)
75
+ # Save two objects
76
+ Person.create(:name => 'McLovin')
77
+ Person.create(:name => 'Seth')
78
+ end
79
+
80
+ after(:each) do
81
+ # Delete the database last
82
+ ActiveCouch::Exporter.delete_database('http://localhost:5984', 'people')
83
+ Object.send(:remove_const, :Person)
84
+ Object.send(:remove_const, :ByName)
85
+ end
86
+
87
+ it "should return all documents if passed :all, with no params specified"
88
+ end
89
+
90
+
91
+ describe "ActiveCouch::Base #find method with an ID passed" do
92
+ before(:each) do
93
+ class Person < ActiveCouch::Base
94
+ site 'http://localhost:5984/'
95
+ has :name
96
+ end
97
+ # Define the migration
98
+ class ByName < ActiveCouch::View
99
+ define :for_db => 'people' do
100
+ with_key 'name'
101
+ end
102
+ end
103
+ # Create the database first
104
+ ActiveCouch::Exporter.create_database('http://localhost:5984', 'people')
105
+ # Create a view
106
+ ActiveCouch::Exporter.export('http://localhost:5984', ByName)
107
+ # Save one object
108
+ p = Person.create(:name => 'McLovin', :id => '123')
109
+ end
110
+
111
+ after(:each) do
112
+ # Delete the database last
113
+ ActiveCouch::Exporter.delete_database('http://localhost:5984', 'people')
114
+ Object.send(:remove_const, :Person)
115
+ Object.send(:remove_const, :ByName)
116
+ end
117
+
118
+ it "should return an ActiveCouch::Base object if the ID exists" do
119
+ person = Person.find('123')
120
+ person.name.should == 'McLovin'
121
+ end
122
+
123
+ it "should return nil if the ID does not exist" do
124
+ person = Person.find('321')
125
+ person.should == nil
126
+ end
127
+ end
128
+
129
+ describe "ActiveCouch::Base #find method with non-String params passed as arguments" do
130
+ before(:each) do
131
+ class Person < ActiveCouch::Base
132
+ site 'http://localhost:5984/'
133
+ has :age
134
+ end
135
+ # Define the migration
136
+ class ByAge < ActiveCouch::View
137
+ define :for_db => 'people' do
138
+ with_key 'age'
139
+ end
140
+ end
141
+ # Create the database first
142
+ ActiveCouch::Exporter.create_database('http://localhost:5984', 'people')
143
+ # Create a view
144
+ ActiveCouch::Exporter.export('http://localhost:5984', ByAge)
145
+ # Save two objects
146
+ p = Person.create(:age => "21")
147
+ end
148
+
149
+ after(:each) do
150
+ # Delete the database last
151
+ ActiveCouch::Exporter.delete_database('http://localhost:5984', 'people')
152
+ Object.send(:remove_const, :Person)
153
+ Object.send(:remove_const, :ByAge)
154
+ end
155
+
156
+ it "should return an ActiveCouch::Base object" do
157
+ person = Person.find(:first, :params => {:age => 21})
158
+ person.age.should == "21"
159
+ end
160
+ end
161
+
162
+
163
+ describe "ActiveCouch::Base #find method with just simple attributes" do
164
+ before(:each) do
165
+ # Define the model
166
+ class Person < ActiveCouch::Base
167
+ site 'http://localhost:5984'
168
+ has :name
169
+ end
170
+ # Define the migration
171
+ class ByName < ActiveCouch::View
172
+ define :for_db => 'people' do
173
+ with_key 'name'
174
+ end
175
+ end
176
+ # Create the database first
177
+ ActiveCouch::Exporter.create_database('http://localhost:5984', 'people')
178
+ # Create a view
179
+ ActiveCouch::Exporter.export('http://localhost:5984', ByName)
180
+ # Save an object
181
+ Person.new(:name => 'McLovin').save
182
+ end
183
+
184
+ after(:each) do
185
+ # Delete the database last
186
+ ActiveCouch::Exporter.delete_database('http://localhost:5984', 'people')
187
+ Object.send(:remove_const, :Person)
188
+ Object.send(:remove_const, :ByName)
189
+ end
190
+
191
+ it "should respond to the find method" do
192
+ Person.should respond_to(:find)
193
+ end
194
+
195
+ it "should return an array with one Person object in it, when sent method find with parameter :all" do
196
+ people = Person.find(:all, :params => {:name => 'McLovin'})
197
+ people.class.should == Array
198
+ # Size of people
199
+ people.size.should == 1
200
+
201
+ people.first.class.should == Person
202
+ people.first.name.should == 'McLovin'
203
+ # Check if id and rev are set
204
+ people.first.id.should_not == nil
205
+ people.first.rev.should_not == nil
206
+ end
207
+
208
+ it "should return one Person object when sent method find with parameter :one" do
209
+ person = Person.find(:first, :params => {:name => 'McLovin'})
210
+ person.class.should == Person
211
+ person.name.should == 'McLovin'
212
+ # Check if id and rev are set
213
+ person.id.should_not == nil
214
+ person.rev.should_not == nil
215
+ end
216
+
217
+ it "should return an empty array when sent method find with parameter :all and is not able to find any" do
218
+ people = Person.find(:all, :params => {:name => 'Seth'})
219
+ people.class.should == Array
220
+ people.size.should == 0
221
+ end
222
+
223
+ it "should return a nil object when sent method find with parameter :first and is not able to find any" do
224
+ person = Person.find(:first, :params => {:name => 'Seth'})
225
+ person.should == nil
226
+ end
227
+ end
228
+
229
+
230
+ describe "ActiveCouch::Base #find method with multiple documents in the CouchDB database" do
231
+ before(:each) do
232
+ class Person < ActiveCouch::Base
233
+ site 'http://localhost:5984'
234
+
235
+ has :first_name
236
+ has :last_name
237
+ end
238
+
239
+ # Define the migration
240
+ class ByLastName < ActiveCouch::View
241
+ define :for_db => 'people' do
242
+ with_key 'last_name'
243
+ end
244
+ end
245
+ # Create the database first
246
+ ActiveCouch::Exporter.create_database('http://localhost:5984', 'people')
247
+ # Create a view
248
+ ActiveCouch::Exporter.export('http://localhost:5984', ByLastName)
249
+ # Save two objects
250
+ Person.create(:last_name => 'McLovin', :first_name => 'Seth')
251
+ Person.create(:last_name => 'McLovin', :first_name => 'Bob')
252
+ end
253
+
254
+ after(:each) do
255
+ # Delete the database last
256
+ ActiveCouch::Exporter.delete_database('http://localhost:5984', 'people')
257
+ Object.send(:remove_const, :Person)
258
+ Object.send(:remove_const, :ByLastName)
259
+ end
260
+
261
+ it "should find all objects in the database when find method is sent the param :all" do
262
+ people = Person.find(:all, :params => {:last_name => 'McLovin'})
263
+ # Check if it is an array and if the size is 2
264
+ people.class.should == Array
265
+ people.size.should == 2
266
+ # The id's and rev's for all the objects must not be nil
267
+ people.each do |p|
268
+ p.id.should_not == nil
269
+ p.rev.should_not == nil
270
+ end
271
+ end
272
+
273
+ it "should find only the first object in the database when find method is sent with the param :first" do
274
+ people = Person.find(:first, :params => {:last_name => 'McLovin'})
275
+ # Check if this is a Person and if the size is 1
276
+ people.class.should == Person
277
+ end
278
+ end
279
+
280
+ describe "ActiveCouch::Base #find method :limit option used" do
281
+ before(:each) do
282
+ class Person < ActiveCouch::Base
283
+ site 'http://localhost:5984'
284
+
285
+ has :first_name
286
+ has :last_name
287
+ end
288
+
289
+ # Define the migration
290
+ class ByLastName < ActiveCouch::View
291
+ define :for_db => 'people' do
292
+ with_key 'last_name'
293
+ end
294
+ end
295
+ # Create the database first
296
+ ActiveCouch::Exporter.create_database('http://localhost:5984', 'people')
297
+ # Create a view
298
+ ActiveCouch::Exporter.export('http://localhost:5984', ByLastName)
299
+ # Save two objects
300
+ Person.create(:last_name => 'McLovin', :first_name => 'Seth')
301
+ Person.create(:last_name => 'McLovin', :first_name => 'Bob')
302
+ end
303
+
304
+ after(:each) do
305
+ # Delete the database last
306
+ ActiveCouch::Exporter.delete_database('http://localhost:5984', 'people')
307
+ Object.send(:remove_const, :Person)
308
+ Object.send(:remove_const, :ByLastName)
309
+ end
310
+
311
+ it "should return only one object in the database when find method is sent the param :limit => 1" do
312
+ people = Person.find(:all, :params => {:last_name => 'McLovin'}, :limit => 1)
313
+ # Check if it is an array and if the size is 2
314
+ people.class.should == Array
315
+ people.size.should == 1
316
+ # The id's and rev's for all the objects must not be nil
317
+ person = people.first
318
+ person.id.should_not == nil
319
+ person.rev.should_not == nil
320
+
321
+ person.first_name == 'Seth'
322
+ person.last_name == 'McLovin'
323
+
324
+ end
325
+ end
326
+
327
+ describe "ActiveCouch::Base #find method :limit and :offset options used" do
328
+ before(:each) do
329
+ class Person < ActiveCouch::Base
330
+ site 'http://localhost:5984'
331
+
332
+ has :first_name
333
+ has :last_name
334
+ end
335
+
336
+ # Define the migration
337
+ class ByLastName < ActiveCouch::View
338
+ define :for_db => 'people' do
339
+ with_key 'last_name'
340
+ end
341
+ end
342
+ # Create the database first
343
+ ActiveCouch::Exporter.create_database('http://localhost:5984', 'people')
344
+ # Create a view
345
+ ActiveCouch::Exporter.export('http://localhost:5984', ByLastName)
346
+ # Save two objects
347
+ Person.create(:id => 'Seth', :last_name => 'McLovin', :first_name => 'Seth')
348
+ Person.create(:id => 'Bob', :last_name => 'McLovin', :first_name => 'Bob')
349
+ Person.create(:id => 'John', :last_name => 'McLovin', :first_name => 'Bob')
350
+ end
351
+
352
+ after(:each) do
353
+ # Delete the database last
354
+ ActiveCouch::Exporter.delete_database('http://localhost:5984', 'people')
355
+ Object.send(:remove_const, :Person)
356
+ Object.send(:remove_const, :ByLastName)
357
+ end
358
+
359
+ it "should return two objects in the database when find method is sent the param :offset => 1" do
360
+ people = Person.find(:all, :params => {:last_name => 'McLovin'}, :offset => 1)
361
+ # Check if it is an array and if the size is 2
362
+ people.class.should == Array
363
+ people.size.should == 2
364
+ # The id's and rev's for all the objects must not be nil
365
+ people.each do |p|
366
+ p.id.should_not == nil
367
+ p.rev.should_not == nil
368
+ end
369
+ end
370
+
371
+ it "should return one object in the database when find method is sent the param :offset => 1 and :limit => 1" do
372
+ people = Person.find(:all, :params => {:last_name => 'McLovin'}, :offset => 1, :limit => 1)
373
+ # Check if it is an array and if the size is 2
374
+ people.class.should == Array
375
+ people.size.should == 1
376
+ # The id's and rev's for all the objects must not be nil
377
+ people.each do |p|
378
+ p.id.should_not == nil
379
+ p.rev.should_not == nil
380
+ end
381
+ end
382
+
383
+ end