From 1ed3c66f5d9f156871ea04cf6d890056772b6de8 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 22 May 2025 14:59:37 +0200 Subject: [PATCH] adds config to perform tests on various server --- PadelClubData/Data/Match.swift | 2 ++ PadelClubData/Utils/PListReader.swift | 16 ++++++---- PadelClubDataTests/Config.swift | 26 +++++++++++++++++ PadelClubDataTests/DeletionTests.swift | 29 +++++++++++++------ PadelClubDataTests/PadelClubDataTests.swift | 5 +++- PadelClubDataTests/SyncDataAccessTests.swift | 6 ++-- PadelClubDataTests/SynchronizationTests.swift | 6 ++-- PadelClubDataTests/config.plist | 25 +++++++++++----- 8 files changed, 87 insertions(+), 28 deletions(-) diff --git a/PadelClubData/Data/Match.swift b/PadelClubData/Data/Match.swift index 55a234f..4074da2 100644 --- a/PadelClubData/Data/Match.swift +++ b/PadelClubData/Data/Match.swift @@ -34,6 +34,8 @@ final public class Match: BaseMatch, SideStorable { required public init() { super.init() + + let drawLog = DrawLog() } // MARK: - DidSet diff --git a/PadelClubData/Utils/PListReader.swift b/PadelClubData/Utils/PListReader.swift index e6095fb..188857c 100644 --- a/PadelClubData/Utils/PListReader.swift +++ b/PadelClubData/Utils/PListReader.swift @@ -10,7 +10,11 @@ import Foundation public class PListReader { static func dictionary(plist: String) -> [String: Any]? { - if let plistPath = Bundle.main.path(forResource: plist, ofType: "plist") { + return self.dictionary(plist: plist, bundle: Bundle.main) + } + + static func dictionary(plist: String, bundle: Bundle) -> [String: Any]? { + if let plistPath = bundle.path(forResource: plist, ofType: "plist") { // Read plist file into Data if let plistData = FileManager.default.contents(atPath: plistPath) { do { @@ -25,21 +29,21 @@ public class PListReader { print("Failed to read plist file at path: \(plistPath)") } } else { - print("Plist file 'Data.plist' not found in bundle") + print("Plist file '\(plist)' not found in bundle") } return nil } - public static func readString(plist: String, key: String) -> String? { - if let dictionary = self.dictionary(plist: plist) { + public static func readString(plist: String, key: String, bundle: Bundle = Bundle.main) -> String? { + if let dictionary = self.dictionary(plist: plist, bundle: bundle) { return dictionary[key] as? String } return nil } - public static func readBool(plist: String, key: String) -> Bool? { - if let dictionary = self.dictionary(plist: plist) { + public static func readBool(plist: String, key: String, bundle: Bundle = Bundle.main) -> Bool? { + if let dictionary = self.dictionary(plist: plist, bundle: bundle) { return dictionary[key] as? Bool } return nil diff --git a/PadelClubDataTests/Config.swift b/PadelClubDataTests/Config.swift index e69de29..d50c29e 100644 --- a/PadelClubDataTests/Config.swift +++ b/PadelClubDataTests/Config.swift @@ -0,0 +1,26 @@ +// +// Config.swift +// PadelClubData +// +// Created by Laurent Morvillier on 21/05/2025. +// + +@testable import PadelClubData + +class Config { + + static var server: (secure: Bool, domain: String) { + + let bundle = Bundle(for: self) + + let secure = PListReader.readBool(plist: "config", key: "secure_server", bundle: bundle) + let domain = PListReader.readString(plist: "config", key: "server_domain", bundle: bundle) + + if let secure, let domain { + return (secure, domain) + } + + fatalError("no server configuration") + } + +} diff --git a/PadelClubDataTests/DeletionTests.swift b/PadelClubDataTests/DeletionTests.swift index ef8db4c..bb1166b 100644 --- a/PadelClubDataTests/DeletionTests.swift +++ b/PadelClubDataTests/DeletionTests.swift @@ -18,11 +18,14 @@ struct DeletionTests { init() async throws { + let conf = Config.server + FileManager.default.deleteDirectoryInDocuments(directoryName: "storage") FileManager.default.deleteDirectoryInDocuments(directoryName: "storage-2") self.secondStoreCenter = StoreCenter(directoryName: "storage-2") - self.secondStoreCenter.configureURLs(secureScheme: false, domain: "127.0.0.1:8000", webSockets: false, useSynchronization: true) + + self.secondStoreCenter.configureURLs(secureScheme: conf.secure, domain: conf.domain, webSockets: false, useSynchronization: true) self.secondStoreCenter.tokenKeychain = MockKeychainStore(fileName: "storage-2/token.json") self.secondStoreCenter.deviceKeychain = MockKeychainStore(fileName: "storage-2/device.json") try self.secondStoreCenter.deviceKeychain.add(value: UUID().uuidString) @@ -34,7 +37,7 @@ struct DeletionTests { try await self.login(storeCenter: self.secondStoreCenter, username: self.username1, password: self.password1) } - StoreCenter.main.configureURLs(secureScheme: false, domain: "127.0.0.1:8000", webSockets: false, useSynchronization: true) + StoreCenter.main.configureURLs(secureScheme: conf.secure, domain: conf.domain, webSockets: false, useSynchronization: true) StoreCenter.main.tokenKeychain = MockKeychainStore(fileName: "storage/token.json") StoreCenter.main.deviceKeychain = MockKeychainStore(fileName: "storage/device.json") try StoreCenter.main.deviceKeychain.add(value: UUID().uuidString) @@ -247,12 +250,20 @@ struct DeletionTests { #expect(clubColB.count == 1) #expect(eventColB.count == 0) #expect(tournamentColB.count == 0) - #expect(groupStageColB.count == 0) - #expect(roundColB.count == 0) - #expect(matchColB.count == 0) - #expect(teamRegistrationColB.count == 0) - #expect(teamScoreColB.count == 0) - #expect(playerRegistrationColB.count == 0) + + do { + let _ = try self.secondStoreCenter.store(identifier: tournament.id) + Issue.record("should go in the catch because the store has been destroyed") + } catch { + #expect(1 == 1) + } + +// #expect(groupStageColB.count == 0) +// #expect(roundColB.count == 0) +// #expect(matchColB.count == 0) +// #expect(teamRegistrationColB.count == 0) +// #expect(teamScoreColB.count == 0) +// #expect(playerRegistrationColB.count == 0) } } @@ -282,7 +293,7 @@ actor BoolChecker { // print("sleep...") // Wait for 100ms before next check - try? await Task.sleep(for: .milliseconds(100)) + try? await Task.sleep(for: .milliseconds(1000)) } // Throw error if timeout is reached diff --git a/PadelClubDataTests/PadelClubDataTests.swift b/PadelClubDataTests/PadelClubDataTests.swift index 9c3d9b7..04ca9f9 100644 --- a/PadelClubDataTests/PadelClubDataTests.swift +++ b/PadelClubDataTests/PadelClubDataTests.swift @@ -21,7 +21,10 @@ struct PadelClubDataTests { let password: String = "MyPass1234--" init() async throws { - StoreCenter.main.configureURLs(secureScheme: false, domain: "127.0.0.1:8000") + + let conf = Config.server + + StoreCenter.main.configureURLs(secureScheme: conf.secure, domain: conf.domain) StoreCenter.main.tokenKeychain = MockKeychainStore(fileName: "token.json") try await self.login() } diff --git a/PadelClubDataTests/SyncDataAccessTests.swift b/PadelClubDataTests/SyncDataAccessTests.swift index 4d0e499..03d1ec1 100644 --- a/PadelClubDataTests/SyncDataAccessTests.swift +++ b/PadelClubDataTests/SyncDataAccessTests.swift @@ -22,6 +22,8 @@ struct SyncDataAccessTests { init() async throws { + let conf = Config.server + let dirA = "storageA" let dirB = "storageB" @@ -32,7 +34,7 @@ struct SyncDataAccessTests { self.storeCenterB = StoreCenter(directoryName: dirB) // StoreCenter A - self.storeCenterA.configureURLs(secureScheme: false, domain: "127.0.0.1:8000", webSockets: false, useSynchronization: true) + self.storeCenterA.configureURLs(secureScheme: conf.secure, domain: conf.domain, webSockets: false, useSynchronization: true) self.storeCenterA.tokenKeychain = MockKeychainStore(fileName: "\(dirA)/token.json") self.storeCenterA.deviceKeychain = MockKeychainStore(fileName: "\(dirA)/device.json") try self.storeCenterA.deviceKeychain.add(value: UUID().uuidString) @@ -44,7 +46,7 @@ struct SyncDataAccessTests { } // StoreCenter B - self.storeCenterB.configureURLs(secureScheme: false, domain: "127.0.0.1:8000", webSockets: false, useSynchronization: true) + self.storeCenterB.configureURLs(secureScheme: conf.secure, domain: conf.domain, webSockets: false, useSynchronization: true) self.storeCenterB.tokenKeychain = MockKeychainStore(fileName: "\(dirB)/token.json") self.storeCenterB.deviceKeychain = MockKeychainStore(fileName: "\(dirB)/device.json") try self.storeCenterB.deviceKeychain.add(value: UUID().uuidString) diff --git a/PadelClubDataTests/SynchronizationTests.swift b/PadelClubDataTests/SynchronizationTests.swift index f04d15d..75e0293 100644 --- a/PadelClubDataTests/SynchronizationTests.swift +++ b/PadelClubDataTests/SynchronizationTests.swift @@ -23,11 +23,13 @@ struct SynchronizationTests { init() async throws { + let conf = Config.server + FileManager.default.deleteDirectoryInDocuments(directoryName: "storage") FileManager.default.deleteDirectoryInDocuments(directoryName: "storage-2") self.secondStoreCenter = StoreCenter(directoryName: "storage-2") - self.secondStoreCenter.configureURLs(secureScheme: false, domain: "127.0.0.1:8000", webSockets: false, useSynchronization: true) + self.secondStoreCenter.configureURLs(secureScheme: conf.secure, domain: conf.domain, webSockets: false, useSynchronization: true) self.secondStoreCenter.tokenKeychain = MockKeychainStore(fileName: "storage-2/token.json") self.secondStoreCenter.deviceKeychain = MockKeychainStore(fileName: "storage-2/device.json") try self.secondStoreCenter.deviceKeychain.add(value: UUID().uuidString) @@ -39,7 +41,7 @@ struct SynchronizationTests { try await self.login(storeCenter: self.secondStoreCenter) } - StoreCenter.main.configureURLs(secureScheme: false, domain: "127.0.0.1:8000", webSockets: false, useSynchronization: true) + StoreCenter.main.configureURLs(secureScheme: conf.secure, domain: conf.domain, webSockets: false, useSynchronization: true) StoreCenter.main.tokenKeychain = MockKeychainStore(fileName: "storage/token.json") StoreCenter.main.deviceKeychain = MockKeychainStore(fileName: "storage/device.json") try StoreCenter.main.deviceKeychain.add(value: UUID().uuidString) diff --git a/PadelClubDataTests/config.plist b/PadelClubDataTests/config.plist index ec9ce34..da04ced 100644 --- a/PadelClubDataTests/config.plist +++ b/PadelClubDataTests/config.plist @@ -1,12 +1,21 @@ - - + + secure_server + + server_domain + test1.padelclub.app + references + + local_server_domain + 127.0.0.1:8000 + prod_server_domain + padelclub.app + test1_server_domain + test1.padelclub.app + xlr_server_domain + xlr.alwaysdata.net + +