mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-05-05 20:51:49 +02:00
adding __future__ division import; addressing pr comments
This commit is contained in:
parent
a5819e9b48
commit
b64b8118e9
3 changed files with 59 additions and 36 deletions
|
@ -53,11 +53,16 @@ class CalculatorStringField(StringField):
|
||||||
|
|
||||||
def process_formdata(self, valuelist):
|
def process_formdata(self, valuelist):
|
||||||
if valuelist:
|
if valuelist:
|
||||||
error_msg = "Not a valid amount or expression"
|
message = _(
|
||||||
|
"Not a valid amount or expression."
|
||||||
|
"Only numbers and + - * / operators"
|
||||||
|
"are accepted."
|
||||||
|
)
|
||||||
value = str(valuelist[0]).replace(",", ".")
|
value = str(valuelist[0]).replace(",", ".")
|
||||||
|
|
||||||
if not match(r'^[ 0-9\.\+\-\*/\(\)]{0,50}$', value) or "**" in value:
|
# avoid exponents to prevent expensive calculations i.e 2**9999999999**9999999
|
||||||
raise ValueError(error_msg)
|
if not match(r'^[ 0-9\.\+\-\*/\(\)]{0,200}$', value) or "**" in value:
|
||||||
|
raise ValueError(Markup(message))
|
||||||
|
|
||||||
valuelist[0] = str(eval_arithmetic_expression(value))
|
valuelist[0] = str(eval_arithmetic_expression(value))
|
||||||
|
|
||||||
|
|
|
@ -1357,49 +1357,66 @@ class APITestCase(IhatemoneyTestCase):
|
||||||
self.api_add_member("raclette", "alexis")
|
self.api_add_member("raclette", "alexis")
|
||||||
self.api_add_member("raclette", "fred")
|
self.api_add_member("raclette", "fred")
|
||||||
|
|
||||||
# add a bill
|
# valid amounts
|
||||||
req = self.client.post("/api/projects/raclette/bills", data={
|
input_expected = [
|
||||||
'date': '2011-08-10',
|
("((100 + 200.25) * 2 - 100) / 2", 250.25),
|
||||||
'what': 'fromage',
|
("3/2", 1.5),
|
||||||
'payer': "1",
|
]
|
||||||
'payed_for': ["1", "2"],
|
|
||||||
'amount': '((100 + 200.25) * 2 - 100) / 2',
|
|
||||||
}, headers=self.get_auth("raclette"))
|
|
||||||
|
|
||||||
# should return the id
|
for i, pair in enumerate(input_expected):
|
||||||
self.assertStatus(201, req)
|
input_amount, expected_amount = pair
|
||||||
self.assertEqual(req.data.decode('utf-8'), "1\n")
|
id = i + 1
|
||||||
|
|
||||||
# get this bill details
|
req = self.client.post(
|
||||||
req = self.client.get("/api/projects/raclette/bills/1",
|
"/api/projects/raclette/bills",
|
||||||
headers=self.get_auth("raclette"))
|
data={
|
||||||
|
'date': '2011-08-10',
|
||||||
|
'what': 'fromage',
|
||||||
|
'payer': "1",
|
||||||
|
'payed_for': ["1", "2"],
|
||||||
|
'amount': input_amount,
|
||||||
|
},
|
||||||
|
headers=self.get_auth("raclette")
|
||||||
|
)
|
||||||
|
|
||||||
# compare with the added info
|
# should return the id
|
||||||
self.assertStatus(200, req)
|
self.assertStatus(201, req)
|
||||||
expected = {
|
self.assertEqual(req.data.decode('utf-8'), "{}\n".format(id))
|
||||||
"what": "fromage",
|
|
||||||
"payer_id": 1,
|
|
||||||
"owers": [
|
|
||||||
{"activated": True, "id": 1, "name": "alexis", "weight": 1},
|
|
||||||
{"activated": True, "id": 2, "name": "fred", "weight": 1}],
|
|
||||||
"amount": 250.25,
|
|
||||||
"date": "2011-08-10",
|
|
||||||
"id": 1}
|
|
||||||
|
|
||||||
got = json.loads(req.data.decode('utf-8'))
|
# get this bill's details
|
||||||
self.assertEqual(
|
req = self.client.get(
|
||||||
datetime.date.today(),
|
"/api/projects/raclette/bills/{}".format(id),
|
||||||
datetime.datetime.strptime(got["creation_date"], '%Y-%m-%d').date()
|
headers=self.get_auth("raclette")
|
||||||
)
|
)
|
||||||
del got["creation_date"]
|
|
||||||
self.assertDictEqual(expected, got)
|
|
||||||
|
|
||||||
|
# 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 = [
|
erroneous_amounts = [
|
||||||
"lambda ", # letters
|
"lambda ", # letters
|
||||||
"(20 + 2", # invalid expression
|
"(20 + 2", # invalid expression
|
||||||
"20/0", # invalid calc
|
"20/0", # invalid calc
|
||||||
"9999**99999999999999999", # exponents
|
"9999**99999999999999999", # exponents
|
||||||
"2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2", # greater than 50 chars,
|
"2" * 201, # greater than 200 chars,
|
||||||
]
|
]
|
||||||
|
|
||||||
for amount in erroneous_amounts:
|
for amount in erroneous_amounts:
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
from __future__ import division
|
||||||
import base64
|
import base64
|
||||||
import re
|
import re
|
||||||
import ast
|
import ast
|
||||||
|
|
Loading…
Reference in a new issue