diff --git a/docs/docs/services/index.rst b/docs/docs/services/index.rst index 9e700986f..80c55065e 100644 --- a/docs/docs/services/index.rst +++ b/docs/docs/services/index.rst @@ -6,6 +6,23 @@ Implemented Services ==================== Please see a list of all currently supported services. Each service will have a list of the endpoints that are implemented. +Each service will also have an example on how to mock an individual service. + +Note that you can mock multiple services at the same time: + +.. sourcecode:: python + + @mock_s3 + @mock_sqs + def test_both_s3_and_sqs(): + ... + + +.. sourcecode:: python + + @mock_all + def test_all_supported_services_at_the_same_time(): + ... .. toctree:: diff --git a/moto/__init__.py b/moto/__init__.py index 90a41efeb..1396718c9 100644 --- a/moto/__init__.py +++ b/moto/__init__.py @@ -1,4 +1,5 @@ import importlib +import sys def lazy_load( @@ -165,6 +166,25 @@ mock_mediastoredata = lazy_load( mock_efs = lazy_load(".efs", "mock_efs") mock_wafv2 = lazy_load(".wafv2", "mock_wafv2") + +def mock_all(): + dec_names = [ + d + for d in dir(sys.modules["moto"]) + if d.startswith("mock_") + and not d.endswith("_deprecated") + and not d == "mock_all" + ] + + def deco(f): + for dec_name in reversed(dec_names): + dec = globals()[dec_name] + f = dec(f) + return f + + return deco + + # import logging # logging.getLogger('boto').setLevel(logging.CRITICAL) diff --git a/scripts/implementation_coverage.py b/scripts/implementation_coverage.py index 94b9c0fb6..1395091ea 100755 --- a/scripts/implementation_coverage.py +++ b/scripts/implementation_coverage.py @@ -238,6 +238,18 @@ def write_implementation_coverage_to_docs(coverage): file.write("====================\n") file.write("\n") file.write("Please see a list of all currently supported services. Each service will have a list of the endpoints that are implemented.\n") + file.write("Each service will also have an example on how to mock an individual service.\n\n") + file.write("Note that you can mock multiple services at the same time:\n\n") + file.write(".. sourcecode:: python\n\n") + file.write(" @mock_s3\n") + file.write(" @mock_sqs\n") + file.write(" def test_both_s3_and_sqs():\n") + file.write(" ...\n") + file.write("\n\n") + file.write(".. sourcecode:: python\n\n") + file.write(" @mock_all\n") + file.write(" def test_all_supported_services_at_the_same_time():\n") + file.write(" ...\n") file.write("\n") file.write("\n") diff --git a/tests/test_core/test_mock_all.py b/tests/test_core/test_mock_all.py new file mode 100644 index 000000000..bed54c72b --- /dev/null +++ b/tests/test_core/test_mock_all.py @@ -0,0 +1,20 @@ +import boto3 +import sure # noqa # pylint: disable=unused-import + +from moto import mock_all + + +@mock_all() +def test_multiple_services(): + rgn = "us-east-1" + sqs = boto3.client("sqs", region_name=rgn) + r = sqs.list_queues() # + r["ResponseMetadata"]["HTTPStatusCode"].should.equal(200) + + lmbda = boto3.client("lambda", region_name=rgn) + r = lmbda.list_event_source_mappings() + r["ResponseMetadata"]["HTTPStatusCode"].should.equal(200) + + ddb = boto3.client("dynamodb", region_name=rgn) + r = ddb.list_tables() + r["ResponseMetadata"]["HTTPStatusCode"].should.equal(200)