test/test_document.rb in couchpillow-0.2.0 vs test/test_document.rb in couchpillow-0.3.0
- old
+ new
@@ -3,17 +3,12 @@
require 'mocha/mini_test'
require './lib/couchpillow.rb'
class TestDocument < Minitest::Test
- class TestDoc < CouchPillow::Document
- type :test
- validate_presence :xyz
- validate :xyz, "must be Numeric", lambda { |v| v.is_a? Numeric }
- end
+ Document = CouchPillow::Document
-
def mock_time
return @time if @time
@time = Time.now
Time.stubs(:now).returns(@time)
@time
@@ -29,80 +24,113 @@
CouchPillow.db = @db
end
def test_create
- d = CouchPillow::Document.new
+ d = Document.new
d.save!
end
def test_timestamp
- d = CouchPillow::Document.new({}, "1")
+ d = Document.new({}, "1")
d.save!
assert d.created_at
assert d.updated_at
end
def test_type
- d = CouchPillow::Document.new({}, "1")
+ d = Document.new({}, "1")
assert_equal "default", d._type
end
def test_type_subclasses
- d = TestDoc.new({}, "1")
+ d = Class.new(Document) do
+ type 'test'
+ end.new
assert_equal "test", d._type
end
def test_validate_presence
- d = TestDoc.new
- assert_raises CouchPillow::Document::ValidationError do
+ d = Class.new(Document) do
+ validate_presence :xyz
+ end.new
+ assert_raises Document::ValidationError do
d.save!
end
d.xyz = 10
d.save!
end
+ def test_validate_type
+ d = Class.new(Document) do
+ validate_type :abc, Hash
+ end.new
+
+ d.abc = "other type"
+ assert_raises Document::ValidationError do
+ d.save!
+ end
+
+ d.abc = { :hello => "world" }
+ d.save!
+ end
+
+
def test_validate_custom
- d = TestDoc.new
+ d = Class.new(Document) do
+ validate :xyz, "must be Numeric", lambda { |v| v.is_a? Numeric }
+ end.new
d.xyz = "string"
- assert_raises CouchPillow::Document::ValidationError do
+ assert_raises Document::ValidationError do
d.save!
end
d.xyz = {}
- assert_raises CouchPillow::Document::ValidationError do
+ assert_raises Document::ValidationError do
d.save!
end
d.xyz = 123
d.save!
end
def test_to_json
mock_time
- d = CouchPillow::Document.new({}, "1")
+ d = Document.new({}, "1")
assert_equal "{\"_id\":\"1\",\"created_at\":\"#{mock_time.to_s}\",\"_type\":\"default\"}", d.to_json
end
def test_brackets
- d = CouchPillow::Document.new({})
+ d = Document.new({})
d[123] = 'test'
assert_equal 'test', d[123]
d['foo'] = 'bar'
assert_equal 'bar', d[:foo]
d[:stuff] = [ 'john', 'mary' ]
assert_equal ['john', 'mary'], d[:stuff]
assert_equal nil, d[:something_else]
+ end
+
+
+ def test_rename_keys
+ d = Class.new(Document) do
+ rename :foo, :bar
+ end.new( { :foo => 123, :other => 'abc' } )
+ assert_equal 123, d[:bar]
+ assert_equal 123, d.bar
+ assert_equal nil, d[:foo]
+ refute d.respond_to?(:foo)
+ assert_equal 'abc', d.other
end
end