31 lines
820 B
Python
31 lines
820 B
Python
from decimal import Decimal
|
|||
|
|
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
from app.calc import ExpressionError, evaluate, even_spread
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.parametrize("text,expected", [
|
||
|
|
("=5512*12", "66144"),
|
||
|
|
("3292.56+(12*839.67)+(12*148.19)", "15146.88"),
|
||
|
|
("4 839,65", "4839.65"),
|
||
|
|
("1.234,50", "1234.50"),
|
||
|
|
("1,234.50", "1234.50"),
|
||
|
|
("", "0"),
|
||
|
|
("-10/4", "-2.5"),
|
||
|
|
])
|
||
|
|
def test_evaluate(text, expected):
|
||
|
|
assert evaluate(text) == Decimal(expected)
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.mark.parametrize("text", ["__import__('os')", "2**10", "1/0", "abc", "1;2"])
|
||
|
|
def test_evaluate_rejects(text):
|
||
|
|
with pytest.raises(ExpressionError):
|
||
|
|
evaluate(text)
|
||
|
|
|
||
|
|
|
||
|
|
def test_even_spread_adds_up():
|
||
|
|
months = even_spread(Decimal("100.00"))
|
||
|
|
assert sum(months) == Decimal("100.00")
|
||
|
|
assert months[0] == Decimal("8.33") and months[-1] == Decimal("8.37")
|