From cfc9c1939dd2079cd85598c82e0003029fe25e1b Mon Sep 17 00:00:00 2001 From: Miguel Alejandro Salgado Zapien Date: Wed, 10 Aug 2022 08:38:33 -0700 Subject: [PATCH] Added basic tests. --- .gitignore | 2 ++ README.md | 2 ++ app.py | 6 ++--- tests.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 tests.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7ad4ad7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +app.pyc tests.pyc diff --git a/README.md b/README.md index 49db89d..283a2a3 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ The solution is implemented in a single file, and it's targeting `python39`. To execute the example you can just run `python app.py` assuming you are using the correct version, and that you are located in the directory for this project. +To execute tests you can run `python -m unittest tests.py` or `pytest tests.py` (provided you installed `pytest`). + # About the solution The problem can be divided in two parts, diff --git a/app.py b/app.py index d54c029..f2ff9af 100644 --- a/app.py +++ b/app.py @@ -29,9 +29,7 @@ class Stats: This method executes in constant time, because it's a list access by element. """ - returnable = self._list[value - 1] - # print(value, returnable) - return returnable + return self._list[value - 1] def between(self, m, M): """ @@ -39,7 +37,7 @@ class Stats: it's consuming two times a constant method, plus an arithmetic operation. """ - return self.less(M + 1) - self.less(m - 1) + return self.less(M + 1) - self.less(m) def greater(self, value): """ diff --git a/tests.py b/tests.py new file mode 100644 index 0000000..5e7d659 --- /dev/null +++ b/tests.py @@ -0,0 +1,78 @@ +from unittest import TestCase +from app import DataCapture, Stats + + +class TestDataCaptureStats(TestCase): + def test_init(self): + """Test no error at initialization.""" + a = DataCapture() + assert isinstance(a, DataCapture) + + def test_add_single_value(self): + """Test no error at add single value.""" + a = DataCapture() + a.add(1) + + def test_add_many_values(self): + """Test no error at add many values.""" + a = DataCapture() + a.add(1, 2) + + def test_generate_stats_zero(self): + """Test no error at add many values.""" + a = DataCapture() + stats = a.build_stats() + assert isinstance(stats, Stats) + + def test_generate_single_element_stats(self): + """Test no error at add many values.""" + a = DataCapture() + a.add(1) + stats = a.build_stats() + assert isinstance(stats, Stats) + + def test_generate_multi_element_stats(self): + """Test no error at add many values.""" + a = DataCapture() + a.add(1, 2, 3, 4, 5, 6, 7) + stats = a.build_stats() + assert isinstance(stats, Stats) + assert 5 == stats.greater(2) + assert 4 == stats.less(5) + assert 4 == stats.between(2, 5) + + def test_generate_multi_element_stats_v2(self): + """Test no error at add many values.""" + a = DataCapture() + a.add(1, 2, 3, 4, 5, 6, 7) + a.add(1, 2, 3, 4, 5, 6, 7) + stats = a.build_stats() + assert isinstance(stats, Stats) + assert 10 == stats.greater(2) + assert 8 == stats.less(5) + assert 8 == stats.between(2, 5) + + def test_generate_multi_element_stats_v3(self): + """Test no error at add many values.""" + a = DataCapture() + a.add(1, 2, 3, 4, 5, 6, 7) + a.add(2, 4, 6, 8) + stats = a.build_stats() + assert isinstance(stats, Stats) + assert 6 == stats.less(5) + assert 6 == stats.between(2, 5) + assert 1 == stats.less(2) + assert 2 == stats.between(2, 2) + assert 8 == stats.greater(2) + + def test_base_case(self): + capture = DataCapture() + capture.add(3) + capture.add(9) + capture.add(3) + capture.add(4) + capture.add(6) + stats = capture.build_stats() + assert 2 == stats.less(4) + assert 2 == stats.greater(4) + assert 4 == stats.between(3, 6)