A native macOS / windows / Linux database IDE built with Swift and AppKit. Connect to PostgreSQL, MySQL, SQLite, and Redis from a single app with a fast, keyboard-driven interface.
C++
test(redis): expose scan and select state gaps (RED)
Tobia5b150a737cb
diff --git a/macos/Tests/GridexTests/RedisAdapterDatabaseSelectionTests.swift b/macos/Tests/GridexTests/RedisAdapterDatabaseSelectionTests.swiftnew file mode 100644index 0000000..5c28a79--- /dev/null+++ b/macos/Tests/GridexTests/RedisAdapterDatabaseSelectionTests.swift@@ -0,0 +1,16 @@+// RedisAdapterDatabaseSelectionTests.swift+// Gridex++import XCTest+@testable import Gridex++final class RedisAdapterDatabaseSelectionTests: XCTestCase {++ func test_successfulSelectAdvancesTrackedCurrentDatabase() {+ var selection = RedisDatabaseSelectionState(initialDatabase: 0)++ selection.recordSuccessfulSelect(arguments: ["12"])++ XCTAssertEqual(selection.currentDatabase, 12)+ }+}diff --git a/macos/Tests/GridexTests/RedisAdapterKeyNameScanTests.swift b/macos/Tests/GridexTests/RedisAdapterKeyNameScanTests.swiftindex 77c32c4..0367f3a 100644--- a/macos/Tests/GridexTests/RedisAdapterKeyNameScanTests.swift+++ b/macos/Tests/GridexTests/RedisAdapterKeyNameScanTests.swift@@ -21,4 +21,171 @@ final class RedisAdapterKeyNameScanTests: XCTestCase {XCTFail("Expected GridexError, got \(error)")}}++ func test_scanLoop_cancellationBeforeSecondPageStopsPagination() async {+ let calls = RedisScanCallRecorder()++ let task = Task {+ try await RedisKeyScanLoop.run(+ maximumCount: 10,+ pageBudget: 10+ ) { cursor in+ await calls.record(cursor)+ if cursor == "0" {+ withUnsafeCurrentTask { $0?.cancel() }+ return RedisKeyScanPage(cursor: "1", keys: ["first"])+ }+ return RedisKeyScanPage(cursor: "0", keys: ["unexpected"])+ }+ }++ do {+ _ = try await task.value+ XCTFail("Expected cancellation before requesting page two")+ } catch is CancellationError {+ // Expected: the loop observes cancellation before the next request.+ } catch {+ XCTFail("Expected CancellationError, got \(error)")+ }++ let recordedCalls = await calls.snapshot()+ XCTAssertEqual(recordedCalls, ["0"])+ }++ func test_scanLoop_roundTripsOpaqueUInt64SizedCursor() async throws {+ let opaqueCursor = "18446744073709551615"+ let calls = RedisScanCallRecorder()++ let result = try await RedisKeyScanLoop.run(+ maximumCount: 10,+ pageBudget: 10+ ) { cursor in+ await calls.record(cursor)+ switch cursor {+ case "0":+ return RedisKeyScanPage(cursor: opaqueCursor, keys: ["first"])+ case opaqueCursor:+ return RedisKeyScanPage(cursor: "0", keys: ["second"])+ default:+ throw UnexpectedRedisScanCursor(cursor: cursor)+ }+ }++ let recordedCalls = await calls.snapshot()+ XCTAssertEqual(recordedCalls, ["0", opaqueCursor])+ XCTAssertEqual(result.keys, ["first", "second"])+ XCTAssertFalse(result.isTruncated)+ }++ func test_scanLoop_rejectsMalformedTopLevelResponse() async {+ let malformedResponse: RedisKeyScanPage? = nil++ await assertMalformedScanResponse {+ malformedResponse+ }+ }++ func test_scanLoop_rejectsMalformedCursor() async {+ let malformedResponse = RedisKeyScanPage(+ cursor: "not-a-cursor",+ keys: ["key"]+ )++ await assertMalformedScanResponse {+ malformedResponse+ }+ }++ func test_scanLoop_rejectsMalformedKeysPayload() async {+ let malformedResponse = RedisKeyScanPage(+ cursor: "0",+ keys: nil+ )++ await assertMalformedScanResponse {+ malformedResponse+ }+ }++ func test_scanLoop_deduplicatesKeysAcrossPages() async throws {+ let result = try await RedisKeyScanLoop.run(+ maximumCount: 10,+ pageBudget: 10+ ) { cursor in+ switch cursor {+ case "0":+ return RedisKeyScanPage(cursor: "1", keys: ["b", "a", "b"])+ case "1":+ return RedisKeyScanPage(cursor: "0", keys: ["b", "c"])+ default:+ throw UnexpectedRedisScanCursor(cursor: cursor)+ }+ }++ XCTAssertEqual(result.keys, ["a", "b", "c"])+ XCTAssertFalse(result.isTruncated)+ }++ func test_scanLoop_pageBudgetReturnsTruncatedResult() async throws {+ let calls = RedisScanCallRecorder()++ let result = try await RedisKeyScanLoop.run(+ maximumCount: 10,+ pageBudget: 2+ ) { cursor in+ await calls.record(cursor)+ switch cursor {+ case "0":+ return RedisKeyScanPage(cursor: "1", keys: ["a"])+ case "1":+ return RedisKeyScanPage(cursor: "2", keys: ["b"])+ case "2":+ return RedisKeyScanPage(cursor: "0", keys: ["unexpected"])+ default:+ throw UnexpectedRedisScanCursor(cursor: cursor)+ }+ }++ let recordedCalls = await calls.snapshot()+ XCTAssertEqual(recordedCalls, ["0", "1"])+ XCTAssertEqual(result.keys, ["a", "b"])+ XCTAssertTrue(result.isTruncated)+ }++ private func assertMalformedScanResponse(+ fetchPage: @escaping @Sendable () async throws -> RedisKeyScanPage?+ ) async {+ do {+ _ = try await RedisKeyScanLoop.run(+ maximumCount: 10,+ pageBudget: 10+ ) { _ in+ try await fetchPage()+ }+ XCTFail("Expected malformed SCAN response to fail")+ } catch let error as GridexError {+ XCTAssertEqual(+ error,+ .queryExecutionFailed("Malformed SCAN response")+ )+ } catch {+ XCTFail("Expected GridexError, got \(error)")+ }+ }+}++private actor RedisScanCallRecorder {+ private var cursors: [String] = []++ func record(_ cursor: String) {+ cursors.append(cursor)+ }++ func snapshot() -> [String] {+ cursors+ }+}++private struct UnexpectedRedisScanCursor: Error {+ let cursor: String}