Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .github/workflows/python-code-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ jobs:
- name: Check Python Formatting
run: |
make python-linting
- name: Check Python Imports
run: |
make python-check-imports
- name: Check for Python Dead Code
run: |
make python-check-dead-code
- name: Check Python Security
run: |
make python-check-security
3 changes: 2 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"streetsidesoftware.code-spell-checker",
"vscode-icons-team.vscode-icons",
"yzhang.dictionary-completion",
"yzhang.markdown-all-in-one"
"yzhang.markdown-all-in-one",
"charliermarsh.ruff"
],
"unwantedRecommendations": [
"googlecloudtools.cloudcode",
Expand Down
36 changes: 5 additions & 31 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -510,13 +510,11 @@ trigger-dos-deployment-pipeline:
rm -rf jenkins.cookies

python-linting:
make python-code-check FILES=application
make docker-run-ruff

python-code-checks:
make python-check-dead-code
make python-check-imports
make python-code-check FILES=application
make python-linting
make unit-test
echo "Python code checks completed"

Expand All @@ -526,32 +524,6 @@ python-check-dead-code:
DIR=$(APPLICATION_DIR) \
CMD="python -m vulture"

python-format:
make python-code-format FILES=application
make python-code-format FILES=test

python-check-imports:
make -s docker-run-python \
IMAGE=$$(make _docker-get-reg)/tester:latest \
DIR=$(APPLICATION_DIR) \
CMD="python -m isort . -l=120 --check-only --profile=black \
--force-alphabetical-sort-within-sections --known-local-folder=common \
"

python-fix-imports:
make -s docker-run-python \
IMAGE=$$(make _docker-get-reg)/tester:latest \
DIR=$(APPLICATION_DIR) \
CMD="python -m isort . -l=120 --profile=black --force-alphabetical-sort-within-sections \
--known-local-folder=common \
"

python-check-security:
make -s docker-run-python \
IMAGE=$$(make _docker-get-reg)/tester:latest \
DIR=$(APPLICATION_DIR) \
CMD="python -m bandit -r . -c pyproject.toml"

create-ecr-repositories:
make docker-create-repository NAME=change-event-dlq-handler
make docker-create-repository NAME=dos-db-handler
Expand Down Expand Up @@ -651,8 +623,10 @@ undeploy-dynamodb-cleanup-job: # Undeploys dynamodb cleanup job

docker-run-ruff: # Runs ruff tests - mandatory: RUFF_OPTS=[options]
make -s docker-run \
IMAGE=$$(make _docker-get-reg)/tester:latest \
IMAGE=$$(make _docker-get-reg)/tester \
CMD="ruff check . $(RUFF_OPTS)"

ruff-auto-fix: # Auto fixes ruff warnings
python-ruff-fix: # Auto fixes ruff warnings
make docker-run-ruff RUFF_OPTS="--fix"

.SILENT: docker-run-ruff
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import Any

from aws_embedded_metrics import metric_scope
from aws_lambda_powertools.logging import Logger
from aws_lambda_powertools.tracing import Tracer
from aws_lambda_powertools.utilities.data_classes import event_source, SQSEvent
from aws_lambda_powertools.utilities.data_classes import SQSEvent, event_source
from aws_lambda_powertools.utilities.typing.lambda_context import LambdaContext

from common.constants import FIFO_DLQ_HANDLER_REPORT_ID
Expand All @@ -20,15 +22,16 @@
@event_source(data_class=SQSEvent)
@logger.inject_lambda_context(clear_state=True)
@metric_scope
def lambda_handler(event: SQSEvent, context: LambdaContext, metrics) -> None:
"""Entrypoint handler for the change event dlq handler lambda
def lambda_handler(event: SQSEvent, context: LambdaContext, metrics: Any) -> None: # noqa: ARG001, ANN401
"""Entrypoint handler for the change event dlq handler lambda.

Messages are sent to the change event dlq handler lambda when a message
fails in either the change event queue or holding queue

Args:
event (SQSEvent): Lambda function invocation event (list of 1 SQS Message)
context (LambdaContext): Lambda function context object
metrics (Metrics): CloudWatch embedded metrics object
"""
record = next(event.records)
handle_sqs_msg_attributes(record.message_attributes)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from dataclasses import dataclass
from json import dumps
from typing import Any, Dict
from typing import Any
from unittest.mock import MagicMock, patch

import pytest
from aws_embedded_metrics.logger.metrics_logger import MetricsLogger
from pytest import fixture

from ..change_event_dlq_handler import lambda_handler
from application.change_event_dlq_handler.change_event_dlq_handler import lambda_handler
from common.tests.conftest import PHARMACY_STANDARD_EVENT, PHARMACY_STANDARD_EVENT_STAFF

FILE_PATH = "application.change_event_dlq_handler.change_event_dlq_handler"
Expand All @@ -30,9 +30,9 @@
}


@fixture
@pytest.fixture()
def dead_letter_change_event_from_change_event_queue():
yield {
return {
"Records": [
{
"messageId": "059f36b4-87a3-44ab-83d2-661975830a7d",
Expand Down Expand Up @@ -62,14 +62,14 @@ def dead_letter_change_event_from_change_event_queue():
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue",
"awsRegion": "us-east-2",
}
]
},
],
}


@fixture
@pytest.fixture()
def dead_letter_staff_change_event_from_change_event_queue():
yield {
return {
"Records": [
{
"messageId": "059f36b4-87a3-44ab-83d2-661975830a7d",
Expand Down Expand Up @@ -99,14 +99,14 @@ def dead_letter_staff_change_event_from_change_event_queue():
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue",
"awsRegion": "us-east-2",
}
]
},
],
}


@fixture
@pytest.fixture()
def dead_letter_change_event_from_holding_queue():
yield {
return {
"Records": [
{
"messageId": "059f36b4-87a3-44ab-83d2-661975830a7d",
Expand All @@ -123,16 +123,16 @@ def dead_letter_change_event_from_holding_queue():
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue",
"awsRegion": "us-east-2",
}
]
},
],
}


@fixture
@pytest.fixture()
def lambda_context():
@dataclass
class LambdaContext:
"""Mock LambdaContext - All dummy values"""
"""Mock LambdaContext - All dummy values."""

function_name: str = "change-event-dlq-handler"
memory_limit_in_mb: int = 128
Expand All @@ -151,8 +151,8 @@ def test_lambda_handler_event_from_change_event_queue(
mock_set_dimensions: MagicMock,
mock_add_change_event_to_dynamodb: MagicMock,
mock_extract_body: MagicMock,
dead_letter_staff_change_event_from_change_event_queue: Dict[str, Any],
dead_letter_change_event_from_change_event_queue: Dict[str, Any],
dead_letter_staff_change_event_from_change_event_queue: dict[str, Any],
dead_letter_change_event_from_change_event_queue: dict[str, Any],
lambda_context,
):
# Arrange
Expand All @@ -163,7 +163,7 @@ def test_lambda_handler_event_from_change_event_queue(
# Assert
mock_extract_body.assert_called_once_with(dead_letter_change_event_from_change_event_queue["Records"][0]["body"])
expected_timestamp = int(
dead_letter_change_event_from_change_event_queue["Records"][0]["attributes"]["SentTimestamp"]
dead_letter_change_event_from_change_event_queue["Records"][0]["attributes"]["SentTimestamp"],
)
mock_add_change_event_to_dynamodb.assert_called_once_with(extracted_body, 123456789, expected_timestamp)

Expand All @@ -175,13 +175,15 @@ def test_lambda_handler_event_from_holding_queue(
mock_put_metric: MagicMock,
mock_set_dimensions: MagicMock,
mock_add_change_event_to_dynamodb: MagicMock,
dead_letter_change_event_from_holding_queue: Dict[str, Any],
dead_letter_change_event_from_holding_queue: dict[str, Any],
lambda_context,
):
# Act
lambda_handler(dead_letter_change_event_from_holding_queue, lambda_context)
# Assert
expected_timestamp = int(dead_letter_change_event_from_holding_queue["Records"][0]["attributes"]["SentTimestamp"])
mock_add_change_event_to_dynamodb.assert_called_once_with(
CHANGE_EVENT_FROM_CHANGE_EVENT_QUEUE, CHANGE_EVENT_FROM_HOLDING_QUEUE["sequence_number"], expected_timestamp
CHANGE_EVENT_FROM_CHANGE_EVENT_QUEUE,
CHANGE_EVENT_FROM_HOLDING_QUEUE["sequence_number"],
expected_timestamp,
)
12 changes: 6 additions & 6 deletions application/common/appconfig.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from os import getenv
from typing import Any, Dict
from typing import Any

from aws_lambda_powertools.utilities.feature_flags.appconfig import AppConfigStore
from aws_lambda_powertools.utilities.feature_flags.feature_flags import FeatureFlags


class AppConfig:
"""Application configuration"""
"""Application configuration."""

def __init__(self, name: str) -> None:
"""Initialise the application configuration
"""Initialise the application configuration.

Args:
name (str): name of the application configuration profile
Expand All @@ -22,16 +22,16 @@ def __init__(self, name: str) -> None:
name=name,
)

def get_raw_configuration(self) -> Dict[str, Any]:
"""Get the raw configuration
def get_raw_configuration(self) -> dict[str, Any]:
"""Get the raw configuration.

Returns:
dict: raw configuration
"""
return self.app_config.get_raw_configuration

def get_feature_flags(self) -> FeatureFlags:
"""Get the feature flags for the given name
"""Get the feature flags for the given name.

Returns:
FeatureFlags: feature flags class
Expand Down
2 changes: 2 additions & 0 deletions application/common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
VALID_SERVICE_TYPES_KEY = "VALID_SERVICE_TYPES"
ODSCODE_LENGTH_KEY = "ODSCODE_LENGTH"

CLOSED_AND_HIDDEN_STATUSES = ["HIDDEN", "CLOSED"]

SERVICE_TYPES = {
PHARMACY_ORG_TYPE_ID: {
SERVICE_TYPES_ALIAS_KEY: PHARMACY_SERVICE_KEY,
Expand Down
Loading