Techdebt: Replace sure with regular assertions in MotoAPI (#6658)

This commit is contained in:
Bert Blommers 2023-08-15 07:45:57 +00:00 committed by GitHub
parent b9c848fd9c
commit 254ecc5e4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 79 additions and 91 deletions

View File

@ -1,6 +1,5 @@
import boto3
import copy
import sure # noqa # pylint: disable=unused-import
from datetime import datetime
from moto import mock_meteringmarketplace
from moto.meteringmarketplace.models import Result
@ -78,7 +77,7 @@ def test_batch_meter_usage():
UsageRecords=USAGE_RECORDS, ProductCode="PUFXZLyUElvQvrsG"
)
res.should.have.key("Results").length_of(10)
assert len(res["Results"]) == 10
records_without_time = copy.copy(USAGE_RECORDS)
for r in records_without_time:

View File

@ -1,4 +1,3 @@
import sure # noqa # pylint: disable=unused-import
from moto.moto_api._internal import mock_random
@ -9,15 +8,15 @@ def test_semi_random_uuids():
# Seed our generator - the next generation should be predetermined
mock_random.seed(42)
fixed_uuid = str(mock_random.uuid4())
fixed_uuid.should.equal("bdd640fb-0667-4ad1-9c80-317fa3b1799d")
assert fixed_uuid == "bdd640fb-0667-4ad1-9c80-317fa3b1799d"
# Ensure they are different
fixed_uuid.shouldnt.equal(random_uuid)
assert fixed_uuid != random_uuid
# Retrieving another 'fixed' UUID should not return a known UUID
second_fixed = str(mock_random.uuid4())
second_fixed.shouldnt.equal(random_uuid)
second_fixed.shouldnt.equal(fixed_uuid)
assert second_fixed != random_uuid
assert second_fixed != fixed_uuid
def test_semi_random_hex_strings():
@ -27,12 +26,12 @@ def test_semi_random_hex_strings():
# Seed our generator - the next generation should be predetermined
mock_random.seed(42)
fixed_hex = mock_random.get_random_hex()
fixed_hex.should.equal("30877432")
assert fixed_hex == "30877432"
# Ensure they are different
fixed_hex.shouldnt.equal(random_hex)
assert fixed_hex != random_hex
# Retrieving another 'fixed' UUID should not return a known UUID
second_hex = mock_random.uuid4()
second_hex.shouldnt.equal(random_hex)
second_hex.shouldnt.equal(fixed_hex)
assert second_hex != random_hex
assert second_hex != fixed_hex

View File

