Skip to content

Commit

Permalink
Refactor tests with fixtures
Browse files Browse the repository at this point in the history
* Add and update mocked fixtures for dynamodb, s3, ses, and sqs
* Update the affected unit and CLI tests
* Reorder fixtures into more logical order

* https://mitlibraries.atlassian.net/browse/DLSPP-130
  • Loading branch information
ehanson8 committed Jan 24, 2022
1 parent 2d2f7b9 commit 3eb7328
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 289 deletions.
115 changes: 77 additions & 38 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
import pytest
import requests_mock
from click.testing import CliRunner
from moto import mock_iam, mock_s3, mock_ssm
from moto import mock_dynamodb2, mock_iam, mock_s3, mock_ses, mock_sqs, mock_ssm

from awd.dynamodb import DynamoDB
from awd.s3 import S3
from awd.ses import SES
from awd.sqs import SQS
from awd.ssm import SSM


@pytest.fixture(scope="function")
def runner():
return CliRunner()


@pytest.fixture(scope="function")
Expand Down Expand Up @@ -58,16 +64,13 @@ def test_aws_user(aws_credentials):


@pytest.fixture(scope="function")
def s3_mock(aws_credentials):
with mock_s3():
s3 = boto3.client("s3", region_name="us-east-1")
s3.create_bucket(Bucket="awd")
yield s3
def dynamodb_class():
return DynamoDB()


@pytest.fixture(scope="function")
def dynamodb_class():
return DynamoDB()
def s3_class():
return S3()


@pytest.fixture(scope="function")
Expand All @@ -81,12 +84,70 @@ def sqs_class():


@pytest.fixture(scope="function")
def s3_class():
return S3()
def ssm_class():
return SSM()


@pytest.fixture(scope="function")
def mocked_dynamodb(aws_credentials):
with mock_dynamodb2():
dynamodb = boto3.client("dynamodb", region_name="us-east-1")
dynamodb.create_table(
TableName="test_dois",
KeySchema=[
{"AttributeName": "doi", "KeyType": "HASH"},
],
AttributeDefinitions=[
{"AttributeName": "doi", "AttributeType": "S"},
],
)
yield dynamodb


@pytest.fixture(scope="function")
def mocked_s3(aws_credentials):
with mock_s3():
s3 = boto3.client("s3", region_name="us-east-1")
s3.create_bucket(Bucket="awd")
yield s3


@pytest.fixture(scope="function")
def mocked_ses(aws_credentials):
with mock_ses():
ses = boto3.client("ses", region_name="us-east-1")
ses.verify_email_identity(EmailAddress="noreply@example.com")
yield ses


@pytest.fixture(scope="function")
def mocked_sqs(aws_credentials):
with mock_sqs():
sqs = boto3.resource("sqs", region_name="us-east-1")
sqs.create_queue(QueueName="mock-input-queue")
sqs.create_queue(QueueName="mock-output-queue")
yield sqs


@pytest.fixture(scope="function")
def mocked_ssm(aws_credentials):
with mock_ssm():
ssm = boto3.client("ssm", region_name="us-east-1")
ssm.put_parameter(
Name="/test/example/collection_handle",
Value="111.1/111",
Type="SecureString",
)
ssm.put_parameter(
Name="/test/example/secure",
Value="true",
Type="SecureString",
)
yield ssm


@pytest.fixture()
def web_mock(crossref_work_record, wiley_pdf):
def mocked_web(crossref_work_record, wiley_pdf):
with requests_mock.Mocker() as m:
request_headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
Expand Down Expand Up @@ -134,25 +195,13 @@ def crossref_value_dict():


@pytest.fixture()
def dspace_metadata():
return json.loads(open("tests/fixtures/dspace_metadata.json", "r").read())
def wiley_pdf():
return open("tests/fixtures/wiley.pdf", "rb").read()


@pytest.fixture(scope="function")
def mocked_ssm(aws_credentials):
with mock_ssm():
ssm = boto3.client("ssm", region_name="us-east-1")
ssm.put_parameter(
Name="/test/example/collection_handle",
Value="111.1/111",
Type="SecureString",
)
ssm.put_parameter(
Name="/test/example/secure",
Value="true",
Type="SecureString",
)
yield ssm
@pytest.fixture()
def dspace_metadata():
return json.loads(open("tests/fixtures/dspace_metadata.json", "r").read())


