Skip to content
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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 \
Expand Down
17 changes: 14 additions & 3 deletions app/calculator/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<a>/<b>")
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("/")
Expand Down
10 changes: 8 additions & 2 deletions app/calculator/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 23 additions & 4 deletions app/tests/integration/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
42 changes: 38 additions & 4 deletions app/tests/unit/test_logic.py
Original file line number Diff line number Diff line change
@@ -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)