@ -3,7 +3,6 @@ import boto3
import json
import requests
import os
import sure # noqa # pylint: disable=unused-import
from moto import (
settings,
mock_apigateway,
@ -47,7 +46,7 @@ class TestRecorder(TestCase):
resp = requests.get(
"http://localhost:5000/moto-api/recorder/download-recording"
)
resp.status_code.should.equal(200)
assert resp.status_code == 200
return resp.content
else:
return recorder.download_recording()
@ -71,7 +70,8 @@ class TestRecorder(TestCase):
)
ec2.run_instances(ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1)
self._download_recording().should.be.empty
# ServerMode returns bytes
assert self._download_recording() in ["", b""]
def test_ec2_instance_creation_recording_on(self):
self._start_recording()
@ -87,8 +87,8 @@ class TestRecorder(TestCase):
else:
body = content["body"]
body.should.contain("Action=RunInstances")
body.should.contain(f"ImageId={EXAMPLE_AMI_ID}")
assert "Action=RunInstances" in body
assert f"ImageId={EXAMPLE_AMI_ID}" in body
def test_multiple_services(self):
self._start_recording()
@ -123,9 +123,9 @@ class TestRecorder(TestCase):
rows = [json.loads(x) for x in content.splitlines()]
actions = [row["headers"].get("X-Amz-Target") for row in rows]
actions.should.contain("DynamoDB_20120810.CreateTable")
actions.should.contain("DynamoDB_20120810.PutItem")
actions.should.contain("Timestream_20181101.CreateDatabase")
assert "DynamoDB_20120810.CreateTable" in actions
assert "DynamoDB_20120810.PutItem" in actions
assert "Timestream_20181101.CreateDatabase" in actions
def test_replay(self):
self._start_recording()
@ -149,13 +149,13 @@ class TestRecorder(TestCase):
self._replay_recording()
ddb.list_tables()["TableNames"].should.equal(["test"])
assert ddb.list_tables()["TableNames"] == ["test"]
apis = apigw.get_rest_apis()["items"]
apis.should.have.length_of(1)
assert len(apis) == 1
# The ID is uniquely generated everytime, but the name is the same
apis[0]["id"].shouldnt.equal(api_id)
apis[0]["name"].should.equal("my_api")
assert apis[0]["id"] != api_id
assert apis[0]["name"] == "my_api"
def test_replay__partial_delete(self):
self._start_recording()
@ -180,11 +180,11 @@ class TestRecorder(TestCase):
self._replay_recording()
# The replay will create, then delete this Table
ddb.list_tables()["TableNames"].should.equal([])
assert ddb.list_tables()["TableNames"] == []
# The replay will create the RestAPI - the deletion was not recorded
apis = apigw.get_rest_apis()["items"]
apis.should.have.length_of(1)
assert len(apis) == 1
def test_s3_upload_data(self):
self._start_recording()
@ -201,7 +201,7 @@ class TestRecorder(TestCase):
# Replaying should recreate the file as is
self._replay_recording()
resp = s3.get_object(Bucket="mybucket", Key="data")
resp["Body"].read().should.equal(b"ABCD")
assert resp["Body"].read() == b"ABCD"
def test_s3_upload_file_using_requests(self):
s3 = boto3.client(
@ -225,7 +225,7 @@ class TestRecorder(TestCase):
# Replay upload, and assert it succeeded
self._replay_recording()
resp = s3.get_object(Bucket="mybucket", Key="file_upload")
resp["Body"].read().should.equal(b"test")
assert resp["Body"].read() == b"test"
# cleanup
os.remove("text.txt")
@ -302,5 +302,5 @@ class TestThreadedMotoServer(TestCase):
aws_secret_access_key="sk",
)
resp = s3.get_object(Bucket="mybucket", Key="data")
resp["Body"].read().should.equal(b"ABCD")
assert resp["Body"].read() == b"ABCD"
server.stop()

View File

@ -1,6 +1,4 @@
import json
import sure # noqa # pylint: disable=unused-import
from moto import server
@ -17,13 +15,13 @@ def test_set_transition():
"http://localhost:5000/moto-api/state-manager/set-transition",
data=json.dumps(post_body),
)
resp.status_code.should.equal(201)
assert resp.status_code == 201
resp = test_client.get(
"http://localhost:5000/moto-api/state-manager/get-transition?model_name=server::test1"
)
resp.status_code.should.equal(200)
json.loads(resp.data).should.equal({"progression": "waiter", "wait_times": 3})
assert resp.status_code == 200
assert json.loads(resp.data) == {"progression": "waiter", "wait_times": 3}
def test_unset_transition():
@ -44,13 +42,13 @@ def test_unset_transition():
"http://localhost:5000/moto-api/state-manager/unset-transition",
data=json.dumps(post_body),
)
resp.status_code.should.equal(201)
assert resp.status_code == 201
resp = test_client.get(
"http://localhost:5000/moto-api/state-manager/get-transition?model_name=server::test2"
)
resp.status_code.should.equal(200)
json.loads(resp.data).should.equal({"progression": "immediate"})
assert resp.status_code == 200
assert json.loads(resp.data) == {"progression": "immediate"}
def test_get_default_transition():
@ -60,5 +58,5 @@ def test_get_default_transition():
resp = test_client.get(
"http://localhost:5000/moto-api/state-manager/get-transition?model_name=unknown"
)
resp.status_code.should.equal(200)
json.loads(resp.data).should.equal({"progression": "immediate"})
assert resp.status_code == 200
assert json.loads(resp.data) == {"progression": "immediate"}

