diff --git a/PadelClubDataTests/SyncDataAccessTests.swift b/PadelClubDataTests/SyncDataAccessTests.swift index 957ce81..45b0319 100644 --- a/PadelClubDataTests/SyncDataAccessTests.swift +++ b/PadelClubDataTests/SyncDataAccessTests.swift @@ -528,7 +528,65 @@ struct SyncDataAccessTests { #expect(tournamentColA.count == 1) } + + @Test func testTournamentSharingTeamScoreIssue() async throws { + guard let userId2 = self.storeCenterB.userId else { + throw TestError.notAuthenticated + } + + // Setup tournament + let tournamentColA: SyncedCollection = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection() + + let tournament = Tournament(name: "test_data_access_children") + try await tournamentColA.addOrUpdateAsync(instance: tournament) + + let tourStoreA = try StoreCenter.main.store(identifier: tournament.id) + let teamRegColA: SyncedCollection = await tourStoreA.asyncLoadingSynchronizedCollection() + let playerRegColA: SyncedCollection = await tourStoreA.asyncLoadingSynchronizedCollection() + let roundColA: SyncedCollection = await tourStoreA.asyncLoadingSynchronizedCollection() + let matchColA: SyncedCollection = await tourStoreA.asyncLoadingSynchronizedCollection() + let teamScoreColA: SyncedCollection = await tourStoreA.asyncLoadingSynchronizedCollection() + + let tr1 = TeamRegistration() + let pr11 = PlayerRegistration(teamRegistration: tr1.id, firstName: "f1", lastName: "l1") + let pr12 = PlayerRegistration(teamRegistration: tr1.id, firstName: "f2", lastName: "l2") + let tr2 = TeamRegistration() + let pr21 = PlayerRegistration(teamRegistration: tr2.id, firstName: "f21", lastName: "l21") + let pr22 = PlayerRegistration(teamRegistration: tr2.id, firstName: "f22", lastName: "l22") + + try await teamRegColA.addOrUpdateAsync(contentOfs: [tr1, tr2]) + try await playerRegColA.addOrUpdateAsync(contentOfs: [pr11, pr12, pr21, pr22]) + + let round = Round(tournament: tournament.id) + let match = Match(round: round.id) + let ts1 = TeamScore(match: match.id, team: tr1) + let ts2 = TeamScore(match: match.id, team: tr2) + + try await roundColA.addOrUpdateAsync(instance: round) + try await matchColA.addOrUpdateAsync(instance: match) + try await teamScoreColA.addOrUpdateAsync(contentOfs: [ts1, ts2]) + + try await StoreCenter.main.setAuthorizedUsersAsync(for: tournament, users: [userId2]) + + let _ = try await self.storeCenterB.testSynchronizeOnceAsync() + let tournamentColB: SyncedCollection = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection() + + #expect(tournamentColB.count == 1) + let tourStoreB = try self.storeCenterB.store(identifier: tournament.id) + let matchColB: SyncedCollection = await tourStoreB.asyncLoadingSynchronizedCollection() + let teamScoreColB: SyncedCollection = await tourStoreB.asyncLoadingSynchronizedCollection() + + #expect(teamScoreColB.count == 2) + + let matchB = matchColB.first! + matchB.courtIndex = 2 + try await matchColB.addOrUpdateAsync(instance: matchB) + + #expect(teamScoreColB.count == 2) + + } + // needs to run on a postgreSQL, otherwise fails because of sqlite database locks @Test func testDataAccessForChildren() async throws { @@ -597,64 +655,6 @@ struct SyncDataAccessTests { } - @Test func testTournamentSharingTeamScoreIssue() async throws { - guard let userId2 = self.storeCenterB.userId else { - throw TestError.notAuthenticated - } - - // Setup tournament - let tournamentColA: SyncedCollection = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection() - - let tournament = Tournament(name: "test_data_access_children") - try await tournamentColA.addOrUpdateAsync(instance: tournament) - - let tourStoreA = try StoreCenter.main.store(identifier: tournament.id) - let teamRegColA: SyncedCollection = await tourStoreA.asyncLoadingSynchronizedCollection() - let playerRegColA: SyncedCollection = await tourStoreA.asyncLoadingSynchronizedCollection() - let roundColA: SyncedCollection = await tourStoreA.asyncLoadingSynchronizedCollection() - let matchColA: SyncedCollection = await tourStoreA.asyncLoadingSynchronizedCollection() - let teamScoreColA: SyncedCollection = await tourStoreA.asyncLoadingSynchronizedCollection() - - let tr1 = TeamRegistration() - let pr11 = PlayerRegistration(teamRegistration: tr1.id, firstName: "f1", lastName: "l1") - let pr12 = PlayerRegistration(teamRegistration: tr1.id, firstName: "f2", lastName: "l2") - let tr2 = TeamRegistration() - let pr21 = PlayerRegistration(teamRegistration: tr2.id, firstName: "f21", lastName: "l21") - let pr22 = PlayerRegistration(teamRegistration: tr2.id, firstName: "f22", lastName: "l22") - - try await teamRegColA.addOrUpdateAsync(contentOfs: [tr1, tr2]) - try await playerRegColA.addOrUpdateAsync(contentOfs: [pr11, pr12, pr21, pr22]) - - let round = Round(tournament: tournament.id) - let match = Match(round: round.id) - let ts1 = TeamScore(match: match.id, team: tr1) - let ts2 = TeamScore(match: match.id, team: tr2) - - try await roundColA.addOrUpdateAsync(instance: round) - try await matchColA.addOrUpdateAsync(instance: match) - try await teamScoreColA.addOrUpdateAsync(contentOfs: [ts1, ts2]) - - try await StoreCenter.main.setAuthorizedUsersAsync(for: tournament, users: [userId2]) - - let _ = try await self.storeCenterB.testSynchronizeOnceAsync() - let tournamentColB: SyncedCollection = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection() - - #expect(tournamentColB.count == 1) - - let tourStoreB = try self.storeCenterB.store(identifier: tournament.id) - let matchColB: SyncedCollection = await tourStoreB.asyncLoadingSynchronizedCollection() - let teamScoreColB: SyncedCollection = await tourStoreB.asyncLoadingSynchronizedCollection() - - #expect(teamScoreColB.count == 2) - - let matchB = matchColB.first! - matchB.courtIndex = 2 - try await matchColB.addOrUpdateAsync(instance: matchB) - - #expect(teamScoreColB.count == 2) - - } - }