aboutsummaryrefslogtreecommitdiff
path: root/ihatemoney/tests/tests.py
diff options
context:
space:
mode:
authorByron Ullauri <Ullauri.Byron@gmail.com>2019-01-03 04:03:13 -0500
committerAlexis Metaireau <alexis@notmyidea.org>2019-01-03 10:03:13 +0100
commit04adfe4155e1bb37609edfe78ae67b361f51e0cf (patch)
tree49237e7eaf0cc5ee3818a8f9984dde4ecc6356e2 /ihatemoney/tests/tests.py
parent7cb339c0bb8aacaeae47838a68c7af60cf414618 (diff)
downloadihatemoney-mirror-04adfe4155e1bb37609edfe78ae67b361f51e0cf.zip
ihatemoney-mirror-04adfe4155e1bb37609edfe78ae67b361f51e0cf.tar.gz
ihatemoney-mirror-04adfe4155e1bb37609edfe78ae67b361f51e0cf.tar.bz2
Provide basic math operations on bills (#413)
* allow basic math ops in amount field for bills form * docs: changing compile-translations to build-translations
Diffstat (limited to 'ihatemoney/tests/tests.py')
-rw-r--r--ihatemoney/tests/tests.py81
1 files changed, 81 insertions, 0 deletions
diff --git a/ihatemoney/tests/tests.py b/ihatemoney/tests/tests.py
index 2f3d4ac..9f9d8fa 100644
--- a/ihatemoney/tests/tests.py
+++ b/ihatemoney/tests/tests.py
@@ -1349,6 +1349,87 @@ class APITestCase(IhatemoneyTestCase):
headers=self.get_auth("raclette"))
self.assertStatus(404, req)
+ def test_bills_with_calculation(self):
+ # create a project
+ self.api_create("raclette")
+
+ # add members
+ self.api_add_member("raclette", "alexis")
+ self.api_add_member("raclette", "fred")
+
+ # valid amounts
+ input_expected = [
+ ("((100 + 200.25) * 2 - 100) / 2", 250.25),
+ ("3/2", 1.5),
+ ("2 + 1 * 5 - 2 / 1", 5),
+ ]
+
+ for i, pair in enumerate(input_expected):
+ input_amount, expected_amount = pair
+ id = i + 1
+
+ req = self.client.post(
+ "/api/projects/raclette/bills",
+ data={
+ 'date': '2011-08-10',
+ 'what': 'fromage',
+ 'payer': "1",
+ 'payed_for': ["1", "2"],
+ 'amount': input_amount,
+ },
+ headers=self.get_auth("raclette")
+ )
+
+ # should return the id
+ self.assertStatus(201, req)
+ self.assertEqual(req.data.decode('utf-8'), "{}\n".format(id))
+
+ # get this bill's details
+ req = self.client.get(
+ "/api/projects/raclette/bills/{}".format(id),
+ headers=self.get_auth("raclette")
+ )
+
+ # compare with the added info
+ self.assertStatus(200, req)
+ expected = {
+ "what": "fromage",
+ "payer_id": 1,
+ "owers": [
+ {"activated": True, "id": 1, "name": "alexis", "weight": 1},
+ {"activated": True, "id": 2, "name": "fred", "weight": 1}],
+ "amount": expected_amount,
+ "date": "2011-08-10",
+ "id": id,
+ }
+
+ got = json.loads(req.data.decode('utf-8'))
+ self.assertEqual(
+ datetime.date.today(),
+ datetime.datetime.strptime(got["creation_date"], '%Y-%m-%d').date()
+ )
+ del got["creation_date"]
+ self.assertDictEqual(expected, got)
+
+ # should raise errors
+ erroneous_amounts = [
+ "lambda ", # letters
+ "(20 + 2", # invalid expression
+ "20/0", # invalid calc
+ "9999**99999999999999999", # exponents
+ "2" * 201, # greater than 200 chars,
+ ]
+
+ for amount in erroneous_amounts:
+ req = self.client.post("/api/projects/raclette/bills", data={
+ 'date': '2011-08-10',
+ 'what': 'fromage',
+ 'payer': "1",
+ 'payed_for': ["1", "2"],
+ 'amount': amount,
+ }, headers=self.get_auth("raclette"))
+ self.assertStatus(400, req)
+
def test_statistics(self):
# create a project
self.api_create("raclette")