Techdebt: Replace sure with regular assertions in Utilities (#6660)

This commit is contained in:
kbalk 2023-08-16 04:04:34 -04:00 committed by GitHub
parent b71723b99a
commit b1195f8eb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 78 deletions

View File

@ -1,4 +1,3 @@
import sure # noqa # pylint: disable=unused-import
import pytest import pytest
from moto.utilities.docker_utilities import parse_image_ref from moto.utilities.docker_utilities import parse_image_ref
@ -19,13 +18,11 @@ from moto.utilities.docker_utilities import parse_image_ref
], ],
) )
def test_parse_image_ref(image_name, expected): def test_parse_image_ref(image_name, expected):
expected.should.be.equal(parse_image_ref(image_name)) assert expected == parse_image_ref(image_name)
def test_parse_image_ref_default_container_registry(monkeypatch): def test_parse_image_ref_default_container_registry(monkeypatch):
import moto.settings import moto.settings
monkeypatch.setattr(moto.settings, "DEFAULT_CONTAINER_REGISTRY", "quay.io") monkeypatch.setattr(moto.settings, "DEFAULT_CONTAINER_REGISTRY", "quay.io")
("quay.io/centos/centos", "latest").should.be.equal( assert ("quay.io/centos/centos", "latest") == parse_image_ref("centos/centos")
parse_image_ref("centos/centos")
)

View File

