|
| 1 | +// |
| 2 | +// ConnectionStoragePersistenceTests.swift |
| 3 | +// TableProTests |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import Testing |
| 8 | +@testable import TablePro |
| 9 | + |
| 10 | +@Suite("ConnectionStorage Persistence", .serialized) |
| 11 | +struct ConnectionStoragePersistenceTests { |
| 12 | + private let storage = ConnectionStorage.shared |
| 13 | + |
| 14 | + @Test("loading empty storage does not write back to UserDefaults") |
| 15 | + func loadEmptyDoesNotWrite() { |
| 16 | + let original = storage.loadConnections() |
| 17 | + defer { storage.saveConnections(original) } |
| 18 | + |
| 19 | + // Clear all connections |
| 20 | + storage.saveConnections([]) |
| 21 | + |
| 22 | + // Force cache clear by saving then loading |
| 23 | + let loaded = storage.loadConnections() |
| 24 | + #expect(loaded.isEmpty) |
| 25 | + |
| 26 | + // Add a connection directly, bypassing cache |
| 27 | + let connection = DatabaseConnection(name: "Persistence Test") |
| 28 | + storage.addConnection(connection) |
| 29 | + defer { storage.deleteConnection(connection) } |
| 30 | + |
| 31 | + // Loading again should return the connection, not overwrite with empty |
| 32 | + let reloaded = storage.loadConnections() |
| 33 | + #expect(reloaded.contains { $0.id == connection.id }) |
| 34 | + } |
| 35 | + |
| 36 | + @Test("round-trip save and load preserves connections") |
| 37 | + func roundTripSaveLoad() { |
| 38 | + let original = storage.loadConnections() |
| 39 | + defer { storage.saveConnections(original) } |
| 40 | + |
| 41 | + let connection = DatabaseConnection( |
| 42 | + name: "Round Trip Test", |
| 43 | + host: "127.0.0.1", |
| 44 | + port: 5432, |
| 45 | + type: .postgresql |
| 46 | + ) |
| 47 | + |
| 48 | + storage.saveConnections([connection]) |
| 49 | + let loaded = storage.loadConnections() |
| 50 | + |
| 51 | + #expect(loaded.count == 1) |
| 52 | + #expect(loaded.first?.id == connection.id) |
| 53 | + #expect(loaded.first?.name == "Round Trip Test") |
| 54 | + #expect(loaded.first?.host == "127.0.0.1") |
| 55 | + } |
| 56 | +} |
0 commit comments