forked from apache/iceberg-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dynamodb.py
More file actions
578 lines (499 loc) · 25.5 KB
/
test_dynamodb.py
File metadata and controls
578 lines (499 loc) · 25.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from typing import List
from unittest import mock
import boto3
import pyarrow as pa
import pytest
from moto import mock_aws
from pyiceberg.catalog import METADATA_LOCATION, TABLE_TYPE
from pyiceberg.catalog.dynamodb import (
ACTIVE,
DYNAMODB_COL_CREATED_AT,
DYNAMODB_COL_IDENTIFIER,
DYNAMODB_COL_NAMESPACE,
DYNAMODB_TABLE_NAME_DEFAULT,
DynamoDbCatalog,
_add_property_prefix,
)
from pyiceberg.exceptions import (
NamespaceAlreadyExistsError,
NamespaceNotEmptyError,
NoSuchIcebergTableError,
NoSuchNamespaceError,
NoSuchPropertyException,
NoSuchTableError,
TableAlreadyExistsError,
)
from pyiceberg.schema import Schema
from tests.conftest import BUCKET_NAME, TABLE_METADATA_LOCATION_REGEX
@mock_aws
def test_create_dynamodb_catalog_with_table_name(_dynamodb, _bucket_initialize: None) -> None: # type: ignore
DynamoDbCatalog("test_ddb_catalog")
response = _dynamodb.describe_table(TableName=DYNAMODB_TABLE_NAME_DEFAULT)
assert response["Table"]["TableName"] == DYNAMODB_TABLE_NAME_DEFAULT
assert response["Table"]["TableStatus"] == ACTIVE
custom_table_name = "custom_table_name"
DynamoDbCatalog("test_ddb_catalog", **{"table-name": custom_table_name})
response = _dynamodb.describe_table(TableName=custom_table_name)
assert response["Table"]["TableName"] == custom_table_name
assert response["Table"]["TableStatus"] == ACTIVE
@mock_aws
def test_create_table_with_database_location(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "test_ddb_catalog"
identifier = (database_name, table_name)
test_catalog = DynamoDbCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name, properties={"location": f"s3://{BUCKET_NAME}/{database_name}.db"})
table = test_catalog.create_table(identifier, table_schema_nested)
assert table.identifier == (catalog_name,) + identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
@mock_aws
def test_create_table_with_pyarrow_schema(
_bucket_initialize: None,
moto_endpoint_url: str,
pyarrow_schema_simple_without_ids: pa.Schema,
database_name: str,
table_name: str,
) -> None:
catalog_name = "test_ddb_catalog"
identifier = (database_name, table_name)
test_catalog = DynamoDbCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name, properties={"location": f"s3://{BUCKET_NAME}/{database_name}.db"})
table = test_catalog.create_table(identifier, pyarrow_schema_simple_without_ids)
assert table.identifier == (catalog_name,) + identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
@mock_aws
def test_create_table_with_default_warehouse(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "test_ddb_catalog"
identifier = (database_name, table_name)
test_catalog = DynamoDbCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}"})
test_catalog.create_namespace(namespace=database_name)
table = test_catalog.create_table(identifier, table_schema_nested)
assert table.identifier == (catalog_name,) + identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
@mock_aws
def test_create_table_with_given_location(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "test_ddb_catalog"
identifier = (database_name, table_name)
test_catalog = DynamoDbCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
table = test_catalog.create_table(
identifier=identifier, schema=table_schema_nested, location=f"s3://{BUCKET_NAME}/{database_name}.db/{table_name}"
)
assert table.identifier == (catalog_name,) + identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
@mock_aws
def test_create_table_with_no_location(
_bucket_initialize: None, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
identifier = (database_name, table_name)
test_catalog = DynamoDbCatalog("test_ddb_catalog")
test_catalog.create_namespace(namespace=database_name)
with pytest.raises(ValueError):
test_catalog.create_table(identifier=identifier, schema=table_schema_nested)
@mock_aws
def test_create_table_with_strips(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "test_ddb_catalog"
identifier = (database_name, table_name)
test_catalog = DynamoDbCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name, properties={"location": f"s3://{BUCKET_NAME}/{database_name}.db/"})
table = test_catalog.create_table(identifier, table_schema_nested)
assert table.identifier == (catalog_name,) + identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
@mock_aws
def test_create_table_with_strips_bucket_root(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "test_ddb_catalog"
identifier = (database_name, table_name)
test_catalog = DynamoDbCatalog(catalog_name, **{"s3.endpoint": moto_endpoint_url, "warehouse": f"s3://{BUCKET_NAME}/"})
test_catalog.create_namespace(namespace=database_name)
table_strip = test_catalog.create_table(identifier, table_schema_nested)
assert table_strip.identifier == (catalog_name,) + identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table_strip.metadata_location)
@mock_aws
def test_create_table_with_no_database(
_bucket_initialize: None, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
identifier = (database_name, table_name)
test_catalog = DynamoDbCatalog("test_ddb_catalog")
with pytest.raises(NoSuchNamespaceError):
test_catalog.create_table(identifier=identifier, schema=table_schema_nested)
@mock_aws
def test_create_duplicated_table(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
identifier = (database_name, table_name)
test_catalog = DynamoDbCatalog("test_ddb_catalog", **{"warehouse": f"s3://{BUCKET_NAME}", "s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
test_catalog.create_table(identifier, table_schema_nested)
with pytest.raises(TableAlreadyExistsError):
test_catalog.create_table(identifier, table_schema_nested)
@mock_aws
def test_create_table_if_not_exists_duplicated_table(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
identifier = (database_name, table_name)
test_catalog = DynamoDbCatalog("test_ddb_catalog", **{"warehouse": f"s3://{BUCKET_NAME}", "s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
table1 = test_catalog.create_table(identifier, table_schema_nested)
table2 = test_catalog.create_table_if_not_exists(identifier, table_schema_nested)
assert table1.identifier == table2.identifier
@mock_aws
def test_load_table(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "test_ddb_catalog"
identifier = (database_name, table_name)
test_catalog = DynamoDbCatalog(catalog_name, **{"warehouse": f"s3://{BUCKET_NAME}", "s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
test_catalog.create_table(identifier, table_schema_nested)
table = test_catalog.load_table(identifier)
assert table.identifier == (catalog_name,) + identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
@mock_aws
def test_load_table_from_self_identifier(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "test_ddb_catalog"
identifier = (database_name, table_name)
test_catalog = DynamoDbCatalog(catalog_name, **{"warehouse": f"s3://{BUCKET_NAME}", "s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
test_catalog.create_table(identifier, table_schema_nested)
intermediate = test_catalog.load_table(identifier)
table = test_catalog.load_table(intermediate.identifier)
assert table.identifier == (catalog_name,) + identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
@mock_aws
def test_load_non_exist_table(_bucket_initialize: None, database_name: str, table_name: str) -> None:
identifier = (database_name, table_name)
test_catalog = DynamoDbCatalog("test_ddb_catalog", warehouse=f"s3://{BUCKET_NAME}")
test_catalog.create_namespace(namespace=database_name)
with pytest.raises(NoSuchTableError):
test_catalog.load_table(identifier)
@mock_aws
def test_drop_table(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "test_ddb_catalog"
identifier = (database_name, table_name)
test_catalog = DynamoDbCatalog(catalog_name, **{"warehouse": f"s3://{BUCKET_NAME}", "s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
test_catalog.create_table(identifier, table_schema_nested)
table = test_catalog.load_table(identifier)
assert table.identifier == (catalog_name,) + identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
test_catalog.drop_table(identifier)
with pytest.raises(NoSuchTableError):
test_catalog.load_table(identifier)
@mock_aws
def test_drop_table_from_self_identifier(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "test_ddb_catalog"
identifier = (database_name, table_name)
test_catalog = DynamoDbCatalog(catalog_name, **{"warehouse": f"s3://{BUCKET_NAME}", "s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
test_catalog.create_table(identifier, table_schema_nested)
table = test_catalog.load_table(identifier)
assert table.identifier == (catalog_name,) + identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
test_catalog.drop_table(table.identifier)
with pytest.raises(NoSuchTableError):
test_catalog.load_table(identifier)
with pytest.raises(NoSuchTableError):
test_catalog.load_table(table.identifier)
@mock_aws
def test_drop_non_exist_table(_bucket_initialize: None, database_name: str, table_name: str) -> None:
identifier = (database_name, table_name)
test_catalog = DynamoDbCatalog("test_ddb_catalog", warehouse=f"s3://{BUCKET_NAME}")
with pytest.raises(NoSuchTableError):
test_catalog.drop_table(identifier)
@mock_aws
def test_rename_table(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "test_ddb_catalog"
new_table_name = f"{table_name}_new"
identifier = (database_name, table_name)
new_identifier = (database_name, new_table_name)
test_catalog = DynamoDbCatalog(catalog_name, **{"warehouse": f"s3://{BUCKET_NAME}", "s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
table = test_catalog.create_table(identifier, table_schema_nested)
assert table.identifier == (catalog_name,) + identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
test_catalog.rename_table(identifier, new_identifier)
new_table = test_catalog.load_table(new_identifier)
assert new_table.identifier == (catalog_name,) + new_identifier
# the metadata_location should not change
assert new_table.metadata_location == table.metadata_location
# old table should be dropped
with pytest.raises(NoSuchTableError):
test_catalog.load_table(identifier)
@mock_aws
def test_rename_table_from_self_identifier(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
catalog_name = "test_ddb_catalog"
new_table_name = f"{table_name}_new"
identifier = (database_name, table_name)
new_identifier = (database_name, new_table_name)
test_catalog = DynamoDbCatalog(catalog_name, **{"warehouse": f"s3://{BUCKET_NAME}", "s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
table = test_catalog.create_table(identifier, table_schema_nested)
assert table.identifier == (catalog_name,) + identifier
assert TABLE_METADATA_LOCATION_REGEX.match(table.metadata_location)
test_catalog.rename_table(table.identifier, new_identifier)
new_table = test_catalog.load_table(new_identifier)
assert new_table.identifier == (catalog_name,) + new_identifier
# the metadata_location should not change
assert new_table.metadata_location == table.metadata_location
# old table should be dropped
with pytest.raises(NoSuchTableError):
test_catalog.load_table(identifier)
with pytest.raises(NoSuchTableError):
test_catalog.load_table(table.identifier)
@mock_aws
def test_fail_on_rename_table_with_missing_required_params(_bucket_initialize: None, database_name: str, table_name: str) -> None:
new_database_name = f"{database_name}_new"
new_table_name = f"{table_name}_new"
identifier = (database_name, table_name)
new_identifier = (new_database_name, new_table_name)
test_catalog = DynamoDbCatalog("test_ddb_catalog", warehouse=f"s3://{BUCKET_NAME}")
test_catalog.create_namespace(namespace=database_name)
test_catalog.create_namespace(namespace=new_database_name)
# Missing required params
# pylint: disable=W0212
test_catalog._put_dynamo_item(
item={
DYNAMODB_COL_IDENTIFIER: {"S": f"{database_name}.{table_name}"},
DYNAMODB_COL_NAMESPACE: {"S": database_name},
},
condition_expression=f"attribute_not_exists({DYNAMODB_COL_IDENTIFIER})",
)
with pytest.raises(NoSuchPropertyException):
test_catalog.rename_table(identifier, new_identifier)
@mock_aws
def test_fail_on_rename_non_iceberg_table(
_dynamodb: boto3.client, _bucket_initialize: None, database_name: str, table_name: str
) -> None:
new_database_name = f"{database_name}_new"
new_table_name = f"{table_name}_new"
identifier = (database_name, table_name)
new_identifier = (new_database_name, new_table_name)
test_catalog = DynamoDbCatalog("test_ddb_catalog", warehouse=f"s3://{BUCKET_NAME}")
test_catalog.create_namespace(namespace=database_name)
test_catalog.create_namespace(namespace=new_database_name)
# Wrong TABLE_TYPE param
# pylint: disable=W0212
test_catalog._put_dynamo_item(
item={
DYNAMODB_COL_IDENTIFIER: {"S": f"{database_name}.{table_name}"},
DYNAMODB_COL_NAMESPACE: {"S": database_name},
DYNAMODB_COL_CREATED_AT: {"S": "test-1873287263487623"},
_add_property_prefix(TABLE_TYPE): {"S": "non-iceberg-table-type"},
_add_property_prefix(METADATA_LOCATION): {"S": "test-metadata-location"},
},
condition_expression=f"attribute_not_exists({DYNAMODB_COL_IDENTIFIER})",
)
with pytest.raises(NoSuchIcebergTableError):
test_catalog.rename_table(identifier, new_identifier)
@mock_aws
def test_list_tables(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_list: List[str]
) -> None:
test_catalog = DynamoDbCatalog("test_ddb_catalog", **{"warehouse": f"s3://{BUCKET_NAME}", "s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
for table_name in table_list:
test_catalog.create_table((database_name, table_name), table_schema_nested)
loaded_table_list = test_catalog.list_tables(database_name)
for table_name in table_list:
assert (database_name, table_name) in loaded_table_list
@mock_aws
def test_list_namespaces(_bucket_initialize: None, database_list: List[str]) -> None:
test_catalog = DynamoDbCatalog("test_ddb_catalog")
for database_name in database_list:
test_catalog.create_namespace(namespace=database_name)
loaded_database_list = test_catalog.list_namespaces()
for database_name in database_list:
assert (database_name,) in loaded_database_list
@mock_aws
def test_create_namespace_no_properties(_bucket_initialize: None, database_name: str) -> None:
test_catalog = DynamoDbCatalog("test_ddb_catalog")
test_catalog.create_namespace(namespace=database_name)
loaded_database_list = test_catalog.list_namespaces()
assert len(loaded_database_list) == 1
assert (database_name,) in loaded_database_list
properties = test_catalog.load_namespace_properties(database_name)
assert properties == {}
@mock_aws
def test_create_namespace_with_comment_and_location(_bucket_initialize: None, database_name: str) -> None:
test_location = f"s3://{BUCKET_NAME}/{database_name}.db"
test_properties = {
"comment": "this is a test description",
"location": test_location,
}
test_catalog = DynamoDbCatalog("test_ddb_catalog")
test_catalog.create_namespace(namespace=database_name, properties=test_properties)
loaded_database_list = test_catalog.list_namespaces()
assert len(loaded_database_list) == 1
assert (database_name,) in loaded_database_list
properties = test_catalog.load_namespace_properties(database_name)
assert properties["comment"] == "this is a test description"
assert properties["location"] == test_location
@mock_aws
def test_create_duplicated_namespace(_bucket_initialize: None, database_name: str) -> None:
test_catalog = DynamoDbCatalog("test_ddb_catalog")
test_catalog.create_namespace(namespace=database_name)
loaded_database_list = test_catalog.list_namespaces()
assert len(loaded_database_list) == 1
assert (database_name,) in loaded_database_list
with pytest.raises(NamespaceAlreadyExistsError):
test_catalog.create_namespace(namespace=database_name, properties={"test": "test"})
@mock_aws
def test_drop_namespace(_bucket_initialize: None, database_name: str) -> None:
test_catalog = DynamoDbCatalog("test_ddb_catalog")
test_catalog.create_namespace(namespace=database_name)
loaded_database_list = test_catalog.list_namespaces()
assert len(loaded_database_list) == 1
assert (database_name,) in loaded_database_list
test_catalog.drop_namespace(database_name)
loaded_database_list = test_catalog.list_namespaces()
assert len(loaded_database_list) == 0
@mock_aws
def test_drop_non_empty_namespace(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
identifier = (database_name, table_name)
test_catalog = DynamoDbCatalog("test_ddb_catalog", **{"warehouse": f"s3://{BUCKET_NAME}", "s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
test_catalog.create_table(identifier, table_schema_nested)
assert len(test_catalog.list_tables(database_name)) == 1
with pytest.raises(NamespaceNotEmptyError):
test_catalog.drop_namespace(database_name)
@mock_aws
def test_drop_non_exist_namespace(_bucket_initialize: None, database_name: str) -> None:
test_catalog = DynamoDbCatalog("test_ddb_catalog")
with pytest.raises(NoSuchNamespaceError):
test_catalog.drop_namespace(database_name)
@mock_aws
def test_load_namespace_properties(_bucket_initialize: None, database_name: str) -> None:
test_location = f"s3://{BUCKET_NAME}/{database_name}.db"
test_properties = {
"comment": "this is a test description",
"location": test_location,
"test_property1": "1",
"test_property2": "2",
"test_property3": "3",
}
test_catalog = DynamoDbCatalog("test_ddb_catalog")
test_catalog.create_namespace(database_name, test_properties)
listed_properties = test_catalog.load_namespace_properties(database_name)
for k, v in listed_properties.items():
assert k in test_properties
assert v == test_properties[k]
@mock_aws
def test_load_non_exist_namespace_properties(_bucket_initialize: None, database_name: str) -> None:
test_catalog = DynamoDbCatalog("test_ddb_catalog")
with pytest.raises(NoSuchNamespaceError):
test_catalog.load_namespace_properties(database_name)
@mock_aws
def test_update_namespace_properties(_bucket_initialize: None, database_name: str) -> None:
test_properties = {
"comment": "this is a test description",
"location": f"s3://{BUCKET_NAME}/{database_name}.db",
"test_property1": "1",
"test_property2": "2",
"test_property3": "3",
}
removals = {"test_property1", "test_property2", "test_property3", "should_not_removed"}
updates = {"test_property4": "4", "test_property5": "5", "comment": "updated test description"}
test_catalog = DynamoDbCatalog("test_ddb_catalog")
test_catalog.create_namespace(database_name, test_properties)
update_report = test_catalog.update_namespace_properties(database_name, removals, updates)
for k in updates.keys():
assert k in update_report.updated
for k in removals:
if k == "should_not_removed":
assert k in update_report.missing
else:
assert k in update_report.removed
assert "updated test description" == test_catalog.load_namespace_properties(database_name)["comment"]
test_catalog.drop_namespace(database_name)
@mock_aws
def test_load_empty_namespace_properties(_bucket_initialize: None, database_name: str) -> None:
test_catalog = DynamoDbCatalog("test_ddb_catalog")
test_catalog.create_namespace(database_name)
listed_properties = test_catalog.load_namespace_properties(database_name)
assert listed_properties == {}
@mock_aws
def test_update_namespace_properties_overlap_update_removal(_bucket_initialize: None, database_name: str) -> None:
test_properties = {
"comment": "this is a test description",
"location": f"s3://{BUCKET_NAME}/{database_name}.db",
"test_property1": "1",
"test_property2": "2",
"test_property3": "3",
}
removals = {"test_property1", "test_property2", "test_property3", "should_not_removed"}
updates = {"test_property1": "4", "test_property5": "5", "comment": "updated test description"}
test_catalog = DynamoDbCatalog("test_ddb_catalog")
test_catalog.create_namespace(database_name, test_properties)
with pytest.raises(ValueError):
test_catalog.update_namespace_properties(database_name, removals, updates)
# should not modify the properties
assert test_catalog.load_namespace_properties(database_name) == test_properties
def test_passing_provided_profile() -> None:
catalog_name = "test_ddb_catalog"
session_props = {
"aws_access_key_id": "abc",
"aws_secret_access_key": "def",
"aws_session_token": "ghi",
"region_name": "eu-central-1",
"botocore_session": None,
"profile_name": None,
}
props = {"py-io-impl": "pyiceberg.io.fsspec.FsspecFileIO"}
props.update(session_props) # type: ignore
with mock.patch('boto3.Session', return_value=mock.Mock()) as mock_session:
mock_client = mock.Mock()
mock_session.return_value.client.return_value = mock_client
mock_client.describe_table.return_value = {'Table': {'TableStatus': 'ACTIVE'}}
test_catalog = DynamoDbCatalog(catalog_name, **props)
assert test_catalog.dynamodb is mock_client
mock_session.assert_called_with(**session_props)
assert test_catalog.dynamodb is mock_session().client()
@mock_aws
def test_table_exists(
_bucket_initialize: None, moto_endpoint_url: str, table_schema_nested: Schema, database_name: str, table_name: str
) -> None:
identifier = (database_name, table_name)
test_catalog = DynamoDbCatalog("test_ddb_catalog", **{"warehouse": f"s3://{BUCKET_NAME}", "s3.endpoint": moto_endpoint_url})
test_catalog.create_namespace(namespace=database_name)
test_catalog.create_table(identifier, table_schema_nested)
# Act and Assert for an existing table
assert test_catalog.table_exists(identifier) is True
# Act and Assert for an non-existing table
assert test_catalog.table_exists(('non', 'exist')) is False