@ -1,7 +1,7 @@
import unittest import unittest
import pytest import pytest
import sure # noqa # pylint: disable=unused-import
from moto.utilities.paginator import Paginator, paginate from moto.utilities.paginator import Paginator, paginate
from moto.core.exceptions import InvalidToken from moto.core.exceptions import InvalidToken
@ -31,11 +31,11 @@ def test_paginator_without_max_results__throws_error():
def test_paginator__paginate_with_just_max_results(): def test_paginator__paginate_with_just_max_results():
p = Paginator(max_results=50) p = Paginator(max_results=50)
resp = p.paginate(results) resp = p.paginate(results)
resp.should.have.length_of(2) assert len(resp) == 2
page, next_token = resp page, next_token = resp
next_token.should.equal(None) assert next_token is None
page.should.equal(results) assert page == results
def test_paginator__paginate_without_range_key__throws_error(): def test_paginator__paginate_without_range_key__throws_error():
@ -53,11 +53,11 @@ def test_paginator__paginate_with_unknown_range_key__throws_error():
def test_paginator__paginate_5(): def test_paginator__paginate_5():
p = Paginator(max_results=5, unique_attribute=["name"]) p = Paginator(max_results=5, unique_attribute=["name"])
resp = p.paginate(results) resp = p.paginate(results)
resp.should.have.length_of(2) assert len(resp) == 2
page, next_token = resp page, next_token = resp
next_token.shouldnt.equal(None) assert next_token is not None
page.should.equal(results[0:5]) assert page == results[0:5]
def test_paginator__paginate_5__use_different_range_keys(): def test_paginator__paginate_5__use_different_range_keys():
@ -67,19 +67,19 @@ def test_paginator__paginate_5__use_different_range_keys():
p = Paginator(max_results=5, unique_attribute=["name"]) p = Paginator(max_results=5, unique_attribute=["name"])
_, token_as_lst = p.paginate(results) _, token_as_lst = p.paginate(results)
token_as_lst.shouldnt.be(None) assert token_as_lst is not None
token_as_lst.should.equal(token_as_str) assert token_as_lst == token_as_str
p = Paginator(max_results=5, unique_attribute=["name", "arn"]) p = Paginator(max_results=5, unique_attribute=["name", "arn"])
_, token_multiple = p.paginate(results) _, token_multiple = p.paginate(results)
token_multiple.shouldnt.be(None) assert token_multiple is not None
token_multiple.shouldnt.equal(token_as_str) assert token_multiple != token_as_str
def test_paginator__paginate_twice(): def test_paginator__paginate_twice():
p = Paginator(max_results=5, unique_attribute=["name"]) p = Paginator(max_results=5, unique_attribute=["name"])
resp = p.paginate(results) resp = p.paginate(results)
resp.should.have.length_of(2) assert len(resp) == 2
page, next_token = resp page, next_token = resp
@ -87,8 +87,8 @@ def test_paginator__paginate_twice():
resp = p.paginate(results) resp = p.paginate(results)
page, next_token = resp page, next_token = resp
next_token.should.equal(None) assert next_token is None
page.should.equal(results[5:]) assert page == results[5:]
def test_paginator__invalid_token(): def test_paginator__invalid_token():
@ -105,8 +105,8 @@ def test_paginator__invalid_token__but_we_just_dont_care():
) )
res, token = p.paginate(results) res, token = p.paginate(results)
res.should.equal([]) assert res == []
token.should.equal(None) assert token is None
class CustomInvalidTokenException(BaseException): class CustomInvalidTokenException(BaseException):
@ -177,7 +177,7 @@ class TestDecorator(unittest.TestCase):
@paginate(pagination_model=PAGINATION_MODEL) # type: ignore[misc] @paginate(pagination_model=PAGINATION_MODEL) # type: ignore[misc]
def method_returning_args(self, *args, **kwargs): def method_returning_args(self, *args, **kwargs):
return [*args] + [(k, v) for k, v in kwargs.items()] return [*args] + list(kwargs.items())
@paginate(pagination_model=PAGINATION_MODEL) # type: ignore[misc] @paginate(pagination_model=PAGINATION_MODEL) # type: ignore[misc]
def method_expecting_token_as_kwarg(self, custom_token=None): def method_expecting_token_as_kwarg(self, custom_token=None):
@ -190,7 +190,9 @@ class TestDecorator(unittest.TestCase):
return [{"name": "item1"}, {"name": "item2"}] return [{"name": "item1"}, {"name": "item2"}]
@paginate(pagination_model=PAGINATION_MODEL) # type: ignore[misc] @paginate(pagination_model=PAGINATION_MODEL) # type: ignore[misc]
def method_with_list_as_kwarg(self, resources=[]): def method_with_list_as_kwarg(self, resources=None):
if not resources:
resources = []
return resources or results return resources or results
@paginate(PAGINATION_MODEL) # type: ignore[misc] @paginate(PAGINATION_MODEL) # type: ignore[misc]
@ -203,26 +205,25 @@ class TestDecorator(unittest.TestCase):
def test__method_returning_dict(self): def test__method_returning_dict(self):
page, token = self.method_returning_dict() page, token = self.method_returning_dict()
page.should.equal(results) assert page == results
token.should.equal(None) assert token is None
def test__method_returning_instances(self): def test__method_returning_instances(self):
page, token = self.method_returning_instances() page, token = self.method_returning_instances()
page.should.equal(model_results[0:10]) assert page == model_results[0:10]
token.shouldnt.equal(None) assert token is not None
def test__method_without_configuration(self): def test__method_without_configuration(self):
with pytest.raises(ValueError): with pytest.raises(ValueError):
self.method_without_configuration() self.method_without_configuration()
def test__input_arguments_are_returned(self): def test__input_arguments_are_returned(self):
resp, token = self.method_returning_args(1, "2", next_token=None, max_results=5) resp, _ = self.method_returning_args(1, "2", next_token=None, max_results=5)
resp.should.have.length_of(4) assert len(resp) == 4
resp.should.contain(1) assert 1 in resp
resp.should.contain("2") assert "2" in resp
resp.should.contain(("next_token", None)) assert ("next_token", None) in resp
resp.should.contain(("max_results", 5)) assert ("max_results", 5) in resp
token.should.equal(None)
def test__pass_exception_on_invalid_token(self): def test__pass_exception_on_invalid_token(self):
# works fine if no token is specified # works fine if no token is specified
@ -233,67 +234,69 @@ class TestDecorator(unittest.TestCase):
self.method_specifying_invalidtoken_exception( self.method_specifying_invalidtoken_exception(
next_token="some invalid token" next_token="some invalid token"
) )
exc.value.should.be.a(CustomInvalidTokenException) assert isinstance(exc.value, CustomInvalidTokenException)
exc.value.message.should.equal("Invalid token: some invalid token") assert exc.value.message == "Invalid token: some invalid token"
def test__pass_generic_exception_on_invalid_token(self): def test__pass_generic_exception_on_invalid_token(self):
# works fine if no token is specified # works fine if no token is specified
self.method_specifying_generic_invalidtoken_exception() self.method_specifying_generic_invalidtoken_exception()
# throws exception if next_token is invalid # throws exception if next_token is invalid
# Exception does not take any arguments - our paginator needs to verify whether the next_token arg is expected # Exception does not take any arguments - our paginator needs to
# verify whether the next_token arg is expected
with pytest.raises(GenericInvalidTokenException) as exc: with pytest.raises(GenericInvalidTokenException) as exc:
self.method_specifying_generic_invalidtoken_exception( self.method_specifying_generic_invalidtoken_exception(
next_token="some invalid token" next_token="some invalid token"
) )
exc.value.should.be.a(GenericInvalidTokenException) assert isinstance(exc.value, GenericInvalidTokenException)
exc.value.message.should.equal("Invalid token!") assert exc.value.message == "Invalid token!"
def test__invoke_function_that_expects_token_as_keyword(self): def test__invoke_function_that_expects_token_as_keyword(self):
resp, first_token = self.method_expecting_token_as_kwarg() resp, first_token = self.method_expecting_token_as_kwarg()
resp.should.equal([{"name": "item1"}]) assert resp == [{"name": "item1"}]
first_token.shouldnt.equal(None) assert first_token is not None
self.custom_token.should.equal(None) assert self.custom_token is None
# Verify the custom_token is received in the business method # Verify the custom_token is received in the business method
# Could be handy for additional validation # Could be handy for additional validation
resp, token = self.method_expecting_token_as_kwarg(custom_token=first_token) resp, _ = self.method_expecting_token_as_kwarg(custom_token=first_token)
self.custom_token.should.equal(first_token) assert self.custom_token == first_token
def test__invoke_function_that_expects_limit_as_keyword(self): def test__invoke_function_that_expects_limit_as_keyword(self):
self.method_expecting_limit_as_kwarg(custom_limit=None) self.method_expecting_limit_as_kwarg(custom_limit=None)
self.custom_limit.should.equal(None) assert self.custom_limit is None
# Verify the custom_limit is received in the business method # Verify the custom_limit is received in the business method
# Could be handy for additional validation # Could be handy for additional validation
self.method_expecting_limit_as_kwarg(custom_limit=1) self.method_expecting_limit_as_kwarg(custom_limit=1)
self.custom_limit.should.equal(1) assert self.custom_limit == 1
def test__verify_kwargs_can_be_a_list(self): def test__verify_kwargs_can_be_a_list(self):
# Use case - verify that the kwarg can be of type list # Use case - verify that the kwarg can be of type list
# Paginator creates a hash for all kwargs # Paginator creates a hash for all kwargs
# We need to be make sure that the hash-function can deal with lists # We need to be make sure that the hash-function can deal with lists
resp, token = self.method_with_list_as_kwarg() resp, token = self.method_with_list_as_kwarg()
resp.should.equal(results[0:1]) assert resp == results[0:1]
resp, token = self.method_with_list_as_kwarg(next_token=token) resp, token = self.method_with_list_as_kwarg(next_token=token)
resp.should.equal(results[1:2]) assert resp == results[1:2]
custom_list = [{"name": "a"}, {"name": "b"}] custom_list = [{"name": "a"}, {"name": "b"}]
resp, token = self.method_with_list_as_kwarg(resources=custom_list) resp, token = self.method_with_list_as_kwarg(resources=custom_list)
resp.should.equal(custom_list[0:1]) assert resp == custom_list[0:1]
resp, token = self.method_with_list_as_kwarg( resp, token = self.method_with_list_as_kwarg(
resources=custom_list, next_token=token resources=custom_list, next_token=token
) )
resp.should.equal(custom_list[1:]) assert resp == custom_list[1:]
token.should.equal(None) assert token is None
def test__paginator_fails_with_inconsistent_arguments(self): def test__paginator_fails_with_inconsistent_arguments(self):
custom_list = [{"name": "a"}, {"name": "b"}] custom_list = [{"name": "a"}, {"name": "b"}]
resp, token = self.method_with_list_as_kwarg(resources=custom_list) resp, token = self.method_with_list_as_kwarg(resources=custom_list)
resp.should.equal(custom_list[0:1]) assert resp == custom_list[0:1]
with pytest.raises(InvalidToken): with pytest.raises(InvalidToken):
# This should fail, as our 'resources' argument is inconsistent with the original resources that were provided # This should fail, as our 'resources' argument is inconsistent
# with the original resources that were provided
self.method_with_list_as_kwarg(resources=results, next_token=token) self.method_with_list_as_kwarg(resources=results, next_token=token)

