小编dre*_*ert的帖子

模拟 AWS lambda 和 boto3

我有一个 lambda (python),它返回客户资料的 json 表示。它通过从顶级帐户 json 开始,然后读取链接的 json 文件直到它用完链接来做到这一点。从 s3 读取的函数是递归的,但递归永远只有一层深。

这是从给定键实际获取json内容的方法。(桶是已知的)

def get_index_from_s3(key):
    try:
        response = s3.get_object(
            Bucket=bucket,
            Key=key
        )
        body = response.get('Body')
        content = body.read().decode('utf-8')
    except ClientError as ex:
        # print 'EXCEPTION MESSAGE: {}'.format(ex.response['Error']['Code'])
        content = '{}'

    message = json.loads(content)
    return message
Run Code Online (Sandbox Code Playgroud)

该代码返回在指定键处找到的 json,或者在 get_object 由于 ClientError (这是 NoSuchKey 的结果)而失败的情况下返回一个空字典。

我已经测试过了,它有效。对该函数的第一次调用会获取一大块 json。解析 json,找到链接,进行第二次调用,然后构建配置文件。如果我删除链接键上的对象,我只会得到一个默认的空表示,正如预期的那样。

我的问题来自测试这个。我写了几个测试类,每个类都有一个排列方法,它们共享一个行为方法。

对于我的幸福之路,我使用以下安排:

def arrange(self):
    super(WhenCognitoAndNerfFoundTestCase, self).arrange()
    # self.s3_response = self.s3.get_object.return_value
    self.s3_body = self.s3.get_object.return_value.get.return_value
    self.s3_body.read.return_value.decode.side_effect = [
        self.cognito_content,
        self.nerf_content]
    signed_url = "https://this.is/a/signed/url/index.html"
    self.s3.generate_presigned_url.return_value = signed_url
Run Code Online (Sandbox Code Playgroud)

这正是我想要的。s3_response是get_object的return_value,它有get返回的Body属性,后续读取的值返回一个json字符串。我使用 …

python mocking amazon-s3 amazon-web-services boto3

3
推荐指数
1
解决办法
7465
查看次数

django-admin startproject 导致“ModuleNotFoundError:没有名为‘config’的模块”

我的设置是: - 运行安装了自制软件的 pyenv 的 Mac 操作系统。
- python 3.6.5 virtualenv - pip install Django==2.0.6

这是我发出的命令和输出。我在搜索时找到的唯一答案是假设现有 django 项目和不正确的配置。我无法到达创建项目的地步。

$ django-admin startproject justatestproj
Traceback (most recent call last):
  File "/Users/rsquire/.pyenv/versions/forever7pi/bin/django-admin", line 11, in <module>
    sys.exit(execute_from_command_line())
  File "/Users/rsquire/.pyenv/versions/3.6.5/envs/forever7pi/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/Users/rsquire/.pyenv/versions/3.6.5/envs/forever7pi/lib/python3.6/site-packages/django/core/management/__init__.py", line 317, in execute
    settings.INSTALLED_APPS
  File "/Users/rsquire/.pyenv/versions/3.6.5/envs/forever7pi/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/Users/rsquire/.pyenv/versions/3.6.5/envs/forever7pi/lib/python3.6/site-packages/django/conf/__init__.py", line 43, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/rsquire/.pyenv/versions/3.6.5/envs/forever7pi/lib/python3.6/site-packages/django/conf/__init__.py", line 106, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/Users/rsquire/.pyenv/versions/3.6.5/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return …
Run Code Online (Sandbox Code Playgroud)

python django

1
推荐指数
1
解决办法
1176
查看次数

标签 统计

python ×2

amazon-s3 ×1

amazon-web-services ×1

boto3 ×1

django ×1

mocking ×1