Skip to content

Commit

Permalink
added one, basic real mongo test for Document / Foundation Layer
Browse files Browse the repository at this point in the history
  • Loading branch information
dill0wn committed Jul 9, 2024
1 parent de453c9 commit 1666e11
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions ming/tests/test_declarative.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from unittest import TestCase
from collections import defaultdict

Expand All @@ -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

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 1666e11

Please sign in to comment.