diff --git a/ming/tests/test_declarative.py b/ming/tests/test_declarative.py index 7557cf0..7be426d 100644 --- a/ming/tests/test_declarative.py +++ b/ming/tests/test_declarative.py @@ -1,3 +1,4 @@ +import os from unittest import TestCase from collections import defaultdict @@ -6,9 +7,11 @@ from pymongo.errors import AutoReconnect from ming.base import Cursor +from ming.datastore import create_datastore from ming.declarative import Document from ming.metadata import Field, Index from ming import schema as S +from ming.odm.odmsession import ODMSession, ThreadLocalODMSession from ming.session import Session from ming.exc import MingException @@ -128,6 +131,45 @@ def test_migrate(self): self.MockSession.find.assert_called_with(self.TestDoc) self.MockSession.save.assert_called_with(doc) +class TestDocumentReal(TestCase): + DATASTORE = f"mongodb://localhost/test_ming_TestDocumentReal_{os.getpid()}?serverSelectionTimeoutMS=100" + + def setUp(self): + self.datastore = create_datastore(self.DATASTORE) + self.session = Session(bind=self.datastore) + + class TestDoc(Document): + class __mongometa__: + name='test_doc' + session = self.session + indexes = [ ('a',) ] + _id = Field(S.Anything) + a=Field(S.Int, if_missing=None) + b=Field(S.Object(dict(a=S.Int(if_missing=None)))) + class TestDocNoSchema(Document): + class __mongometa__: + name='test_doc' + session = self.session + self.TestDoc = TestDoc + self.TestDocNoSchema = TestDocNoSchema + def tearDown(self): + self.TestDoc.m.remove() + self.TestDocNoSchema.m.remove() + + def test_field(self): + doc = self.TestDoc(dict(_id=1, a=1, b=dict(a=5))) + doc.m.save() + + self.assertEqual(doc.a, 1) + self.assertEqual(doc.b, dict(a=5)) + doc.a = 5 + self.assertEqual(doc, dict(_id=1, a=5, b=dict(a=5))) + del doc.a + self.assertEqual(doc, dict(_id=1, b=dict(a=5))) + self.assertRaises(AttributeError, getattr, doc, 'c') + self.assertRaises(AttributeError, getattr, doc, 'a') + self.assertEqual(self.session.count(self.TestDoc), 1) + class TestIndexes(TestCase): def setUp(self):