Skip to content

Commit

Permalink
Merge pull request #35 from sliedig/py11
Browse files Browse the repository at this point in the history
feat: updated runtime to python 3.11
  • Loading branch information
igorlg authored Aug 7, 2023
2 parents 7cdfcf9 + 46636fe commit 3379647
Show file tree
Hide file tree
Showing 47 changed files with 1,363 additions and 1,017 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/reusable_unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v3

- uses: actions/setup-python@v4
with: { python-version: 3.10.11 }
with: { python-version: 3.11.4 }

- uses: snok/install-poetry@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion unicorn_contracts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ stackName := uni-prop-local-contract
build:
cfn-lint template.yaml -a cfn_lint_serverless.rules
poetry export --without-hashes --format=requirements.txt --output=src/requirements.txt
sam build -c
sam build -c $(DOCKER_OPTS)

deps:
poetry install
Expand Down
576 changes: 275 additions & 301 deletions unicorn_contracts/poetry.lock

Large diffs are not rendered by default.

26 changes: 15 additions & 11 deletions unicorn_contracts/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ name = "contracts_service"
version = "0.2.0"
description = "Unicorn Properties Contact Service"
authors = ["Amazon Web Services"]
exclude = ["src/events/"]
packages = [{ include = "contracts_service", from = "src" }]
packages = [
{ include = "contracts_service", from = "src" },
]

[tool.poetry.dependencies]
python = "^3.10"
boto3 = "^1.26.153"
aws-lambda-powertools = {extras = ["tracer"], version = "^2.16.2"}
python = "^3.11"
boto3 = "^1.28.15"
aws-lambda-powertools = {extras = ["tracer"], version = "^2.22.0"}
aws-xray-sdk = "^2.12.0"

[tool.poetry.group.dev.dependencies]
pytest = "^7.1.2"
pytest-mock = "^3.7.0"
pytest-cov = "^3.0.0"
coverage = "^6.4.2"
pytest = "^7.4.0"
pytest-mock = "^3.11.1"
pytest-cov = "^4.1.0"
coverage = "^7.2.7"
requests = "^2.31.0"
moto = "^3.1.17"
importlib-metadata = "^4.12.0"
moto = "^4.1.13"
importlib-metadata = "^6.8.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand All @@ -32,3 +33,6 @@ testpaths = [
"./tests/unit",
"./tests/integration",
]

[tool.ruff]
line-length = 150
1 change: 1 addition & 0 deletions unicorn_contracts/src/contracts_service/contract_status.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

from enum import Enum

class ContractStatus(Enum):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

import json
import os
import uuid
Expand Down
1 change: 1 addition & 0 deletions unicorn_contracts/src/contracts_service/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

import json

class ContractNotFoundException(Exception):
Expand Down
9 changes: 5 additions & 4 deletions unicorn_contracts/src/contracts_service/helper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

import os
import json
from datetime import datetime
Expand All @@ -8,7 +9,7 @@
from aws_lambda_powertools.tracing import Tracer
from aws_lambda_powertools.logging import Logger

from contracts_service.exceptions import EventValidationException, EventValidationException
from contracts_service.exceptions import EventValidationException

# Initialise Environment variables
if (SERVICE_NAMESPACE := os.environ.get("SERVICE_NAMESPACE")) is None:
Expand Down Expand Up @@ -76,16 +77,16 @@ def get_event_body(event):
try:
event_json = json.loads(event_body)
except json.decoder.JSONDecodeError as e:
logger.fatal("This event input is not a valid JSON")
logger.critical("This event input is not a valid JSON")
raise e
except TypeError as e:
logger.fatal("This event input is not a valid JSON")
logger.critical("This event input is not a valid JSON")
raise e

# Check if event body contains data, otherwise log & raise exception
if not event_json:
msg = "This event input did not contain body payload."
logger.fatal(msg)
logger.critical(msg)
raise EventValidationException(msg)

return event_json
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

import json
import os

Expand Down
2 changes: 1 addition & 1 deletion unicorn_contracts/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Globals:
Api:
OpenApiVersion: 3.0.1
Function:
Runtime: python3.10
Runtime: python3.11
MemorySize: 128
Timeout: 3
Tracing: Active
Expand Down
6 changes: 4 additions & 2 deletions unicorn_contracts/tests/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

import pytest
import boto3
import os

import boto3

import pytest
from moto import mock_dynamodb, mock_events


Expand Down
19 changes: 15 additions & 4 deletions unicorn_contracts/tests/unit/helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import os, inspect, json
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

