Skip to content

Commit

Permalink
Merge 8ea40d7 into 4311d0d
Browse files Browse the repository at this point in the history
  • Loading branch information
amol- committed May 28, 2020
2 parents 4311d0d + 8ea40d7 commit d74c71f
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 27 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: python
services: mongodb
python:
- "2.7"
- "3.4"
Expand Down
2 changes: 1 addition & 1 deletion ming/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def __getattr__(self, name):

@property
def conn(self):
return self.connection
return self.bind.conn

@property
def db(self):
Expand Down
10 changes: 10 additions & 0 deletions ming/mim.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ def __init__(self, client, name, **__):
else:
self._jsruntime = None

def __repr__(self):
return "mim.Database(%r, %r)" % (self.__client, self.__name)

def with_options(self, codec_options=None, read_preference=None, write_concern=None, read_concern=None):
# options have no meaning for MIM
return self

@property
def name(self):
return self._name
Expand Down Expand Up @@ -351,6 +358,9 @@ def __init__(self, database, name):
self._unique_indexes = {}
self._indexes = {}

def __repr__(self):
return "mim.Collection(%r, %r)" % (self._database, self.__name)

def clear(self):
self._data = {}
for ui in self._unique_indexes.values():
Expand Down
19 changes: 11 additions & 8 deletions ming/odm/declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import six

class _MappedClassMeta(type):

def __init__(cls, name, bases, dct):
cls._registry['%s.%s' % (cls.__module__, cls.__name__)] = mapper(cls)
if dct.get("_mm_mapped", True):
cls._registry['%s.%s' % (cls.__module__, cls.__name__)] = mapper(cls)
cls._compiled = False

