Skip to content

Commit

Permalink
Adding unit testing with pytest.
Browse files Browse the repository at this point in the history
  • Loading branch information
guydavis committed Nov 20, 2021
1 parent cc07832 commit 1c56c1a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ jobs:
python -m pip install --upgrade pip
pip install -r docker/requirements.txt
- name: Test with pytest
run: pytest -vv
run: pytest
16 changes: 8 additions & 8 deletions common/utils/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
# Common utility methods
#

import locale
import math
import re
import traceback

locale.setlocale(locale.LC_ALL, '')

def sizeof_fmt(num, suffix='B'):
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']:
if abs(num) < 1024.0:
Expand Down Expand Up @@ -56,14 +59,11 @@ def convert_date_for_luxon(datestr):
return "{0}-{1}-{2}T{3}".format(year, month, day, time)

def round_balance(value):
if value > 100:
value = '{:.6g}'.format(value)
else:
value = '{:.6f}'.format(value)
if value.endswith(".000000"):
return value[:-5]
return value

if abs(value) < 10 and abs(value) >= 1:
return "%.1f"% round(value, 2)
elif value > 1:
return f"{value:n}"
return str(round(value, 4))

##################################################################################################
#
Expand Down
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
filterwarnings =
ignore::DeprecationWarning
6 changes: 5 additions & 1 deletion tests/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
See: https://testdriven.io/blog/flask-pytest/
To run tests:

$ pytests

For functional testing, see: https://testdriven.io/blog/flask-pytest/
46 changes: 43 additions & 3 deletions tests/unit/common/utils/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TestRoundBalance(unittest.TestCase):
def test_zero(self):
data = 0
result = converters.round_balance(data)
self.assertEqual(result, "0.0")
self.assertEqual(result, "0")

def test_neg_one(self):
data = -1
Expand All @@ -22,7 +22,47 @@ def test_plus_one(self):
result = converters.round_balance(data)
self.assertEqual(result, "1.0")

def test_two_million(self):
def test_tenths(self):
data = 0.2
result = converters.round_balance(data)
self.assertEqual(result, "0.2")

def test_hundredths(self):
data = 0.02
result = converters.round_balance(data)
self.assertEqual(result, "0.02")

def test_thousandths(self):
data = 0.002
result = converters.round_balance(data)
self.assertEqual(result, "0.002")

def test_tenthousandths(self):
data = 0.0002
result = converters.round_balance(data)
self.assertEqual(result, "0.0002")

def test_millionths(self):
data = 0.000002
result = converters.round_balance(data)
self.assertEqual(result, "0.0")

def test_singledigit(self):
data = 2
result = converters.round_balance(data)
self.assertEqual(result, "2.0")

def test_thousand(self):
data = 2000
result = converters.round_balance(data)
self.assertEqual(result, "2,000")

def test_tenthousand(self):
data = 20000
result = converters.round_balance(data)
self.assertEqual(result, "20,000")

def test_million(self):
data = 2000000
result = converters.round_balance(data)
self.assertEqual(result, "2000000")
self.assertEqual(result, "2,000,000")

0 comments on commit 1c56c1a

Please sign in to comment.