View File

@ -1,7 +1,5 @@
import json
import requests
import sure # noqa # pylint: disable=unused-import
from moto import settings
from unittest import SkipTest
@ -18,13 +16,13 @@ def test_set_transition():
"http://localhost:5000/moto-api/state-manager/set-transition",
data=json.dumps(post_body),
)
resp.status_code.should.equal(201)
assert resp.status_code == 201
resp = requests.get(
"http://localhost:5000/moto-api/state-manager/get-transition?model_name=test_model0"
)
resp.status_code.should.equal(200)
json.loads(resp.content).should.equal({"progression": "waiter", "wait_times": 3})
assert resp.status_code == 200
assert json.loads(resp.content) == {"progression": "waiter", "wait_times": 3}
def test_unset_transition():
@ -48,8 +46,8 @@ def test_unset_transition():
resp = requests.get(
"http://localhost:5000/moto-api/state-manager/get-transition?model_name=test::model1"
)
resp.status_code.should.equal(200)
json.loads(resp.content).should.equal({"progression": "immediate"})
assert resp.status_code == 200
assert json.loads(resp.content) == {"progression": "immediate"}
def test_get_default_transition():
@ -59,5 +57,5 @@ def test_get_default_transition():
resp = requests.get(
"http://localhost:5000/moto-api/state-manager/get-transition?model_name=unknown"
)
resp.status_code.should.equal(200)
json.loads(resp.content).should.equal({"progression": "immediate"})
assert resp.status_code == 200
assert json.loads(resp.content) == {"progression": "immediate"}

View File

@ -2,8 +2,6 @@ from tests.markers import requires_docker
from tests.test_batch import _get_clients, _setup
from tests.test_batch.test_batch_jobs import prepare_job, _wait_for_job_status
import sure # noqa # pylint: disable=unused-import
from moto import mock_batch, mock_iam, mock_ec2, mock_ecs, mock_logs, settings
from moto.moto_api import state_manager
from unittest import SkipTest
@ -44,10 +42,10 @@ def test_cancel_pending_job():
_wait_for_job_status(batch_client, job_id, "FAILED", seconds_to_wait=20)
resp = batch_client.describe_jobs(jobs=[job_id])
resp["jobs"][0]["jobName"].should.equal("test_job_name")
resp["jobs"][0]["statusReason"].should.equal("test_cancel")
assert resp["jobs"][0]["jobName"] == "test_job_name"
assert resp["jobs"][0]["statusReason"] == "test_cancel"
@mock_batch
def test_state_manager_should_return_registered_model():
state_manager.get_registered_models().should.contain("batch::job")
assert "batch::job" in state_manager.get_registered_models()

View File

