Skip to content

Commit ca52be5

Browse files
committed
Add unit tests
1 parent e0df693 commit ca52be5

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

tests/table/test_deletes.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
from pathlib import PosixPath
18+
19+
import pyarrow as pa
20+
import pytest
21+
22+
from pyiceberg.catalog import Catalog
23+
from pyiceberg.catalog.memory import InMemoryCatalog
24+
from pyiceberg.exceptions import NoSuchTableError
25+
from pyiceberg.expressions import LessThanOrEqual
26+
from pyiceberg.manifest import ManifestContent, ManifestEntryStatus
27+
28+
29+
@pytest.fixture
30+
def catalog(tmp_path: PosixPath) -> InMemoryCatalog:
31+
catalog = InMemoryCatalog("test.in_memory.catalog", warehouse=tmp_path.absolute().as_posix())
32+
catalog.create_namespace("default")
33+
return catalog
34+
35+
36+
def _drop_table(catalog: Catalog, identifier: str) -> None:
37+
try:
38+
catalog.drop_table(identifier)
39+
except NoSuchTableError:
40+
pass
41+
42+
43+
def test_manifest_entry_after_deletes(catalog: Catalog) -> None:
44+
identifier = "default.test_manifest_entry_after_deletes"
45+
_drop_table(catalog, identifier)
46+
47+
schema = pa.schema(
48+
[
49+
("id", pa.int32()),
50+
("name", pa.string()),
51+
]
52+
)
53+
54+
table = catalog.create_table(identifier, schema)
55+
data = pa.Table.from_pylist(
56+
[
57+
{"id": 1, "name": "foo"},
58+
{"id": 2, "name": "bar"},
59+
{"id": 3, "name": "bar"},
60+
{"id": 4, "name": "bar"},
61+
],
62+
schema=schema,
63+
)
64+
table.append(data)
65+
66+
def assert_manifest_entry(expected_status: ManifestEntryStatus, expected_snapshot_id: int) -> None:
67+
current_snapshot = table.refresh().current_snapshot()
68+
manifest_files = current_snapshot.manifests(table.io)
69+
assert len(manifest_files) == 1
70+
71+
entries = manifest_files[0].fetch_manifest_entry(table.io, discard_deleted=False)
72+
assert len(entries) == 1
73+
entry = entries[0]
74+
assert entry.status == expected_status
75+
assert entry.snapshot_id == expected_snapshot_id
76+
77+
before_delete_snapshot_id = table.current_snapshot().snapshot_id
78+
assert_manifest_entry(ManifestEntryStatus.ADDED, before_delete_snapshot_id)
79+
80+
table.delete(LessThanOrEqual("id", 4))
81+
after_delete_snapshot_id = table.refresh().current_snapshot().snapshot_id
82+
assert_manifest_entry(ManifestEntryStatus.DELETED, after_delete_snapshot_id)

0 commit comments

Comments
 (0)