Fixed database tests after schema change.

This commit is contained in:
s3lph 2018-05-29 23:53:16 +02:00
parent cedfcfc3d4
commit 85409a642f
3 changed files with 12 additions and 18 deletions

View file

@ -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:

View file

@ -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:

View file

@ -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