Skip to content

Commit

Permalink
Merge pull request #19 from xoeye/fix/all_items_for_next_attempt-mult…
Browse files Browse the repository at this point in the history
…i-schema

fix: Update all_items_for_next_attempt to handle different key schemas
  • Loading branch information
Peter Gaultney authored Apr 27, 2021
2 parents 19518bf + 381912f commit 866da95
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.12.1

Update `all_items_for_next_attempt` to be able to handle multiple tables with different key schemas

## 1.12.0

An enhanced API supporting the use of
Expand Down
48 changes: 48 additions & 0 deletions tests/xoto3/dynamodb/write_versioned/prepare_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import pytest

import xoto3.dynamodb.write_versioned as wv
from xoto3.dynamodb.write_versioned.prepare import (
add_item_to_base_request,
all_items_for_next_attempt,
parse_batch_get_request,
)


def test_disallow_non_matching_keys():
with pytest.raises(AssertionError):
parse_batch_get_request(dict(tbl1=[dict(id=1), dict(other_key=2)]))


def test_deduplicate_keys():
req = [dict(id=1), dict(id=1), dict(id=2)]

res = parse_batch_get_request(dict(tbl1=req))
assert res == dict(tbl1=[dict(id=1), dict(id=2)])


def test_add_item():
tname_onto_item_keys = add_item_to_base_request(
dict(table1=[dict(id=1)]), ("table2", dict(id=3)),
)

assert tname_onto_item_keys == dict(table1=[dict(id=1)], table2=[dict(id=3)],)


def test_all_items_for_next_attempt_different_key_schemas():
FooTable = wv.ItemTable("Foo")
BarTable = wv.ItemTable("Bar")
BazTable = wv.ItemTable("Baz")
vt = wv.VersionedTransaction(dict())
vt = FooTable.define("someKey")(vt)
vt = BarTable.define("someOtherKey")(vt)
vt = BazTable.define("aThirdKey")(vt)
vt = FooTable.presume(dict(someKey="foo-1"), dict(someKey="foo-1"))(vt)
vt = BarTable.presume(dict(someOtherKey="bar-1"), dict(someOtherKey="bar-1"))(vt)
vt = BazTable.presume(dict(aThirdKey="baz-1"), dict(aThirdKey="baz-1"))(vt)
vt = BazTable.presume(dict(aThirdKey="baz-2"), dict(aThirdKey="baz-2"))(vt)

result = all_items_for_next_attempt(vt)
assert result["Foo"] == [dict(someKey="foo-1")]
assert result["Bar"] == [dict(someOtherKey="bar-1")]
assert dict(aThirdKey="baz-1") in result["Baz"]
assert dict(aThirdKey="baz-2") in result["Baz"]
Empty file.
23 changes: 0 additions & 23 deletions tests/xoto3/dynamodb/write_versioned/transact/prepare_test.py

This file was deleted.

2 changes: 1 addition & 1 deletion xoto3/__about__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""xoto3"""
__version__ = "1.12.0"
__version__ = "1.12.1"
__author__ = "Peter Gaultney"
__author_email__ = "pgaultney@xoi.io"
9 changes: 7 additions & 2 deletions xoto3/dynamodb/write_versioned/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,21 @@ def all_items_for_next_attempt(
table_name_onto_hashable_keys: Dict[str, Set[HashableItemKey]] = {
table_name: set() for table_name in failed_transaction.tables
}
for (table_name, table_data,) in failed_transaction.tables.items():
table_name_onto_key_attributes: Dict[str, Sequence[str]] = {
table_name: [] for table_name in failed_transaction.tables
}
for (table_name, table_data) in failed_transaction.tables.items():
items, effects, key_attributes = table_data
table_name_onto_key_attributes[table_name] = key_attributes
for hashable_item_key in items.keys():
table_name_onto_hashable_keys[table_name].add(hashable_item_key)
for hashable_item_key in effects.keys():
table_name_onto_hashable_keys[table_name].add(hashable_item_key)

return {
table_name: [
hashable_key_to_key(key_attributes, hashable_key) for hashable_key in hashable_keys
hashable_key_to_key(table_name_onto_key_attributes[table_name], hashable_key)
for hashable_key in hashable_keys
]
for table_name, hashable_keys in table_name_onto_hashable_keys.items()
}

0 comments on commit 866da95

Please sign in to comment.