@pytest.fixture()
Expand Down Expand Up @@ -205,11 +254,6 @@ def result_success_message_body():
return result_success_message_body


@pytest.fixture(scope="function")
def runner():
return CliRunner()


@pytest.fixture()
def submission_message_attributes():
submission_message_attributes = {
Expand All @@ -235,8 +279,3 @@ def submission_message_body():
],
}
return json.dumps(submission_message_body)


@pytest.fixture()
def wiley_pdf():
return open("tests/fixtures/wiley.pdf", "rb").read()
139 changes: 48 additions & 91 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import logging

import boto3
from moto import mock_dynamodb2, mock_ses, mock_sqs

from awd.cli import cli, doi_to_be_added, doi_to_be_retried
from awd.status import Status

Expand Down Expand Up @@ -33,32 +30,24 @@ def test_doi_to_be_retried_false():
assert validation_status is False


@mock_dynamodb2
@mock_ses
@mock_sqs
def test_deposit_success(
caplog, web_mock, s3_mock, s3_class, sqs_class, submission_message_body, runner
caplog,
mocked_web,
mocked_dynamodb,
mocked_s3,
mocked_ses,
mocked_sqs,
s3_class,
sqs_class,
submission_message_body,
runner,
):
with caplog.at_level(logging.DEBUG):
s3_class.put_file(
open("tests/fixtures/doi_success.csv", "rb"),
"awd",
"doi_success.csv",
)
sqs = boto3.resource("sqs", region_name="us-east-1")
sqs.create_queue(QueueName="mock-input-queue")
ses_client = boto3.client("ses", region_name="us-east-1")
ses_client.verify_email_identity(EmailAddress="noreply@example.com")
dynamodb = boto3.client("dynamodb", region_name="us-east-1")
dynamodb.create_table(
TableName="test_dois",
KeySchema=[
{"AttributeName": "doi", "KeyType": "HASH"},
],
AttributeDefinitions=[
{"AttributeName": "doi", "AttributeType": "S"},
],
)
assert len(s3_class.client.list_objects(Bucket="awd")["Contents"]) == 1
result = runner.invoke(
cli,
Expand Down Expand Up @@ -105,27 +94,22 @@ def test_deposit_success(
assert "Logs sent to" in caplog.text


@mock_dynamodb2
@mock_ses
def test_deposit_insufficient_metadata(caplog, web_mock, s3_mock, s3_class, runner):
def test_deposit_insufficient_metadata(
caplog,
mocked_web,
mocked_dynamodb,
mocked_s3,
mocked_ses,
mocked_sqs,
s3_class,
runner,
):
with caplog.at_level(logging.DEBUG):
s3_class.put_file(
open("tests/fixtures/doi_insufficient_metadata.csv", "rb"),
"awd",
"doi_insufficient_metadata.csv",
)
ses_client = boto3.client("ses", region_name="us-east-1")
ses_client.verify_email_identity(EmailAddress="noreply@example.com")
dynamodb = boto3.client("dynamodb", region_name="us-east-1")
dynamodb.create_table(
TableName="test_dois",
KeySchema=[
{"AttributeName": "doi", "KeyType": "HASH"},
],
AttributeDefinitions=[
{"AttributeName": "doi", "AttributeType": "S"},
],
)
result = runner.invoke(
cli,
[
Expand Down Expand Up @@ -162,27 +146,22 @@ def test_deposit_insufficient_metadata(caplog, web_mock, s3_mock, s3_class, runn
assert "Logs sent to" in caplog.text


@mock_dynamodb2
@mock_ses
def test_deposit_pdf_unavailable(caplog, web_mock, s3_mock, s3_class, runner):
def test_deposit_pdf_unavailable(
caplog,
mocked_web,
mocked_dynamodb,
mocked_s3,
mocked_ses,
mocked_sqs,
s3_class,
runner,
):
with caplog.at_level(logging.DEBUG):
s3_class.put_file(
open("tests/fixtures/doi_pdf_unavailable.csv", "rb"),
"awd",
"doi_pdf_unavailable.csv",
)
ses_client = boto3.client("ses", region_name="us-east-1")
ses_client.verify_email_identity(EmailAddress="noreply@example.com")
dynamodb = boto3.client("dynamodb", region_name="us-east-1")
dynamodb.create_table(
TableName="test_dois",
KeySchema=[
{"AttributeName": "doi", "KeyType": "HASH"},
],
AttributeDefinitions=[
{"AttributeName": "doi", "AttributeType": "S"},
],
)
result = runner.invoke(
cli,
[
Expand Down Expand Up @@ -216,22 +195,17 @@ def test_deposit_pdf_unavailable(caplog, web_mock, s3_mock, s3_class, runner):
assert "Logs sent to" in caplog.text


@mock_dynamodb2
@mock_ses
def test_deposit_s3_nonexistent_bucket(caplog, web_mock, s3_mock, s3_class, runner):
def test_deposit_s3_nonexistent_bucket(
caplog,
mocked_web,
mocked_dynamodb,
mocked_s3,
mocked_ses,
mocked_sqs,
s3_class,
runner,
):
with caplog.at_level(logging.DEBUG):
ses_client = boto3.client("ses", region_name="us-east-1")
ses_client.verify_email_identity(EmailAddress="noreply@example.com")
dynamodb = boto3.client("dynamodb", region_name="us-east-1")
dynamodb.create_table(
TableName="test_dois",
KeySchema=[
{"AttributeName": "doi", "KeyType": "HASH"},
],
AttributeDefinitions=[
{"AttributeName": "doi", "AttributeType": "S"},
],
)
result = runner.invoke(
cli,
[
Expand Down Expand Up @@ -264,11 +238,13 @@ def test_deposit_s3_nonexistent_bucket(caplog, web_mock, s3_mock, s3_class, runn
) in caplog.text


@mock_dynamodb2
@mock_ses
@mock_sqs
def test_listen_success(
caplog,
mocked_dynamodb,
mocked_s3,
mocked_ses,
mocked_sqs,
dynamodb_class,
sqs_class,
result_failure_message_attributes,
result_success_message_attributes,
Expand All @@ -277,10 +253,6 @@ def test_listen_success(
runner,
):
with caplog.at_level(logging.DEBUG):
sqs = boto3.resource("sqs", region_name="us-east-1")
sqs.create_queue(QueueName="mock-output-queue")
ses_client = boto3.client("ses", region_name="us-east-1")
ses_client.verify_email_identity(EmailAddress="noreply@example.com")
sqs_class.send(
"https://queue.amazonaws.com/123456789012/",
"mock-output-queue",
Expand All @@ -293,25 +265,15 @@ def test_listen_success(
result_success_message_attributes,
result_success_message_body,
)
dynamodb = boto3.client("dynamodb", region_name="us-east-1")
dynamodb.create_table(
TableName="test_dois",
KeySchema=[
{"AttributeName": "doi", "KeyType": "HASH"},
],
AttributeDefinitions=[
{"AttributeName": "doi", "AttributeType": "S"},
],
)
dynamodb.put_item(
dynamodb_class.client.put_item(
TableName="test_dois",
Item={
"doi": {"S": "111.1/1111"},
"status": {"S": str(Status.PROCESSING.value)},
"attempts": {"S": "1"},
},
)
dynamodb.put_item(
dynamodb_class.client.put_item(
TableName="test_dois",
Item={
"doi": {"S": "222.2/2222"},
Expand Down Expand Up @@ -348,13 +310,8 @@ def test_listen_success(
assert "Logs sent to" in caplog.text


@mock_dynamodb2
@mock_ses
@mock_sqs
def test_listen_failure(caplog, runner):
def test_listen_failure(caplog, mocked_ses, runner):
with caplog.at_level(logging.DEBUG):
ses_client = boto3.client("ses", region_name="us-east-1")
ses_client.verify_email_identity(EmailAddress="noreply@example.com")
result = runner.invoke(
cli,
[
Expand Down
Loading

0 comments on commit 3eb7328

Please sign in to comment.