From f1159d808a82e4f27776fadd01d8317440653d39 Mon Sep 17 00:00:00 2001 From: b4-sharp <Bradley2.Sharp@live.uwe.ac.uk> Date: Mon, 27 Mar 2023 23:51:49 +0100 Subject: [PATCH] Add calculate_price() to ItemSet model --- README.md | 2 +- store/models.py | 7 +++++-- unit_tests.py | 9 +++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8a2861b..d4e6155 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This terminal must be left open for the server to run and for it to be accessibl Ensure all dependencies are installed by running the command ``pip install -r requirements.txt`` in the root project directory. It is recommended to do this in a virtual environment, https://docs.python.org/3/library/venv.html in a subfolder named venv so that it can be correctly git ignored. For Windows: 1. Create a virtual environment, ``python -m venv venv``. -2. Activate the virtual environment, ``.\venv\Scripts\activate ``. +2. Activate the virtual environment, ``.\venv\Scripts\activate``. 3. Install the depedendencies, ``pip install -r requirements.txt``. # Project Organisattion diff --git a/store/models.py b/store/models.py index b5dec8b..3955191 100644 --- a/store/models.py +++ b/store/models.py @@ -60,7 +60,7 @@ class ItemSet(db.Model): description = db.Column(db.String(256), nullable=False) price = db.Column( db.Integer, nullable=False - ) # In pounds, as we're not dividing or multiplying this will not matter in calculations. Do we neccesarily need this? It could be calculated dynamically from the items held. + ) # TODO: Rename price to quantity items = db.relationship( "Item", secondary=itemSets, @@ -68,8 +68,11 @@ class ItemSet(db.Model): backref=db.backref("ItemSets", lazy=True), ) + def calculate_price(self): + return sum(int(item.price) for item in self.items) + def __repr__(self): - return f"id: {self.id}, description: {self.description}, items: {self.items}" + return f"id: {self.id}, description: {self.description}, items: {self.items}, quantity: {self.price}, price: { self.calculate_price() }" # TODO: Rename price to quantity class User(db.Model, UserMixin): diff --git a/unit_tests.py b/unit_tests.py index 8345b4a..f710e56 100644 --- a/unit_tests.py +++ b/unit_tests.py @@ -1,9 +1,10 @@ from store import app +""" An instance of the database needs to exist to unit test, +as the database is seeded from file this can be used as a test database. +Really there should be an inmemory database for testing purposes, but +further research into how flask works with such a thing needs to be done. +""" -# An instance of the database needs to exist to unit test, -# as the database is seeded from file this can be used as a test database. -# It would probably be best to create a variant of it here, but for now -# just use the main database to test against. from store.utility import * import unittest -- GitLab