Python 如何使用moto模拟DynamoDB分页?

Python 如何使用moto模拟DynamoDB分页?,python,amazon-web-services,pagination,amazon-dynamodb,moto,Python,Amazon Web Services,Pagination,Amazon Dynamodb,Moto,我正在测试一些dynamodb访问代码。在过去,对分页的不正确处理会导致错误(开发人员倾向于使用少量数据手动测试,因此很容易对分页的工作方式做出错误的假设,这些假设只有在处理实际数据量时才会暴露出来) 我通常使用普通的unittest和unittest.mock对访问代码进行单元测试,并以这种方式测试分页,但我最终编写了一些相当复杂的测试代码来模拟不同操作(扫描、查询、批处理获取项目)的分页 我正在寻找一种更简单的测试方法;moto带来了一些希望 然而,我并不想将1MB+的数据加载到moto中以

我正在测试一些dynamodb访问代码。在过去,对分页的不正确处理会导致错误(开发人员倾向于使用少量数据手动测试,因此很容易对分页的工作方式做出错误的假设,这些假设只有在处理实际数据量时才会暴露出来)

我通常使用普通的
unittest
unittest.mock
对访问代码进行单元测试,并以这种方式测试分页,但我最终编写了一些相当复杂的测试代码来模拟不同操作(扫描、查询、批处理获取项目)的分页

我正在寻找一种更简单的测试方法;moto带来了一些希望

然而,我并不想将1MB+的数据加载到moto中以诱导分页,我想强制它对少量数据进行分页

