From 98568402c7e422dd2a2dd5aec8e2f2398dc9ff62 Mon Sep 17 00:00:00 2001 From: Konstantin Baierer Date: Tue, 2 Mar 2021 16:13:03 +0100 Subject: [PATCH] counter: init-overrideable --- qurator/eynollah/utils/counter.py | 2 ++ tests/test_counter.py | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 tests/test_counter.py diff --git a/qurator/eynollah/utils/counter.py b/qurator/eynollah/utils/counter.py index e2ba34f..f076c23 100644 --- a/qurator/eynollah/utils/counter.py +++ b/qurator/eynollah/utils/counter.py @@ -7,6 +7,8 @@ class EynollahIdCounter(): def __init__(self, region_idx=0, line_idx=0): self._counter = Counter() + self.set('region', region_idx) + self.set('line', line_idx) def inc(self, name, val=1): self._counter.update({name: val}) diff --git a/tests/test_counter.py b/tests/test_counter.py new file mode 100644 index 0000000..44715b8 --- /dev/null +++ b/tests/test_counter.py @@ -0,0 +1,27 @@ +from tests.base import main +from qurator.eynollah.utils.counter import EynollahIdCounter + +def test_counter_string(): + c = EynollahIdCounter() + assert c.next_region_id == 'region_0001' + assert c.next_region_id == 'region_0002' + assert c.next_line_id == 'region_0002_line_0001' + assert c.next_region_id == 'region_0003' + assert c.next_line_id == 'region_0003_line_0001' + +def test_counter_init(): + c = EynollahIdCounter(region_idx=2) + assert c.get('region') == 2 + +def test_counter_methods(): + c = EynollahIdCounter() + assert c.get('region') == 0 + c.inc('region', 5) + assert c.get('region') == 5 + c.set('region', 10) + assert c.get('region') == 10 + c.inc('region', -9) + assert c.get('region') == 1 + +if __name__ == '__main__': + main(__file__)