public
Fork of chuyeow/activecouch
Description: ActiveCouch is a simple, convenient, Ruby-idiomatic wrapper for CouchDB
Homepage: http://github.com/arunthampi/activecouch
Clone URL: git://github.com/arunthampi/activecouch.git
Search Repo:
- More specs for find
  - Including find for multiple documents in a database
  - Including find for objects with associations
arunthampi (author)
Mon Jan 21 18:59:07 -0800 2008
commit  df0a90bb288a90541651c7e3604a4fcbe334ff96
tree    5d264f0be875ddc47c06035fc74900c04f7ef84e
parent  1ebee0afd10cf2eae37999170f29d21aade13b9e
...
62
63
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
66
...
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
0
@@ -62,4 +62,104 @@ describe "ActiveCouch::Base #find method with just simple attributes" do
0
     person = Person.find(:first, :params => {:name => 'Seth'})
0
     person.should == nil
0
   end
0
+end
0
+
0
+describe "ActiveCouch::Base #find method with multiple documents in the CouchDB database" do
0
+ before(:each) do
0
+ class Person < ActiveCouch::Base
0
+ site 'http://localhost:5984'
0
+
0
+ has :first_name
0
+ has :last_name
0
+ end
0
+
0
+ # Define the migration
0
+ class ByLastName < ActiveCouch::Migration
0
+ define :for_db => 'people' do
0
+ with_key 'last_name'
0
+ end
0
+ end
0
+ # Create the database first
0
+ ActiveCouch::Migrator.create_database('http://localhost:5984', 'people')
0
+ # Create a view
0
+ ActiveCouch::Migrator.migrate('http://localhost:5984', ByLastName)
0
+ # Save two objects
0
+ Person.create(:last_name => 'McLovin', :first_name => 'Seth')
0
+ Person.create(:last_name => 'McLovin', :first_name => 'Bob')
0
+ end
0
+
0
+ after(:each) do
0
+ # Delete the database last
0
+ ActiveCouch::Migrator.delete_database('http://localhost:5984', 'people')
0
+ end
0
+
0
+ it "should find all objects in the database when find method is sent the param :all" do
0
+ people = Person.find(:all, :params => {:last_name => 'McLovin'})
0
+ # Check if it is an array and if the size is 2
0
+ people.class.should == Array
0
+ people.size.should == 2
0
+ # The id's and rev's for all the objects must not be nil
0
+ people.each do |p|
0
+ p.id.should_not == nil
0
+ p.rev.should_not == nil
0
+ end
0
+ end
0
+
0
+ it "should find only the first object in the database when find method is sent with the param :first" do
0
+ people = Person.find(:first, :params => {:last_name => 'McLovin'})
0
+ # Check if this is a Person and if the size is 1
0
+ people.class.should == Person
0
+ end
0
+end
0
+
0
+describe "ActiveCouch::Base #find method with an object which has associations" do
0
+ before(:each) do
0
+ class Comment < ActiveCouch::Base
0
+ site 'http://localhost:5984'
0
+ has :body
0
+ end
0
+
0
+ class Blog < ActiveCouch::Base
0
+ site 'http://localhost:5984'
0
+ has :title
0
+ has_many :comments
0
+ end
0
+
0
+ # Define the migration
0
+ class ByTitle < ActiveCouch::Migration
0
+ define :for_db => 'blogs' do
0
+ with_key 'title'
0
+ end
0
+ end
0
+
0
+ # Create the database first
0
+ ActiveCouch::Migrator.create_database('http://localhost:5984', 'blogs')
0
+ # Create a view
0
+ ActiveCouch::Migrator.migrate('http://localhost:5984', ByTitle)
0
+ blog = Blog.new(:title => 'iPhone in Singapore')
0
+ # Associations
0
+ blog.add_comment(Comment.new(:body => 'soon plz'))
0
+ blog.add_comment(Comment.new(:body => 'ya rly!'))
0
+ # Save the blog
0
+ blog.save
0
+ end
0
+
0
+ after(:each) do
0
+ # Create the database first
0
+ ActiveCouch::Migrator.delete_database('http://localhost:5984', 'blogs')
0
+ end
0
+
0
+ it "should be able to retrieve the simple attributes" do
0
+ blog = Blog.find(:first, :params => {:title => 'iPhone in Singapore'})
0
+ blog.title.should == 'iPhone in Singapore'
0
+ end
0
+
0
+ it "should be able to retrieve associations" do
0
+ blog = Blog.find(:first, :params => {:title => 'iPhone in Singapore'})
0
+ blog.comments.size.should == 2
0
+ # Check whether the bodies of the comments exist
0
+ (blog.comments.inspect =~ /soon plz/).should_not == nil
0
+ (blog.comments.inspect =~ /ya rly!/).should_not == nil
0
+ end
0
+
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.