Merge commit 'mattetti/master'

This commit is contained in:
Peter Gumeson 2009-07-19 00:01:07 -07:00
commit 1e44302d1a
25 changed files with 695 additions and 135 deletions

View file

@ -191,7 +191,7 @@ describe CouchRest do
describe "using a proxy for RestClient connections" do
it "should set proxy url for RestClient" do
CouchRest.proxy 'http://localhost:8888/'
proxy_uri = URI.parse(RestClient.proxy)
proxy_uri = URI.parse(HttpAbstraction.proxy)
proxy_uri.host.should eql( 'localhost' )
proxy_uri.port.should eql( 8888 )
CouchRest.proxy nil

View file

@ -690,7 +690,7 @@ describe CouchRest::Database do
it "should recreate a db even tho it doesn't exist" do
@cr.databases.should_not include(@db2.name)
@db2.recreate!
begin @db2.recreate! rescue nil end
@cr.databases.should include(@db2.name)
end

View file

@ -121,15 +121,28 @@ describe "ExtendedDocument" do
end
describe "a new model" do
it "should be a new_record" do
it "should be a new document" do
@obj = Basic.new
@obj.rev.should be_nil
@obj.should be_new
@obj.should be_new_document
@obj.should be_new_record
end
it "should be a new_document" do
@obj = Basic.new
@obj.rev.should be_nil
@obj.should be_new
end
describe "creating a new document" do
it "should instantialize and save a document" do
article = Article.create(:title => 'my test')
article.title.should == 'my test'
article.should_not be_new
end
it "should trigger the create callbacks" do
doc = WithCallBacks.create(:name => 'my other test')
doc.run_before_create.should be_true
doc.run_after_create.should be_true
doc.run_before_save.should be_true
doc.run_after_save.should be_true
end
end

View file

@ -121,7 +121,7 @@ describe "ExtendedDocument views" do
describe "a model class not tied to a database" do
before(:all) do
reset_test_db!
@db = DB
@db = DB
%w{aaa bbb ddd eee}.each do |title|
u = Unattached.new(:title => title)
u.database = @db
@ -133,14 +133,15 @@ describe "ExtendedDocument views" do
lambda{Unattached.all}.should raise_error
end
it "should query all" do
rs = Unattached.all :database=>@db
Unattached.cleanup_design_docs!(@db)
rs = Unattached.all :database => @db
rs.length.should == 4
end
it "should barf on query if no database given" do
lambda{Unattached.view :by_title}.should raise_error
end
it "should make the design doc upon first query" do
Unattached.by_title :database=>@db
Unattached.by_title :database => @db
doc = Unattached.design_doc
doc['views']['all']['map'].should include('Unattached')
end
@ -157,7 +158,7 @@ describe "ExtendedDocument views" do
things = []
Unattached.view(:by_title, :database=>@db) do |thing|
things << thing
end
end
things[0]["doc"]["title"].should =='aaa'
end
it "should yield with by_key method" do
@ -337,5 +338,78 @@ describe "ExtendedDocument views" do
Article.design_doc["views"].keys.should include("by_updated_at")
end
end
describe "with a collection" do
before(:all) do
reset_test_db!
@titles = ["very uniq one", "really interesting", "some fun",
"really awesome", "crazy bob", "this rocks", "super rad"]
@titles.each_with_index do |title,i|
a = Article.new(:title => title, :date => Date.today)
a.save
end
end
it "should return a proxy that looks like an array of 7 Article objects" do
articles = Article.by_date :key => Date.today
articles.class.should == Array
articles.size.should == 7
end
it "should get a subset of articles using paginate" do
articles = Article.by_date :key => Date.today
articles.paginate(:page => 1, :per_page => 3).size.should == 3
articles.paginate(:page => 2, :per_page => 3).size.should == 3
articles.paginate(:page => 3, :per_page => 3).size.should == 1
end
it "should get all articles, a few at a time, using paginated each" do
articles = Article.by_date :key => Date.today
articles.paginated_each(:per_page => 3) do |a|
a.should_not be_nil
end
end
it "should provide a class method to access the collection directly" do
articles = Article.collection_proxy_for('Article', 'by_date', :descending => true,
:key => Date.today, :include_docs => true)
articles.class.should == Array
articles.size.should == 7
end
it "should provide a class method for paginate" do
articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date',
:per_page => 3, :descending => true, :key => Date.today, :include_docs => true)
articles.size.should == 3
articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date',
:per_page => 3, :page => 2, :descending => true, :key => Date.today, :include_docs => true)
articles.size.should == 3
articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date',
:per_page => 3, :page => 3, :descending => true, :key => Date.today, :include_docs => true)
articles.size.should == 1
end
it "should provide a class method for paginated_each" do
options = { :design_doc => 'Article', :view_name => 'by_date',
:per_page => 3, :page => 1, :descending => true, :key => Date.today,
:include_docs => true }
Article.paginated_each(options) do |a|
a.should_not be_nil
end
end
it "should provide a class method to get a collection for a view" do
class Article
provides_collection :article_details, 'Article', 'by_date', :descending => true, :include_docs => true
end
articles = Article.find_all_article_details(:key => Date.today)
articles.class.should == Array
articles.size.should == 7
end
it "should raise an exception if design_doc is not provided" do
lambda{Article.collection_proxy_for(nil, 'by_date')}.should raise_error
lambda{Article.paginate(:view_name => 'by_date')}.should raise_error
end
it "should raise an exception if view_name is not provided" do
lambda{Article.collection_proxy_for('Article', nil)}.should raise_error
lambda{Article.paginate(:design_doc => 'Article')}.should raise_error
end
end
end

View file

@ -142,7 +142,29 @@ describe "ExtendedDocument properties" do
@event['occurs_at'].should be_an_instance_of(Time)
end
end
end
describe "casting to Float object" do
class RootBeerFloat < CouchRest::ExtendedDocument
use_database DB
property :price, :cast_as => 'Float'
end
it "should convert a string into a float if casted as so" do
RootBeerFloat.new(:price => '12.50').price.should == 12.50
RootBeerFloat.new(:price => '9').price.should == 9.0
RootBeerFloat.new(:price => '-9').price.should == -9.0
end
it "should not convert a string if it's not a string that can be cast as a float" do
RootBeerFloat.new(:price => 'test').price.should == 'test'
end
it "should work fine when a float is being passed" do
RootBeerFloat.new(:price => 9.99).price.should == 9.99
end
end
end
end
describe "a newly created casted model" do