Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unit tests #6

Merged
merged 3 commits into from
May 7, 2024
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
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ max-line-length = 120
extend-ignore = W503
inline-quotes = double

exclude = .venv
exclude = .venv, .tox
19 changes: 19 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Test

on:
pull_request:

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install "tox~=4.4"
- run: tox
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
pythonpath = src
2 changes: 1 addition & 1 deletion src/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cumulus-message-adapter==2.0.3
cumulus-message-adapter-python==2.2.0
jsonpath-ng==1.4.2
jsonschema==2.6.0
jsonschema==4.17.3
mandible @ git+https://github.com/asfadmin/mandible@v0.4.0
ply==3.11
108 changes: 108 additions & 0 deletions tests/test_granule_to_sns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import boto3
import pytest

from granule_to_sns import generate_message, granule_to_sns


@pytest.fixture
def event():
return {
"input": {
"granules": [
{
"granuleId": "foo",
"dataType": "bar",
"version": "1.0",
"provider": "ASFDAAC",
"files": [
{
"size": 162946863,
"bucket": "data-bucket",
"key": "foo.h5",
"source": "foo.h5",
"fileName": "foo.h5", "type": "data",
"checksumType": "md5", "checksum": "a88dda7180fc2738fe42af65721577d6",
},
{
"size": 32,
"bucket": "data-bucket",
"key": "foo.h5.md5",
"source": "foo.h5.md5",
"fileName": "foo.h5.md5",
"type": "metadata", "checksumType": "md5", "checksum": "228d1f939face34465b26fbf4056898f",
},
{
"size": 200178,
"bucket": "data-bucket",
"key": "foo.iso.xml",
"source": "foo.iso.xml",
"fileName": "foo.iso.xml",
"type": "metadata", "checksumType": "md5", "checksum": "b8d18182e9ec0c781430babcdeafaea9",
},
{
"size": 32,
"bucket": "data-bucket",
"key": "foo.iso.xml.md5",
"source": "foo.iso.xml.md5",
"fileName": "foo.iso.xml.md5",
"type": "metadata", "checksumType": "md5", "checksum": "aef479deb833175bbfcfb048a0f4d06e",
},
{
"bucket": "data-bucket",
"key": "UMMG/foo.cmr.json",
"fileName": "foo.cmr.json",
},
],
},
],
},
}


@pytest.fixture
def message():
return {
"identifier": "foo",
"collection": "bar",
"product": {
"name": "foo",
"files": [
{
"name": "foo.h5",
"uri": "s3://data-bucket/foo.h5",
},
{
"name": "foo.h5.md5",
"uri": "s3://data-bucket/foo.h5.md5",
},
{
"name": "foo.iso.xml",
"uri": "s3://data-bucket/foo.iso.xml",
},
{
"name": "foo.iso.xml.md5",
"uri": "s3://data-bucket/foo.iso.xml.md5",
},
{
"name": "foo.cmr.json",
"uri": "s3://data-bucket/UMMG/foo.cmr.json",
},
],
},
}


@pytest.fixture
def sns_client(mocker):
sns_client = boto3.client("sns", region_name="us-west-2")
mocker.patch("boto3.client", return_value=sns_client)
return sns_client
mckadesorensen marked this conversation as resolved.
Show resolved Hide resolved


def test_generate_message(event, message):
assert generate_message(event["input"]["granules"][0]) == message


def test_granule_to_sns(sns_client, event, mocker):
mocker.patch.object(sns_client, "publish", return_value={})
assert granule_to_sns(event, None) == event
11 changes: 11 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[tox]
env_list = py310
minversion = 4.4.6

[testenv]
deps =
pytest>=7
pytest-mock>=3.14.0
-r src/requirements.txt
commands =
pytest {tty:--color=yes} {posargs}
Loading