TABLE_NAME = "table1"
import os
import inspect
import json

TABLE_NAME = 'table1'
EVENTBUS_NAME = 'test-eventbridge'


def load_event(filename):
Expand Down Expand Up @@ -53,7 +59,7 @@ def create_ddb_table_contracts(dynamodb):
'WriteCapacityUnits':1
}
)
table.meta.client.get_waiter('table_exists').wait(TableName='table1')
table.meta.client.get_waiter('table_exists').wait(TableName=TABLE_NAME)
return table


Expand All @@ -77,7 +83,7 @@ def create_ddb_table_contracts_with_entry(dynamodb):
'WriteCapacityUnits':1
}
)
table.meta.client.get_waiter('table_exists').wait(TableName='table1')
table.meta.client.get_waiter('table_exists').wait(TableName=TABLE_NAME)
contract = {
"property_id": "usa/anytown/main-street/123", # PK
"contact_created": "01/08/2022 20:36:30",
Expand All @@ -94,3 +100,8 @@ def create_ddb_table_contracts_with_entry(dynamodb):
}
table.put_item(Item=contract)
return table


def create_test_eventbridge_bus(eventbridge):
bus = eventbridge.create_event_bus(Name=EVENTBUS_NAME)
return bus
14 changes: 6 additions & 8 deletions unicorn_contracts/tests/unit/test_create_contract_function.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

import json
import os
import pytest
from unittest import mock
import json
from importlib import reload

import pytest
from unittest import mock
from botocore.exceptions import ClientError

from .lambda_context import LambdaContext
from .helper import load_event, return_env_vars_dict, create_ddb_table_contracts
from .helper import load_event, return_env_vars_dict, create_ddb_table_contracts, create_test_eventbridge_bus


@mock.patch.dict(os.environ, return_env_vars_dict(), clear=True)
Expand All @@ -24,6 +24,7 @@ def test_valid_event(dynamodb, eventbridge, mocker):
reload(create_contract_function)

create_ddb_table_contracts(dynamodb)
create_test_eventbridge_bus(eventbridge)

context = LambdaContext()
ret = create_contract_function.lambda_handler(apigw_event, context)
Expand Down Expand Up @@ -87,7 +88,6 @@ def test_wrong_event_data(dynamodb, eventbridge, mocker):
@mock.patch.dict(os.environ, return_env_vars_dict(), clear=True)
def test_missing_ddb_env_var(dynamodb, eventbridge, mocker):
del os.environ['DYNAMODB_TABLE']
apigw_event = load_event('events/create_valid_event.json')
# Loading function here so that mocking works correctly
from contracts_service import create_contract_function
with pytest.raises(EnvironmentError):
Expand All @@ -97,7 +97,6 @@ def test_missing_ddb_env_var(dynamodb, eventbridge, mocker):
@mock.patch.dict(os.environ, return_env_vars_dict(), clear=True)
def test_missing_eb_env_var(dynamodb, eventbridge, mocker):
del os.environ['EVENT_BUS']
apigw_event = load_event('events/create_valid_event.json')
# Loading function here so that mocking works correctly
from contracts_service import helper
with pytest.raises(EnvironmentError):
Expand All @@ -107,7 +106,6 @@ def test_missing_eb_env_var(dynamodb, eventbridge, mocker):
@mock.patch.dict(os.environ, return_env_vars_dict(), clear=True)
def test_missing_sm_env_var(dynamodb, eventbridge, mocker):
del os.environ['SERVICE_NAMESPACE']
apigw_event = load_event('events/create_valid_event.json')
# Loading function here so that mocking works correctly
from contracts_service import helper
with pytest.raises(EnvironmentError):
Expand All @@ -123,4 +121,4 @@ def test_wrong_dynamodb_table(dynamodb, eventbridge, mocker):

context = LambdaContext()
with pytest.raises(ClientError):
ret = create_contract_function.lambda_handler(apigw_event, context)
create_contract_function.lambda_handler(apigw_event, context)
21 changes: 14 additions & 7 deletions unicorn_contracts/tests/unit/test_helper.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import json
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

import os
import json
from importlib import reload

import pytest
from unittest import mock
from importlib import reload
from moto import mock_dynamodb, mock_events
# from moto import mock_dynamodb, mock_events

from .helper import return_env_vars_dict
# from contracts_service.exceptions import EventValidationException

from .helper import return_env_vars_dict, create_test_eventbridge_bus
from .lambda_context import LambdaContext
from contracts_service.exceptions import EventValidationException


