Skip to content

Commit

Permalink
[test][feat] extend client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
M3ssman committed Aug 27, 2024
1 parent d9d9fec commit 29ab8fc
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/digiflow/record/record_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

STATETIME_FORMAT = '%Y-%m-%d_%H:%M:%S'

DATA_EXHAUSTED_MARK = 'no open records'
DATA_EXHAUSTED_PREFIX = 'no open records'
DATA_EXHAUSTED_MARK = DATA_EXHAUSTED_PREFIX + ' in {}'


@dataclasses.dataclass
Expand All @@ -46,9 +47,9 @@ def __init__(self, data_path, logger):
self.logger = logger


class RecordExhaustedException(Exception):
class RecordsExhaustedException(Exception):
"""Mark state when no more records can be
served to clients anymore"""
achieved anymore"""


class RecordRequestHandler(http.server.SimpleHTTPRequestHandler,
Expand Down Expand Up @@ -209,12 +210,10 @@ class Client(df.FallbackLogger):
"""

def __init__(self, oai_record_list_label, host, port,

notify_callback=None, logger=None):
logger=None):
self.oai_record_list_label = oai_record_list_label
self.record: df_r.Record = None
self.oai_server_url = f'http://{host}:{port}/{oai_record_list_label}'
self.notify_callback = notify_callback
super().__init__(some_logger=logger)

def get_record(self, get_record_state, set_record_state):
Expand All @@ -228,9 +227,7 @@ def get_record(self, get_record_state, set_record_state):
timeout=300, headers=the_headers)
except requests.exceptions.RequestException as err:
if self.logger is not None:
self.logger.error("OAI server connection fails: %s", err)
if self.notify_callback:
self.notify_callback(f'[OCR-D-ODEM] Failure for {self.oai_server_url}', err)
self.logger.error("connection fails: %s", err)
sys.exit(1)
status = response.status_code
result = response.content
Expand All @@ -239,7 +236,7 @@ def get_record(self, get_record_state, set_record_state):
if DATA_EXHAUSTED_MARK in str(result):
if self.logger is not None:
self.logger.info(result)
raise RecordExhaustedException(result.decode(encoding='utf-8'))
raise RecordsExhaustedException(result.decode(encoding='utf-8'))
# otherwise exit anyway
sys.exit(1)

Expand Down
42 changes: 42 additions & 0 deletions tests/test_record_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import ast
import typing
import unittest.mock

import pytest

import digiflow.record as df_r
import digiflow.record.record_service as df_rs


RECORD_IDENTIFIER = 'IDENTIFIER'
Expand Down Expand Up @@ -123,3 +125,43 @@ def test_record_update_dealing_invalid_data():

# assert
assert 'ppn#3345' in record.info


@pytest.mark.parametrize("file_path,result",
[
('/data/oai/test.csv', 'no open records in /data/oai/test.csv'),
('', 'no open records in '),
(None, 'no open records in None')
])
def test_mark_exhausted_matching(file_path, result):
"""Check formatting behavior"""

# assert
assert df_rs.DATA_EXHAUSTED_MARK.format(file_path) == result


@unittest.mock.patch('digiflow.requests.get')
def test_exit_on_data_exhausted(mock_request):
"""Ensure dedicated state is communicated
to OAIClient when no more records present
Please note: *real* responses return
byte-object rather!
"""

# arrange
_the_list_label = 'oai-record-test'
_rsp = f'{df_rs.DATA_EXHAUSTED_MARK.format(_the_list_label)}'.encode()
client = df_r.Client(_the_list_label, '1.2.3.4', '9999')
mock_resp = unittest.mock.Mock()
mock_resp.status_code = 404
mock_resp.headers = {'Content-Type': 'text/xml'}
mock_resp.content = _rsp
mock_request.return_value = mock_resp

# act
with pytest.raises(SystemExit) as proc_exit:
client.get_record(get_record_state=df_r.UNSET_LABEL, set_record_state='busy')

# assert
assert proc_exit.value.args[0] == 1

0 comments on commit 29ab8fc

Please sign in to comment.