Skip to content

Commit

Permalink
Convert fixtures/exceptions to Pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
bblommers committed Nov 11, 2020
1 parent 6d364dc commit cb6731f
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 170 deletions.
1 change: 0 additions & 1 deletion requirements-tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ pytest
pytest-cov
sure==1.4.11
freezegun
parameterized>=0.7.0
86 changes: 44 additions & 42 deletions tests/test_acm/test_acm.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,8 @@ def test_operations_with_invalid_tags():
DomainName="example.com",
Tags=[{"Key": "X" * 200, "Value": "Valid"}],
)
ex.exception.response["Error"]["Code"].should.equal("ValidationException")
ex.exception.response["Error"]["Message"].should.contain(
ex.value.response["Error"]["Code"].should.equal("ValidationException")
ex.value.response["Error"]["Message"].should.contain(
"Member must have length less than or equal to 128"
)

Expand All @@ -424,8 +424,8 @@ def test_operations_with_invalid_tags():
],
)

ex.exception.response["Error"]["Code"].should.equal("ValidationException")
ex.exception.response["Error"]["Message"].should.contain(
ex.value.response["Error"]["Code"].should.equal("ValidationException")
ex.value.response["Error"]["Message"].should.contain(
"Member must have length less than or equal to 256"
)

Expand All @@ -437,8 +437,8 @@ def test_operations_with_invalid_tags():
CertificateArn=arn,
Tags=[{"Key": "aws:xxx", "Value": "Valid"}, {"Key": "key2"}],
)
ex.exception.response["Error"]["Code"].should.equal("ValidationException")
ex.exception.response["Error"]["Message"].should.contain(
ex.value.response["Error"]["Code"].should.equal("ValidationException")
ex.value.response["Error"]["Message"].should.contain(
"AWS internal tags cannot be changed with this API"
)

Expand All @@ -447,8 +447,8 @@ def test_operations_with_invalid_tags():
client.remove_tags_from_certificate(
CertificateArn=arn, Tags=[{"Key": "aws:xxx", "Value": "Valid"}]
)
ex.exception.response["Error"]["Code"].should.equal("ValidationException")
ex.exception.response["Error"]["Message"].should.contain(
ex.value.response["Error"]["Code"].should.equal("ValidationException")
ex.value.response["Error"]["Message"].should.contain(
"AWS internal tags cannot be changed with this API"
)

Expand All @@ -464,8 +464,8 @@ def test_add_too_many_tags():
CertificateArn=arn,
Tags=[{"Key": "a-%d" % i, "Value": "abcd"} for i in range(1, 52)],
)
ex.exception.response["Error"]["Code"].should.equal("TooManyTagsException")
ex.exception.response["Error"]["Message"].should.contain("contains too many Tags")
ex.value.response["Error"]["Code"].should.equal("TooManyTagsException")
ex.value.response["Error"]["Message"].should.contain("contains too many Tags")
client.list_tags_for_certificate(CertificateArn=arn)["Tags"].should.have.empty

# Add 49 tags first, then try to add 2 more.
Expand All @@ -481,10 +481,10 @@ def test_add_too_many_tags():
CertificateArn=arn,
Tags=[{"Key": "x-1", "Value": "xyz"}, {"Key": "x-2", "Value": "xyz"}],
)
ex.exception.response["Error"]["Code"].should.equal("TooManyTagsException")
ex.exception.response["Error"]["Message"].should.contain("contains too many Tags")
ex.exception.response["Error"]["Message"].count("pqrs").should.equal(49)
ex.exception.response["Error"]["Message"].count("xyz").should.equal(2)
ex.value.response["Error"]["Code"].should.equal("TooManyTagsException")
ex.value.response["Error"]["Message"].should.contain("contains too many Tags")
ex.value.response["Error"]["Message"].count("pqrs").should.equal(49)
ex.value.response["Error"]["Message"].count("xyz").should.equal(2)
client.list_tags_for_certificate(CertificateArn=arn)["Tags"].should.have.length_of(
49
)
Expand All @@ -502,20 +502,21 @@ def test_request_certificate_no_san():


# Also tests the SAN code
@freeze_time("2012-01-01 12:00:00", as_arg=True)
@mock_acm
def test_request_certificate_issued_status(frozen_time):
def test_request_certificate_issued_status():
# After requesting a certificate, it should then auto-validate after 1 minute
# Some sneaky programming for that ;-)
client = boto3.client("acm", region_name="eu-central-1")

