Skip to content

Commit

Permalink
HDX-10153 - add pyproject.toml file and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danmihaila committed Oct 4, 2024
1 parent 0732adc commit 330bd96
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 18 deletions.
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 13 additions & 0 deletions processing/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 2 additions & 15 deletions processing/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion processing/novu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
5 changes: 5 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -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
Empty file added tests/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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()
74 changes: 74 additions & 0 deletions tests/test_notifications.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 330bd96

Please sign in to comment.