samlown-couchrest 0.35
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +176 -0
- data/README.md +46 -0
- data/Rakefile +67 -0
- data/THANKS.md +19 -0
- data/examples/model/example.rb +144 -0
- data/examples/word_count/markov +38 -0
- data/examples/word_count/views/books/chunked-map.js +3 -0
- data/examples/word_count/views/books/united-map.js +1 -0
- data/examples/word_count/views/markov/chain-map.js +6 -0
- data/examples/word_count/views/markov/chain-reduce.js +7 -0
- data/examples/word_count/views/word_count/count-map.js +6 -0
- data/examples/word_count/views/word_count/count-reduce.js +3 -0
- data/examples/word_count/word_count.rb +46 -0
- data/examples/word_count/word_count_query.rb +40 -0
- data/examples/word_count/word_count_views.rb +26 -0
- data/history.txt +114 -0
- data/lib/couchrest/commands/generate.rb +71 -0
- data/lib/couchrest/commands/push.rb +103 -0
- data/lib/couchrest/core/adapters/restclient.rb +35 -0
- data/lib/couchrest/core/database.rb +377 -0
- data/lib/couchrest/core/design.rb +79 -0
- data/lib/couchrest/core/document.rb +84 -0
- data/lib/couchrest/core/http_abstraction.rb +48 -0
- data/lib/couchrest/core/response.rb +16 -0
- data/lib/couchrest/core/rest_api.rb +49 -0
- data/lib/couchrest/core/server.rb +88 -0
- data/lib/couchrest/core/view.rb +4 -0
- data/lib/couchrest/helper/pager.rb +103 -0
- data/lib/couchrest/helper/streamer.rb +51 -0
- data/lib/couchrest/helper/upgrade.rb +51 -0
- data/lib/couchrest/middlewares/logger.rb +263 -0
- data/lib/couchrest/mixins/attachments.rb +31 -0
- data/lib/couchrest/mixins/attribute_protection.rb +74 -0
- data/lib/couchrest/mixins/callbacks.rb +532 -0
- data/lib/couchrest/mixins/class_proxy.rb +124 -0
- data/lib/couchrest/mixins/collection.rb +260 -0
- data/lib/couchrest/mixins/design_doc.rb +103 -0
- data/lib/couchrest/mixins/document_queries.rb +80 -0
- data/lib/couchrest/mixins/extended_attachments.rb +70 -0
- data/lib/couchrest/mixins/extended_document_mixins.rb +9 -0
- data/lib/couchrest/mixins/properties.rb +154 -0
- data/lib/couchrest/mixins/validation.rb +246 -0
- data/lib/couchrest/mixins/views.rb +173 -0
- data/lib/couchrest/mixins.rb +4 -0
- data/lib/couchrest/monkeypatches.rb +113 -0
- data/lib/couchrest/more/casted_model.rb +58 -0
- data/lib/couchrest/more/extended_document.rb +310 -0
- data/lib/couchrest/more/property.rb +50 -0
- data/lib/couchrest/more/typecast.rb +175 -0
- data/lib/couchrest/support/blank.rb +42 -0
- data/lib/couchrest/support/class.rb +190 -0
- data/lib/couchrest/support/rails.rb +42 -0
- data/lib/couchrest/validation/auto_validate.rb +157 -0
- data/lib/couchrest/validation/contextual_validators.rb +78 -0
- data/lib/couchrest/validation/validation_errors.rb +125 -0
- data/lib/couchrest/validation/validators/absent_field_validator.rb +74 -0
- data/lib/couchrest/validation/validators/confirmation_validator.rb +107 -0
- data/lib/couchrest/validation/validators/format_validator.rb +122 -0
- data/lib/couchrest/validation/validators/formats/email.rb +66 -0
- data/lib/couchrest/validation/validators/formats/url.rb +43 -0
- data/lib/couchrest/validation/validators/generic_validator.rb +120 -0
- data/lib/couchrest/validation/validators/length_validator.rb +139 -0
- data/lib/couchrest/validation/validators/method_validator.rb +89 -0
- data/lib/couchrest/validation/validators/numeric_validator.rb +109 -0
- data/lib/couchrest/validation/validators/required_field_validator.rb +114 -0
- data/lib/couchrest.rb +162 -0
- data/spec/couchrest/core/couchrest_spec.rb +184 -0
- data/spec/couchrest/core/database_spec.rb +840 -0
- data/spec/couchrest/core/design_spec.rb +138 -0
- data/spec/couchrest/core/document_spec.rb +275 -0
- data/spec/couchrest/core/server_spec.rb +35 -0
- data/spec/couchrest/helpers/pager_spec.rb +122 -0
- data/spec/couchrest/helpers/streamer_spec.rb +52 -0
- data/spec/couchrest/more/attribute_protection_spec.rb +150 -0
- data/spec/couchrest/more/casted_extended_doc_spec.rb +79 -0
- data/spec/couchrest/more/casted_model_spec.rb +406 -0
- data/spec/couchrest/more/extended_doc_attachment_spec.rb +135 -0
- data/spec/couchrest/more/extended_doc_inherited_spec.rb +40 -0
- data/spec/couchrest/more/extended_doc_spec.rb +797 -0
- data/spec/couchrest/more/extended_doc_subclass_spec.rb +98 -0
- data/spec/couchrest/more/extended_doc_view_spec.rb +456 -0
- data/spec/couchrest/more/property_spec.rb +628 -0
- data/spec/fixtures/attachments/README +3 -0
- data/spec/fixtures/attachments/couchdb.png +0 -0
- data/spec/fixtures/attachments/test.html +11 -0
- data/spec/fixtures/more/article.rb +35 -0
- data/spec/fixtures/more/card.rb +22 -0
- data/spec/fixtures/more/cat.rb +20 -0
- data/spec/fixtures/more/course.rb +22 -0
- data/spec/fixtures/more/event.rb +8 -0
- data/spec/fixtures/more/invoice.rb +17 -0
- data/spec/fixtures/more/person.rb +9 -0
- data/spec/fixtures/more/question.rb +6 -0
- data/spec/fixtures/more/service.rb +12 -0
- data/spec/fixtures/more/user.rb +22 -0
- data/spec/fixtures/views/lib.js +3 -0
- data/spec/fixtures/views/test_view/lib.js +3 -0
- data/spec/fixtures/views/test_view/only-map.js +4 -0
- data/spec/fixtures/views/test_view/test-map.js +3 -0
- data/spec/fixtures/views/test_view/test-reduce.js +3 -0
- data/spec/spec.opts +6 -0
- data/spec/spec_helper.rb +49 -0
- data/utils/remap.rb +27 -0
- data/utils/subset.rb +30 -0
- metadata +223 -0
@@ -0,0 +1,98 @@
|
|
1
|
+
require File.expand_path("../../../spec_helper", __FILE__)
|
2
|
+
require File.join(FIXTURE_PATH, 'more', 'card')
|
3
|
+
require File.join(FIXTURE_PATH, 'more', 'course')
|
4
|
+
|
5
|
+
# add a default value
|
6
|
+
Card.property :bg_color, :default => '#ccc'
|
7
|
+
|
8
|
+
class BusinessCard < Card
|
9
|
+
property :extension_code
|
10
|
+
property :job_title
|
11
|
+
end
|
12
|
+
|
13
|
+
class DesignBusinessCard < BusinessCard
|
14
|
+
property :bg_color, :default => '#eee'
|
15
|
+
end
|
16
|
+
|
17
|
+
class OnlineCourse < Course
|
18
|
+
property :url
|
19
|
+
view_by :url
|
20
|
+
end
|
21
|
+
|
22
|
+
class Animal < CouchRest::ExtendedDocument
|
23
|
+
use_database TEST_SERVER.default_database
|
24
|
+
property :name
|
25
|
+
view_by :name
|
26
|
+
end
|
27
|
+
|
28
|
+
class Dog < Animal; end
|
29
|
+
|
30
|
+
describe "Subclassing an ExtendedDocument" do
|
31
|
+
|
32
|
+
before(:each) do
|
33
|
+
@card = BusinessCard.new
|
34
|
+
end
|
35
|
+
|
36
|
+
it "shouldn't messup the parent's properties" do
|
37
|
+
Card.properties.should_not == BusinessCard.properties
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should share the same db default" do
|
41
|
+
@card.database.uri.should == Card.database.uri
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should share the same autovalidation details" do
|
45
|
+
@card.auto_validation.should be_true
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should have kept the validation details" do
|
49
|
+
@card.should_not be_valid
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should have added the new validation details" do
|
53
|
+
validated_fields = @card.class.validators.contexts[:default].map{|v| v.field_name}
|
54
|
+
validated_fields.should include(:extension_code)
|
55
|
+
validated_fields.should include(:job_title)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should not add to the parent's validations" do
|
59
|
+
validated_fields = Card.validators.contexts[:default].map{|v| v.field_name}
|
60
|
+
validated_fields.should_not include(:extension_code)
|
61
|
+
validated_fields.should_not include(:job_title)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should inherit default property values" do
|
65
|
+
@card.bg_color.should == '#ccc'
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should be able to overwrite a default property" do
|
69
|
+
DesignBusinessCard.new.bg_color.should == '#eee'
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should have a design doc slug based on the subclass name" do
|
73
|
+
Course.refresh_design_doc
|
74
|
+
OnlineCourse.design_doc_slug.should =~ /^OnlineCourse/
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should have its own design_doc_fresh" do
|
78
|
+
Animal.refresh_design_doc
|
79
|
+
Dog.design_doc_fresh.should_not == true
|
80
|
+
Dog.refresh_design_doc
|
81
|
+
Dog.design_doc_fresh.should == true
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should not add views to the parent's design_doc" do
|
85
|
+
Course.design_doc['views'].keys.should_not include('by_url')
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should not add the parent's views to its design doc" do
|
89
|
+
Course.refresh_design_doc
|
90
|
+
OnlineCourse.refresh_design_doc
|
91
|
+
OnlineCourse.design_doc['views'].keys.should_not include('by_title')
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should have an all view with a guard clause for couchrest-type == subclass name in the map function" do
|
95
|
+
OnlineCourse.design_doc['views']['all']['map'].should =~ /if \(doc\['couchrest-type'\] == 'OnlineCourse'\)/
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
@@ -0,0 +1,456 @@
|
|
1
|
+
require File.expand_path("../../../spec_helper", __FILE__)
|
2
|
+
require File.join(FIXTURE_PATH, 'more', 'article')
|
3
|
+
require File.join(FIXTURE_PATH, 'more', 'course')
|
4
|
+
|
5
|
+
describe "ExtendedDocument views" do
|
6
|
+
|
7
|
+
class Unattached < CouchRest::ExtendedDocument
|
8
|
+
# Note: no use_database here
|
9
|
+
property :title
|
10
|
+
property :questions
|
11
|
+
property :professor
|
12
|
+
view_by :title
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "a model with simple views and a default param" do
|
16
|
+
before(:all) do
|
17
|
+
Article.all.map{|a| a.destroy(true)}
|
18
|
+
Article.database.bulk_delete
|
19
|
+
written_at = Time.now - 24 * 3600 * 7
|
20
|
+
@titles = ["this and that", "also interesting", "more fun", "some junk"]
|
21
|
+
@titles.each do |title|
|
22
|
+
a = Article.new(:title => title)
|
23
|
+
a.date = written_at
|
24
|
+
a.save
|
25
|
+
written_at += 24 * 3600
|
26
|
+
end
|
27
|
+
end
|
28
|
+
it "should have a design doc" do
|
29
|
+
Article.design_doc["views"]["by_date"].should_not be_nil
|
30
|
+
end
|
31
|
+
it "should save the design doc" do
|
32
|
+
Article.by_date #rescue nil
|
33
|
+
doc = Article.database.get Article.design_doc.id
|
34
|
+
doc['views']['by_date'].should_not be_nil
|
35
|
+
end
|
36
|
+
it "should return the matching raw view result" do
|
37
|
+
view = Article.by_date :raw => true
|
38
|
+
view['rows'].length.should == 4
|
39
|
+
end
|
40
|
+
it "should not include non-Articles" do
|
41
|
+
Article.database.save_doc({"date" => 1})
|
42
|
+
view = Article.by_date :raw => true
|
43
|
+
view['rows'].length.should == 4
|
44
|
+
end
|
45
|
+
it "should return the matching objects (with default argument :descending => true)" do
|
46
|
+
articles = Article.by_date
|
47
|
+
articles.collect{|a|a.title}.should == @titles.reverse
|
48
|
+
end
|
49
|
+
it "should allow you to override default args" do
|
50
|
+
articles = Article.by_date :descending => false
|
51
|
+
articles.collect{|a|a.title}.should == @titles
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "another model with a simple view" do
|
56
|
+
before(:all) do
|
57
|
+
reset_test_db!
|
58
|
+
%w{aaa bbb ddd eee}.each do |title|
|
59
|
+
Course.new(:title => title).save
|
60
|
+
end
|
61
|
+
end
|
62
|
+
it "should make the design doc upon first query" do
|
63
|
+
Course.by_title
|
64
|
+
doc = Course.design_doc
|
65
|
+
doc['views']['all']['map'].should include('Course')
|
66
|
+
end
|
67
|
+
it "should can query via view" do
|
68
|
+
# register methods with method-missing, for local dispatch. method
|
69
|
+
# missing lookup table, no heuristics.
|
70
|
+
view = Course.view :by_title
|
71
|
+
designed = Course.by_title
|
72
|
+
view.should == designed
|
73
|
+
end
|
74
|
+
it "should get them" do
|
75
|
+
rs = Course.by_title
|
76
|
+
rs.length.should == 4
|
77
|
+
end
|
78
|
+
it "should yield" do
|
79
|
+
courses = []
|
80
|
+
Course.view(:by_title) do |course|
|
81
|
+
courses << course
|
82
|
+
end
|
83
|
+
courses[0]["doc"]["title"].should =='aaa'
|
84
|
+
end
|
85
|
+
it "should yield with by_key method" do
|
86
|
+
courses = []
|
87
|
+
Course.by_title do |course|
|
88
|
+
courses << course
|
89
|
+
end
|
90
|
+
courses[0]["doc"]["title"].should =='aaa'
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "a ducktype view" do
|
95
|
+
before(:all) do
|
96
|
+
reset_test_db!
|
97
|
+
@id = DB.save_doc({:dept => true})['id']
|
98
|
+
end
|
99
|
+
it "should setup" do
|
100
|
+
duck = Course.get(@id) # from a different db
|
101
|
+
duck["dept"].should == true
|
102
|
+
end
|
103
|
+
it "should make the design doc" do
|
104
|
+
@as = Course.by_dept
|
105
|
+
@doc = Course.design_doc
|
106
|
+
@doc["views"]["by_dept"]["map"].should_not include("couchrest")
|
107
|
+
end
|
108
|
+
it "should not look for class" do
|
109
|
+
@as = Course.by_dept
|
110
|
+
@as[0]['_id'].should == @id
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe "a model class not tied to a database" do
|
115
|
+
before(:all) do
|
116
|
+
reset_test_db!
|
117
|
+
@db = DB
|
118
|
+
%w{aaa bbb ddd eee}.each do |title|
|
119
|
+
u = Unattached.new(:title => title)
|
120
|
+
u.database = @db
|
121
|
+
u.save
|
122
|
+
@first_id ||= u.id
|
123
|
+
end
|
124
|
+
end
|
125
|
+
it "should barf on all if no database given" do
|
126
|
+
lambda{Unattached.all}.should raise_error
|
127
|
+
end
|
128
|
+
it "should query all" do
|
129
|
+
Unattached.cleanup_design_docs!(@db)
|
130
|
+
rs = Unattached.all :database => @db
|
131
|
+
rs.length.should == 4
|
132
|
+
end
|
133
|
+
it "should barf on query if no database given" do
|
134
|
+
lambda{Unattached.view :by_title}.should raise_error
|
135
|
+
end
|
136
|
+
it "should make the design doc upon first query" do
|
137
|
+
Unattached.by_title :database => @db
|
138
|
+
doc = Unattached.design_doc
|
139
|
+
doc['views']['all']['map'].should include('Unattached')
|
140
|
+
end
|
141
|
+
it "should merge query params" do
|
142
|
+
rs = Unattached.by_title :database=>@db, :startkey=>"bbb", :endkey=>"eee"
|
143
|
+
rs.length.should == 3
|
144
|
+
end
|
145
|
+
it "should query via view" do
|
146
|
+
view = Unattached.view :by_title, :database=>@db
|
147
|
+
designed = Unattached.by_title :database=>@db
|
148
|
+
view.should == designed
|
149
|
+
end
|
150
|
+
it "should yield" do
|
151
|
+
things = []
|
152
|
+
Unattached.view(:by_title, :database=>@db) do |thing|
|
153
|
+
things << thing
|
154
|
+
end
|
155
|
+
things[0]["doc"]["title"].should =='aaa'
|
156
|
+
end
|
157
|
+
it "should yield with by_key method" do
|
158
|
+
things = []
|
159
|
+
Unattached.by_title(:database=>@db) do |thing|
|
160
|
+
things << thing
|
161
|
+
end
|
162
|
+
things[0]["doc"]["title"].should =='aaa'
|
163
|
+
end
|
164
|
+
it "should return nil on get if no database given" do
|
165
|
+
Unattached.get("aaa").should be_nil
|
166
|
+
end
|
167
|
+
it "should barf on get! if no database given" do
|
168
|
+
lambda{Unattached.get!("aaa")}.should raise_error
|
169
|
+
end
|
170
|
+
it "should get from specific database" do
|
171
|
+
u = Unattached.get(@first_id, @db)
|
172
|
+
u.title.should == "aaa"
|
173
|
+
end
|
174
|
+
it "should barf on first if no database given" do
|
175
|
+
lambda{Unattached.first}.should raise_error
|
176
|
+
end
|
177
|
+
it "should get first" do
|
178
|
+
u = Unattached.first :database=>@db
|
179
|
+
u.title.should =~ /\A...\z/
|
180
|
+
end
|
181
|
+
it "should barf on all_design_doc_versions if no database given" do
|
182
|
+
lambda{Unattached.all_design_doc_versions}.should raise_error
|
183
|
+
end
|
184
|
+
it "should be able to cleanup the db/bump the revision number" do
|
185
|
+
# if the previous specs were not run, the model_design_doc will be blank
|
186
|
+
Unattached.use_database DB
|
187
|
+
Unattached.view_by :questions
|
188
|
+
Unattached.by_questions(:database => @db)
|
189
|
+
original_revision = Unattached.model_design_doc(@db)['_rev']
|
190
|
+
Unattached.cleanup_design_docs!(@db)
|
191
|
+
Unattached.model_design_doc(@db)['_rev'].should_not == original_revision
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
describe "class proxy" do
|
196
|
+
before(:all) do
|
197
|
+
reset_test_db!
|
198
|
+
# setup the class default doc to save the design doc
|
199
|
+
Unattached.use_database nil # just to be sure it is really unattached
|
200
|
+
@us = Unattached.on(DB)
|
201
|
+
%w{aaa bbb ddd eee}.each do |title|
|
202
|
+
u = @us.new(:title => title)
|
203
|
+
u.save
|
204
|
+
@first_id ||= u.id
|
205
|
+
end
|
206
|
+
end
|
207
|
+
it "should query all" do
|
208
|
+
rs = @us.all
|
209
|
+
rs.length.should == 4
|
210
|
+
end
|
211
|
+
it "should count" do
|
212
|
+
@us.count.should == 4
|
213
|
+
end
|
214
|
+
it "should make the design doc upon first query" do
|
215
|
+
@us.by_title
|
216
|
+
doc = @us.design_doc
|
217
|
+
doc['views']['all']['map'].should include('Unattached')
|
218
|
+
end
|
219
|
+
it "should merge query params" do
|
220
|
+
rs = @us.by_title :startkey=>"bbb", :endkey=>"eee"
|
221
|
+
rs.length.should == 3
|
222
|
+
end
|
223
|
+
it "should query via view" do
|
224
|
+
view = @us.view :by_title
|
225
|
+
designed = @us.by_title
|
226
|
+
view.should == designed
|
227
|
+
end
|
228
|
+
it "should yield" do
|
229
|
+
things = []
|
230
|
+
@us.view(:by_title) do |thing|
|
231
|
+
things << thing
|
232
|
+
end
|
233
|
+
things[0]["doc"]["title"].should =='aaa'
|
234
|
+
end
|
235
|
+
it "should yield with by_key method" do
|
236
|
+
things = []
|
237
|
+
@us.by_title do |thing|
|
238
|
+
things << thing
|
239
|
+
end
|
240
|
+
things[0]["doc"]["title"].should =='aaa'
|
241
|
+
end
|
242
|
+
it "should get from specific database" do
|
243
|
+
u = @us.get(@first_id)
|
244
|
+
u.title.should == "aaa"
|
245
|
+
end
|
246
|
+
it "should get first" do
|
247
|
+
u = @us.first
|
248
|
+
u.title.should =~ /\A...\z/
|
249
|
+
end
|
250
|
+
it "should set database on first retreived document" do
|
251
|
+
u = @us.first
|
252
|
+
u.database.should === DB
|
253
|
+
end
|
254
|
+
it "should set database on all retreived documents" do
|
255
|
+
@us.all.each do |u|
|
256
|
+
u.database.should === DB
|
257
|
+
end
|
258
|
+
end
|
259
|
+
it "should set database on each retreived document" do
|
260
|
+
rs = @us.by_title :startkey=>"bbb", :endkey=>"eee"
|
261
|
+
rs.length.should == 3
|
262
|
+
rs.each do |u|
|
263
|
+
u.database.should === DB
|
264
|
+
end
|
265
|
+
end
|
266
|
+
it "should set database on document retreived by id" do
|
267
|
+
u = @us.get(@first_id)
|
268
|
+
u.database.should === DB
|
269
|
+
end
|
270
|
+
it "should not attempt to set database on raw results using :all" do
|
271
|
+
@us.all(:raw => true).each do |u|
|
272
|
+
u.respond_to?(:database).should be_false
|
273
|
+
end
|
274
|
+
end
|
275
|
+
it "should not attempt to set database on raw results using view" do
|
276
|
+
@us.by_title(:raw => true).each do |u|
|
277
|
+
u.respond_to?(:database).should be_false
|
278
|
+
end
|
279
|
+
end
|
280
|
+
it "should clean up design docs left around on specific database" do
|
281
|
+
@us.by_title
|
282
|
+
original_id = @us.model_design_doc['_rev']
|
283
|
+
Unattached.view_by :professor
|
284
|
+
@us.by_professor
|
285
|
+
@us.model_design_doc['_rev'].should_not == original_id
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
describe "a model with a compound key view" do
|
290
|
+
before(:all) do
|
291
|
+
Article.by_user_id_and_date.each{|a| a.destroy(true)}
|
292
|
+
Article.database.bulk_delete
|
293
|
+
written_at = Time.now - 24 * 3600 * 7
|
294
|
+
@titles = ["uniq one", "even more interesting", "less fun", "not junk"]
|
295
|
+
@user_ids = ["quentin", "aaron"]
|
296
|
+
@titles.each_with_index do |title,i|
|
297
|
+
u = i % 2
|
298
|
+
a = Article.new(:title => title, :user_id => @user_ids[u])
|
299
|
+
a.date = written_at
|
300
|
+
a.save
|
301
|
+
written_at += 24 * 3600
|
302
|
+
end
|
303
|
+
end
|
304
|
+
it "should create the design doc" do
|
305
|
+
Article.by_user_id_and_date rescue nil
|
306
|
+
doc = Article.design_doc
|
307
|
+
doc['views']['by_date'].should_not be_nil
|
308
|
+
end
|
309
|
+
it "should sort correctly" do
|
310
|
+
articles = Article.by_user_id_and_date
|
311
|
+
articles.collect{|a|a['user_id']}.should == ['aaron', 'aaron', 'quentin',
|
312
|
+
'quentin']
|
313
|
+
articles[1].title.should == 'not junk'
|
314
|
+
end
|
315
|
+
it "should be queryable with couchrest options" do
|
316
|
+
articles = Article.by_user_id_and_date :limit => 1, :startkey => 'quentin'
|
317
|
+
articles.length.should == 1
|
318
|
+
articles[0].title.should == "even more interesting"
|
319
|
+
end
|
320
|
+
end
|
321
|
+
|
322
|
+
describe "with a custom view" do
|
323
|
+
before(:all) do
|
324
|
+
@titles = ["very uniq one", "even less interesting", "some fun",
|
325
|
+
"really junk", "crazy bob"]
|
326
|
+
@tags = ["cool", "lame"]
|
327
|
+
@titles.each_with_index do |title,i|
|
328
|
+
u = i % 2
|
329
|
+
a = Article.new(:title => title, :tags => [@tags[u]])
|
330
|
+
a.save
|
331
|
+
end
|
332
|
+
end
|
333
|
+
it "should be available raw" do
|
334
|
+
view = Article.by_tags :raw => true
|
335
|
+
view['rows'].length.should == 5
|
336
|
+
end
|
337
|
+
|
338
|
+
it "should be default to :reduce => false" do
|
339
|
+
ars = Article.by_tags
|
340
|
+
ars.first.tags.first.should == 'cool'
|
341
|
+
end
|
342
|
+
|
343
|
+
it "should be raw when reduce is true" do
|
344
|
+
view = Article.by_tags :reduce => true, :group => true
|
345
|
+
view['rows'].find{|r|r['key'] == 'cool'}['value'].should == 3
|
346
|
+
end
|
347
|
+
end
|
348
|
+
|
349
|
+
# TODO: moved to Design, delete
|
350
|
+
describe "adding a view" do
|
351
|
+
before(:each) do
|
352
|
+
reset_test_db!
|
353
|
+
Article.by_date
|
354
|
+
@original_doc_rev = Article.model_design_doc['_rev']
|
355
|
+
@design_docs = Article.database.documents :startkey => "_design/", :endkey => "_design/\u9999"
|
356
|
+
end
|
357
|
+
it "should not create a design doc on view definition" do
|
358
|
+
Article.view_by :created_at
|
359
|
+
newdocs = Article.database.documents :startkey => "_design/", :endkey => "_design/\u9999"
|
360
|
+
newdocs["rows"].length.should == @design_docs["rows"].length
|
361
|
+
end
|
362
|
+
it "should create a new version of the design document on view access" do
|
363
|
+
ddocs = Article.all_design_doc_versions["rows"].length
|
364
|
+
Article.view_by :updated_at
|
365
|
+
Article.by_updated_at
|
366
|
+
@original_doc_rev.should_not == Article.model_design_doc['_rev']
|
367
|
+
Article.design_doc["views"].keys.should include("by_updated_at")
|
368
|
+
end
|
369
|
+
end
|
370
|
+
|
371
|
+
describe "with a collection" do
|
372
|
+
before(:all) do
|
373
|
+
reset_test_db!
|
374
|
+
titles = ["very uniq one", "really interesting", "some fun",
|
375
|
+
"really awesome", "crazy bob", "this rocks", "super rad"]
|
376
|
+
titles.each_with_index do |title,i|
|
377
|
+
a = Article.new(:title => title, :date => Date.today)
|
378
|
+
a.save
|
379
|
+
end
|
380
|
+
|
381
|
+
titles = ["yesterday very uniq one", "yesterday really interesting", "yesterday some fun",
|
382
|
+
"yesterday really awesome", "yesterday crazy bob", "yesterday this rocks"]
|
383
|
+
titles.each_with_index do |title,i|
|
384
|
+
a = Article.new(:title => title, :date => Date.today - 1)
|
385
|
+
a.save
|
386
|
+
end
|
387
|
+
end
|
388
|
+
require 'date'
|
389
|
+
it "should return a proxy that looks like an array of 7 Article objects" do
|
390
|
+
articles = Article.by_date :key => Date.today
|
391
|
+
articles.class.should == Array
|
392
|
+
articles.size.should == 7
|
393
|
+
end
|
394
|
+
it "should get a subset of articles using paginate" do
|
395
|
+
articles = Article.by_date :key => Date.today
|
396
|
+
articles.paginate(:page => 1, :per_page => 3).size.should == 3
|
397
|
+
articles.paginate(:page => 2, :per_page => 3).size.should == 3
|
398
|
+
articles.paginate(:page => 3, :per_page => 3).size.should == 1
|
399
|
+
end
|
400
|
+
it "should get all articles, a few at a time, using paginated each" do
|
401
|
+
articles = Article.by_date :key => Date.today
|
402
|
+
articles.paginated_each(:per_page => 3) do |a|
|
403
|
+
a.should_not be_nil
|
404
|
+
end
|
405
|
+
end
|
406
|
+
it "should provide a class method to access the collection directly" do
|
407
|
+
articles = Article.collection_proxy_for('Article', 'by_date', :descending => true,
|
408
|
+
:key => Date.today, :include_docs => true)
|
409
|
+
articles.class.should == Array
|
410
|
+
articles.size.should == 7
|
411
|
+
end
|
412
|
+
it "should provide a class method for paginate" do
|
413
|
+
articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date',
|
414
|
+
:per_page => 3, :descending => true, :key => Date.today, :include_docs => true)
|
415
|
+
articles.size.should == 3
|
416
|
+
|
417
|
+
articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date',
|
418
|
+
:per_page => 3, :page => 2, :descending => true, :key => Date.today, :include_docs => true)
|
419
|
+
articles.size.should == 3
|
420
|
+
|
421
|
+
articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date',
|
422
|
+
:per_page => 3, :page => 3, :descending => true, :key => Date.today, :include_docs => true)
|
423
|
+
articles.size.should == 1
|
424
|
+
end
|
425
|
+
it "should provide a class method for paginated_each" do
|
426
|
+
options = { :design_doc => 'Article', :view_name => 'by_date',
|
427
|
+
:per_page => 3, :page => 1, :descending => true, :key => Date.today,
|
428
|
+
:include_docs => true }
|
429
|
+
Article.paginated_each(options) do |a|
|
430
|
+
a.should_not be_nil
|
431
|
+
end
|
432
|
+
end
|
433
|
+
it "should provide a class method to get a collection for a view" do
|
434
|
+
articles = Article.find_all_article_details(:key => Date.today)
|
435
|
+
articles.class.should == Array
|
436
|
+
articles.size.should == 7
|
437
|
+
end
|
438
|
+
it "should raise an exception if design_doc is not provided" do
|
439
|
+
lambda{Article.collection_proxy_for(nil, 'by_date')}.should raise_error
|
440
|
+
lambda{Article.paginate(:view_name => 'by_date')}.should raise_error
|
441
|
+
end
|
442
|
+
it "should raise an exception if view_name is not provided" do
|
443
|
+
lambda{Article.collection_proxy_for('Article', nil)}.should raise_error
|
444
|
+
lambda{Article.paginate(:design_doc => 'Article')}.should raise_error
|
445
|
+
end
|
446
|
+
it "should be able to span multiple keys" do
|
447
|
+
articles = Article.by_date :startkey => Date.today, :endkey => Date.today - 1
|
448
|
+
articles.paginate(:page => 1, :per_page => 3).size.should == 3
|
449
|
+
articles.paginate(:page => 2, :per_page => 3).size.should == 3
|
450
|
+
articles.paginate(:page => 3, :per_page => 3).size.should == 3
|
451
|
+
articles.paginate(:page => 4, :per_page => 3).size.should == 3
|
452
|
+
articles.paginate(:page => 5, :per_page => 3).size.should == 1
|
453
|
+
end
|
454
|
+
end
|
455
|
+
|
456
|
+
end
|