resp = client.request_certificate(
DomainName="google.com",
SubjectAlternativeNames=["google.com", "www.google.com", "mail.google.com"],
)
with freeze_time("2012-01-01 12:00:00"):
resp = client.request_certificate(
DomainName="google.com",
SubjectAlternativeNames=["google.com", "www.google.com", "mail.google.com"],
)
arn = resp["CertificateArn"]

resp = client.describe_certificate(CertificateArn=arn)
with freeze_time("2012-01-01 12:00:00"):
resp = client.describe_certificate(CertificateArn=arn)
resp["Certificate"]["CertificateArn"].should.equal(arn)
resp["Certificate"]["DomainName"].should.equal("google.com")
resp["Certificate"]["Issuer"].should.equal("Amazon")
Expand All @@ -525,33 +526,34 @@ def test_request_certificate_issued_status(frozen_time):
len(resp["Certificate"]["SubjectAlternativeNames"]).should.equal(3)

# validation will be pending for 1 minute.
resp = client.describe_certificate(CertificateArn=arn)
with freeze_time("2012-01-01 12:00:00"):
resp = client.describe_certificate(CertificateArn=arn)
resp["Certificate"]["CertificateArn"].should.equal(arn)
resp["Certificate"]["Status"].should.equal("PENDING_VALIDATION")

if not settings.TEST_SERVER_MODE:
# Move time to get it issued.
frozen_time.move_to("2012-01-01 12:02:00")
resp = client.describe_certificate(CertificateArn=arn)
with freeze_time("2012-01-01 12:02:00"):
resp = client.describe_certificate(CertificateArn=arn)
resp["Certificate"]["CertificateArn"].should.equal(arn)
resp["Certificate"]["Status"].should.equal("ISSUED")


@freeze_time("2012-01-01 12:00:00", as_arg=True)
@mock_acm
def test_request_certificate_with_mutiple_times(frozen_time):
def test_request_certificate_with_mutiple_times():
if settings.TEST_SERVER_MODE:
raise SkipTest("Cant manipulate time in server mode")

# After requesting a certificate, it should then auto-validate after 1 minute
# Some sneaky programming for that ;-)
client = boto3.client("acm", region_name="eu-central-1")

resp = client.request_certificate(
IdempotencyToken="test_token",
DomainName="google.com",
SubjectAlternativeNames=["google.com", "www.google.com", "mail.google.com"],
)
with freeze_time("2012-01-01 12:00:00"):
resp = client.request_certificate(
IdempotencyToken="test_token",
DomainName="google.com",
SubjectAlternativeNames=["google.com", "www.google.com", "mail.google.com"],
)
original_arn = resp["CertificateArn"]

# Should be able to request a certificate multiple times in an hour
Expand All @@ -561,21 +563,21 @@ def test_request_certificate_with_mutiple_times(frozen_time):
"2012-01-01 12:30:00",
"2012-01-01 12:45:00",
):
frozen_time.move_to(time_intervals)
with freeze_time(time_intervals):
resp = client.request_certificate(
IdempotencyToken="test_token",
DomainName="google.com",
SubjectAlternativeNames=["google.com", "www.google.com", "mail.google.com"],
)
arn = resp["CertificateArn"]
arn.should.equal(original_arn)

# Move time
with freeze_time("2012-01-01 13:01:00"):
resp = client.request_certificate(
IdempotencyToken="test_token",
DomainName="google.com",
SubjectAlternativeNames=["google.com", "www.google.com", "mail.google.com"],
)
arn = resp["CertificateArn"]
arn.should.equal(original_arn)

# Move time
frozen_time.move_to("2012-01-01 13:01:00")
resp = client.request_certificate(
IdempotencyToken="test_token",
DomainName="google.com",
SubjectAlternativeNames=["google.com", "www.google.com", "mail.google.com"],
)
arn = resp["CertificateArn"]
arn.should_not.equal(original_arn)
3 changes: 1 addition & 2 deletions tests/test_applicationautoscaling/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pytest
import sure # noqa
from botocore.exceptions import ClientError
from parameterized import parameterized
from .test_applicationautoscaling import register_scalable_target

DEFAULT_REGION = "us-east-1"
Expand Down Expand Up @@ -106,7 +105,7 @@ def test_register_scalable_target_ecs_with_non_existent_service_should_return_va
err.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400)


