Skip to content

Commit f73c8b7

Browse files
committed
add tests
1 parent 281370b commit f73c8b7

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

pyiceberg/table/snapshots.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class Snapshot(IcebergBaseModel):
247247
first_row_id: Optional[int] = Field(
248248
alias="first-row-id", default=None, description="assigned to the first row in the first data file in the first manifest"
249249
)
250-
_added_rows: Optional[int] = PrivateAttr()
250+
added_rows: Optional[int] = Field(alias="added-rows", default=None, description="The upper bound of the number of rows with assigned row IDs")
251251

252252
def __str__(self) -> str:
253253
"""Return the string representation of the Snapshot class."""
@@ -261,10 +261,6 @@ def manifests(self, io: FileIO) -> List[ManifestFile]:
261261
"""Return the manifests for the given snapshot."""
262262
return list(_manifests(io, self.manifest_list))
263263

264-
@property
265-
def added_rows(self) -> Optional[int]:
266-
return self._added_rows
267-
268264

269265
class MetadataLogEntry(IcebergBaseModel):
270266
metadata_file: str = Field(alias="metadata-file")

tests/integration/test_writes/test_writes.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
StringType,
6565
UUIDType,
6666
)
67-
from utils import _create_table
67+
from utils import TABLE_SCHEMA, _create_table
6868

6969

7070
@pytest.fixture(scope="session", autouse=True)
@@ -2490,3 +2490,44 @@ def test_stage_only_overwrite_files(
24902490
assert operations == ["append", "append", "delete", "append", "append"]
24912491

24922492
assert parent_snapshot_id == [None, first_snapshot, second_snapshot, second_snapshot, second_snapshot]
2493+
2494+
2495+
@pytest.mark.integration
2496+
def test_v3_write_and_read(spark: SparkSession, session_catalog: Catalog) -> None:
2497+
"""Test writing to a v3 table and reading with Spark."""
2498+
identifier = "default.test_v3_write_and_read"
2499+
tbl = _create_table(session_catalog, identifier, {"format-version": "3"})
2500+
assert tbl.format_version == 3, f"Expected v3, got: v{tbl.format_version}"
2501+
assert tbl.metadata.next_row_id is not None, "Expected next_row_id to be initialized"
2502+
initial_next_row_id = tbl.metadata.next_row_id
2503+
2504+
test_data = pa.Table.from_pydict(
2505+
{
2506+
"bool": [True, False, True],
2507+
"string": ["a", "b", "c"],
2508+
"string_long": ["a_long", "b_long", "c_long"],
2509+
"int": [1, 2, 3],
2510+
"long": [11, 22, 33],
2511+
"float": [1.1, 2.2, 3.3],
2512+
"double": [1.11, 2.22, 3.33],
2513+
"timestamp": [datetime(2023, 1, 1, 1, 1, 1), datetime(2023, 2, 2, 2, 2, 2), datetime(2023, 3, 3, 3, 3, 3)],
2514+
"timestamptz": [
2515+
datetime(2023, 1, 1, 1, 1, 1, tzinfo=pytz.utc),
2516+
datetime(2023, 2, 2, 2, 2, 2, tzinfo=pytz.utc),
2517+
datetime(2023, 3, 3, 3, 3, 3, tzinfo=pytz.utc),
2518+
],
2519+
"date": [date(2023, 1, 1), date(2023, 2, 2), date(2023, 3, 3)],
2520+
"binary": [b"\x01", b"\x02", b"\x03"],
2521+
"fixed": [b"1234567890123456", b"1234567890123456", b"1234567890123456"],
2522+
},
2523+
schema=TABLE_SCHEMA.as_arrow(),
2524+
)
2525+
2526+
tbl.append(test_data)
2527+
2528+
assert (
2529+
tbl.metadata.next_row_id == initial_next_row_id + len(test_data)
2530+
), "Expected next_row_id to be incremented by the number of added rows"
2531+
2532+
df = spark.table(identifier)
2533+
assert df.count() == 3, "Expected 3 rows"

tests/table/test_snapshots.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,15 @@ def test_deserialize_snapshot_with_properties(snapshot_with_properties: Snapshot
139139
def test_snapshot_repr(snapshot: Snapshot) -> None:
140140
assert (
141141
repr(snapshot)
142-
== """Snapshot(snapshot_id=25, parent_snapshot_id=19, sequence_number=200, timestamp_ms=1602638573590, manifest_list='s3:/a/b/c.avro', summary=Summary(Operation.APPEND), schema_id=3, first_row_id=None)"""
142+
== """Snapshot(snapshot_id=25, parent_snapshot_id=19, sequence_number=200, timestamp_ms=1602638573590, manifest_list='s3:/a/b/c.avro', summary=Summary(Operation.APPEND), schema_id=3)"""
143143
)
144144
assert snapshot == eval(repr(snapshot))
145145

146146

147147
def test_snapshot_with_properties_repr(snapshot_with_properties: Snapshot) -> None:
148148
assert (
149149
repr(snapshot_with_properties)
150-
== """Snapshot(snapshot_id=25, parent_snapshot_id=19, sequence_number=200, timestamp_ms=1602638573590, manifest_list='s3:/a/b/c.avro', summary=Summary(Operation.APPEND, **{'foo': 'bar'}), schema_id=3, first_row_id=None)"""
150+
== """Snapshot(snapshot_id=25, parent_snapshot_id=19, sequence_number=200, timestamp_ms=1602638573590, manifest_list='s3:/a/b/c.avro', summary=Summary(Operation.APPEND, **{'foo': 'bar'}), schema_id=3)"""
151151
)
152152
assert snapshot_with_properties == eval(repr(snapshot_with_properties))
153153

0 commit comments

Comments
 (0)