From 85409a642f44c9ef065b3774de902f9580894d2d Mon Sep 17 00:00:00 2001 From: s3lph Date: Tue, 29 May 2018 23:53:16 +0200 Subject: [PATCH] Fixed database tests after schema change. --- matemat/db/database.py | 5 +++-- matemat/db/test/test_database.py | 8 +++++--- matemat/primitives/Product.py | 17 ++++------------- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/matemat/db/database.py b/matemat/db/database.py index 5c41300..c297721 100644 --- a/matemat/db/database.py +++ b/matemat/db/database.py @@ -1,3 +1,4 @@ + from typing import List, Optional import apsw @@ -254,7 +255,7 @@ class Database(object): FROM products '''): product_id, name, price_member, price_external = row - products.append(Product.from_database(product_id, name, price_member, price_external)) + products.append(Product(product_id, name, price_member, price_external)) return products def create_product(self, name: str, price_member: int, price_non_member: int) -> Product: @@ -273,7 +274,7 @@ class Database(object): }) c.execute('SELECT last_insert_rowid()') product_id = int(c.fetchone()[0]) - return Product.from_database(product_id, name, price_member, price_non_member) + return Product(product_id, name, price_member, price_non_member) def change_product(self, product: Product): if product.id == -1: diff --git a/matemat/db/test/test_database.py b/matemat/db/test/test_database.py index 76f57c7..a03fbd4 100644 --- a/matemat/db/test/test_database.py +++ b/matemat/db/test/test_database.py @@ -49,11 +49,13 @@ class DatabaseTest(unittest.TestCase): """ with self.db as db: with db.transaction() as c: - c.execute("INSERT INTO users VALUES (1, 'testuser', NULL, 'supersecurepassword', NULL, 1, 1, 0, NULL)") + c.execute(''' + INSERT INTO users VALUES (1, 'testuser', NULL, 'supersecurepassword', NULL, 1, 1, 0, 42) + ''') c = db._sqlite_db.cursor() c.execute("SELECT * FROM users") user = c.fetchone() - self.assertEqual((1, 'testuser', None, 'supersecurepassword', None, 1, 1, 0, None), user) + self.assertEqual((1, 'testuser', None, 'supersecurepassword', None, 1, 1, 0, 42), user) def test_transaction_rollback(self): """ @@ -63,7 +65,7 @@ class DatabaseTest(unittest.TestCase): try: with db.transaction() as c: c.execute(""" - INSERT INTO users VALUES (1, 'testuser', NULL, 'supersecurepassword', NULL, 1, 1, 0, NULL) + INSERT INTO users VALUES (1, 'testuser', NULL, 'supersecurepassword', NULL, 1, 1, 0, 42) """) raise ValueError('This should trigger a rollback') except ValueError as e: diff --git a/matemat/primitives/Product.py b/matemat/primitives/Product.py index a761064..f3d1674 100644 --- a/matemat/primitives/Product.py +++ b/matemat/primitives/Product.py @@ -2,24 +2,15 @@ class Product(object): def __init__(self, - product_id: int = -1, - name: str = '', - price_member: int = 0, - price_non_member: int = 0): + product_id: int, + name: str, + price_member: int, + price_non_member: int): self._product_id: int = product_id self._name: str = name self._price_member: int = price_member self._price_non_member: int = price_non_member - @classmethod - def from_database(cls, - product_id: int, - name: str, - price_member: int, - price_non_member: int) -> 'Product': - product = cls(product_id, name, price_member, price_non_member) - return product - @property def id(self) -> int: return self._product_id