test/test_model.rb in couchbase-model-0.5.3 vs test/test_model.rb in couchbase-model-0.5.4
- old
+ new
@@ -39,10 +39,15 @@
class Beer < Couchbase::Model
attribute :name
belongs_to :brewery
end
+class Wine < Couchbase::Model
+ attribute :name
+ belongs_to :winery, :class_name => :Brewery
+end
+
class Attachment < Couchbase::Model
defaults :format => :plain
end
class Comments < Couchbase::Model
@@ -57,11 +62,11 @@
class TestModel < MiniTest::Unit::TestCase
def setup
@mock = start_mock
bucket = Couchbase.connect(:hostname => @mock.host, :port => @mock.port)
- [Post, ValidPost, Brewery, Beer, Attachment].each do |model|
+ [Post, ValidPost, Brewery, Beer, Attachment, Wine].each do |model|
model.bucket = bucket
end
end
def teardown
@@ -98,10 +103,21 @@
}
post = Post.wrap(Post.bucket, doc)
assert_equal 'foo', post.title
end
+ def test_access_attribute_by_key
+ post = Post.new(:title => 'Hello, world')
+ assert_equal 'Hello, world', post[:title]
+ end
+
+ def test_update_attribute_by_key
+ post = Post.new(:title => 'Hello, world')
+ post[:title] = 'world, Hello'
+ assert_equal 'world, Hello', 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
@@ -138,10 +154,19 @@
double.update(:title => 'Good bye, world')
orig.reload
assert_equal 'Good bye, world', orig.title
end
+ def test_reloads_cas_value_with_reload_method
+ orig = Post.create(:title => "Hello, world")
+ double = Post.find(orig.id)
+ orig.update(:title => "Good bye, world")
+ double.reload
+
+ assert_equal orig.meta[:cas], double.meta[:cas]
+ end
+
def test_it_raises_not_found_exception
assert_raises Couchbase::Error::NotFound do
Post.find('missing_key')
end
end
@@ -204,9 +229,19 @@
post = Post.create(:id => uniq_id)
assert post.delete
assert_raises Couchbase::Error::NotFound do
Post.bucket.get(uniq_id)
end
+ end
+
+ def test_belongs_to_with_class_name_assoc
+ brewery = Brewery.create(:name => "R Wines")
+ assert_includes Wine.attributes.keys, :winery_id
+ wine = Wine.create(:name => "Classy", :winery_id => brewery.id)
+ assert_respond_to wine, :winery
+ assoc = wine.winery
+ assert_instance_of Brewery, assoc
+ assert_equal "R Wines", assoc.name
end
def test_fails_to_delete_model_without_id
post = Post.new(:title => 'Hello')
refute post.id