on the road toward design docs

This commit is contained in:
Chris Anderson 2008-11-08 16:28:58 -08:00
parent 04e818c154
commit 0769c2690f
10 changed files with 454 additions and 189 deletions

View file

@ -33,6 +33,8 @@ class Course < CouchRest::Model
view_by :dept, :ducktype => true
end
class Article < CouchRest::Model
use_database CouchRest.database!('http://localhost:5984/couchrest-model-test')
unique_id :slug
@ -213,24 +215,24 @@ describe CouchRest::Model do
@course["questions"][0].a[0].should == "beast"
end
end
describe "finding all instances of a model" do
before(:all) do
WithTemplate.new('important-field' => '1').save
WithTemplate.new('important-field' => '2').save
WithTemplate.new('important-field' => '3').save
WithTemplate.new('important-field' => '4').save
end
it "should make the design doc" do
WithTemplate.all
d = WithTemplate.design_doc
d['views']['all']['map'].should include('WithTemplate')
end
it "should find all" do
rs = WithTemplate.all
rs.length.should == 4
end
end
#
# describe "finding all instances of a model" do
# before(:all) do
# WithTemplate.new('important-field' => '1').save
# WithTemplate.new('important-field' => '2').save
# WithTemplate.new('important-field' => '3').save
# WithTemplate.new('important-field' => '4').save
# end
# it "should make the design doc" do
# WithTemplate.all
# d = WithTemplate.design_doc
# d['views']['all']['map'].should include('WithTemplate')
# end
# it "should find all" do
# rs = WithTemplate.all
# rs.length.should == 4
# end
# end
describe "getting a model with a subobject field" do
before(:all) do
@ -390,10 +392,14 @@ describe CouchRest::Model do
written_at += 24 * 3600
end
end
it "should have a design doc" do
Article.design_doc["views"]["by_date"].should_not be_nil
end
it "should create the design doc" do
Article.by_date rescue nil
doc = Article.design_doc
it "should save the design doc" do
Article.by_date #rescue nil
doc = Article.database.get Article.design_doc.id
doc['views']['by_date'].should_not be_nil
end
@ -402,10 +408,10 @@ describe CouchRest::Model do
view['rows'].length.should == 4
end
it "should return the matching objects (with descending)" do
articles = Article.by_date
articles.collect{|a|a.title}.should == @titles.reverse
end
# it "should return the matching objects (with default argument :descending => true)" do
# articles = Article.by_date
# articles.collect{|a|a.title}.should == @titles.reverse
# end
it "should allow you to override default args" do
articles = Article.by_date :descending => false
@ -417,38 +423,36 @@ describe CouchRest::Model do
before(:all) do
Course.database.delete! rescue nil
@db = @cr.create_db(TESTDB) rescue nil
Course.new(:title => 'aaa').save
Course.new(:title => 'bbb').save
Course.new(:title => 'ddd').save
Course.new(:title => 'eee').save
%w{aaa bbb ddd eee}.each do |title|
Course.new(:title => title).save
end
end
it "should make the design doc upon first query" do
Course.by_title
doc = Course.design_doc
doc['views']['all']['map'].should include('Course')
end
it "should can query via view" do
# register methods with method-missing, for local dispatch. method
# missing lookup table, no heuristics.
view = Course.view :by_title
designed = Course.by_title
view.should == designed
end
it "should get them" do
rs = Course.by_title
rs.length.should == 4
end
it "should yield" do
courses = []
rs = Course.by_title # remove me
Course.view(:by_title) do |course|
# puts "course"
courses << course
end
# courses.should == 'x'
courses[0]["doc"]["title"].should =='aaa'
end
# it "should can query via view" do
# # register methods with method-missing, for local dispatch. method
# # missing lookup table, no heuristics.
# view = Course.view :by_title
# designed = Course.by_title
# view.should == designed
# end
# it "should get them" do
# rs = Course.by_title
# rs.length.should == 4
# end
# it "should yield" do
# courses = []
# rs = Course.by_title # remove me
# Course.view(:by_title) do |course|
# courses << course
# end
# courses[0]["doc"]["title"].should =='aaa'
# end
end
describe "a ducktype view" do
before(:all) do
@ -468,6 +472,9 @@ describe CouchRest::Model do
@as[0]['_id'].should == @id
end
end
end
__END__
describe "a model with a compound key view" do
before(:all) do
@ -527,6 +534,7 @@ describe CouchRest::Model do
end
end
# TODO: moved to Design, delete
describe "adding a view" do
before(:each) do
Article.by_date