diff --git a/docker-compose.yml b/docker-compose.yml index f5bc064..ab5a9e0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,8 +9,8 @@ networks: services: hdx-notifications: -# image: - build: . + image: public.ecr.aws/unocha/hdx-jp-sw-notification-platform:dev +# build: . hostname: hdx-notifications volumes: - ".:/srv/listener" diff --git a/processing/helpers.py b/processing/helpers.py index 0958d6d..809fd94 100644 --- a/processing/helpers.py +++ b/processing/helpers.py @@ -22,6 +22,19 @@ EVENT_TYPE_SPREADSHEET_SHEET_CHANGED, } +def get_change_summary(event): + if event : + ev_type = event.get('event_type') + resource_name = event.get('resource_name','A resource') + if ev_type == EVENT_TYPE_RESOURCE_CREATED: + return f'{resource_name} was created' + if ev_type == EVENT_TYPE_RESOURCE_DELETED: + return f'{resource_name} was deleted' + if ev_type == EVENT_TYPE_RESOURCE_DATA_CHANGED: + return f'{resource_name} was updated' + if ev_type == EVENT_TYPE_SPREADSHEET_SHEET_CHANGED or ev_type == EVENT_TYPE_SPREADSHEET_SHEET_CREATED or ev_type == EVENT_TYPE_SPREADSHEET_SHEET_DELETED: + return f'Resource structure was changed for {resource_name}' + return None def do_nothing_for_ever(): while True: diff --git a/processing/main.py b/processing/main.py index bd74adf..212c915 100644 --- a/processing/main.py +++ b/processing/main.py @@ -1,29 +1,16 @@ import logging from typing import Dict, Set -from helpers import EVENT_TYPE_RESOURCE_DELETED, EVENT_TYPE_RESOURCE_CREATED, EVENT_TYPE_RESOURCE_DATA_CHANGED, \ - EVENT_TYPE_SPREADSHEET_SHEET_CHANGED, EVENT_TYPE_SPREADSHEET_SHEET_CREATED, EVENT_TYPE_SPREADSHEET_SHEET_DELETED +from helpers import get_change_summary from processing.novu import push_notification_to_novu logger = logging.getLogger(__name__) -def _get_change_summary(event): - ev_type = event.get('event_type') - if ev_type == EVENT_TYPE_RESOURCE_CREATED: - return f'{event.get('resource_name')} was created' - if ev_type == EVENT_TYPE_RESOURCE_DELETED: - return f'{event.get('resource_name')} was deleted' - if ev_type == EVENT_TYPE_RESOURCE_DATA_CHANGED: - return f'{event.get('resource_name')} was updated' - if ev_type == EVENT_TYPE_SPREADSHEET_SHEET_CHANGED or ev_type == EVENT_TYPE_SPREADSHEET_SHEET_CREATED or ev_type == EVENT_TYPE_SPREADSHEET_SHEET_DELETED: - return f'Resource structure was changed for {event.get('resource_name')}' - - def process(dataset_id_list: Set[str], event: Dict): if dataset_id_list: if event and 'dataset_id' in event and event.get('dataset_id') in dataset_id_list: - change_summary = _get_change_summary(event) + change_summary = get_change_summary(event) data_dict = { 'event': event, 'change_summary': change_summary diff --git a/processing/novu.py b/processing/novu.py index 7717b48..1d9f2a6 100644 --- a/processing/novu.py +++ b/processing/novu.py @@ -12,7 +12,7 @@ def push_notification_to_novu(data_dict: Dict): event = data_dict.get('event') - dataset_id = data_dict.get('dataset_id') + dataset_id = event.get('dataset_id') url = config.NOVU_API_URL payload = json.dumps({ diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..ba8ed17 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,5 @@ +[pytest] +log_cli = True +log_cli_format = %(asctime)s %(levelname)s %(message)s +log_cli_date_format = %Y-%m-%d %H:%M:%S +log_cli_level = INFO \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..3beab55 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,37 @@ +import pytest +import os + +from config.config import Config, get_config + +DEFAULT_ENV = { + # 'REDIS_STREAM_HOST': 'gisredis', + 'REDIS_STREAM_DB': '14' +} + + +@pytest.fixture(scope='module', autouse=True) +def prepare_environment(): + for key, value in DEFAULT_ENV.items(): + os.environ[key] = value + + +# +# @pytest.fixture(scope='module') +# def key_value_store() -> RedisKeyValueStore: +# key_value_store = connect_to_key_value_store_with_env_vars() +# return key_value_store + + +@pytest.fixture(scope='module') +def config() -> Config: + config = get_config() + return config + +# @pytest.fixture(scope='module') +# def context(key_value_store: RedisKeyValueStore, config: Config) -> Context: +# context = Context(store=key_value_store, config=config, gsheets=None, slack_client=SlackClientWrapper()) +# return context + +# @pytest.fixture(scope='function') +# def clean_redis(key_value_store: RedisKeyValueStore) -> None: +# key_value_store.redis_conn.flushdb() diff --git a/tests/test_notifications.py b/tests/test_notifications.py new file mode 100644 index 0000000..091e874 --- /dev/null +++ b/tests/test_notifications.py @@ -0,0 +1,74 @@ +import logging + +from processing.helpers import get_change_summary +from processing.datasets import get_dataset_id_list + + +logger = logging.getLogger(__name__) + + +def _generate_resource_created_test_event(): + return { + 'event_type': 'resource-created', + 'event_time': '2024-10-02T11:22:15.973707', + 'event_source': 'ckan', + 'initiator_user_name': 'test-user', + 'dataset_name': 'test-dataset', + 'dataset_title': 'Test dataset', + 'dataset_id': 'test-dataset-id', + 'changed_fields': [ + { + 'field': 'microdata', + 'new_value': False, + 'new_display_value': 'does not contain microdata', + 'old_value': None, + 'old_display_value': 'does not contain microdata' + }, + { + 'field': 'resource_type', + 'new_value': 'file.upload', + 'new_display_value': 'file.upload', + 'old_value': None, + 'old_display_value': None + }, + { + 'field': 'name', + 'new_value': 'ING.Account.Statement_30-09-24_999911495277_USD.pdf', + 'new_display_value': 'ING.Account.Statement_30-09-24_999911495277_USD.pdf', + 'old_value': None, + 'old_display_value': None + }, + { + 'field': 'format', + 'new_value': 'PDF', + 'new_display_value': 'PDF', + 'old_value': None, + 'old_display_value': None + }, + { + 'field': 'url', + 'new_value': 'https://data.humdata.local/dataset/f679245a-5740-4ba6-a395-ec4e1ac20325/resource/b017f603-fb5e-4169-a7c5-88dbf202d368/download/ing.account.statement_30-09-24_999911495277_usd.pdf', + 'new_display_value': 'https://data.humdata.local/dataset/f679245a-5740-4ba6-a395-ec4e1ac20325/resource/b017f603-fb5e-4169-a7c5-88dbf202d368/download/ing.account.statement_30-09-24_999911495277_usd.pdf', + 'old_value': None, + 'old_display_value': None + } + ], + 'org_id': 'test-org-id', + 'org_name': 'test-org', + 'org_title': 'Test Org', + 'resource_name': 'test.csv', + 'resource_id': 'b017f603-fb5e-4169-a7c5-88dbf202d368' + } + + + +def test_get_change_summary(): + resource_create_event = _generate_resource_created_test_event() + summary = get_change_summary(event=resource_create_event) + assert resource_create_event.get('resource_name') in summary + assert 'was created' in summary + + +def test_get_dataset_id_list(): + dataset_id_list = get_dataset_id_list() + assert len(dataset_id_list) > 0