test/test_document.rb in couchpillow-0.3.3 vs test/test_document.rb in couchpillow-0.3.4
- old
+ new
@@ -61,11 +61,11 @@
end
def test_validate_type
d = Class.new(Document) do
- validate_type :abc, Hash
+ validate_type :abc, Hash
end.new
d.abc = "other type"
assert_raises Document::ValidationError do
d.save!
@@ -74,10 +74,98 @@
d.abc = { :hello => "world" }
d.save!
end
+ def test_validate_type_also_ensures_types_on_create_integer
+ klass = Class.new(Document) do
+ type "test"
+ validate_type :abc, Integer
+ end
+
+ CouchPillow.db.
+ expects(:get).
+ with('123').
+ returns( { '_type' => 'test', 'abc' => '100' } )
+
+ d = klass.get('123')
+ assert d
+ assert_equal 100, d.abc
+ end
+
+
+ def test_validate_type_also_ensures_types_on_create_string
+ klass = Class.new(Document) do
+ type "test"
+ validate_type :abc, String
+ end
+
+ CouchPillow.db.
+ expects(:get).
+ with('123').
+ returns( { '_type' => 'test', 'abc' => 100 } )
+
+ d = klass.get('123')
+ assert d
+ assert_equal '100', d.abc
+ end
+
+
+ def test_validate_type_also_ensures_types_on_create_float
+ klass = Class.new(Document) do
+ type "test"
+ validate_type :abc, Float
+ end
+
+ CouchPillow.db.
+ expects(:get).
+ with('123').
+ returns( { '_type' => 'test', 'abc' => '121.21' } )
+
+ d = klass.get('123')
+ assert d
+ assert_equal 121.21, d.abc
+ end
+
+
+ def test_validate_type_also_ensures_types_on_create_array
+ klass = Class.new(Document) do
+ type "test"
+ validate_type :abc, Array
+ end
+
+ CouchPillow.db.
+ expects(:get).
+ with('123').
+ returns( { '_type' => 'test', 'abc' => [1, 2, 3] } )
+
+ d = klass.get('123')
+ assert d
+ assert_equal [1, 2, 3], d.abc
+ end
+
+
+ def test_validate_type_also_ensures_types_on_create_time
+ klass = Class.new(Document) do
+ type "test"
+ validate_type :abc, Time
+ end
+
+ CouchPillow.db.
+ expects(:get).
+ with('123').
+ returns( { '_type' => 'test', 'abc' => "2014/07/04" } )
+
+ d = klass.get('123')
+ assert d
+ assert d.abc.is_a?(Time)
+ assert_equal 2014, d.abc.year
+ assert_equal 7, d.abc.month
+ assert_equal 4, d.abc.day
+ end
+
+
def test_validate_custom
d = Class.new(Document) do
validate :xyz, "must be Numeric", lambda { |v| v.is_a? Numeric }
end.new
d.xyz = "string"
@@ -157,9 +245,24 @@
def test_get_returns_nil
CouchPillow.db.expects(:get).with('123').returns(nil)
d = Document.get('123')
+ assert_equal nil, d
+ end
+
+
+ def test_get_returns_a_document_of_a_different_type
+ klass = Class.new(Document) do
+ type 'test'
+ end
+
+ CouchPillow.db
+ .expects(:get)
+ .with('123')
+ .returns( { '_type' => 'something else', 'stuff' => 'data' } )
+
+ d = klass.get('123')
assert_equal nil, d
end
end