test/test_model.rb in couchbase-model-0.0.1 vs test/test_model.rb in couchbase-model-0.1.0
- old
+ new
@@ -18,10 +18,12 @@
require File.join(File.dirname(__FILE__), 'setup')
class Post < Couchbase::Model
attribute :title
attribute :body
+ attribute :author, :default => 'Anonymous'
+ attribute :created_at, :default => lambda { Time.new("2010-01-01") }
end
class TestModel < MiniTest::Unit::TestCase
def setup
@@ -39,10 +41,24 @@
assert_equal "Hello, world", post.title
refute post.body
refute post.id
end
+ def test_uses_default_value_or_nil
+ post = Post.new(:title => "Hello, world")
+ refute post.body
+ assert_equal 'Anonymous', post.author
+ assert_equal 'Anonymous', post.attributes[:author]
+ end
+
+ def test_allows_lambda_as_default_value
+ post = Post.new(:title => "Hello, world")
+ expected = Time.new("2010-01-01")
+ assert_equal expected, post.created_at
+ assert_equal expected, post.attributes[:created_at]
+ end
+
def test_assings_id_and_saves_the_object
post = Post.create(:title => "Hello, world")
assert post.id
end
@@ -58,19 +74,42 @@
double.update(:title => "Good bye, world")
orig.reload
assert_equal "Good bye, world", orig.title
end
+ def test_it_raises_not_found_exception
+ assert_raises Couchbase::Error::MissingId do
+ Post.find("missing_key")
+ end
+ end
+
+ def test_it_raises_not_found_exception
+ refute Post.find_by_id("missing_key")
+ end
+
def test_doesnt_raise_if_the_attribute_redefined
eval <<-EOC
class RefinedPost < Couchbase::Model
attribute :title
attribute :title
end
EOC
end
+ def test_allows_to_define_several_attributes_at_once
+ eval <<-EOC
+ class Comment < Couchbase::Model
+ attribute :name, :email, :body
+ end
+ EOC
+
+ comment = Comment.new
+ assert comment.respond_to?(:name)
+ assert comment.respond_to?(:email)
+ assert comment.respond_to?(:body)
+ end
+
def test_allows_arbitrary_ids
Post.create(:id => uniq_id, :title => "Foo")
assert_equal "Foo", Post.find(uniq_id).title
end
@@ -98,10 +137,12 @@
end
def test_deletes_an_existent_model
post = Post.create(:id => uniq_id)
assert post.delete
- refute Post.bucket.get(uniq_id)
+ assert_raises Couchbase::Error::NotFound do
+ Post.bucket.get(uniq_id)
+ end
end
def test_fails_to_delete_model_without_id
post = Post.new(:title => "Hello")
refute post.id