Skip to content

Commit

Permalink
Enforce unique items
Browse files Browse the repository at this point in the history
  • Loading branch information
doruirimescu committed Jun 6, 2024
1 parent 1277aa3 commit e766c42
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/stateful_data_processor/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def __init__(
signal.signal(signal.SIGTERM, self._signal_handler)

@abstractmethod
def process_data(self, *args, **kwargs):
def process_data(self, items, *args, **kwargs):
"""Template method for processing data. Get data, and call _iterate_items.
Arguments are forwarded to _iterate_items. You can override this method to implement
more custom processing."""
self._iterate_items(*args, **kwargs)
self._iterate_items(items, *args, **kwargs)

def _iterate_items(self, items, *args, **kwargs):
"""General iteration method for processing items. This should be called from process_data.
Expand Down Expand Up @@ -89,10 +89,18 @@ def _signal_handler(self, signum, frame):
self.logger.info("Data saved, exiting.")
exit(0)

def run(self, *args, **kwargs):
def run(self, items, *args, **kwargs):
"""Main method to run the processor."""
if not items:
self.logger.error("No items to process.")
return

if sorted(list(set(items))) != list(items):
self.logger.error("Items must be unique.")
return

try:
self.process_data(*args, **kwargs)
self.process_data(items, *args, **kwargs)
except Exception as e:
raise e
self.file_rw.write(self.data)
8 changes: 8 additions & 0 deletions tests/test_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ def tearDown(self) -> None:
os.remove(TEST_FILE_JSON_PATH)
del self.file_rw

def test_items_must_be_unique(self):
processor = SymbolProcessor(self.file_rw, should_read=False, logger=self.mock_logger)
processor.run(items=["a", "a", "b"], delay=0)
calls = [
call("Items must be unique."),
]
self.mock_logger.error.assert_has_calls(calls, any_order=True)

def test_process_data(self):
processor = SymbolProcessor(
self.file_rw, should_read=False, logger=self.mock_logger
Expand Down

0 comments on commit e766c42

Please sign in to comment.