From 6fb55ddc2d84ca36608cf37d4ea6283cd4cce4ed Mon Sep 17 00:00:00 2001 From: s3lph Date: Tue, 29 May 2018 21:49:40 +0200 Subject: [PATCH 01/11] GitLab CI --- .gitlab-ci.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..fc9d150 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,10 @@ +--- +image: debian/stretch + +test: + script: + - apt-get update -qy + - apt-get install -y --no-install-recommends python3-dev python3-pip python3-coverage python3-setuptools + - pip3 install -r requirements.txt + - python3-coverage run --branch setup.py test + - python3-coverage report -m From 15bd9e97477a8aea351ff80fea56c461c7d810bf Mon Sep 17 00:00:00 2001 From: s3lph Date: Tue, 29 May 2018 21:52:20 +0200 Subject: [PATCH 02/11] Fixed Docker image name in GitLab CI config. --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index fc9d150..e5fd1db 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,5 +1,5 @@ --- -image: debian/stretch +image: debian:stretch test: script: From 0f1a150653a6a93ba47c5c94015ab5ce74ad82df Mon Sep 17 00:00:00 2001 From: s3lph Date: Tue, 29 May 2018 22:51:08 +0200 Subject: [PATCH 03/11] GitLab CI: Added missing pip dependency. --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e5fd1db..e93d871 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -5,6 +5,7 @@ test: script: - apt-get update -qy - apt-get install -y --no-install-recommends python3-dev python3-pip python3-coverage python3-setuptools + - pip3 install wheel - pip3 install -r requirements.txt - python3-coverage run --branch setup.py test - python3-coverage report -m From 516f7f08d9cd631c850c93e9d8ea4b89a901fd4b Mon Sep 17 00:00:00 2001 From: s3lph Date: Tue, 29 May 2018 22:53:46 +0200 Subject: [PATCH 04/11] GitLab CI: Added missing apt dependency. --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e93d871..b14cabc 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,7 +4,7 @@ image: debian:stretch test: script: - apt-get update -qy - - apt-get install -y --no-install-recommends python3-dev python3-pip python3-coverage python3-setuptools + - 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 setup.py test From fab2c701d538d6182be4c63e659cba06196d4dde Mon Sep 17 00:00:00 2001 From: s3lph Date: Tue, 29 May 2018 23:01:05 +0200 Subject: [PATCH 05/11] GitLab CI: Use buster instead of stretch. --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b14cabc..47a8ebb 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,5 +1,5 @@ --- -image: debian:stretch +image: debian:buster test: script: From 6127872bd648bc04b30ba8ab90bcda64dbb8c24b Mon Sep 17 00:00:00 2001 From: s3lph Date: Tue, 29 May 2018 23:12:10 +0200 Subject: [PATCH 06/11] GitLab CI: Use buster instead of stretch. --- matemat/primitives/Product.py | 11 +++++++++++ setup.py | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/matemat/primitives/Product.py b/matemat/primitives/Product.py index 72aaa1a..8974366 100644 --- a/matemat/primitives/Product.py +++ b/matemat/primitives/Product.py @@ -1,6 +1,17 @@ +from inspect import stack + + class Product(object): + def __init__(self): + print(stack()) + self._product_id: int = 0 + self._name: str = '' + self._price_member: int = 0 + self._price_non_member: int = 0 + raise NotImplementedError('This shoudt not be called!') + def __init__(self, product_id: int, name: str, 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='', From 1439c3072c5ec2cdf718bc8e9fbee4caf3297f09 Mon Sep 17 00:00:00 2001 From: s3lph Date: Tue, 29 May 2018 23:16:28 +0200 Subject: [PATCH 07/11] GitLab CI: Use buster instead of stretch. --- matemat/primitives/Product.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/matemat/primitives/Product.py b/matemat/primitives/Product.py index 8974366..9a97050 100644 --- a/matemat/primitives/Product.py +++ b/matemat/primitives/Product.py @@ -12,17 +12,17 @@ class Product(object): self._price_non_member: int = 0 raise NotImplementedError('This shoudt not be called!') - def __init__(self, - product_id: int, - 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 - self._price_non_member: int = price_non_member + # def __init__(self, + # product_id: int, + # 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 + # self._price_non_member: int = price_non_member @property def id(self) -> int: From 3794a570c8cd9699f43ebf2c452352b6a54e66b6 Mon Sep 17 00:00:00 2001 From: s3lph Date: Tue, 29 May 2018 23:36:52 +0200 Subject: [PATCH 08/11] Reworked Product to allow a 0 argument constructor, because unittest apparently breaks stuff... --- matemat/db/database.py | 12 +++++++++-- matemat/primitives/Product.py | 38 ++++++++++++++++------------------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/matemat/db/database.py b/matemat/db/database.py index 1b49c76..5c41300 100644 --- a/matemat/db/database.py +++ b/matemat/db/database.py @@ -254,7 +254,7 @@ class Database(object): FROM products '''): product_id, name, price_member, price_external = row - products.append(Product(product_id, name, price_member, price_external)) + products.append(Product.from_database(product_id, name, price_member, price_external)) return products def create_product(self, name: str, price_member: int, price_non_member: int) -> Product: @@ -273,9 +273,11 @@ class Database(object): }) c.execute('SELECT last_insert_rowid()') product_id = int(c.fetchone()[0]) - return Product(product_id, name, price_member, price_non_member) + return Product.from_database(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 +293,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 +302,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 +348,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/primitives/Product.py b/matemat/primitives/Product.py index 9a97050..a761064 100644 --- a/matemat/primitives/Product.py +++ b/matemat/primitives/Product.py @@ -1,28 +1,24 @@ -from inspect import stack - - class Product(object): - def __init__(self): - print(stack()) - self._product_id: int = 0 - self._name: str = '' - self._price_member: int = 0 - self._price_non_member: int = 0 - raise NotImplementedError('This shoudt not be called!') + def __init__(self, + product_id: int = -1, + name: str = '', + price_member: int = 0, + price_non_member: int = 0): + self._product_id: int = product_id + self._name: str = name + self._price_member: int = price_member + self._price_non_member: int = price_non_member - # def __init__(self, - # product_id: int, - # 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 - # 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: From cedfcfc3d48118a664435b2465be0a65d3b529af Mon Sep 17 00:00:00 2001 From: s3lph Date: Tue, 29 May 2018 23:53:02 +0200 Subject: [PATCH 09/11] Changed GitLab CI from running setup.py test to -m unittest discover --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 47a8ebb..d783cc3 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,5 +7,5 @@ test: - 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 setup.py test + - python3-coverage run --branch -m unittest discover matemat - python3-coverage report -m From 85409a642f44c9ef065b3774de902f9580894d2d Mon Sep 17 00:00:00 2001 From: s3lph Date: Tue, 29 May 2018 23:53:16 +0200 Subject: [PATCH 10/11] 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 From 5c6f130163a0149d09f30a308dc4cc73904b38b4 Mon Sep 17 00:00:00 2001 From: s3lph Date: Tue, 29 May 2018 23:59:02 +0200 Subject: [PATCH 11/11] Added coverage report and egg-info to gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) 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