test/test_document.rb in couchpillow-0.4.3 vs test/test_document.rb in couchpillow-0.4.4
- old
+ new
@@ -31,19 +31,19 @@
end
def test_default_type
d = Document.new({}, "1")
- assert_equal "default", d._type
+ assert_equal "couchpillow", d.doc_type
end
def test_type_subclasses
d = Class.new(Document) do
type 'test'
end.new
- assert_equal "test", d._type
+ assert_equal "test", d.doc_type
end
def test_update
d = Class.new(Document) do
@@ -122,18 +122,18 @@
def test_to_json
mock_time
d = Document.new({}, "1")
- assert_equal "{\"_id\":\"1\",\"_type\":\"default\",\"_created_at\":\"#{mock_time.to_s}\",\"_updated_at\":\"#{mock_time.to_s}\"}", d.to_json
+ assert_equal "{\"_id\":\"1\",\"_type\":\"couchpillow\",\"_created_at\":\"#{mock_time.to_s}\",\"_updated_at\":\"#{mock_time.to_s}\"}", d.to_json
end
def test_to_hash
mock_time
d = Document.new({}, "1")
- assert_equal({ :_id => "1", :_created_at => mock_time, :_type => "default", :_updated_at => mock_time }, d.to_hash)
+ assert_equal({ :_id => "1", :_created_at => mock_time, :_type => "couchpillow", :_updated_at => mock_time }, d.to_hash)
end
def test_brackets
d = Document.new({})
@@ -199,19 +199,20 @@
end
k = klass.new
k.foo = 100
k.save!
- k_id = k._id
+ k_id = k.id
d = klass.get(k_id)
assert_equal 100, d.foo
end
def test_get_returns_nil
CouchPillow.db.expects(:get).with('123').returns(nil)
+ Document.expects(:default_db).returns(CouchPillow.db)
d = Document.get('123')
assert_equal nil, d
end
@@ -336,10 +337,29 @@
assert_equal "1", d.foo
assert_equal "100", d.not_auto
end
+ def test_auto_convert_triggers_on_update
+ d = Class.new(Document) do
+ type 'test'
+
+ attribute :foo do
+ type(String)
+ auto_convert
+ end
+
+ attribute :not_auto do
+ type Integer
+ end
+ end.new( foo: 1, not_auto: "100" )
+
+ d.update( foo: 1776 )
+ assert_equal "1776", d.foo
+ end
+
+
def test_dsl_style
d = Class.new(Document) do
type 'test'
attribute :foo do
@@ -389,8 +409,81 @@
end
end.new
d.save! ttl: 100
+ end
+
+
+ def test_custom_db_connection
+ conn1 = FakeCouchbaseServer.new
+
+ d = Class.new(Document) do
+ type 'test'
+
+ db conn1
+
+ attribute :foo do
+ type String
+ auto_convert
+ required
+ default { "tester" }
+ end
+
+
+ def initialize doc = {}
+ super(doc, "1")
+ end
+
+ end.new
+ d.foo = "hello"
+ d.save!
+
+ fromdefdb = CouchPillow.db.get("1")
+ assert_nil fromdefdb
+
+ fromprimdb = conn1.get("1")
+ assert_equal "hello", fromprimdb[:foo]
+ end
+
+
+ def test_multiple_db_connections
+ db1 = FakeCouchbaseServer.new
+ db2 = FakeCouchbaseServer.new
+ db3 = FakeCouchbaseServer.new
+
+ d = Class.new(Document) do
+ type 'test'
+
+ db db1
+ db db2
+ db db3
+
+ attribute :foo do
+ type String
+ auto_convert
+ required
+ default { "tester" }
+ end
+
+
+ def initialize doc = {}
+ super(doc, "1")
+ end
+
+ end.new
+ d.foo = "hello"
+ d.save!
+
+ res = d.wait
+
+ data = db1.get("1")
+ assert_equal "hello", data[:foo]
+
+ data = db2.get("1")
+ assert_equal "hello", data[:foo]
+
+ data = db3.get("1")
+ assert_equal "hello", data[:foo]
end
end