aboutsummaryrefslogtreecommitdiff
path: root/ihatemoney/tests
diff options
context:
space:
mode:
authorDavidRThrashJr <60800614+DavidRThrashJr@users.noreply.github.com>2020-02-17 12:39:51 -0500
committerGitHub <noreply@github.com>2020-02-17 18:39:51 +0100
commit32d76178c0ebe05bddb596f85b283f3f0ee74c85 (patch)
treec587ac56026bf2a1eefa44c6ee225bbe88668c90 /ihatemoney/tests
parente4f18f0600282b575328b44575aac2bec62bbc17 (diff)
downloadihatemoney-mirror-32d76178c0ebe05bddb596f85b283f3f0ee74c85.zip
ihatemoney-mirror-32d76178c0ebe05bddb596f85b283f3f0ee74c85.tar.gz
ihatemoney-mirror-32d76178c0ebe05bddb596f85b283f3f0ee74c85.tar.bz2
Use SQL statement for summing up weights
* Update models: Bill.pay_each() * Import sql func * reformatted using black * Added ModelsTestCase.test_bill_pay_each() in order to test the SQL query change within pay_each. Had to add Project.ProjectQuery.get_by_name() for the test.
Diffstat (limited to 'ihatemoney/tests')
-rw-r--r--ihatemoney/tests/tests.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/ihatemoney/tests/tests.py b/ihatemoney/tests/tests.py
index 0c99bca..a12613c 100644
--- a/ihatemoney/tests/tests.py
+++ b/ihatemoney/tests/tests.py
@@ -19,6 +19,7 @@ from ihatemoney.run import create_app, db, load_configuration
from ihatemoney.manage import GenerateConfig, GeneratePasswordHash, DeleteProject
from ihatemoney import models
from ihatemoney import utils
+from sqlalchemy import orm
# Unset configuration file env var if previously set
os.environ.pop("IHATEMONEY_SETTINGS_FILE_PATH", None)
@@ -2140,5 +2141,68 @@ class CommandTestCase(BaseTestCase):
self.assertEqual(len(models.Project.query.all()), 0)
+class ModelsTestCase(IhatemoneyTestCase):
+ def test_bill_pay_each(self):
+
+ self.post_project("raclette")
+
+ # add members
+ self.client.post("/raclette/members/add", data={"name": "alexis", "weight": 2})
+ self.client.post("/raclette/members/add", data={"name": "fred"})
+ self.client.post("/raclette/members/add", data={"name": "tata"})
+ # Add a member with a balance=0 :
+ self.client.post("/raclette/members/add", data={"name": "toto"})
+
+ # create bills
+ self.client.post(
+ "/raclette/add",
+ data={
+ "date": "2011-08-10",
+ "what": "fromage à raclette",
+ "payer": 1,
+ "payed_for": [1, 2, 3],
+ "amount": "10.0",
+ },
+ )
+
+ self.client.post(
+ "/raclette/add",
+ data={
+ "date": "2011-08-10",
+ "what": "red wine",
+ "payer": 2,
+ "payed_for": [1],
+ "amount": "20",
+ },
+ )
+
+ self.client.post(
+ "/raclette/add",
+ data={
+ "date": "2011-08-10",
+ "what": "delicatessen",
+ "payer": 1,
+ "payed_for": [1, 2],
+ "amount": "10",
+ },
+ )
+
+ project = models.Project.query.get_by_name(name="raclette")
+ alexis = models.Person.query.get_by_name(name="alexis", project=project)
+ alexis_bills = models.Bill.query.options(
+ orm.subqueryload(models.Bill.owers)
+ ).filter(models.Bill.owers.contains(alexis))
+ for bill in alexis_bills.all():
+ if bill.what == "red wine":
+ pay_each_expected = 20 / 2
+ self.assertEqual(bill.pay_each(), pay_each_expected)
+ if bill.what == "fromage à raclette":
+ pay_each_expected = 10 / 4
+ self.assertEqual(bill.pay_each(), pay_each_expected)
+ if bill.what == "delicatessen":
+ pay_each_expected = 10 / 3
+ self.assertEqual(bill.pay_each(), pay_each_expected)
+
+
if __name__ == "__main__":
unittest.main()