File: __init__.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (58 lines) | stat: -rw-r--r-- 1,661 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from functools import wraps
from uuid import uuid4

import boto3

from moto import mock_aws
from tests import allow_aws_request


def sqs_aws_verified(fifo_queue: bool = False):
    """
    Function that is verified to work against AWS.
    Can be run against AWS at any time by setting:
      MOTO_TEST_ALLOW_AWS_REQUEST=true

    If this environment variable is not set, the function runs in a `mock_aws` context.

    This decorator will:
      - Create an SQS queue
      - Run the test and pass the queue_name as an argument
      - Delete the queue
    """

    def inner(func):
        @wraps(func)
        def pagination_wrapper(**kwargs):
            queue_name = "q" + str(uuid4())[0:6]
            if fifo_queue:
                queue_name += ".fifo"
            kwargs["queue_name"] = queue_name

            def create_queue_and_test():
                client = boto3.client("sqs", region_name="us-east-1")

                create_kwargs = {}
                if fifo_queue:
                    create_kwargs["Attributes"] = {"FifoQueue": "true"}

                queue_url = client.create_queue(QueueName=queue_name, **create_kwargs)[
                    "QueueUrl"
                ]
                kwargs["queue_url"] = queue_url
                try:
                    resp = func(**kwargs)
                finally:
                    client.delete_queue(QueueUrl=queue_url)

                return resp

            if allow_aws_request():
                return create_queue_and_test()
            else:
                with mock_aws():
                    return create_queue_and_test()

        return pagination_wrapper

    return inner