1
0

Added basic tests.

This commit is contained in:
Miguel Salgado 2022-08-10 08:38:33 -07:00
parent 6d55d3307e
commit cfc9c1939d
4 changed files with 84 additions and 4 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
__pycache__
app.pyc tests.pyc

View File

@ -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,

6
app.py
View File

@ -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):
"""

78
tests.py Normal file
View File

@ -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)