sync3
Laurent 2 months ago
parent 4146886e85
commit b11905b168
  1. 1
      PadelClubDataTests/PadelClubDataTests.swift
  2. 58
      PadelClubDataTests/SyncDataAccessTests.swift

@ -13,6 +13,7 @@ enum TestError: Error {
case notAuthenticated
case sameDeviceId
case missingEvent
case missingTournament
}
struct PadelClubDataTests {

@ -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<Event> = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection()
let tournamentColA: SyncedCollection<Tournament> = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection()
let eventColB: SyncedCollection<Event> = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection()
let tournamentColB: SyncedCollection<Tournament> = 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
}
}
}

Loading…
Cancel
Save