diff --git a/.gitignore b/.gitignore index 5cab996..664c482 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ **/__pycache__/ *.pyc +**/*.egg-info/ +*.coverage *.sqlite3 *.db \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..d783cc3 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,11 @@ +--- +image: debian:buster + +test: + script: + - apt-get update -qy + - apt-get install -y --no-install-recommends python3-dev python3-pip python3-coverage python3-setuptools build-essential + - pip3 install wheel + - pip3 install -r requirements.txt + - python3-coverage run --branch -m unittest discover matemat + - python3-coverage report -m diff --git a/matemat/db/database.py b/matemat/db/database.py index 1b49c76..c297721 100644 --- a/matemat/db/database.py +++ b/matemat/db/database.py @@ -1,3 +1,4 @@ + from typing import List, Optional import apsw @@ -276,6 +277,8 @@ class Database(object): return Product(product_id, name, price_member, price_non_member) def change_product(self, product: Product): + if product.id == -1: + raise ValueError('Invalid product ID') with self.transaction() as c: c.execute(''' UPDATE products @@ -291,6 +294,8 @@ class Database(object): }) def delete_product(self, product: Product): + if product.id == -1: + raise ValueError('Invalid product ID') with self.transaction() as c: c.execute(''' DELETE FROM products @@ -298,6 +303,8 @@ class Database(object): ''', [product.id]) def increment_consumption(self, user: User, product: Product, count: int = 1): + if product.id == -1: + raise ValueError('Invalid product ID') with self.transaction() as c: c.execute(''' SELECT count @@ -342,6 +349,8 @@ class Database(object): }) def restock(self, product: Product, count: int): + if product.id == -1: + raise ValueError('Invalid product ID') with self.transaction() as c: c.execute(''' UPDATE products 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 72aaa1a..f3d1674 100644 --- a/matemat/primitives/Product.py +++ b/matemat/primitives/Product.py @@ -6,8 +6,6 @@ class Product(object): name: str, price_member: int, price_non_member: int): - if product_id == -1: - raise ValueError('Invalid product ID') self._product_id: int = product_id self._name: str = name self._price_member: int = price_member diff --git a/setup.py b/setup.py index 034a818..0fc605f 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup( name='matemat', - version='2.00', + version='2.0', packages=['matemat'], url='', license='',