From e26ea077d962f44cd7f6e4e008d7c581bf089f7c Mon Sep 17 00:00:00 2001 From: Dave Brondsema Date: Thu, 19 Dec 2024 12:59:56 -0500 Subject: [PATCH] MIM: Connection supports with like pymongo MongoClient does --- ming/mim.py | 5 +++++ ming/tests/test_mim.py | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/ming/mim.py b/ming/mim.py index 4c1e298..fcaec1a 100644 --- a/ming/mim.py +++ b/ming/mim.py @@ -127,6 +127,11 @@ def _is_writable(self): def is_mongos(self): return False + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb) -> None: + self.close() class Database(database.Database): def __init__(self, client, name, **__): diff --git a/ming/tests/test_mim.py b/ming/tests/test_mim.py index c9df195..4618704 100644 --- a/ming/tests/test_mim.py +++ b/ming/tests/test_mim.py @@ -190,6 +190,16 @@ def test_search(self): assert coll.count_documents({'$text': {'$search': 'searched'}}) == 1 +class TestConnection(TestCase): + + def setUp(self): + self.conn = create_datastore('mim:///testdb').conn + + def test_context_manager(self): + with self.conn: + pass + + class TestDottedOperators(TestCase): def setUp(self):