- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在测试一些 dynamodb 访问代码。在过去,对分页的不正确处理导致了错误(开发人员倾向于使用少量数据进行手动测试,因此很容易对分页的工作原理做出错误的假设,只有在处理实际数据量时才会出现这种假设)
我通常使用普通 unittest
和 unittest.mock
对访问代码进行单元测试,并以这种方式测试分页,但我最终写了一些合理的复杂的测试代码来模拟不同操作的分页(扫描、查询、batch_get_item)。
我正在寻找一种更简单的测试方法; moto 带来了一些希望
但是,我真的不想将 1MB+ 的数据加载到 moto 中以引起分页,我想强制它对少量数据进行分页
所以我要问的关键是:
最佳答案
引用资料
moto 是否完全支持 DynamoDB 分页?
是的,它通过 moto.mock_dynamodb2
功能实现。我尝试使用 PynamoDB 的 query
功能进行分页,它在 moto.mock_dynamodb2
提供的模拟 DynamoDB 环境中运行良好。
我可以配置分页阈值吗?
使用PynamoDB的query
,可以在limit
参数中配置。
分页具有以下核心概念:
hash_key
+ range_key_condition
+ filter_condition
限制
scan_index_forward
last_evaluated_key
[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 =======================================================================================
关于python - 如何使用 moto 模拟 DynamoDB 分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62083300/
我正在使用 Boto3 和 Python 开发 Dyanamodb。我发现的问题之一是我们应该何时使用 dynamodb.client、dynamodb.resource 和 dynamodb.Tab
DynamoDB documentation描述了表分区原则上是如何工作的,但它对细节(即数字)非常了解。 DynamoDB 表分区究竟如何以及何时发生? 最佳答案 我找到了这个 presentati
我在 DynamoDB 表中有一个项目。该项目看起来像这样: { data: [ 1, 2, 3, 4, 5, 6 ] more_data: [ 2, 3, 4, 5, 6, 7 ] } 使用
我的 DynamoDB 表中的一个属性是一个名为 REQUEST_IDS 的列表,我想在更新项目之前检查该列表的长度以查看它是否满足条件(小于 10) .如何在 nodejs 的 ConditionE
我正在使用 Amazon Dynamodb,但没有太多经验。我有这样的价格表: 编号 |插入日期 |产品名称 |店名 |价格 相同的商店名称和产品名称可以有不同的值(价格和插入日期可能不同)。例如 i
我对 DynamoDB 上查询/扫描的限制有疑问。 我的表有 1000 条记录,对所有记录的查询返回 50 个值,但是如果我将 Limit 设置为 5,这并不意味着查询将返回前 5 个值,它只是说查询
我需要在 jsp 上显示最大计数为 10 的搜索结果,并且它应该有一个分页来作为分页功能来回遍历。 Dynamodb 有一个 最后评估 key ,但返回上一页无济于事,尽管我可以通过 移动到下一个结果
我是 CouchDB 的忠实粉丝,并且完全爱上了每个文档发出不止一次的 map 函数。我想知道在 DynamoDB 中是否可以通过使用字符串或数字集类型作为散列和范围主键的一部分(作为散列或范围属性)
我目前正在使用 DynamoDB。如果该记录的日期早于新记录日期字段,我想使用条件写入来更新记录。 有没有办法比较条件写入的 DateTime 类型?还是目前仅适用于整数、字符串和流? 谢谢。 最佳答
如何对 dynamoDB 表建模以构建一个标签系统,其中产品可以分配多个标签,并且我们应该能够过滤具有特定标签或标签集合的产品集,并获取分配给给定标签的所有标签产品? 我考虑过有一张 table :
我试图更好地理解在 AWS DynamoDB 中使用邻接列表模式进行多对多 (m:n) 关系设计。 在此处查看 AWS 文档:https://docs.aws.amazon.com/amazondyn
我怀疑 DynamoDB 中的这一说法是否属实或我的理解不正确。它说, ProvisionedThroughputExceededException 消息:您超出了表或一个或多个全局二级索引的最大允许
考虑一个 DynamoDB 表,它由一个主键和两个描述开始日期和结束日期的属性组成。如何在不扫描整个表的情况下查询时间范围是否与表中的时间范围重叠? 例子: 发电机表有两条记录 PK Start
我有一个 DynamoDB 表,其中包含将由许多应用程序读取的键值对。在启动时,每个应用程序将读取整个表并将其缓存在内存中。 我试图解决的问题是,如果 DynamoDB 表中的一个或多个项目已被修改,
我正在有条件地更新 dynamoDB 记录(仅当记录具有其属性之一的特定值时)。无论是否成功更新(条件是否满足),我都想取回记录。 docClient.update(params, function(
我目前正在对 DynamoDB 进行批量加载并将我们的数据项划分为批处理单元: 根据限制文件: https://docs.aws.amazon.com/amazondynamodb/latest/AP
我正在跟踪dynamodb的Python教程,以在端口8000上设置本地dynomodb http://docs.aws.amazon.com/amazondynamodb/latest/gettin
我正在创建一个 DynamoDB 表来保存与单个对象关联的注释。 评论在特定时间发布到对象,我使用发布的时间作为范围,因此评论可以按时间降序排序。我有发布评论的用户的 userId 的全局二级索引,这
我正在运行一个简单的 api,它在每次调用时从 dynamodb 表中获取一个项目,我将自动缩放设置为最小值 25 和最大值 10 000。 但是,如果我使用 wrk 或 hey 之类的工具发送 15
我在模型中有一个字段已声明为字符串,如下所示: App.Student= DS.Model.extend({ name: DS.attr('string'), address1: DS.attr('s
我是一名优秀的程序员,十分优秀!