emmental/tests/db/test_connection.py
Anna Schumaker deb4f3d252 db: Create a base Connection manager
This is a wrapper around the sqlite3.Connection objct that adds some
nice functionality to make working with SQL easier.

I defined the following magic methods:
  * __enter__() to manually begin a transaction
  * __exit__() to commit or rollback a manual transaction
  * __call__() to execute a SQL statement with either positional or
    keyword arguments.

Additionally:
  * I define a "CASEFOLD" function that can be used in queries
    to lowercase unicode text when searching.
  * I set foreign_keys = ON so foreign keys checking is always enabled
  * I provide an executemany() function for running running the same
    statement multiple times with different arguments.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
2023-04-12 09:23:14 -04:00

117 lines
4.6 KiB
Python

# Copyright 2022 (c) Anna Schumaker
"""Test our custom db Connection object."""
import sqlite3
import emmental.db.connection
import unittest
from gi.repository import GObject
class TestConnection(unittest.TestCase):
"""Test case for our database connection manager."""
def setUp(self):
"""Set up common variables."""
self.sql = emmental.db.connection.Connection()
def tearDown(self):
"""Clean up."""
self.sql.close()
def test_paths(self):
"""Check that path constants are pointing to the right places."""
self.assertEqual(emmental.db.connection.DATA_FILE,
emmental.gsetup.DATA_DIR / "emmental-debug.sqlite3")
self.assertEqual(emmental.db.connection.DATABASE, ":memory:")
def test_connection(self):
"""Check that the connection manager is initialized properly."""
self.assertIsInstance(self.sql, GObject.GObject)
self.assertIsInstance(self.sql._sql, sqlite3.Connection)
self.assertEqual(self.sql._sql.row_factory, sqlite3.Row)
self.assertTrue(self.sql.connected)
def test_foreign_keys(self):
"""Test that foreign keys are enabled."""
cur = self.sql("PRAGMA foreign_keys")
self.assertEqual(cur.fetchone()["foreign_keys"], 1)
def test_call(self):
"""Check that we can call the connection to execute a sql statement."""
self.sql("CREATE TABLE test (a INT UNIQUE, b INT)")
self.sql("INSERT INTO test VALUES (?, ?)", 1, 2)
cur = self.sql("SELECT * FROM test")
self.assertIsInstance(cur, sqlite3.Cursor)
row = cur.fetchone()
self.assertIsInstance(row, sqlite3.Row)
self.assertEqual(row["a"], 1)
self.assertEqual(row["b"], 2)
def test_call_keyword(self):
"""Test running a sql statement with keyword arguments."""
self.sql("CREATE TABLE test (a INT UNIQUE, b INT)")
self.sql("INSERT INTO test VALUES (:a, :b)", a=1, b=2)
cur = self.sql("SELECT * FROM test")
self.assertIsInstance(cur, sqlite3.Cursor)
row = cur.fetchone()
self.assertIsInstance(row, sqlite3.Row)
self.assertEqual(row["a"], 1)
self.assertEqual(row["b"], 2)
def test_casefold(self):
"""Test the casefold function."""
self.sql("CREATE TABLE test (a INT, text TEXT)")
self.sql("INSERT INTO test VALUES (?, ?)", 1, "TEST")
self.sql("INSERT INTO test VALUES (?, ?)", 2, None)
rows = self.sql("SELECT CASEFOLD(text) as text FROM test").fetchall()
self.assertEqual(rows[0]["text"], "test")
self.assertEqual(rows[1]["text"], None)
def test_executemany(self):
"""Test the executemany function."""
self.sql("CREATE TABLE test (a INT, b TEXT)")
self.sql.executemany("INSERT INTO test VALUES (?, ?)",
(1, "a"), (2, "b"), (3, "c"), (4, "d"), (5, "e"))
rows = self.sql("SELECT * FROM test").fetchall()
self.assertEqual(tuple(rows[0]), (1, "a"))
self.assertEqual(tuple(rows[1]), (2, "b"))
self.assertEqual(tuple(rows[2]), (3, "c"))
self.assertEqual(tuple(rows[3]), (4, "d"))
self.assertEqual(tuple(rows[4]), (5, "e"))
def test_transaction(self):
"""Test that we can manually start a transaction."""
self.assertFalse(self.sql._sql.in_transaction)
with self.sql:
self.assertTrue(self.sql._sql.in_transaction)
self.sql("CREATE TABLE test_table (test TEXT)")
self.sql("INSERT INTO test_table VALUES (?)", "Test")
self.assertTrue(self.sql._sql.in_transaction)
self.assertFalse(self.sql._sql.in_transaction)
cur = self.sql("SELECT COUNT(*) FROM test_table")
self.assertEqual(cur.fetchone()["COUNT(*)"], 1)
def test_transaction_rollback(self):
"""Test that errors roll back the transaction."""
with self.assertRaises(Exception):
with self.sql:
self.sql("CREATE TABLE other_table (test TEXT)")
self.sql("INSERT INTO other_table VALUES (?)", "Test")
raise Exception("Test Exeption")
self.assertFalse(self.sql._sql.in_transaction)
with self.assertRaises(sqlite3.OperationalError):
self.sql("SELECT COUNT(*) FROM other_table")
def test_close(self):
"""Check closing the connection."""
self.sql.close()
self.assertFalse(self.sql.connected)
with self.assertRaises(sqlite3.ProgrammingError):
self.assertIsNone(self.sql("SELECT COUNT(*) FROM test_table"))
self.sql.close()