test/test_document.rb in couchpillow-0.4.5 vs test/test_document.rb in couchpillow-0.4.6

- old
+ new

@@ -207,27 +207,22 @@ assert_equal 100, d.foo end def test_get_returns_nil - CouchPillow.db.expects(:get).with('123', { extended: true }).returns(nil) - Document.expects(:default_db).returns(CouchPillow.db) + CouchPillow.db.expects(:get).with('couchpillow::123', { extended: true }).returns(nil) + Document.expects(:_default_db).returns(CouchPillow.db) 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', { extended: true }) - .returns( { '_type' => 'something else', 'stuff' => 'data' } ) - d = klass.get('123') assert_equal nil, d end @@ -415,46 +410,43 @@ def test_custom_db_connection conn1 = FakeCouchbaseServer.new - d = Class.new(Document) do + klass = Class.new(Document) do type 'test' db conn1 attribute :foo do type String auto_convert required default { "tester" } end + end - - def initialize doc = {} - super(doc, "1") - end - - end.new + d = klass.new({}, "1") d.foo = "hello" d.save! - fromdefdb = CouchPillow.db.get("1") + fromdefdb = CouchPillow.db.get("test::1") assert_nil fromdefdb - fromprimdb = conn1.get("1") + fromprimdb = conn1.get("test::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 + klass = Class.new(Document) do type 'test' + type_prefix false db db1 db db2 db db3 @@ -463,20 +455,17 @@ auto_convert required default { "tester" } end + end - def initialize doc = {} - super(doc, "1") - end - - end.new + d = klass.new( {}, "1" ) d.foo = "hello" d.save! - res = d.wait + res = klass.wait data = db1.get("1") assert_equal "hello", data[:foo] data = db2.get("1") @@ -546,9 +535,129 @@ d2.foo = "Someone else" d2.save! d1.save! assert_equal "This is mine", klass.get(doc_id).foo + end + + + def test_type_prefix_directive_default + assert Document._is_type_prefixed?, "type_prefix default is not true" + end + + + def test_type_prefix_directive_false + klass = Class.new(Document) do + type 'test' + type_prefix false + + attribute :foo do + type String + end + end + + d = klass.new( { foo: "Hello" } , "10" ) + d.save! + + dataindb = CouchPillow.db.get("10") + assert dataindb + assert_equal "Hello", dataindb[:foo] + end + + + def test_type_prefix_directive_true + klass = Class.new(Document) do + type 'test' + + attribute :foo do + type String + end + end + + d = klass.new( { foo: "Hello" } , "10" ) + assert_equal "10", d.id + d.save! + + d1 = klass.get("10") + assert_equal "Hello", d1.foo + + dataindb = CouchPillow.db.get("test::10") + assert dataindb + assert_equal "Hello", dataindb[:foo] + end + + + def test_new_with_type_prefix + klass = Class.new(Document) do + type 'test' + + attribute :foo do + type String + end + end + + d = klass.new( { foo: "Hello" } , "test::10" ) + assert_equal "10", d.id + + d = klass.new( { foo: "Hello" } , "wrong::10" ) + assert_equal "wrong::10", d.id + + klass = Class.new(Document) do + type 'test' + type_prefix false + + attribute :foo do + type String + end + end + + d = klass.new( { foo: "Hello" } , "test::10" ) + assert_equal "test::10", d.id + + d = klass.new( { foo: "Hello" } , "wrong::10" ) + assert_equal "wrong::10", d.id + end + + + def test_content_directive + klass = Class.new(Document) do + type 'test' + + attribute :foo do + type String + content { |v| v == "must" } + end + end + + d = klass.new( foo: "Hello" ) + + assert_raises CouchPillow::ValidationError do + d.save! + end + + d.foo = "must" + d.save! + end + + + def test_validate_content_alias + klass = Class.new(Document) do + type 'test' + + attribute :foo do + type String + validate_content { |v| v == "must" } + end + end + + d = klass.new( foo: "Hello" ) + + assert_raises CouchPillow::ValidationError do + d.save! + end + + d.foo = "must" + d.save! end end