diff --git a/matemat/db/test/test_wrapper.py b/matemat/db/test/test_wrapper.py index dafff65..e5a52c2 100644 --- a/matemat/db/test/test_wrapper.py +++ b/matemat/db/test/test_wrapper.py @@ -75,3 +75,29 @@ class DatabaseTest(unittest.TestCase): c = db._sqlite_db.cursor() c.execute("SELECT * FROM users") self.assertIsNone(c.fetchone()) + + def test_connect_twice(self): + """ + If a connection is already established, a RuntimeError should be raised when attempting to connect a + second time. + """ + self.db.connect() + with self.assertRaises(RuntimeError): + self.db.connect() + self.db.close() + + def test_close_not_opened(self): + """ + Attempting to close an unopened connection should raise a RuntimeError. + """ + with self.assertRaises(RuntimeError): + self.db.close() + + def test_close_in_transaction(self): + """ + Attempting to close a connection inside a transaction should raise a RuntimeError. + """ + with self.db as db: + with db.transaction(): + with self.assertRaises(RuntimeError): + self.db.close()