most of the callbacks are installed, adding view generation

This commit is contained in:
Chris Anderson 2008-09-29 16:28:57 -07:00
parent ce3a3258bc
commit 5e0cb81ad7
2 changed files with 297 additions and 13 deletions

View file

@ -8,6 +8,20 @@ class Article
include CouchRest::Model
use_database CouchRest.database!('http://localhost:5984/couchrest-model-test')
uniq_id :slug
key_accessor :title
key_reader :slug, :created_at, :updated_at
before(:create, :generate_slug_from_title)
timestamps!
def generate_slug_from_title
doc['slug'] = title.downcase.gsub(/[^a-z0-9]/,'-').squeeze('-').gsub(/^\-|\-$/,'')
end
key_writer :date
view_by :date
end
describe CouchRest::Model do
@ -16,6 +30,9 @@ describe CouchRest::Model do
@db = @cr.database(TESTDB)
@db.delete! rescue nil
@db = @cr.create_db(TESTDB) rescue nil
@adb = @cr.database('couchrest-model-test')
@adb.delete! rescue nil
CouchRest.database!('http://localhost:5984/couchrest-model-test')
CouchRest::Model.default_database = CouchRest.database!('http://localhost:5984/couchrest-test')
end
@ -27,6 +44,64 @@ describe CouchRest::Model do
Article.database.info['db_name'].should == 'couchrest-model-test'
end
describe "a new model" do
it "should be a new_record" do
@obj = Basic.new
@obj.should be_a_new_record
end
end
describe "a model with key_accessors" do
it "should allow reading keys" do
@art = Article.new
@art.doc['title'] = 'My Article Title'
@art.title.should == 'My Article Title'
end
it "should allow setting keys" do
@art = Article.new
@art.title = 'My Article Title'
@art.doc['title'].should == 'My Article Title'
end
end
describe "a model with key_writers" do
it "should allow setting keys" do
@art = Article.new
t = Time.now
@art.date = t
@art.doc['date'].should == t
end
it "should not allow reading keys" do
@art = Article.new
t = Time.now
@art.date = t
lambda{@art.date}.should raise_error
end
end
describe "a model with key_readers" do
it "should allow reading keys" do
@art = Article.new
@art.doc['slug'] = 'my-slug'
@art.slug.should == 'my-slug'
end
it "should not allow setting keys" do
@art = Article.new
lambda{@art.slug = 'My Article Title'}.should raise_error
end
end
describe "getting a model" do
before(:all) do
@art = Article.new(:title => 'All About Getting')
@art.save
end
it "should load and instantiate it" do
foundart = Article.get @art.id
foundart.title.should == "All About Getting"
end
end
describe "saving a model" do
before(:all) do
@obj = Basic.new
@ -55,13 +130,85 @@ describe CouchRest::Model do
end
describe "saving a model with a uniq_id configured" do
before(:all) do
before(:each) do
@art = Article.new
@old = Article.database.get('this-is-the-title') rescue nil
Article.database.delete(@old) if @old
end
it "should require the slug" do
it "should require the title" do
lambda{@art.save}.should raise_error
@art.slug = 'this-becomes-the-id'
@art.title = 'This is the title'
@art.save.should == true
end
it "should not change the slug on update" do
@art.title = 'This is the title'
@art.save.should == true
@art.title = 'new title'
@art.save.should == true
@art.slug.should == 'this-is-the-title'
end
it "should raise an error when the slug is taken" do
@art.title = 'This is the title'
@art.save.should == true
@art2 = Article.new(:title => 'This is the title!')
lambda{@art2.save}.should raise_error
end
it "should set the slug" do
@art.title = 'This is the title'
@art.save.should == true
@art.slug.should == 'this-is-the-title'
end
it "should set the id" do
@art.title = 'This is the title'
@art.save.should == true
@art.id.should == 'this-is-the-title'
end
end
describe "a model with timestamps" do
before(:all) do
@art = Article.new(:title => "Saving this")
@art.save
end
it "should set the time on create" do
(Time.now - @art.created_at).should < 2
foundart = Article.get @art.id
foundart.created_at.should == foundart.updated_at
end
it "should set the time on update" do
@art.save
@art.created_at.should < @art.updated_at
end
end
describe "a model with simple views" do
before(:all) do
written_at = Time.now - 24 * 3600 * 7
["this and that", "also interesting", "more fun", "some junk"].each do |title|
a = Article.new(:title => title)
a.date = written_at
a.save
written_at += 24 * 3600
end
end
it "should create the design doc" do
Article.by_date
doc = Article.database.get("_design/Article")
doc['views']['by_date'].should_not be_nil
end
it "should return the matching view result" do
view = Article.by_date :raw => true
# view.should == 'x'
# view['rows'].should == 4
end
end
end