def test_fetch_record(create_movies_sql): fields = dict(title='Frozen', revenue=1_290_000_000) with connect() as con: con.execute(create_movies_sql) pk = insert_record('movies', fields) row = fetch_record('movies', pk) assert tuple(row) == (1, 'Frozen', 1_290_000_000.0)
def test_insert_record(create_movies_sql): fields = dict(title='Frozen', revenue=1_290_000_000) with connect() as con: con.execute(create_movies_sql) for _ in range(3): result = insert_record('movies', fields) assert result == 3
def _save(self) -> int: table = self.__class__._ensure_table() if self._pk is None: self._pk = db.insert_record(table, self._asdict()) else: db.update_record(table, self._pk, self._asdict()) return self._pk
def test_delete_record(create_movies_sql): fields = dict(title='Frozen', revenue=1_290_000_000) with connect() as con: con.execute(create_movies_sql) pk = insert_record('movies', fields) delete_record('movies', pk) with pytest.raises(NoSuchRecord) as e: fetch_record('movies', pk) assert e.value.pk == pk
def test_update_record(create_movies_sql): fields = dict(title='Frozen', revenue=1_290_000_000) with connect() as con: con.execute(create_movies_sql) pk = insert_record('movies', fields) fields['revenue'] = 1_299_999_999 sql, values = update_record('movies', pk, fields) row = fetch_record('movies', pk) assert sql == 'UPDATE movies SET (title, revenue) = (?, ?) WHERE pk = ?' assert values == ('Frozen', 1_299_999_999, 1) assert tuple(row) == (1, 'Frozen', 1_299_999_999.0)