emmental/tests/db/test_db.py

41 lines
1.4 KiB
Python

# Copyright 2022 (c) Anna Schumaker
"""Test our custom db Connection object."""
import pathlib
import emmental.db
import tests.util
import unittest.mock
class TestConnection(tests.util.TestCase):
"""Test case for our database connection manager."""
def test_paths(self):
"""Check that path constants are pointing to the right places."""
script = pathlib.Path(emmental.db.__file__).parent / "emmental.sql"
self.assertEqual(emmental.db.SQL_SCRIPT, script)
def test_connection(self):
"""Check that the connection manager is initialized properly."""
self.assertIsInstance(self.sql, emmental.db.connection.Connection)
def test_version(self):
"""Test checking the database schema version."""
cur = self.sql("PRAGMA user_version")
self.assertEqual(cur.fetchone()["user_version"], 1)
def test_tables(self):
"""Check that the connection has pointers to our tables."""
self.assertIsInstance(self.sql.settings, emmental.db.settings.Table)
def test_load(self):
"""Check that calling load() loads the tables."""
table_loaded = unittest.mock.Mock()
self.sql.connect("table-loaded", table_loaded)
self.sql.load()
self.assertTrue(self.sql.settings.loaded)
calls = [unittest.mock.call(self.sql, tbl)
for tbl in [self.sql.settings]]
table_loaded.assert_has_calls(calls)