@ -1,5 +1,3 @@
import sure # noqa # pylint: disable=unused-import
from moto.moto_api._internal.managed_state_model import ManagedState
from moto.moto_api import state_manager
@ -16,13 +14,13 @@ class ExampleModel(ManagedState):
def test_initial_state():
ExampleModel().status.should.equal("frist_status")
assert ExampleModel().status == "frist_status"
def test_advancing_without_specifying_configuration_does_nothing():
model = ExampleModel()
for _ in range(5):
model.status.should.equal("frist_status")
assert model.status == "frist_status"
model.advance()
@ -38,11 +36,11 @@ def test_advance_immediately():
model_name="example::model", transition={"progression": "immediate"}
)
model.status.should.equal("fifth")
assert model.status == "fifth"
model.advance()
model.status.should.equal("fifth")
assert model.status == "fifth"
def test_advance_x_times():
@ -52,18 +50,18 @@ def test_advance_x_times():
)
for _ in range(2):
model.advance()
model.status.should.equal("frist_status")
assert model.status == "frist_status"
# 3rd time is a charm
model.advance()
model.status.should.equal("second_status")
assert model.status == "second_status"
# Status is still the same if we keep asking for it
model.status.should.equal("second_status")
assert model.status == "second_status"
# Advancing more does not make a difference - there's nothing to advance to
model.advance()
model.status.should.equal("second_status")
assert model.status == "second_status"
def test_advance_multiple_stages():
@ -78,20 +76,20 @@ def test_advance_multiple_stages():
model_name="example::model", transition={"progression": "manual", "times": 1}
)
model.status.should.equal("frist_status")
model.status.should.equal("frist_status")
assert model.status == "frist_status"
assert model.status == "frist_status"
model.advance()
model.status.should.equal("second")
model.status.should.equal("second")
assert model.status == "second"
assert model.status == "second"
model.advance()
model.status.should.equal("third")
model.status.should.equal("third")
assert model.status == "third"
assert model.status == "third"
model.advance()
model.status.should.equal("fourth")
model.status.should.equal("fourth")
assert model.status == "fourth"
assert model.status == "fourth"
model.advance()
model.status.should.equal("fifth")
model.status.should.equal("fifth")
assert model.status == "fifth"
assert model.status == "fifth"
def test_override_status():
@ -102,22 +100,22 @@ def test_override_status():
model_name="example::model", transition={"progression": "manual", "times": 1}
)
model.status.should.equal("creating")
assert model.status == "creating"
model.advance()
model.status.should.equal("ready")
assert model.status == "ready"
model.advance()
# We're still ready
model.status.should.equal("ready")
assert model.status == "ready"
# Override status manually
model.status = "updating"
model.status.should.equal("updating")
assert model.status == "updating"
model.advance()
model.status.should.equal("ready")
model.status.should.equal("ready")
assert model.status == "ready"
assert model.status == "ready"
model.advance()
model.status.should.equal("ready")
assert model.status == "ready"
class SlowModel(ManagedState):
@ -134,15 +132,15 @@ def test_realworld_delay():
transition={"progression": "time", "seconds": 2},
)
model.status.should.equal("first")
assert model.status == "first"
# The status will stick to 'first' for a long time
# Advancing the model doesn't do anything, really
for _ in range(10):
model.advance()
model.status.should.equal("first")
assert model.status == "first"
import time
time.sleep(2)
# Status has only progressed after 2 seconds have passed
model.status.should.equal("second")
assert model.status == "second"

View File

@ -1,12 +1,10 @@
import sure # noqa # pylint: disable=unused-import
from moto.moto_api._internal.state_manager import StateManager
def test_public_api():
from moto.moto_api import state_manager
state_manager.should.be.a(StateManager)
assert isinstance(state_manager, StateManager)
def test_default_transition():
@ -17,7 +15,7 @@ def test_default_transition():
)
actual = manager.get_transition(model_name="dax::cluster")
actual.should.equal({"progression": "manual"})
assert actual == {"progression": "manual"}
def test_set_transition():
@ -28,7 +26,7 @@ def test_set_transition():
)
actual = manager.get_transition(model_name="dax::cluster")
actual.should.equal({"progression": "waiter", "wait_times": 3})
assert actual == {"progression": "waiter", "wait_times": 3}
def test_set_transition_overrides_default():
@ -43,7 +41,7 @@ def test_set_transition_overrides_default():
)
actual = manager.get_transition(model_name="dax::cluster")
actual.should.equal({"progression": "waiter", "wait_times": 3})
assert actual == {"progression": "waiter", "wait_times": 3}
def test_unset_transition():
@ -60,17 +58,17 @@ def test_unset_transition():
manager.unset_transition(model_name="dax::cluster")
actual = manager.get_transition(model_name="dax::cluster")
actual.should.equal({"progression": "manual"})
assert actual == {"progression": "manual"}
def test_get_default_transition():
manager = StateManager()
actual = manager.get_transition(model_name="unknown")
actual.should.equal({"progression": "immediate"})
assert actual == {"progression": "immediate"}
def test_get_registered_models():
manager = StateManager()
manager.get_registered_models().should.equal([])
assert manager.get_registered_models() == []