View File

@ -6,7 +6,7 @@ def test_list_empty():
svc = TaggingService() svc = TaggingService()
result = svc.list_tags_for_resource("test") result = svc.list_tags_for_resource("test")
{"Tags": []}.should.be.equal(result) assert {"Tags": []} == result
def test_create_tag(): def test_create_tag():
@ -16,7 +16,7 @@ def test_create_tag():
actual = svc.list_tags_for_resource("arn") actual = svc.list_tags_for_resource("arn")
expected = {"TheTags": [{"TagKey": "key_key", "TagValue": "value_value"}]} expected = {"TheTags": [{"TagKey": "key_key", "TagValue": "value_value"}]}
expected.should.be.equal(actual) assert expected == actual
def test_create_tag_without_value(): def test_create_tag_without_value():
@ -26,7 +26,7 @@ def test_create_tag_without_value():
actual = svc.list_tags_for_resource("arn") actual = svc.list_tags_for_resource("arn")
expected = {"Tags": [{"Key": "key_key", "Value": None}]} expected = {"Tags": [{"Key": "key_key", "Value": None}]}
expected.should.be.equal(actual) assert expected == actual
def test_delete_tag_using_names(): def test_delete_tag_using_names():
@ -36,7 +36,7 @@ def test_delete_tag_using_names():
svc.untag_resource_using_names("arn", ["key_key"]) svc.untag_resource_using_names("arn", ["key_key"])
result = svc.list_tags_for_resource("arn") result = svc.list_tags_for_resource("arn")
{"Tags": []}.should.be.equal(result) assert {"Tags": []} == result
def test_delete_all_tags_for_resource(): def test_delete_all_tags_for_resource():
@ -48,7 +48,7 @@ def test_delete_all_tags_for_resource():
svc.delete_all_tags_for_resource("arn") svc.delete_all_tags_for_resource("arn")
result = svc.list_tags_for_resource("arn") result = svc.list_tags_for_resource("arn")
{"Tags": []}.should.be.equal(result) assert {"Tags": []} == result
def test_list_empty_delete(): def test_list_empty_delete():
@ -56,7 +56,7 @@ def test_list_empty_delete():
svc.untag_resource_using_names("arn", ["key_key"]) svc.untag_resource_using_names("arn", ["key_key"])
result = svc.list_tags_for_resource("arn") result = svc.list_tags_for_resource("arn")
{"Tags": []}.should.be.equal(result) assert {"Tags": []} == result
def test_delete_tag_using_tags(): def test_delete_tag_using_tags():
@ -66,7 +66,7 @@ def test_delete_tag_using_tags():
svc.untag_resource_using_tags("arn", tags) svc.untag_resource_using_tags("arn", tags)
result = svc.list_tags_for_resource("arn") result = svc.list_tags_for_resource("arn")
{"Tags": []}.should.be.equal(result) assert {"Tags": []} == result
def test_extract_tag_names(): def test_extract_tag_names():
@ -75,7 +75,7 @@ def test_extract_tag_names():
actual = svc.extract_tag_names(tags) actual = svc.extract_tag_names(tags)
expected = ["key1", "key2"] expected = ["key1", "key2"]
expected.should.be.equal(actual) assert expected == actual
def test_copy_non_existing_arn(): def test_copy_non_existing_arn():
@ -89,7 +89,7 @@ def test_copy_non_existing_arn():
actual = sorted( actual = sorted(
svc.list_tags_for_resource("new_arn")["Tags"], key=lambda t: t["Key"] svc.list_tags_for_resource("new_arn")["Tags"], key=lambda t: t["Key"]
) )
actual.should.equal(tags) assert actual == tags
def test_copy_existing_arn(): def test_copy_existing_arn():
@ -104,9 +104,10 @@ def test_copy_existing_arn():
actual = sorted( actual = sorted(
svc.list_tags_for_resource("new_arn")["Tags"], key=lambda t: t["Key"] svc.list_tags_for_resource("new_arn")["Tags"], key=lambda t: t["Key"]
) )
actual.should.equal( assert actual == [
[{"Key": "key1", "Value": "value1"}, {"Key": "key2", "Value": "value2"}] {"Key": "key1", "Value": "value1"},
) {"Key": "key2", "Value": "value2"},
]
def test_validate_tags(): def test_validate_tags():

