# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/notwithstanding-what-mock-and-assertion-shortcuts-must-an-instance-decouple-test-oracles-from-implementation-internals-to-prevent-tautological-green-builds.md # Remnant Warrant: https://corpusfetch.com/c/notwithstanding-what-mock-and-assertion-shortcuts-must-an-instance-decouple-test-oracles-from-implementation-internals-to-prevent-tautological-green-builds.warrant.json # Markdown SHA-256: 1f9d0ca8774ee67825200918896ffc93774fe1a4b9e51aadf3f763656bdd29e8 # Code AST SHA-256: e8fe76dfccdcb515202150475fe14526325f061de7f4bc1030129b1008740566 # Verified Classes: TestTransferFundsContract # Defensive Guards: 5 fail-closed invariants (zero external deps) import sqlite3 import unittest from typing import Any # --- Production Function Under Test --- def transfer_funds( conn: sqlite3.Connection, from_acct: str, to_acct: str, amount_cents: int, ) -> dict[str, int]: '''Transfers positive integer cents between two accounts atomically.''' if isinstance(amount_cents, bool) or not isinstance(amount_cents, int) or amount_cents <= 0: raise ValueError("amount_cents must be a positive integer") if from_acct == to_acct: raise ValueError("Cannot transfer to the same account") with conn: row = conn.execute( "SELECT balance_cents FROM accounts WHERE id = ?", (from_acct,) ).fetchone() if row is None: raise KeyError(f"Source account {from_acct!r} not found") if row[0] < amount_cents: raise RuntimeError("Insufficient funds") dest = conn.execute( "SELECT balance_cents FROM accounts WHERE id = ?", (to_acct,) ).fetchone() if dest is None: raise KeyError(f"Destination account {to_acct!r} not found") conn.execute( "UPDATE accounts SET balance_cents = balance_cents - ? WHERE id = ?", (amount_cents, from_acct), ) conn.execute( "UPDATE accounts SET balance_cents = balance_cents + ? WHERE id = ?", (amount_cents, to_acct), ) updated = dict( conn.execute( "SELECT id, balance_cents FROM accounts WHERE id IN (?, ?)", (from_acct, to_acct), ).fetchall() ) return updated # --- Contract-Level Verification Suite (Zero Internal Mocks) --- class TestTransferFundsContract(unittest.TestCase): def setUp(self) -> None: # Gate 1: Use real in-memory SQLite engine rather than mocking cursor.execute() self.conn = sqlite3.connect(":memory:") self.conn.execute( "CREATE TABLE accounts (id TEXT PRIMARY KEY, balance_cents INTEGER NOT NULL)" ) self.conn.executemany( "INSERT INTO accounts (id, balance_cents) VALUES (?, ?)", [("alice", 1000), ("bob", 250)], ) self.conn.commit() def tearDown(self) -> None: self.conn.close() def test_transfer_updates_both_persisted_balances_exact_literals(self) -> None: # Gate 2: Assert against exact pre-computed literals, never `1000 - amount` result = transfer_funds(self.conn, "alice", "bob", 400) self.assertEqual(result, {"alice": 600, "bob": 650}) # Verify actual persisted database state independently of return value persisted = dict( self.conn.execute("SELECT id, balance_cents FROM accounts").fetchall() ) self.assertEqual(len(persisted), 2) # Non-empty guard before inspecting self.assertEqual(persisted["alice"], 600) self.assertEqual(persisted["bob"], 650) def test_insufficient_funds_aborts_and_leaves_balances_unchanged(self) -> None: # Gate 3: Verify negative boundary AND atomic rollback invariant with self.assertRaises(RuntimeError): transfer_funds(self.conn, "alice", "bob", 1500) persisted = dict( self.conn.execute("SELECT id, balance_cents FROM accounts").fetchall() ) self.assertEqual(persisted, {"alice": 1000, "bob": 250}) def test_rejects_boolean_and_non_positive_amounts(self) -> None: for invalid in (0, -50, True, False): with self.assertRaises(ValueError): transfer_funds(self.conn, "alice", "bob", invalid) # type: ignore[arg-type]