Skip to content

Commit

Permalink
chore: added missing test
Browse files Browse the repository at this point in the history
For #186
  • Loading branch information
tstorek committed May 31, 2023
1 parent 884a7e6 commit 4c2d94b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion filip/models/ngsi_v2/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def __getattr__(self, item):
Return unit as attribute by name or code.
Notes:
Underscores will be substituted with whitespaces
Args:
item: if len(row) == 0:
Expand All @@ -223,7 +224,7 @@ def quantities(self):
"""
raise NotImplementedError("The used dataset does currently not "
"contain the information about quantity")

@lru_cache()
def __getitem__(self, item: str) -> Unit:
"""
Get unit by name or code
Expand Down
44 changes: 44 additions & 0 deletions tests/models/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Test for filip.models.units
"""
from unittest import TestCase
import functools
from filip.models.ngsi_v2.units import \
Unit, \
Units, \
Expand Down Expand Up @@ -39,16 +40,47 @@ def test_unit_text(self):
def test_unit_model(self):
"""
Test unit model
Returns:
None
"""
# test creation
unit = Unit(**self.unit)
unit_from_json = Unit.parse_raw(unit.json(by_alias=True))
self.assertEqual(unit, unit_from_json)

def test_unit_model_caching(self):
"""
Test caching of unit model
Returns:
None
"""

unit = Unit(**self.unit)
# testing hashing and caching
from functools import lru_cache
from time import perf_counter_ns

self.assertEqual(unit.__hash__(), unit.__hash__())

@functools.lru_cache
def cache_unit(unit: Unit):
return Unit(name=unit.name)

timers = []
for i in range(5):
start = perf_counter_ns()
cache_unit(unit)
stop = perf_counter_ns()
timers.append(stop - start)
if i > 0:
self.assertLess(timers[i], timers[0])

def test_units(self):
"""
Test units api
Returns:
None
"""
Expand All @@ -69,6 +101,18 @@ def test_unit_validator(self):
Returns:
None
"""
# using garbage collector to clean up all caches
import gc
gc.collect()

# All objects collected
objects = [i for i in gc.get_objects()
if isinstance(i, functools._lru_cache_wrapper)]

# All objects cleared
for object in objects:
object.cache_clear()

unit_data = self.unit.copy()
unit_data['name'] = "celcius"
with self.assertRaises(ValueError):
Expand Down

0 comments on commit 4c2d94b

Please sign in to comment.