62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
# Copyright 2023 (c) Anna Schumaker.
|
|
"""Mock Row and Table objects for testing."""
|
|
import emmental.db.table
|
|
import sqlite3
|
|
from gi.repository import GObject
|
|
|
|
|
|
class MockRow(emmental.db.table.Row):
|
|
"""A fake Row customized for testing."""
|
|
|
|
number = GObject.Property(type=int)
|
|
|
|
@property
|
|
def primary_key(self) -> int:
|
|
"""Get the primary key for this MockRow."""
|
|
return self.number
|
|
|
|
|
|
class MockTable(emmental.db.table.Table):
|
|
"""A fake Table customized for testing."""
|
|
|
|
def __init__(self, sql: GObject.TYPE_PYOBJECT):
|
|
"""Initialize the Mock Table."""
|
|
super().__init__(sql)
|
|
self.sql("CREATE TABLE mock_table (number INTEGER PRIMARY KEY)")
|
|
|
|
def do_construct(self, *args, **kwargs) -> MockRow:
|
|
"""Construct a MockRow."""
|
|
return MockRow(*args, **kwargs)
|
|
|
|
def do_get_sort_key(self, row: MockRow) -> any:
|
|
"""Get the sort key for a MockRow."""
|
|
return (row.number, row.number)
|
|
|
|
def do_sql_delete(self, row: MockRow) -> sqlite3.Cursor:
|
|
"""Delete a MockRow from the Table."""
|
|
return self.sql("DELETE FROM mock_table WHERE number=?", row.number)
|
|
|
|
def do_sql_glob(self, glob: str) -> sqlite3.Cursor:
|
|
"""Select matching rows from the Table."""
|
|
return self.sql("SELECT number FROM mock_table WHERE number GLOB ?",
|
|
glob)
|
|
|
|
def do_sql_insert(self, number: int) -> sqlite3.Cursor:
|
|
"""Insert a MockRow into the Table."""
|
|
return self.sql("""INSERT INTO mock_table (number)
|
|
VALUES (?) RETURNING *""", number)
|
|
|
|
def do_sql_select_all(self) -> sqlite3.Cursor:
|
|
"""Return all rows in the Table."""
|
|
return self.sql("SELECT * FROM mock_table ORDER BY number")
|
|
|
|
def do_sql_select_one(self, number: int) -> sqlite3.Cursor:
|
|
"""Look up a single MockRow in the Table."""
|
|
return self.sql("SELECT number FROM mock_table WHERE number=?", number)
|
|
|
|
def do_sql_update(self, row: MockRow, column: str,
|
|
newval: int) -> sqlite3.Cursor:
|
|
"""Update a MockRow in the Table."""
|
|
return self.sql(f"UPDATE mock_table SET {column}=? WHERE number=?",
|
|
newval, row.number)
|