@mock.patch.dict(os.environ, return_env_vars_dict(), clear=True)
Expand All @@ -28,6 +33,8 @@ def test_push_event(dynamodb, eventbridge, mocker):
from contracts_service import helper
reload(helper)

create_test_eventbridge_bus(eventbridge)

ret = helper.publish_event(contract, context.aws_request_id)
assert ret['FailedEntryCount'] == 0
assert len(ret['Entries']) == 1
Expand Down Expand Up @@ -62,7 +69,7 @@ def test_get_event_body_bad_json(dynamodb, eventbridge, mocker):
reload(create_contract_function)

with pytest.raises(json.decoder.JSONDecodeError):
ret = create_contract_function.get_event_body(event)
create_contract_function.get_event_body(event)


@mock.patch.dict(os.environ, return_env_vars_dict(), clear=True)
Expand All @@ -76,4 +83,4 @@ def test_get_event_body_bad_type(dynamodb, eventbridge, mocker):
reload(create_contract_function)

with pytest.raises(TypeError):
ret = create_contract_function.get_event_body(event)
create_contract_function.get_event_body(event)
22 changes: 13 additions & 9 deletions unicorn_contracts/tests/unit/test_update_contract_function.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0

import json
import os
import json
from importlib import reload

import pytest
from unittest import mock
from importlib import reload
from moto import mock_dynamodb, mock_events
# from moto import mock_dynamodb, mock_events
# from botocore.exceptions import ClientError

from botocore.exceptions import ClientError
# from contracts_service.exceptions import EventValidationException

from .lambda_context import LambdaContext
from .helper import load_event, return_env_vars_dict, create_ddb_table_contracts, create_ddb_table_contracts_with_entry
from contracts_service.exceptions import EventValidationException
from .helper import load_event, return_env_vars_dict, create_ddb_table_contracts, create_ddb_table_contracts_with_entry, create_test_eventbridge_bus


@mock.patch.dict(os.environ, return_env_vars_dict(), clear=True)
def test_valid_event(dynamodb, eventbridge, mocker):
apigw_event = load_event('events/update_valid_event.json')
# Loading function here so that mocking works correctly
from contracts_service import update_contract_function
reload(update_contract_function)

create_ddb_table_contracts_with_entry(dynamodb)
create_test_eventbridge_bus(eventbridge)

context = LambdaContext()
ret = update_contract_function.lambda_handler(apigw_event, context)
Expand Down Expand Up @@ -84,7 +88,7 @@ def test_wrong_event_data(dynamodb, eventbridge, mocker):
@mock.patch.dict(os.environ, return_env_vars_dict(), clear=True)
def test_missing_ddb_env_var(dynamodb, eventbridge, mocker):
del os.environ['DYNAMODB_TABLE']
apigw_event = load_event('events/update_valid_event.json')
load_event('events/update_valid_event.json')
# Loading function here so that mocking works correctly
from contracts_service import update_contract_function
with pytest.raises(EnvironmentError):
Expand All @@ -94,7 +98,7 @@ def test_missing_ddb_env_var(dynamodb, eventbridge, mocker):
@mock.patch.dict(os.environ, return_env_vars_dict(), clear=True)
def test_missing_eb_env_var(dynamodb, eventbridge, mocker):
del os.environ['EVENT_BUS']
apigw_event = load_event('events/update_valid_event.json')
load_event('events/update_valid_event.json')
# Loading function here so that mocking works correctly
from contracts_service import helper
with pytest.raises(EnvironmentError):
Expand All @@ -104,7 +108,7 @@ def test_missing_eb_env_var(dynamodb, eventbridge, mocker):
@mock.patch.dict(os.environ, return_env_vars_dict(), clear=True)
def test_missing_sm_env_var(dynamodb, eventbridge, mocker):
del os.environ['SERVICE_NAMESPACE']
apigw_event = load_event('events/update_valid_event.json')
load_event('events/update_valid_event.json')
# Loading function here so that mocking works correctly
from contracts_service import helper
with pytest.raises(EnvironmentError):
Expand Down
4 changes: 2 additions & 2 deletions unicorn_properties/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ stackName := uni-prop-local-properties
build:
cfn-lint template.yaml -a cfn_lint_serverless.rules
poetry export --without-hashes --format=requirements.txt --output=src/requirements.txt
sam build -c
sam build -c $(DOCKER_OPTS)

deps:
poetry install
Expand All @@ -16,7 +16,7 @@ sync:

test: unit-test

unit-test: deps
unit-test:
poetry run pytest tests/unit/

logs:
Expand Down
Loading

0 comments on commit 3379647

Please sign in to comment.