diff --git a/includes/utilities.php b/includes/utilities.php index 5978072..c2cd183 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -147,11 +147,14 @@ function processLogEntries($logEntries, $user, $insertStmt) { continue; } + $runningBalance = isset($entry['data']['balance']) ? (int)$entry['data']['balance'] : null; + $insertStmt->bindValue(':id', $id); $insertStmt->bindValue(':user', $user); $insertStmt->bindValue(':timestamp', $timestamp); $insertStmt->bindValue(':description', $description); $insertStmt->bindValue(':amount', $amount); + $insertStmt->bindValue(':running_balance', $runningBalance); $insertStmt->execute(); } } @@ -250,8 +253,8 @@ function fetchAndStoreLogPage($pdo, $user, $url) { validateApiResponse($responseData); $insertStmt = $pdo->prepare( - 'INSERT INTO vault (id, user, timestamp, description, amount) ' - . 'VALUES (:id, :user, :timestamp, :description, :amount) ' + 'INSERT INTO vault (id, user, timestamp, description, amount, running_balance) ' + . 'VALUES (:id, :user, :timestamp, :description, :amount, :running_balance) ' . 'ON CONFLICT(id) DO NOTHING' ); diff --git a/tests/process_log_entries_test.php b/tests/process_log_entries_test.php index 39c8c6c..e78455a 100644 --- a/tests/process_log_entries_test.php +++ b/tests/process_log_entries_test.php @@ -69,17 +69,18 @@ $pdo = new PDO('sqlite::memory:'); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec( 'CREATE TABLE vault ( - id TEXT PRIMARY KEY, - user TEXT NOT NULL, - timestamp INTEGER NOT NULL, - description TEXT NOT NULL, - amount REAL NOT NULL + id TEXT PRIMARY KEY, + user TEXT NOT NULL, + timestamp INTEGER NOT NULL, + description TEXT NOT NULL, + amount REAL NOT NULL, + running_balance INTEGER )' ); $insertStmt = $pdo->prepare( - 'INSERT INTO vault (id, user, timestamp, description, amount) ' - . 'VALUES (:id, :user, :timestamp, :description, :amount) ' + 'INSERT INTO vault (id, user, timestamp, description, amount, running_balance) ' + . 'VALUES (:id, :user, :timestamp, :description, :amount, :running_balance) ' . 'ON CONFLICT(id) DO NOTHING' ); @@ -100,7 +101,7 @@ $count = (int)$pdo->query('SELECT COUNT(*) FROM vault')->fetchColumn(); assertSame(100, $count, 'row count'); // Spot-check first entry (newest, withdraw) and last (oldest, deposit). -$rows = $pdo->query('SELECT id, user, timestamp, description, amount FROM vault ORDER BY timestamp DESC')->fetchAll(PDO::FETCH_ASSOC); +$rows = $pdo->query('SELECT id, user, timestamp, description, amount, running_balance FROM vault ORDER BY timestamp DESC')->fetchAll(PDO::FETCH_ASSOC); $first = $rows[0]; assertSame('c1pEDQ7jl3kuBHV4NqDI', $first['id'], 'first.id'); @@ -120,4 +121,10 @@ processLogEntries($fixture['log'], 'zarathos', $insertStmt); $count2 = (int)$pdo->query('SELECT COUNT(*) FROM vault')->fetchColumn(); assertSame(100, $count2, 'row count after re-run'); +// round-trip the running_balance column from $entry['data']['balance'] +$firstRunning = (int)$first['running_balance']; +$lastRunning = (int)$last['running_balance']; +assertSame(69235831, $firstRunning, 'first.running_balance'); +assertSame(509622821, $lastRunning, 'last.running_balance'); + echo "OK: processLogEntries v2 golden-file test passed (100 entries).\n";