def __new__(meta, name, bases, dct):
Expand All @@ -18,7 +19,7 @@ def __new__(meta, name, bases, dct):
mapper(b).collection for b in mapped_bases ]
# Build up the mongometa class
mm_bases = tuple(
(b.__mongometa__ for b in mapped_bases
(b.__mongometa__ for b in bases
if hasattr(b, '__mongometa__')))
if not mm_bases:
mm_bases = (object,)
Expand All @@ -45,11 +46,12 @@ def __new__(meta, name, bases, dct):
else:
clsdict[k] = v
cls = type.__new__(meta, name, bases, clsdict)
mapper(cls, collection_class, mm.session,
properties=properties,
include_properties=include_properties,
exclude_properties=exclude_properties,
extensions=extensions)
if dct.get("_mm_mapped", True):
mapper(cls, collection_class, mm.session,
properties=properties,
include_properties=include_properties,
exclude_properties=exclude_properties,
extensions=extensions)
return cls

@classmethod
Expand Down Expand Up @@ -118,6 +120,7 @@ class __mongometa__:
text = FieldProperty(schema.String(if_missing=''))
"""
_mm_mapped = False # All other subclasses will be mapped, this one wont
_registry = {}

class __mongometa__:
Expand Down
5 changes: 4 additions & 1 deletion ming/odm/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ def clear_all(cls):
for m in cls.all_mappers():
m._compiled = False
cls._all_mappers = []

cls._mapper_by_classname.clear()
cls._mapper_by_class.clear()
cls._mapper_by_collection.clear()

@classmethod
def ensure_all_indexes(cls):
"""Ensures indexes for each registered :class:`.MappedClass` subclass are created"""
Expand Down
34 changes: 30 additions & 4 deletions ming/tests/odm/test_declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ class __mongometa__:
assert len(mgr.indexes) == 1, mgr.indexes

class TestRelation(TestCase):
DATASTORE = 'mim:///test_db'

def setUp(self):
self.datastore = create_datastore('mim:///test_db')
Mapper.clear_all()
self.datastore = create_datastore(self.DATASTORE)
self.session = ODMSession(bind=self.datastore)
class Parent(MappedClass):
class __mongometa__:
Expand All @@ -49,7 +51,10 @@ class __mongometa__:

def tearDown(self):
self.session.clear()
self.datastore.conn.drop_all()
try:
self.datastore.conn.drop_all()
except TypeError:
self.datastore.conn.drop_database(self.datastore.db)

def test_parent(self):
parent = self.Parent(_id=1)
Expand Down Expand Up @@ -136,9 +141,14 @@ def test_nullable_foreignid(self):
self.assertEqual(len(parent.children), 4)


class TestRealMongoRelation(TestRelation):
DATASTORE = "ming_tests"


class TestManyToManyListRelation(TestCase):

def setUp(self):
Mapper.clear_all()
self.datastore = create_datastore('mim:///test_db')
self.session = ODMSession(bind=self.datastore)
class Parent(MappedClass):
Expand Down Expand Up @@ -250,6 +260,7 @@ def test_writable_backref(self):
class TestManyToManyListReverseRelation(TestCase):

def setUp(self):
Mapper.clear_all()
self.datastore = create_datastore('mim:///test_db')
self.session = ODMSession(bind=self.datastore)
class Parent(MappedClass):
Expand Down Expand Up @@ -297,6 +308,7 @@ def test_parent(self):
class TestManyToManyListCyclic(TestCase):

def setUp(self):
Mapper.clear_all()
self.datastore = create_datastore('mim:///test_db')
self.session = ODMSession(bind=self.datastore)

Expand Down Expand Up @@ -340,6 +352,7 @@ def test_cyclic(self):
class TestRelationWithNone(TestCase):

def setUp(self):
Mapper.clear_all()
self.datastore = create_datastore('mim:///test_db')
self.session = ODMSession(bind=self.datastore)
class GrandParent(MappedClass):
Expand Down Expand Up @@ -407,6 +420,8 @@ def test_none_not_allowed(self):

class ObjectIdRelationship(TestCase):
def setUp(self):
Mapper.clear_all()

self.datastore = create_datastore('mim:///test_db')
self.session = ODMSession(bind=self.datastore)
class Parent(MappedClass):
Expand All @@ -430,6 +445,7 @@ class __mongometa__:
Parent,
if_missing=lambda:bson.ObjectId('deadbeefdeadbeefdeadbeef'))
field_with_default = RelationProperty('Parent', 'field_with_default_id')

Mapper.compile_all()
self.Parent = Parent
self.Child = Child
Expand Down Expand Up @@ -500,9 +516,10 @@ def test_mapper_extension(self):
self.session.flush()

class TestBasicMapping(TestCase):
DATASTORE = 'mim:///test_db'

def setUp(self):
self.datastore = create_datastore('mim:///test_db')
self.datastore = create_datastore(self.DATASTORE)
self.session = ODMSession(bind=self.datastore)
class Basic(MappedClass):
class __mongometa__:
Expand All @@ -521,7 +538,10 @@ class __mongometa__:

def tearDown(self):
self.session.clear()
self.datastore.conn.drop_all()
try:
self.datastore.conn.drop_all()
except TypeError:
self.datastore.conn.drop_database(self.datastore.db)

def test_repr(self):
doc = self.Basic(a=1, b=[2,3], c=dict(d=4, e=5))
Expand Down Expand Up @@ -619,9 +639,14 @@ def test_imap(self):
self.session.expunge(doc)


class TestRealBasicMapping(TestBasicMapping):
DATASTORE = "test_ming"


class TestPolymorphic(TestCase):

def setUp(self):
Mapper.clear_all()
self.datastore = create_datastore('mim:///test_db')
self.doc_session = Session(self.datastore)
self.odm_session = ODMSession(self.doc_session)
Expand Down Expand Up @@ -671,6 +696,7 @@ class Base(MappedClass):
class TestHooks(TestCase):

def setUp(self):
Mapper.clear_all()
self.datastore = create_datastore('mim:///test_db')
self.session = ODMSession(bind=self.datastore)
self.hooks_called = defaultdict(list)
Expand Down
6 changes: 6 additions & 0 deletions ming/tests/test_gridfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def test_simple(self):
assert not self.TestFS.m.exists(filename='test.txt')
fobj = self.TestFS.m.get()
assert fobj is None

def test_seek(self):
with self.TestFS.m.new_file('test.txt') as fp:
fp.write('The quick brown fox')
fobj = self.TestFS.m.fs.get(fp._id)
fobj.seek(0)

def test_strange_mimetype(self):
with self.TestFS.m.new_file('test.ming') as fp:
Expand Down
1 change: 1 addition & 0 deletions ming/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class TestRelation(TestCase):

def setUp(self):
Mapper.clear_all()
self.datastore = create_datastore('mim:///test_db')
self.session = ThreadLocalODMSession(Session(bind=self.datastore))
class Parent(MappedClass):
Expand Down
3 changes: 1 addition & 2 deletions ming/tests/test_mim.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from ming import create_datastore, mim
from pymongo import UpdateOne
from pymongo.errors import OperationFailure, DuplicateKeyError
from nose import SkipTest
from mock import patch


Expand Down Expand Up @@ -326,7 +325,7 @@ class TestMRCommands(TestCommands):
def setUp(self):
super(TestMRCommands, self).setUp()
if not self.bind.db._jsruntime:
raise SkipTest
self.skipTest("Javascript Runtime Unavailable")

def test_mr_inline(self):
result = self.bind.db.command(
Expand Down
9 changes: 4 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@
include_package_data=True,
zip_safe=True,
install_requires=[
"pymongo>=3.0,<3.8",
"pymongo>=3.0,<3.12",
"pytz",
"six>=1.6.1"
],
tests_require=[
"nose",
"mock >=0.8.0",
"pytz",
"WebOb",
"webtest",
"FormEncode >= 1.2.1",
# "python-spidermonkey >= 0.0.10", # required for full MIM functionality
],
test_suite="ming.tests",
extras_require={
"configure": [
"FormEncode >= 1.2.1", # required to use ``ming.configure``
Expand All @@ -57,6 +57,5 @@
# -*- Entry points: -*-
[paste.filter_factory]
ming_autoflush=ming.odm.middleware:make_ming_autoflush_middleware
""",
test_suite='nose.collector'
)
"""
)
10 changes: 4 additions & 6 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
skip_missing_interpreters = True

[testenv]
# https://bitbucket.org/hpk42/tox/issue/13/tox-should-reuse-tests_require
deps = nose
deps =
mock
pytz
WebOb
webtest
formencode
# python-spidermonkey
coverage
commands =
pip install coverage
pip install -e .
nosetests --with-coverage --cover-package=ming --cover-erase
coverage run --source ming -m unittest discover -v
- coverage report

sitepackages = False

0 comments on commit d74c71f

Please sign in to comment.