From e8f7764f9be1955b95091a773797498d1e017095 Mon Sep 17 00:00:00 2001 From: bpsoos Date: Wed, 24 Jul 2024 17:18:42 +0200 Subject: [PATCH 1/2] adds low level api without describe table tests --- tests/integration/base_integration_test.py | 86 ++++++++++++++++++++- tests/integration/table_integration_test.py | 63 +++++++++++++++ 2 files changed, 148 insertions(+), 1 deletion(-) diff --git a/tests/integration/base_integration_test.py b/tests/integration/base_integration_test.py index 87c386361..b197bef24 100644 --- a/tests/integration/base_integration_test.py +++ b/tests/integration/base_integration_test.py @@ -2,12 +2,16 @@ Runs tests against dynamodb """ import time +from pynamodb.attributes import UnicodeAttribute from pynamodb.connection import Connection -from pynamodb.constants import PROVISIONED_THROUGHPUT, READ_CAPACITY_UNITS +from pynamodb.connection.table import TableConnection +from pynamodb.constants import MAP, PROVISIONED_THROUGHPUT, READ_CAPACITY_UNITS from pynamodb.expressions.condition import BeginsWith, NotExists from pynamodb.expressions.operand import Path, Value from pynamodb.exceptions import TableDoesNotExist +from pynamodb.expressions.update import Action, SetAction +from pynamodb.models import Model from pynamodb.types import STRING, HASH, RANGE, NUMBER import pytest @@ -167,3 +171,83 @@ def test_connection_integration(ddb_url): ) print("conn.delete_table...") conn.delete_table(table_name) + + +def test_conn_without_describe_table_called(ddb_url, table: str): + conn = Connection(host=ddb_url) + # conn.describe_table(table) # bug: operations don't work without calling describe_table first + + conn.put_item( + table, + 'item1-hash', + attributes={'foo': {'S': 'bar'}}, + ) + get_response = conn.get_item( + table, + 'item1-hash', + ) + assert get_response.get('Item') == {'id': {'S': 'item1-hash'}, 'foo': {'S': 'bar'}} + + conn.update_item( + table, + 'item1-hash', + actions=[Path('foo').set("rab")] + ) + + get_response_after_update = conn.get_item( + table, + 'item1-hash', + ) + assert get_response_after_update.get('Item') == {'id': {'S': 'item1-hash'}, 'foo': {'S': 'rab'}} + + conn.delete_item( + table, + 'item1-hash', + ) + get_response_after_delete = conn.get_item( + table, + 'item1-hash', + ) + assert get_response_after_delete.get('Item') == None + + +@pytest.fixture +def table(ddb_url): + table_name = 'pynamodb-ci-connection' + + conn = Connection(host=ddb_url) + params = { + 'read_capacity_units': 1, + 'write_capacity_units': 1, + 'attribute_definitions': [ + { + 'attribute_type': STRING, + 'attribute_name': 'id' + }, + ], + 'key_schema': [ + { + 'key_type': HASH, + 'attribute_name': 'id' + } + ], + } + conn.create_table(table_name, **params) + for i in range(0,10): + time.sleep(1) + if conn.describe_table(table_name) is not None: + break + if i == 9: + raise TimeoutError + + for i in range(0,10): + time.sleep(1) + if conn.describe_table(table_name)['TableStatus'] == 'ACTIVE': + break + if i == 9: + raise TimeoutError + + yield table_name + + conn.delete_table(table_name) + diff --git a/tests/integration/table_integration_test.py b/tests/integration/table_integration_test.py index f05b5e711..9af9465ec 100644 --- a/tests/integration/table_integration_test.py +++ b/tests/integration/table_integration_test.py @@ -2,6 +2,7 @@ Run tests against dynamodb using the table abstraction """ import time +from pynamodb.connection.base import Connection from pynamodb.constants import PROVISIONED_THROUGHPUT, READ_CAPACITY_UNITS from pynamodb.connection import TableConnection from pynamodb.expressions.condition import BeginsWith, NotExists @@ -154,3 +155,65 @@ def test_table_integration(ddb_url): conn.scan() print("conn.delete_table...") conn.delete_table() + + +def test_table_conn_without_describe_table_called(ddb_url, table: str): + conn = TableConnection(table_name=table, host=ddb_url) + # conn.describe_table() # bug: operations don't work without calling describe_table first + conn.put_item( + 'item1-hash', + attributes={'foo': {'S': 'bar'}}, + ) + assert conn.get_item('item1-hash').get('Item') == {'id': {'S': 'item1-hash'}, 'foo': {'S': 'bar'}} + + conn.update_item( + 'item1-hash', + actions=[Path('foo').set("rab")] + ) + + assert conn.get_item('item1-hash').get('Item') == {'id': {'S': 'item1-hash'}, 'foo': {'S': 'rab'}} + + conn.delete_item('item1-hash') + assert conn.get_item('item1-hash').get('Item') == None + + +@pytest.fixture +def table(ddb_url): + table_name = 'pynamodb-ci-connection' + + conn = Connection(host=ddb_url) + params = { + 'read_capacity_units': 1, + 'write_capacity_units': 1, + 'attribute_definitions': [ + { + 'attribute_type': STRING, + 'attribute_name': 'id' + }, + ], + 'key_schema': [ + { + 'key_type': HASH, + 'attribute_name': 'id' + } + ], + } + conn.create_table(table_name, **params) + for i in range(0,10): + time.sleep(1) + if conn.describe_table(table_name) is not None: + break + if i == 9: + raise TimeoutError + + for i in range(0,10): + time.sleep(1) + if conn.describe_table(table_name)['TableStatus'] == 'ACTIVE': + break + if i == 9: + raise TimeoutError + + yield table_name + + conn.delete_table(table_name) + From f46a73785fc6be044d0bc974d8e96bc9de57e89b Mon Sep 17 00:00:00 2001 From: bpsoos Date: Wed, 24 Jul 2024 17:21:17 +0200 Subject: [PATCH 2/2] formats --- tests/integration/base_integration_test.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/integration/base_integration_test.py b/tests/integration/base_integration_test.py index b197bef24..d4b02b9e1 100644 --- a/tests/integration/base_integration_test.py +++ b/tests/integration/base_integration_test.py @@ -2,16 +2,12 @@ Runs tests against dynamodb """ import time -from pynamodb.attributes import UnicodeAttribute from pynamodb.connection import Connection -from pynamodb.connection.table import TableConnection -from pynamodb.constants import MAP, PROVISIONED_THROUGHPUT, READ_CAPACITY_UNITS +from pynamodb.constants import PROVISIONED_THROUGHPUT, READ_CAPACITY_UNITS from pynamodb.expressions.condition import BeginsWith, NotExists from pynamodb.expressions.operand import Path, Value from pynamodb.exceptions import TableDoesNotExist -from pynamodb.expressions.update import Action, SetAction -from pynamodb.models import Model from pynamodb.types import STRING, HASH, RANGE, NUMBER import pytest