所以我要问的关键是:

  • moto支持DynamoDB分页吗
  • 我可以配置分页阈值吗
  • 怎么做
  • 参考资料

    moto是否支持DynamoDB分页?

    是的,它是通过moto.mock_dynamodb2功能实现的。我已经尝试使用PynamoDB的
    查询
    功能进行分页,它在我的模拟DynamoDB环境中运行良好,该环境由
    moto.mock_dynamodb2
    提供

    我可以配置分页阈值吗?

    通过使用PynamoDB的
    查询
    ,您可以在
    限制
    参数中对其进行配置

    分页具有以下核心概念:

  • hash\u-key
    +
    range\u-key\u-condition
    +
    filter\u-condition
    • 要分页的DynamoDB记录列表
  • 限制
    • 查询返回的最大结果数
  • 向前扫描索引
    • 结果的顺序。您希望获取的记录按范围键/排序键按升序(例如1、2、3)或降序(例如3、2、1)排序
  • last\u evaluated\u key
    • 这表示数据库中最后处理的项(对于密钥)。这会将该项标记为将获取下一组项的引用点。None表示从已排序记录的开头进行查询。否则,从指定的键开始查询
    • 把这想象成对
      [0,5,10,15,20,25,30,35,40,45,50]
      的二进制搜索。如果我们从一开始就对4个项目进行分页,我们将得到
      [0,5,10,15]
      。如果我们想要获得接下来的4项,我们不需要从开始(
      0
      )到目标(
      20
      )一直迭代。这种算法在最坏情况下会导致线性O(n)时间复杂度,其中n是所有记录的计数。相反,我们所能做的是对第一个大于最后一个取回的项(即
      15
      )的项执行二进制搜索,在这里我们将得到
      20
      ,仅为对数O(log(n))
    怎么做?

    请参阅Python代码片段

    # Testing date: 2020 9September 29
    
    # Versions
    # moto==1.3.16
    # pynamodb==4.3.3
    # pytest==6.1.0
    
    import itertools
    
    from moto import mock_dynamodb2
    from pynamodb.attributes import *
    from pynamodb.models import Model
    import pytest
    
    
    # Model
    
    class Location(Model):
        class Meta:
            table_name = 'Location-table'
            region = 'ap-southeast-1'
    
        continent = UnicodeAttribute(hash_key=True)  # also known as partition_key
        country = UnicodeAttribute(range_key=True)  # also known as sort_key
        capital = UnicodeAttribute()
        gmt = NumberAttribute()
    
        def __iter__(self):
            for name, attr in self.get_attributes().items():
                yield name, attr.serialize(getattr(self, name))
    
    
    # Test data
    
    LOCATIONS = [
        {
            'continent': 'Europe',
            'country': 'Spain',
            'capital': 'Madrid',
            'gmt': 2,
        },
        {
            'continent': 'Europe',
            'country': 'Germany',
            'capital': 'Berlin',
            'gmt': 2,
        },
        {
            'continent': 'South America',
            'country': 'Venezuela',
            'capital': 'Caracas',
            'gmt': -4,
        },
        {
            'continent': 'Europe',
            'country': 'Ukraine',
            'capital': 'Kyiv',
            'gmt': 3,
        },
        {
            'continent': 'South America',
            'country': 'Brazil',
            'capital': 'Brasília',
            'gmt': -3,
        },
        {
            'continent': 'Europe',
            'country': 'Finland',
            'capital': 'Helsinki',
            'gmt': 3,
        },
        {
            'continent': 'Europe',
            'country': 'Ireland',
            'capital': 'Dublin',
            'gmt': 1,
        },
    ]
    
    
    # Test algorithms
    
    def _setup_table(locations):
        Location.create_table()
    
        for location in locations:
            Location(**location).save()
    
    def _get_filter_condition():
        # Put logic here for the filter condition. Uncomment the code below to try.
        # filter_condition = (Location.gmt >= 2) \
        #                     & (Location.capital.contains('in') | Location.capital.startswith('A'))
        # return filter_condition
        return None
    
    
    @mock_dynamodb2
    def test_dynamodb_pagination():
        _setup_table(LOCATIONS)
        filter_condition = _get_filter_condition()
    
        # Expected query order for Europe. This should be sorted by country (which is the sort_key field).
        SORTED_EUROPE_COUNTRIES = [
            'Finland',
            'Germany',
            'Ireland',
            'Spain',
            'Ukraine',
        ]
        country_index = 0
    
        # This indicates the last processed item (for the key) from the database. This marks that item
        # as the reference point to where the next set of items will be fetched. None means query from
        # the beginning of the sorted records. Otherwise, start the query from the indicated key.
        last_evaluated_key = None
    
        for query_index in itertools.count(0):
            result = Location.query(
                hash_key='Europe',
                filter_condition=filter_condition,  # Filter the query results
                limit=2,  # Maximum number of items to fetch from the database
                last_evaluated_key=last_evaluated_key,  # The reference starting point of the fetch
                scan_index_forward=True,  # Indicate if in lexicographical order (increasing) or in reverse (decreasing)
            )
    
            for item in result:
                print(f"Query #{query_index} - Country #{country_index} - {item}")
    
                assert item.country == SORTED_EUROPE_COUNTRIES[country_index]
                country_index += 1
    
            print(f"result.last_evaluated_key {result.last_evaluated_key}\n")
            last_evaluated_key = result.last_evaluated_key
    
            if last_evaluated_key is None:
                print(f"Reached the last queried item in the database")
                break
    
    输出:

    (venv) nponcian 2020_9Sep_10_DynamoDB$ pytest pagination_test.py -rP
    ====================================================================================== test session starts ======================================================================================
    platform linux -- Python 3.8.2, pytest-6.1.0, py-1.9.0, pluggy-0.13.1
    rootdir: /home/nponcian/Documents/Program/2020_9Sep_10_DynamoDB
    plugins: cov-2.10.1, mock-3.3.1
    collected 1 item                                                                                                                                                                                
    
    pagination_test.py .                                                                                                                                                                      [100%]
    
    ============================================================================================ PASSES =============================================================================================
    ___________________________________________________________________________________ test_dynamodb_pagination ____________________________________________________________________________________
    ------------------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------------------
    Query #0 - Country #0 - Location-table<Europe, Finland>
    Query #0 - Country #1 - Location-table<Europe, Germany>
    result.last_evaluated_key {'continent': {'S': 'Europe'}, 'country': {'S': 'Germany'}}
    
    Query #1 - Country #2 - Location-table<Europe, Ireland>
    Query #1 - Country #3 - Location-table<Europe, Spain>
    result.last_evaluated_key {'continent': {'S': 'Europe'}, 'country': {'S': 'Spain'}}
    
    Query #2 - Country #4 - Location-table<Europe, Ukraine>
    result.last_evaluated_key None
    
    Reached the last queried item in the database
    ======================================================================================= 1 passed in 0.40s =======================================================================================
    
    (venv)NPOCIAN 2020年9月10日发电机$pytest分页\u test.py-rP
    ====================================================================================================================================测试会话开始======================================================================================
    平台linux——Python 3.8.2、pytest-6.1.0、py-1.9.0、pluggy-0.13.1
    rootdir:/home/npocian/Documents/Program/2020\u 9Sep\u 10\u DynamoDB
    插件:cov-2.10.1,mock-3.3.1
    收集1项
    分页_test.py。[100%]
    =========================================================================================================================================================通行证=============================================================================================
    ___________________________________________________________________________________测试发电机分页____________________________________________________________________________________
    -------------------------------------------------------------------------------------捕获的stdout调用--------------------------------------------------------------------------------------
    查询#0-国家#0-位置表
    查询#0-国家#1-位置表
    result.last_评估了_key{‘大陆’:{‘欧洲’},‘国家’:{‘德国’}
    查询#1-国家#2-位置表
    查询#1-国家#3-位置表
    result.last_评估_key{‘大陆’:{‘欧洲’},‘国家’:{‘西班牙’}
    查询#2-国家#4-位置表
    结果。最后一次评估\u键无
    已到达数据库中最后查询的项
    =================================================================================================================================================1在0.40秒内通过=======================================================================================