From b11905b1685797c1390e03dcc08164d5bb9e218a Mon Sep 17 00:00:00 2001 From: Laurent Date: Wed, 17 Sep 2025 14:27:49 +0200 Subject: [PATCH] adds test --- PadelClubDataTests/PadelClubDataTests.swift | 1 + PadelClubDataTests/SyncDataAccessTests.swift | 58 ++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/PadelClubDataTests/PadelClubDataTests.swift b/PadelClubDataTests/PadelClubDataTests.swift index f25dd10..5a31f5f 100644 --- a/PadelClubDataTests/PadelClubDataTests.swift +++ b/PadelClubDataTests/PadelClubDataTests.swift @@ -13,6 +13,7 @@ enum TestError: Error { case notAuthenticated case sameDeviceId case missingEvent + case missingTournament } struct PadelClubDataTests { diff --git a/PadelClubDataTests/SyncDataAccessTests.swift b/PadelClubDataTests/SyncDataAccessTests.swift index c33453c..133e781 100644 --- a/PadelClubDataTests/SyncDataAccessTests.swift +++ b/PadelClubDataTests/SyncDataAccessTests.swift @@ -847,6 +847,64 @@ struct SyncDataAccessTests { } + /// In this test, the first user: + /// - creates an event + /// - shares the event with a second user + /// - the second user creates a tournament on that event + /// We test that the tournament related_user is the second user + @Test func testRelatedUsers() async throws { + + guard let userId1 = StoreCenter.main.userId else { + throw TestError.notAuthenticated + } + guard let userId2 = self.storeCenterB.userId else { + throw TestError.notAuthenticated + } + + // Setup + let eventColA: SyncedCollection = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection() + let tournamentColA: SyncedCollection = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection() + let eventColB: SyncedCollection = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection() + let tournamentColB: SyncedCollection = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection() + + if let dataAccessCollection = StoreCenter.main.dataAccessCollection { + try await dataAccessCollection.deleteAsync(contentOfs: Array(dataAccessCollection)) + } + + try await eventColA.deleteAsync(contentOfs: Array(eventColA)) + try await tournamentColA.deleteAsync(contentOfs: Array(tournamentColA)) + + let _ = try await self.storeCenterB.testSynchronizeOnceAsync() + + #expect(eventColB.count == 0) + #expect(tournamentColB.count == 0) + + // Create + let eventA = Event(creator: userId1) + try await eventColA.addOrUpdateAsync(instance: eventA) + + // Share with user2 + try await StoreCenter.main.setAuthorizedUsersAsync(for: eventA, users: [userId2]) + + let dataB = try await self.storeCenterB.testSynchronizeOnceAsync() + var syncDataB = try SyncData(data: dataB, storeCenter: self.storeCenterB) + + #expect(syncDataB.shared.count == 1) + #expect(eventColB.count == 1) + #expect(eventColB.first?.relatedUser == userId1) + + let tournamentB = Tournament(event: eventA.id, name: "P100") + try await tournamentColB.addOrUpdateAsync(instance: tournamentB) + + let tournaments: [Tournament] = try await self.storeCenterA.service().get() + + if let tournamentServerCopy = tournaments.first(where: { $0.id == tournamentB.id }) { + #expect(tournamentServerCopy.relatedUser == userId2) + } else { + throw TestError.missingTournament + } + + } }