diff --git a/Dockerfile b/Dockerfile index e85bc81..0ba50ae 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,14 @@ FROM python:2 AS builder # Install the required python packages WORKDIR /app/ -# Copy flask app source code to the /app dir on the container -COPY app/ . +# Copy requirements files to the container +COPY app/*requirements.txt ./ # Install the required python packages RUN pip install \ --no-cache-dir \ -r dev-requirements.txt +# Copy flask app source code to the /app dir on the container +COPY app/ . FROM builder AS unit-tester RUN py.test ./tests/unit -v \ diff --git a/app/calculator/app.py b/app/calculator/app.py index 751c3ad..856bdfe 100644 --- a/app/calculator/app.py +++ b/app/calculator/app.py @@ -10,13 +10,24 @@ def multiply(a, b): c = Calculator() - result = c.mul(int(a), int(b)) - return str(result) + try: + result = c.mul(int(a), int(b)) + return str(result) + except Exception as e: + return str(e.args[0]), 403 @app.route("/calc//") def divide(a, b): - return "Unsupported operation", 501 + c = Calculator() + + try: + result = c.div(int(a), int(b)) + return str(result) + except ValueError as e: + return str(e.args[0]), 403 + except ZeroDivisionError as e: + return 'Divide by Zero error.', 403 @app.route("/") diff --git a/app/calculator/logic.py b/app/calculator/logic.py index a4bd1db..e30f354 100644 --- a/app/calculator/logic.py +++ b/app/calculator/logic.py @@ -15,7 +15,13 @@ def __init__(self, min_value=-1000, max_value=1000): self.max_value = max_value def mul(self, a, b): - pass + if (a < self.min_value or a > self.max_value or b < self.min_value or b > self.max_value): + raise ValueError('Out of bounds min/max values used. Values should be >= ' + + str(self.min_value) + ' and <= ' + str(self.max_value)) + return a*b def div(self, a, b): - pass + if (a < self.min_value or a > self.max_value or b < self.min_value or b > self.max_value): + raise ValueError('Out of bounds min/max values used. Values should be >= ' + + str(self.min_value) + ' and <= ' + str(self.max_value)) + return a/b diff --git a/app/tests/integration/test_views.py b/app/tests/integration/test_views.py index a0df45b..e852f0d 100644 --- a/app/tests/integration/test_views.py +++ b/app/tests/integration/test_views.py @@ -9,7 +9,26 @@ def setUp(self): self.client = TestClient(app) def test_multiply(self): - # r = self.client.get("/calc/3*10") - # self.assertEquals(r.status_code, 200) - # self.assertEquals(r.body, "30") - pass + r = self.client.get("/calc/3*10") + self.assertEquals(r.status_code, 200) + self.assertEquals(r.body, "30") + + def test_multiply_invalid(self): + r = self.client.get("/calc/1001*10") + self.assertEquals(r.status_code, 403) + self.assertEquals(r.body, "Out of bounds min/max values used. Values should be >= -1000 and <= 1000") + + def test_divide(self): + r = self.client.get("/calc/30/10") + self.assertEquals(r.status_code, 200) + self.assertEquals(r.body, "3") + + def test_divide_invalid(self): + r = self.client.get("/calc/-1010/10") + self.assertEquals(r.status_code, 403) + self.assertEquals(r.body, "Out of bounds min/max values used. Values should be >= -1000 and <= 1000") + + def test_divide_by_zero(self): + r = self.client.get("/calc/10/0") + self.assertEquals(r.status_code, 403) + self.assertEquals(r.body, "Divide by Zero error.") \ No newline at end of file diff --git a/app/tests/unit/test_logic.py b/app/tests/unit/test_logic.py index 9d8c6bc..dc50bc5 100644 --- a/app/tests/unit/test_logic.py +++ b/app/tests/unit/test_logic.py @@ -1,10 +1,44 @@ +import pytest from unittest import TestCase from calculator.logic import Calculator class CalculatorTests(TestCase): - def test_mul(self): - pass + def test_mul_with_two_positive_numbers(self): + calculator = Calculator() + assert calculator.mul(5,10) == 50 - def test_div(self): - pass + def test_mul_with_two_negative_numbers(self): + calculator = Calculator() + assert calculator.mul(-5,-10) == 50 + + def test_mul_with_one_negative_one_positive(self): + calculator = Calculator() + assert calculator.mul(-5,10) == -50 + + def test_calculator_throws_e_when_out_of_bounds_low(self): + with pytest.raises(ValueError): + calculator = Calculator() + calculator.mul(-1001,100) + + def test_calculator_throws_e_when_out_of_bounds_high(self): + with pytest.raises(ValueError): + calculator = Calculator() + calculator.mul(-10,1001) + + def test_div_with_two_positive(self): + calculator = Calculator() + assert calculator.div(10,5) == 2 + + def test_div_with_two_negative(self): + calculator = Calculator() + assert calculator.div(-10,-5) == 2 + + def test_div_with_one_negative_one_positive(self): + calculator = Calculator() + assert calculator.div(-10,5) == -2 + + def test_div_by_zero_raises_e(self): + with pytest.raises(ZeroDivisionError): + calculator = Calculator() + calculator.div(10,0)