|
| 1 | +import uuid |
| 2 | +from copy import deepcopy |
| 3 | + |
1 | 4 | import boto3
|
2 | 5 | import pytest
|
3 | 6 |
|
|
18 | 21 | simple_key_dict,
|
19 | 22 | )
|
20 | 23 | from ...requests import (
|
| 24 | + basic_delete_item_request_ddb, |
21 | 25 | basic_put_item_request_ddb,
|
22 | 26 | basic_put_item_request_dict,
|
23 | 27 | basic_query_paginator_request,
|
@@ -87,36 +91,68 @@ def use_complex_item(request):
|
87 | 91 | return request.param
|
88 | 92 |
|
89 | 93 |
|
| 94 | +# Append a suffix to the partition key to avoid collisions between test runs. |
| 95 | +@pytest.fixture(scope="module") |
| 96 | +def test_run_suffix(): |
| 97 | + return str(uuid.uuid4()) |
| 98 | + |
| 99 | + |
90 | 100 | @pytest.fixture
|
91 |
| -def test_key(expect_standard_dictionaries, use_complex_item): |
| 101 | +def test_key(expect_standard_dictionaries, use_complex_item, test_run_suffix): |
92 | 102 | """Get a single test item in the appropriate format for the client."""
|
93 | 103 | if expect_standard_dictionaries:
|
94 | 104 | if use_complex_item:
|
95 |
| - return complex_key_dict |
96 |
| - return simple_key_dict |
97 |
| - if use_complex_item: |
98 |
| - return complex_key_ddb |
99 |
| - return simple_key_ddb |
| 105 | + key = deepcopy(complex_key_dict) |
| 106 | + else: |
| 107 | + key = deepcopy(simple_key_dict) |
| 108 | + else: |
| 109 | + if use_complex_item: |
| 110 | + key = deepcopy(complex_key_ddb) |
| 111 | + else: |
| 112 | + key = deepcopy(simple_key_ddb) |
| 113 | + # Add a suffix to the partition key to avoid collisions between test runs. |
| 114 | + if isinstance(key["partition_key"], dict): |
| 115 | + key["partition_key"]["S"] += test_run_suffix |
| 116 | + else: |
| 117 | + key["partition_key"] += test_run_suffix |
| 118 | + return key |
100 | 119 |
|
101 | 120 |
|
102 | 121 | @pytest.fixture
|
103 |
| -def multiple_test_keys(expect_standard_dictionaries): |
| 122 | +def multiple_test_keys(expect_standard_dictionaries, test_run_suffix): |
104 | 123 | """Get two test keys in the appropriate format for the client."""
|
105 | 124 | if expect_standard_dictionaries:
|
106 |
| - return [simple_key_dict, complex_key_dict] |
107 |
| - return [simple_key_ddb, complex_key_ddb] |
| 125 | + keys = [deepcopy(simple_key_dict), deepcopy(complex_key_dict)] |
| 126 | + else: |
| 127 | + keys = [deepcopy(simple_key_ddb), deepcopy(complex_key_ddb)] |
| 128 | + # Add a suffix to the partition key to avoid collisions between test runs. |
| 129 | + for key in keys: |
| 130 | + if isinstance(key["partition_key"], dict): |
| 131 | + key["partition_key"]["S"] += test_run_suffix |
| 132 | + else: |
| 133 | + key["partition_key"] += test_run_suffix |
| 134 | + return keys |
108 | 135 |
|
109 | 136 |
|
110 | 137 | @pytest.fixture
|
111 |
| -def test_item(expect_standard_dictionaries, use_complex_item): |
| 138 | +def test_item(expect_standard_dictionaries, use_complex_item, test_run_suffix): |
112 | 139 | """Get a single test item in the appropriate format for the client."""
|
113 | 140 | if expect_standard_dictionaries:
|
114 | 141 | if use_complex_item:
|
115 |
| - return complex_item_dict |
116 |
| - return simple_item_dict |
117 |
| - if use_complex_item: |
118 |
| - return complex_item_ddb |
119 |
| - return simple_item_ddb |
| 142 | + item = deepcopy(complex_item_dict) |
| 143 | + else: |
| 144 | + item = deepcopy(simple_item_dict) |
| 145 | + else: |
| 146 | + if use_complex_item: |
| 147 | + item = deepcopy(complex_item_ddb) |
| 148 | + else: |
| 149 | + item = deepcopy(simple_item_ddb) |
| 150 | + # Add a suffix to the partition key to avoid collisions between test runs. |
| 151 | + if isinstance(item["partition_key"], dict): |
| 152 | + item["partition_key"]["S"] += test_run_suffix |
| 153 | + else: |
| 154 | + item["partition_key"] += test_run_suffix |
| 155 | + return item |
120 | 156 |
|
121 | 157 |
|
122 | 158 | @pytest.fixture
|
@@ -188,3 +224,14 @@ def test_GIVEN_scan_paginator_WHEN_paginate_THEN_returns_expected_items(
|
188 | 224 | actual_item = sort_dynamodb_json_lists(items[0])
|
189 | 225 | # Then: Items are equal
|
190 | 226 | assert expected_item == actual_item
|
| 227 | + |
| 228 | + |
| 229 | +# Delete the items in the table after the module runs |
| 230 | +@pytest.fixture(scope="module", autouse=True) |
| 231 | +def cleanup_after_module(test_run_suffix): |
| 232 | + yield |
| 233 | + table = boto3.client("dynamodb") |
| 234 | + items = [deepcopy(simple_item_ddb), deepcopy(complex_item_ddb)] |
| 235 | + for item in items: |
| 236 | + item["partition_key"]["S"] += test_run_suffix |
| 237 | + table.delete_item(**basic_delete_item_request_ddb(item)) |
0 commit comments