@parameterized(
@pytest.mark.parametrize("namespace,r_id,dimension,expected",
[
("ecs", "service/default/test-svc", "ecs:service:DesiredCount", True),
("ecs", "banana/default/test-svc", "ecs:service:DesiredCount", False),
Expand Down
3 changes: 2 additions & 1 deletion tests/test_batch/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,8 @@ def test_submit_job():
else:
raise RuntimeError("Batch job timed out")

resp = logs_client.describe_log_streams(logGroupName="/aws/batch/job")
resp = logs_client.describe_log_streams(logGroupName="/aws/batch/job",
logStreamNamePrefix="sayhellotomylittlefriend")
len(resp["logStreams"]).should.equal(1)
ls_name = resp["logStreams"][0]["logStreamName"]

Expand Down
5 changes: 3 additions & 2 deletions tests/test_dynamodb2/test_dynamodb_executor.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import pytest

from moto.dynamodb2.exceptions import IncorrectOperandType, IncorrectDataType
from moto.dynamodb2.models import Item, DynamoType
from moto.dynamodb2.parsing.executors import UpdateExpressionExecutor
from moto.dynamodb2.parsing.expressions import UpdateExpressionParser
from moto.dynamodb2.parsing.validators import UpdateExpressionValidator
from parameterized import parameterized


def test_execution_of_if_not_exists_not_existing_value():
Expand Down Expand Up @@ -405,7 +406,7 @@ def test_execution_of_add_to_a_set():
assert expected_item == item


@parameterized(
@pytest.mark.parametrize("expression_attribute_values,unexpected_data_type",
[
(
{":value": {"S": "10"}},
Expand Down
8 changes: 4 additions & 4 deletions tests/test_dynamodb2/test_dynamodb_validation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from moto.dynamodb2.exceptions import (
AttributeIsReservedKeyword,
ExpressionAttributeValueNotDefined,
Expand All @@ -10,12 +12,10 @@
from moto.dynamodb2.parsing.ast_nodes import (
NodeDepthLeftTypeFetcher,
UpdateExpressionSetAction,
UpdateExpressionValue,
DDBTypedValue,
)
from moto.dynamodb2.parsing.expressions import UpdateExpressionParser
from moto.dynamodb2.parsing.validators import UpdateExpressionValidator
from parameterized import parameterized


def test_validation_of_update_expression_with_keyword():
Expand All @@ -41,7 +41,7 @@ def test_validation_of_update_expression_with_keyword():
assert e.keyword == "path"


@parameterized(
@pytest.mark.parametrize("update_expression",
[
"SET a = #b + :val2",
"SET a = :val2 + #b",
Expand Down Expand Up @@ -101,7 +101,7 @@ def test_validation_of_update_expression_with_attribute_that_does_not_exist_in_i
assert True


@parameterized(
@pytest.mark.parametrize("update_expression",
[
"SET a = #c",
"SET a = #c + #d",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ec2/test_subnets.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def test_create_subnet_with_invalid_cidr_range_multiple_vpc_cidr_blocks():
subnet_cidr_block = "10.2.0.0/20"
with pytest.raises(ClientError) as ex:
subnet = ec2.create_subnet(VpcId=vpc.id, CidrBlock=subnet_cidr_block)
str(ex.exception).should.equal(
str(ex.value).should.equal(
"An error occurred (InvalidSubnet.Range) when calling the CreateSubnet "
"operation: The CIDR '{}' is invalid.".format(subnet_cidr_block)
)
Expand Down
8 changes: 3 additions & 5 deletions tests/test_emr/test_emr_boto3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from datetime import datetime

import boto3
import json
import pytz
import six
import sure # noqa
Expand Down Expand Up @@ -803,11 +804,8 @@ def test_instance_groups():
x["AutoScalingPolicy"]["Status"]["State"].should.equal("ATTACHED")
returned_policy = dict(x["AutoScalingPolicy"])
del returned_policy["Status"]
for dimension in y["AutoScalingPolicy"]["Rules"]["Trigger"][
"CloudWatchAlarmDefinition"
]["Dimensions"]:
dimension["Value"] = cluster_id
returned_policy.should.equal(y["AutoScalingPolicy"])
policy = json.loads(json.dumps(y["AutoScalingPolicy"]).replace("${emr.clusterId}", cluster_id))
returned_policy.should.equal(policy)
if "EbsConfiguration" in y:
_do_assertion_ebs_configuration(x, y)
# Configurations
Expand Down
Loading

0 comments on commit cb6731f

Please sign in to comment.