From 1e26c24f74c93794fd71000ee475163be82f7dd1 Mon Sep 17 00:00:00 2001 From: fox0430 Date: Thu, 16 Apr 2026 17:44:54 +0900 Subject: [PATCH] Refactor parseAffectedRows and add tests --- async_postgres/pg_types.nim | 10 ++++------ tests/test_types.nim | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/async_postgres/pg_types.nim b/async_postgres/pg_types.nim index ea454e7..8c02674 100644 --- a/async_postgres/pg_types.nim +++ b/async_postgres/pg_types.nim @@ -1912,12 +1912,10 @@ converter toRow*(cells: seq[Option[seq[byte]]]): Row = proc parseAffectedRows*(tag: string): int64 = ## Extract row count from command tag (e.g. "UPDATE 3" -> 3, "INSERT 0 1" -> 1). let parts = tag.split(' ') - if parts.len > 0: - try: - return parseBiggestInt(parts[^1]) - except ValueError: - return 0 - return 0 + try: + parseBiggestInt(parts[^1]) + except ValueError, OverflowDefect: + 0 proc initCommandResult*(tag: string): CommandResult {.inline.} = CommandResult(commandTag: tag) diff --git a/tests/test_types.nim b/tests/test_types.nim index 0d32526..8a8ce05 100644 --- a/tests/test_types.nim +++ b/tests/test_types.nim @@ -989,6 +989,27 @@ suite "parseAffectedRows": test "non-numeric tag": check parseAffectedRows("CREATE TABLE") == 0 + test "COPY tag": + check parseAffectedRows("COPY 100") == 100 + + test "MERGE tag": + check parseAffectedRows("MERGE 7") == 7 + + test "MOVE tag": + check parseAffectedRows("MOVE 12") == 12 + + test "FETCH tag": + check parseAffectedRows("FETCH 4") == 4 + + test "trailing whitespace falls back to 0": + check parseAffectedRows("UPDATE 3 ") == 0 + + test "overflow falls back to 0": + check parseAffectedRows("UPDATE 99999999999999999999999999") == 0 + + test "single-token DDL tag": + check parseAffectedRows("BEGIN") == 0 + suite "Option accessors": test "getStrOpt some": let row: Row = @[some(toBytes("hello"))]