Skip to content

Commit

Permalink
Add lambda tests and ci cd
Browse files Browse the repository at this point in the history
  • Loading branch information
martinng01 committed Jul 31, 2024
1 parent 2bf9230 commit 3a7e623
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 3 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/frontend-cicd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ on:
- main

jobs:
backend-testing:
runs-on: ubuntu-latest
name: Backend Testing
steps:
- name: checkout
uses: actions/checkout@v4

- name: Set up python
uses: actions/setup-python@v5
with:
python-version: "3.11.9"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r infra/lambda/requirements.txt
- name: Run tests
run: |
pytest --cov=infra/lambda
terraform:
name: Terraform Infrastructure
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.coverage
.pytest_cache
1 change: 1 addition & 0 deletions infra/lambda/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
7 changes: 4 additions & 3 deletions infra/lambda/index.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
import boto3

dynamodb = boto3.resource('dynamodb')
dynamodb = boto3.resource('dynamodb', region_name='ap-southeast-1')
table = dynamodb.Table('cloud-resume')


def lambda_handler(event, context):
response = table.get_item(
Key={
Expand All @@ -16,7 +17,7 @@ def lambda_handler(event, context):
Item={
'id': '1',
'views': num_views
}
}
)

return num_views
69 changes: 69 additions & 0 deletions infra/lambda/index_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import os
import pytest
import boto3
from moto import mock_aws


@pytest.fixture(scope="function")
def aws_credentials():
"""
Mocked AWS Credentials for moto.
"""

os.environ["AWS_ACCESS_KEY_ID"] = "testing"
os.environ["AWS_SECRET_ACCESS_KEY"] = "testing"
os.environ["AWS_SECURITY_TOKEN"] = "testing"
os.environ["AWS_SESSION_TOKEN"] = "testing"
os.environ["AWS_DEFAULT_REGION"] = "ap-southeast-1"


@pytest.fixture(scope="function")
def table(aws_credentials):
with mock_aws():
dynamodb = boto3.resource('dynamodb', region_name='ap-southeast-1')
table = dynamodb.create_table(
TableName='cloud-resume',
KeySchema=[
{
'AttributeName': 'id',
'KeyType': 'HASH'
}
],
AttributeDefinitions=[
{
'AttributeName': 'id',
'AttributeType': 'S'
}
],
ProvisionedThroughput={
'ReadCapacityUnits': 1,
'WriteCapacityUnits': 1
}
)

yield table


def test_lambda_increments_viewer_count(table):
"""
Test that the lambda function increments the viewer count by 1
"""

table.put_item(
Item={
'id': '1',
'views': 0
}
)

from index import lambda_handler
response = lambda_handler({}, {})
assert response == 1

# Check that the views have been incremented
response = table.get_item(
Key={
'id': '1'
}
)
assert response['Item']['views'] == 1
27 changes: 27 additions & 0 deletions infra/lambda/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
boto3==1.34.151
botocore==1.34.151
certifi==2024.7.4
cffi==1.16.0
charset-normalizer==3.3.2
coverage==7.6.0
cryptography==43.0.0
idna==3.7
iniconfig==2.0.0
Jinja2==3.1.4
jmespath==1.0.1
MarkupSafe==2.1.5
moto==5.0.11
packaging==24.1
pluggy==1.5.0
pycparser==2.22
pytest==8.3.2
pytest-cov==5.0.0
python-dateutil==2.9.0.post0
PyYAML==6.0.1
requests==2.32.3
responses==0.25.3
s3transfer==0.10.2
six==1.16.0
urllib3==2.2.2
Werkzeug==3.0.3
xmltodict==0.13.0

0 comments on commit 3a7e623

Please sign in to comment.