View File

@ -1,10 +1,11 @@
import boto3
import sure # noqa # pylint: disable=unused-import
import requests
import unittest import unittest
from unittest import SkipTest
import boto3
import requests
from moto import mock_s3, settings from moto import mock_s3, settings
from moto.server import ThreadedMotoServer from moto.server import ThreadedMotoServer
from unittest import SkipTest
class TestThreadedMotoServer(unittest.TestCase): class TestThreadedMotoServer(unittest.TestCase):
@ -27,8 +28,8 @@ class TestThreadedMotoServer(unittest.TestCase):
) )
s3_client.create_bucket(Bucket="test") s3_client.create_bucket(Bucket="test")
buckets = s3_client.list_buckets()["Buckets"] buckets = s3_client.list_buckets()["Buckets"]
buckets.should.have.length_of(1) assert len(buckets) == 1
[b["Name"] for b in buckets].should.equal(["test"]) assert [b["Name"] for b in buckets] == ["test"]
def test_server_can_handle_multiple_services(self): def test_server_can_handle_multiple_services(self):
s3_client = boto3.client( s3_client = boto3.client(
@ -49,9 +50,9 @@ class TestThreadedMotoServer(unittest.TestCase):
) )
buckets = s3_client.list_buckets()["Buckets"] buckets = s3_client.list_buckets()["Buckets"]
[b["Name"] for b in buckets].should.equal(["test"]) assert [b["Name"] for b in buckets] == ["test"]
dynamodb_client.list_tables()["TableNames"].should.equal(["table1"]) assert dynamodb_client.list_tables()["TableNames"] == ["table1"]
@mock_s3 @mock_s3
def test_load_data_from_inmemory_client(self): def test_load_data_from_inmemory_client(self):
@ -65,7 +66,7 @@ class TestThreadedMotoServer(unittest.TestCase):
in_mem_client = boto3.client("s3") in_mem_client = boto3.client("s3")
buckets = in_mem_client.list_buckets()["Buckets"] buckets = in_mem_client.list_buckets()["Buckets"]
[b["Name"] for b in buckets].should.equal(["test"]) assert [b["Name"] for b in buckets] == ["test"]
def test_threaded_moto_server__different_port(): def test_threaded_moto_server__different_port():
@ -83,7 +84,7 @@ def test_threaded_moto_server__different_port():
) )
s3_client.create_bucket(Bucket="test") s3_client.create_bucket(Bucket="test")
buckets = s3_client.list_buckets()["Buckets"] buckets = s3_client.list_buckets()["Buckets"]
[b["Name"] for b in buckets].should.equal(["test"]) assert [b["Name"] for b in buckets] == ["test"]
finally: finally:
server.stop() server.stop()
@ -96,8 +97,7 @@ def test_threaded_moto_server__using_requests():
requests.post("http://localhost:5001/moto-api/reset") requests.post("http://localhost:5001/moto-api/reset")
try: try:
r = requests.get("http://localhost:5001/moto-api") r = requests.get("http://localhost:5001/moto-api")
r.content.should.contain(b"<title>Moto</title>") assert b"<title>Moto</title>" in r.content
r.status_code.should.equal(200) assert r.status_code == 200
finally: finally:
server.stop() server.stop()
pass