Я высмеиваю вызовы AWS на s3 с помощью декоратора @ mock_s3 из moto и boto3. Я запускаю тесты с «покрытием» из моего корневого каталога.
Всякий раз, когда в моих тестах используется какая-либо команда, включающая вывод содержимого AWS S3, я получаю эту ошибку:
File "/Users/Name/Documents/project-name/test/lib/python3.7/site-packages/jinja2/compiler.py", line 1044, in visit_For
if node.test:
AttributeError: 'For' object has no attribute 'test'
Некоторые примеры команд, которые терпят неудачу с этой ошибкой:
s3_client = s3_session.client("s3")
s3_client.list_buckets() #fails at this line
и
s3_session = boto3.session.Session()
s3_client = s3_session.client("s3")
paginator = s3_client.get_paginator("list_objects_v2")
iterator = paginator.paginate(Bucket="mybucket", Prefix="mypath")
keys = iterator.search(query)
regex = re.compile('(\S)')
data_files = list(filter(regex.search, keys)) #fails at this line
Перед запуском этих строк кода я использую boto3 для создания блоков и объектов внутри этих блоков.
Это пример теста:
@mock_s3
class TestModel(TestCase):
def test_my_model_save(self):
s3_client = boto3.client('s3', region_name='us-east-1')
s3_client.create_bucket(Bucket='mybucket') # printing the response from this returns {'ResponseMetadata': {'HTTPStatusCode': 200, 'HTTPHeaders': {}, 'RetryAttempts': 0}}
s3_client.put_object(Bucket='mybucket', Key="mypath", Body="details") # printing the response from this returns {'ResponseMetadata': {'HTTPStatusCode': 200, 'HTTPHeaders': {'etag': '"27792947ed5d5da7c0d1f43327ed9dab"', 'last-modified': 'Fri, 15 Mar 2019 20:15:44 GMT', 'content-length': '7'}, 'RetryAttempts': 0}, 'ETag': '"27792947ed5d5da7c0d1f43327ed9dab"'}
print(s3_client.list_buckets()) #code breaks here
Я признаю, что этот тест не завершен (пропущены утверждения и фактически не проверяется мой код), это всего лишь общий случай, чтобы продемонстрировать ошибку.
Я также замечаю, что ответы от create_bucket и put_object не соответствуют документации boto3 для этих двух функций (см. Комментарии в приведенном выше примере тестового кода).
Вот полная трассировка при выполнении моего теста:
Traceback (most recent call last):
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/moto/core/models.py", line 74, in wrapper
result = func(*args, **kwargs)
File "/Users/user_name/Documents/project_name/api/tests/test_my_Model.py", line 24, in test_my_model_save
print(s3_client.list_buckets())
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/client.py", line 320, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/client.py", line 611, in _make_api_call
operation_model, request_dict)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/endpoint.py", line 102, in make_request
return self._send_request(request_dict, operation_model)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/endpoint.py", line 136, in _send_request
success_response, exception):
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/endpoint.py", line 210, in _needs_retry
caught_exception=caught_exception, request_dict=request_dict)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/hooks.py", line 356, in emit
return self._emitter.emit(aliased_event_name, **kwargs)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/hooks.py", line 228, in emit
return self._emit(event_name, kwargs)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/hooks.py", line 211, in _emit
response = handler(**kwargs)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/retryhandler.py", line 183, in __call__
if self._checker(attempts, response, caught_exception):
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/retryhandler.py", line 251, in __call__
caught_exception)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/retryhandler.py", line 269, in _should_retry
return self._checker(attempt_number, response, caught_exception)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/retryhandler.py", line 317, in __call__
caught_exception)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/retryhandler.py", line 223, in __call__
attempt_number, caught_exception)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/retryhandler.py", line 359, in _check_caught_exception
raise caught_exception
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/endpoint.py", line 176, in _get_response
responses = self._event_emitter.emit(event_name, request=request)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/hooks.py", line 356, in emit
return self._emitter.emit(aliased_event_name, **kwargs)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/hooks.py", line 228, in emit
return self._emit(event_name, kwargs)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/botocore/hooks.py", line 211, in _emit
response = handler(**kwargs)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/moto/core/models.py", line 292, in __call__
status, headers, body = response_callback(request, request.url, request.headers)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/moto/s3/responses.py", line 116, in bucket_response
response = self._bucket_response(request, full_url, headers)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/moto/s3/responses.py", line 138, in _bucket_response
return self.all_buckets()
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/moto/s3/responses.py", line 53, in all_buckets
template = self.response_template(S3_ALL_BUCKETS)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/moto/core/responses.py", line 103, in response_template
return self.environment.get_template(template_id)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/environment.py", line 830, in get_template
return self._load_template(name, self.make_globals(globals))
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/environment.py", line 804, in _load_template
template = self.loader.load(self, name, globals)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/loaders.py", line 125, in load
code = environment.compile(source, name, filename)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/environment.py", line 581, in compile
defer_init=defer_init)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/environment.py", line 543, in _generate
optimized=self.optimized)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/compiler.py", line 82, in generate
generator.visit(node)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/visitor.py", line 38, in visit
return f(node, *args, **kwargs)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/compiler.py", line 754, in visit_Template
self.blockvisit(node.body, frame)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/compiler.py", line 378, in blockvisit
self.visit(node, frame)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/visitor.py", line 38, in visit
return f(node, *args, **kwargs)
File "/Users/user_name/Documents/project_name/test/lib/python3.7/site-packages/jinja2/compiler.py", line 1044, in visit_For
if node.test:
AttributeError: 'For' object has no attribute 'test'
Я пользуюсь последними версиями moto и boto3.
В чем здесь может быть проблема?