def setUp(self): self.db = Mock() self.metadata = Metadata() self.Base = collection( self.metadata, "mydoc", Field("_id", int), Field("disc", str, default="base"), polymorphic_discriminator="disc", ) self.Derived = derived( self.Base, "derived", Field("x", int, default=10), ) self.Derived2 = derived( self.Base, "derived2", Field("x", int, default=10), allow_extra=True, strip_extra=False, ) self.metadata.bind(self.db) self.db.mydoc.with_options.return_value = self.db.mydoc
def setUp(self): self.db = Mock() self.metadata = Metadata() self.MyDoc = collection( self.metadata, "mydoc", Field("_id", int, default=0), Field("x", int, default=0), ) self.metadata.bind(self.db) self.db.mydoc.with_options.return_value = self.db.mydoc
class TestDerived(TestCase): def setUp(self): self.db = Mock() self.metadata = Metadata() self.Base = collection( self.metadata, "mydoc", Field("_id", int), Field("disc", str, default="base"), polymorphic_discriminator="disc", ) self.Derived = derived( self.Base, "derived", Field("x", int, default=10), ) self.Derived2 = derived( self.Base, "derived2", Field("x", int, default=10), allow_extra=True, strip_extra=False, ) self.metadata.bind(self.db) self.db.mydoc.with_options.return_value = self.db.mydoc def test_query_base(self): self.assertEqual(self.Base.m.query._compile_query(), {"filter": {}}) def test_query_derived(self): self.assertEqual( self.Derived.m.query._compile_query(), {"filter": {"disc": re.compile("^derived")}}, ) def test_poly_query(self): self.db.mydoc.find.return_value = iter([self.Derived.m.create(_id=1)]) res = self.Base.m.query.one() self.assertEqual(type(res), self.Derived) def test_derived_with_extra(self): self.assertEqual(self.Derived.m.schema.allow_extra, False) self.assertEqual(self.Derived.m.schema.strip_extra, False) self.assertEqual(self.Derived2.m.schema.allow_extra, True) self.assertEqual(self.Derived2.m.schema.strip_extra, False)
class TestCollection(TestCase): def setUp(self): self.db = Mock() self.metadata = Metadata() self.MyDoc = collection( self.metadata, "mydoc", Field("_id", int), Field("x", int), Index("x"), ) self.metadata.bind(self.db) self.db.mydoc.with_options.return_value = self.db.mydoc def test_can_find(self): self.db.mydoc.find.return_value = iter( [self.MyDoc({"_id": 0, "x": 5})] ) spec = {"a": {"$exists": True}} curs = self.MyDoc.m.find(spec) self.db.mydoc.find.assert_called_with(spec) doc = curs.first() self.assertIsInstance(doc, self.MyDoc) def test_can_insert(self): doc = self.MyDoc(_id=0, x=5) doc.m.insert() self.db.mydoc.insert_one.assert_called_with({"_id": 0, "x": 5}) def test_can_update_with_refresh(self): doc = self.MyDoc(_id=1, x=5) self.db.mydoc.find_one_and_update.return_value = dict(_id=1, x=6) doc.m.update({"$inc": {"x": 1}}, refresh=True) self.db.mydoc.find_one_and_update.assert_called_with( {"_id": 1}, {"$inc": {"x": 1}}, return_document=pymongo.ReturnDocument.AFTER, ) self.assertEqual(doc.x, 6) def test_can_update_without_refresh(self): doc = self.MyDoc(_id=1, x=5) doc.m.update({"$inc": {"x": 1}}) self.db.mydoc.update_one.assert_called_with( {"_id": 1}, {"$inc": {"x": 1}} ) self.assertEqual(doc.x, 5)
def setUp(self): metadata = Metadata() subdoc = S.compile_schema(metadata, {"x": int}) self.MyDoc = collection( metadata, "mydoc", Field("x", subdoc), Field("y", [subdoc]) ) self.doc = self.MyDoc.m.create(x=dict(x=5), y=[])
def setUp(self): metadata = Metadata() subdoc = subdocument(metadata, "subdoc", Field("x", int)) self.MyDoc = collection( metadata, "mydoc", Field("x", subdoc), Field("y", [subdoc]) ) self.doc = self.MyDoc.m.create(x=dict(x=5), y=[])
class TestCollection(TestCase): def setUp(self): self.db = Mock() self.metadata = Metadata() self.MyDoc = collection( self.metadata, 'mydoc', Field('_id', int), Field('x', int), Index('x')) self.metadata.bind(self.db) self.db.mydoc.with_options.return_value = self.db.mydoc def test_can_find(self): self.db.mydoc.find.return_value = iter([ self.MyDoc({'_id': 0, 'x': 5})]) spec = {'a': {'$exists': True}} curs = self.MyDoc.m.find(spec) self.db.mydoc.find.assert_called_with(spec) doc = curs.first() self.assertIsInstance(doc, self.MyDoc) def test_can_insert(self): doc = self.MyDoc(_id=0, x=5) doc.m.insert() self.db.mydoc.insert_one.assert_called_with({'_id': 0, 'x': 5}) def test_can_update_with_refresh(self): doc = self.MyDoc(_id=1, x=5) self.db.mydoc.find_one_and_update.return_value = dict( _id=1, x=6) doc.m.update({'$inc': {'x': 1}}, refresh=True) self.db.mydoc.find_one_and_update.assert_called_with( {'_id': 1}, {'$inc': {'x': 1}}, return_document=pymongo.ReturnDocument.AFTER) self.assertEqual(doc.x, 6) def test_can_update_without_refresh(self): doc = self.MyDoc(_id=1, x=5) doc.m.update({'$inc': {'x': 1}}) self.db.mydoc.update_one.assert_called_with( {'_id': 1}, {'$inc': {'x': 1}}) self.assertEqual(doc.x, 5)
def setUp(self): self.db = Mock() self.metadata = Metadata() self.MyDoc = collection( self.metadata, 'mydoc', Field('_id', int, default=0), Field('x', int, default=0)) self.metadata.bind(self.db) self.db.mydoc.with_options.return_value = self.db.mydoc
class TestCollection(TestCase): def setUp(self): self.db = Mock() self.metadata = Metadata() self.MyDoc = collection( self.metadata, "mydoc", Field("_id", int, default=0), Field("x", int, default=0), ) self.metadata.bind(self.db) self.db.mydoc.with_options.return_value = self.db.mydoc def test_before_insert(self): before_insert_one = mock.Mock() event.listen(self.MyDoc, "before_insert_one", before_insert_one) doc = self.MyDoc.m.create() doc.m.insert() before_insert_one.assert_called() args, kwargs = before_insert_one.call_args self.assertEqual(args[1], doc) def test_before_replace(self): before_replace = mock.Mock() event.listen(self.MyDoc, "before_replace", before_replace) doc = self.MyDoc.m.create() doc.m.replace() before_replace.assert_called() args, kwargs = before_replace.call_args self.assertEqual(args[0].instance, doc) def test_before_delete(self): before_delete = mock.Mock() event.listen(self.MyDoc, "before_delete", before_delete) doc = self.MyDoc.m.create() doc.m.delete() before_delete.assert_called() args, kwargs = before_delete.call_args self.assertEqual(args[0].instance, doc)
class TestCollection(TestCase): def setUp(self): self.db = Mock() self.metadata = Metadata() self.MyDoc = collection( self.metadata, 'mydoc', Field('_id', int, default=0), Field('x', int, default=0)) self.metadata.bind(self.db) self.db.mydoc.with_options.return_value = self.db.mydoc def test_before_insert(self): before_insert_one = mock.Mock() event.listen(self.MyDoc, 'before_insert_one', before_insert_one) doc = self.MyDoc.m.create() doc.m.insert() before_insert_one.assert_called() args, kwargs = before_insert_one.call_args self.assertEqual(args[1], doc) def test_before_replace(self): before_replace = mock.Mock() event.listen(self.MyDoc, 'before_replace', before_replace) doc = self.MyDoc.m.create() doc.m.replace() before_replace.assert_called() args, kwargs = before_replace.call_args self.assertEqual(args[0].instance, doc) def test_before_delete(self): before_delete = mock.Mock() event.listen(self.MyDoc, 'before_delete', before_delete) doc = self.MyDoc.m.create() doc.m.delete() before_delete.assert_called() args, kwargs = before_delete.call_args self.assertEqual(args[0].instance, doc)
class TestDerived(TestCase): def setUp(self): self.db = Mock() self.metadata = Metadata() self.Base = collection( self.metadata, 'mydoc', Field('_id', int), Field('disc', str, default='base'), polymorphic_discriminator='disc') self.Derived = derived( self.Base, 'derived', Field('x', int, default=10)) self.metadata.bind(self.db) self.db.mydoc.with_options.return_value = self.db.mydoc def test_query_base(self): self.assertEqual( self.Base.m.query._compile_query(), {'filter': {}}) def test_query_derived(self): self.assertEqual( self.Derived.m.query._compile_query(), {'filter': {'disc': 'derived'}}) def test_poly_query(self): self.db.mydoc.find.return_value = iter([ self.Derived.m.create(_id=1) ]) res = self.Base.m.query.one() self.assertEqual( type(res), self.Derived)
def setUp(self): self.db = Mock() self.metadata = Metadata() self.Base = collection( self.metadata, 'mydoc', Field('_id', int), Field('disc', str, default='base'), polymorphic_discriminator='disc') self.Derived = derived( self.Base, 'derived', Field('x', int, default=10)) self.metadata.bind(self.db) self.db.mydoc.with_options.return_value = self.db.mydoc
def setUp(self): self.metadata = Metadata() self.MyDoc = collection( self.metadata, 'mydoc', Field('x', None))
def test_can_make_schema(self): metadata = Metadata() subdoc = subdocument(metadata, "subdoc", Field("x", int)) self.MyDoc = collection( metadata, "mydoc", Field("x", subdoc), Field("y", [subdoc]) )
def setUp(self): metadata = Metadata() subdoc = subdocument(metadata, "subdoc", Field("x", int)) self.MyDoc = collection( metadata, "mydoc", Field("y", subdoc, default=None) )