test/test_model.rb in couchbase-model-0.4.4 vs test/test_model.rb in couchbase-model-0.5.0
- old
+ new
@@ -22,31 +22,71 @@
attribute :body
attribute :author, :default => 'Anonymous'
attribute :created_at, :default => lambda { Time.utc("2010-01-01") }
end
+class ValidPost < Couchbase::Model
+ attribute :title
+
+ def valid?
+ title && !title.empty?
+ end
+end
+
class Brewery < Couchbase::Model
attribute :name
end
class Beer < Couchbase::Model
attribute :name
belongs_to :brewery
end
+class Attachment < Couchbase::Model
+ defaults :format => :plain
+end
+
class TestModel < MiniTest::Unit::TestCase
def setup
@mock = start_mock
- Post.bucket = Couchbase.connect(:hostname => @mock.host,
- :port => @mock.port)
+ bucket = Couchbase.connect(:hostname => @mock.host, :port => @mock.port)
+ [Post, ValidPost, Brewery, Beer, Attachment].each do |model|
+ model.bucket = bucket
+ end
end
def teardown
stop_mock(@mock)
end
+ def test_it_supports_value_property
+ doc = {
+ "id" => "x",
+ "key" => "x",
+ "value" => "x",
+ "doc" => {
+ "value" => {"title" => "foo"}
+ }
+ }
+ post = Post.wrap(Post.bucket, doc)
+ assert_equal "foo", post.title
+ end
+
+ def test_it_supports_json_property
+ doc = {
+ "id" => "x",
+ "key" => "x",
+ "value" => "x",
+ "doc" => {
+ "json" => {"title" => "foo"}
+ }
+ }
+ post = Post.wrap(Post.bucket, doc)
+ assert_equal "foo", post.title
+ end
+
def test_assigns_attributes_from_the_hash
post = Post.new(:title => "Hello, world")
assert_equal "Hello, world", post.title
refute post.body
refute post.id
@@ -177,8 +217,27 @@
end
def test_to_param
assert_equal "the-id", Post.new(:id => "the-id").to_param
assert_equal "the-key", Post.new(:key => ["the", "key"]).to_param
+ end
+
+ def test_validation
+ post = ValidPost.create(:title => 'Hello, World!')
+ assert post.valid?, "post with title should be valid"
+ post.title = nil
+ assert_raises(Couchbase::Error::RecordInvalid) do
+ post.save
+ end
+ assert_raises(Couchbase::Error::RecordInvalid) do
+ ValidPost.create(:title => nil)
+ end
+ end
+
+ def test_blob_documents
+ contents = File.read(__FILE__)
+ id = Attachment.create(:raw => contents).id
+ blob = Attachment.find(id)
+ assert_equal contents, blob.raw
end
end