Permalink
Browse files

Finalizing changes for initial working version

  • Loading branch information...
1 parent 1796e63 commit 22bbaad29e60092b5c3ee2d4f4f5dcf4c184bb06 @samlown samlown committed Jun 8, 2012
@@ -40,15 +40,21 @@ def sync(db = nil)
# Different! Update.
doc.merge!(to_hash)
else
- # No previous doc, use our version.
- doc = self
+ # No previous doc, use a *copy* of our version.
+ # Using a copy prevents reverse updates
+ doc = to_hash.dup
end
db.save_doc(doc)
end
set_cache_checksum(db, checksum)
end
self
+ rescue
+ puts "DESIGN DOC SYNC FAILED:"
+ puts docdb.inspect
+ puts doc.inspect
+ raise
end
@@ -1,5 +1,5 @@
class WithDefaultValues < CouchRest::Model::Base
- use_database TEST_SERVER.default_database
+ use_database DB
property :preset, Object, :default => {:right => 10, :top_align => false}
property :set_by_proc, Time, :default => Proc.new{Time.now}
property :tags, [String], :default => []
@@ -10,15 +10,15 @@ class WithDefaultValues < CouchRest::Model::Base
end
class WithSimplePropertyType < CouchRest::Model::Base
- use_database TEST_SERVER.default_database
+ use_database DB
property :name, String
property :preset, String, :default => 'none'
property :tags, [String]
timestamps!
end
class WithCallBacks < CouchRest::Model::Base
- use_database TEST_SERVER.default_database
+ use_database DB
property :name
property :run_before_validation
property :run_after_validation
@@ -85,19 +85,19 @@ def conditional_two_method
# Following two fixture classes have __intentionally__ diffent syntax for setting the validation context
class WithContextualValidationOnCreate < CouchRest::Model::Base
- use_database TEST_SERVER.default_database
+ use_database DB
property(:name, String)
validates(:name, :presence => {:on => :create})
end
class WithContextualValidationOnUpdate < CouchRest::Model::Base
- use_database TEST_SERVER.default_database
+ use_database DB
property(:name, String)
validates(:name, :presence => true, :on => :update)
end
class WithTemplateAndUniqueID < CouchRest::Model::Base
- use_database TEST_SERVER.default_database
+ use_database DB
unique_id do |model|
model.slug
end
@@ -108,7 +108,7 @@ class WithTemplateAndUniqueID < CouchRest::Model::Base
end
class WithGetterAndSetterMethods < CouchRest::Model::Base
- use_database TEST_SERVER.default_database
+ use_database DB
property :other_arg
def arg
@@ -121,7 +121,7 @@ def arg=(value)
end
class WithAfterInitializeMethod < CouchRest::Model::Base
- use_database TEST_SERVER.default_database
+ use_database DB
property :some_value
@@ -164,7 +164,7 @@ class WithScopedUniqueValidation < CouchRest::Model::Base
end
class WithDateAndTime < CouchRest::Model::Base
- use_database TEST_SERVER.default_database
+ use_database DB
property :exec_date, Date
property :exec_time, Time
end
View
@@ -402,25 +402,24 @@ def set_name; self.name = "foobar"; end
describe "counting all instances of a model" do
before(:each) do
- @db = reset_test_db!
+ reset_test_db!
end
-
+
it ".count should return 0 if there are no docuemtns" do
WithTemplateAndUniqueID.count.should == 0
end
-
+
it ".count should return the number of documents" do
WithTemplateAndUniqueID.new('slug' => '1').save
WithTemplateAndUniqueID.new('slug' => '2').save
WithTemplateAndUniqueID.new('slug' => '3').save
-
WithTemplateAndUniqueID.count.should == 3
end
end
describe "finding the first instance of a model" do
before(:each) do
- @db = reset_test_db!
+ reset_test_db!
WithTemplateAndUniqueID.new('slug' => '1').save
WithTemplateAndUniqueID.new('slug' => '2').save
WithTemplateAndUniqueID.new('slug' => '3').save
@@ -1,86 +0,0 @@
-require "spec_helper"
-
-describe "Collections" 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
-
- titles = ["yesterday very uniq one", "yesterday really interesting", "yesterday some fun",
- "yesterday really awesome", "yesterday crazy bob", "yesterday this rocks"]
- titles.each_with_index do |title,i|
- a = Article.new(:title => title, :date => Date.today - 1)
- a.save
- end
- end
- it "should return a proxy that looks like an array of 7 Article objects" 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)
- articles.size.should == 3
-
- articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date',
- :per_page => 3, :page => 2, :descending => true, :key => Date.today)
- articles.size.should == 3
-
- articles = Article.paginate(:design_doc => 'Article', :view_name => 'by_date',
- :per_page => 3, :page => 3, :descending => true, :key => Date.today)
- 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 }
- 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
- articles = Article.find_all_article_details(:key => Date.today)
- articles.class.should == Array
- articles.size.should == 7
- end
- it "should get a subset of articles using paginate" do
- articles = Article.collection_proxy_for('Article', 'by_date', :key => Date.today, :include_docs => true)
- 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.collection_proxy_for('Article', 'by_date', :key => Date.today, :include_docs => true)
- articles.paginated_each(:per_page => 3) do |a|
- a.should_not be_nil
- end
- 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
- it "should be able to span multiple keys" do
- articles = Article.collection_proxy_for('Article', 'by_date', :startkey => Date.today - 1, :endkey => Date.today, :include_docs => true)
- articles.paginate(:page => 1, :per_page => 3).size.should == 3
- articles.paginate(:page => 3, :per_page => 3).size.should == 3
- articles.paginate(:page => 5, :per_page => 3).size.should == 1
- end
- it "should pass database parameter to pager" do
- proxy = mock(:proxy)
- proxy.stub!(:paginate)
- ::CouchRest::Model::Collection::CollectionProxy.should_receive(:new).with('database', anything(), anything(), anything(), anything()).and_return(proxy)
- Article.paginate(:design_doc => 'Article', :view_name => 'by_date', :database => 'database')
- end
-
-end
@@ -145,6 +145,7 @@ class DesignSampleModel < CouchRest::Model::Base
@doc.send(:set_cache_checksum, @doc.database, nil)
@doc.sync
@doc['views'].should_not have_key('test')
+ @doc['_rev'].should be_nil
end
it "should be re-created if database destroyed" do
View
@@ -71,19 +71,24 @@ class Dog < Animal; end
end
it "should have a design doc slug based on the subclass name" do
- OnlineCourse.design_doc_slug.should =~ /^OnlineCourse/
+ OnlineCourse.design_doc['_id'].should =~ /OnlineCourse$/
end
it "should not add views to the parent's design_doc" do
Course.design_doc['views'].keys.should_not include('by_url')
end
- it "should not add the parent's views to its design doc" do
- OnlineCourse.design_doc['views'].keys.should_not include('by_title')
+ it "should add the parent's views to its design doc" do
+ OnlineCourse.design_doc['views'].keys.should include('by_title')
+ end
+
+ it "should add the parent's views but alter the model names in map function" do
+ OnlineCourse.design_doc['views']['by_title']['map'].should =~ /doc\['#{OnlineCourse.model_type_key}'\] == 'OnlineCourse'/
end
it "should have an all view with a guard clause for model == subclass name in the map function" do
OnlineCourse.design_doc['views']['all']['map'].should =~ /if \(doc\['#{OnlineCourse.model_type_key}'\] == 'OnlineCourse'\)/
end
+
end

0 comments on commit 22bbaad

Please sign in to comment.