|
|
|
|
@ -27,7 +27,7 @@ struct SynchronizationTests { |
|
|
|
|
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) |
|
|
|
|
self.secondStoreCenter.configureURLs(secureScheme: false, domain: "127.0.0.1:8000", 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 +39,7 @@ struct SynchronizationTests { |
|
|
|
|
try await self.login(storeCenter: self.secondStoreCenter) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
StoreCenter.main.configureURLs(secureScheme: false, domain: "127.0.0.1:8000", webSockets: false) |
|
|
|
|
StoreCenter.main.configureURLs(secureScheme: false, domain: "127.0.0.1:8000", 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) |
|
|
|
|
@ -75,7 +75,7 @@ struct SynchronizationTests { |
|
|
|
|
let eventCollection1: SyncedCollection<Event> = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection() |
|
|
|
|
#expect(eventCollection1.hasLoaded == true) |
|
|
|
|
|
|
|
|
|
await eventCollection1.deleteAsync(contentOfs: Array(eventCollection1)) |
|
|
|
|
try await eventCollection1.deleteAsync(contentOfs: Array(eventCollection1)) |
|
|
|
|
|
|
|
|
|
let eventCollection2: SyncedCollection<Event> = await self.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection() |
|
|
|
|
#expect(eventCollection2.hasLoaded == true) |
|
|
|
|
@ -86,7 +86,7 @@ struct SynchronizationTests { |
|
|
|
|
|
|
|
|
|
// Create |
|
|
|
|
let event: Event = Event(creator: userId, club: nil, name: "test") |
|
|
|
|
await eventCollection1.addOrUpdateAsync(instance: event) |
|
|
|
|
try await eventCollection1.addOrUpdateAsync(instance: event) |
|
|
|
|
|
|
|
|
|
let serverEvents: [Event] = try await StoreCenter.main.service().get() |
|
|
|
|
#expect(serverEvents.count == 1) |
|
|
|
|
@ -118,13 +118,13 @@ struct SynchronizationTests { |
|
|
|
|
|
|
|
|
|
// Setup events collections |
|
|
|
|
let eventCollectionA: SyncedCollection<Event> = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection() |
|
|
|
|
await eventCollectionA.deleteAsync(contentOfs: Array(eventCollectionA)) |
|
|
|
|
try await eventCollectionA.deleteAsync(contentOfs: Array(eventCollectionA)) |
|
|
|
|
let eventCollectionB: SyncedCollection<Event> = await self.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection() |
|
|
|
|
eventCollectionB.clear() |
|
|
|
|
|
|
|
|
|
// Setup clubs collections |
|
|
|
|
let clubCollectionA: SyncedCollection<Club> = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection() |
|
|
|
|
await clubCollectionA.deleteAsync(contentOfs: Array(clubCollectionA)) |
|
|
|
|
try await clubCollectionA.deleteAsync(contentOfs: Array(clubCollectionA)) |
|
|
|
|
let clubCollectionB: SyncedCollection<Club> = await self.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection() |
|
|
|
|
clubCollectionB.clear() |
|
|
|
|
|
|
|
|
|
@ -133,7 +133,7 @@ struct SynchronizationTests { |
|
|
|
|
|
|
|
|
|
// Create |
|
|
|
|
let eventA: Event = Event(creator: userId, club: nil, name: "test-b") |
|
|
|
|
await eventCollectionA.addOrUpdateAsync(instance: eventA) |
|
|
|
|
try await eventCollectionA.addOrUpdateAsync(instance: eventA) |
|
|
|
|
|
|
|
|
|
// Retrieve Event |
|
|
|
|
let dataB = try await self.secondStoreCenter.testSynchronizeOnceAsync() |
|
|
|
|
@ -143,13 +143,13 @@ struct SynchronizationTests { |
|
|
|
|
|
|
|
|
|
// Create club on 2nd StoreCenter |
|
|
|
|
let club = Club(creator: userId, name: "Padel Club", acronym: "PC") |
|
|
|
|
await clubCollectionB.addOrUpdateAsync(instance: club) |
|
|
|
|
try await clubCollectionB.addOrUpdateAsync(instance: club) |
|
|
|
|
|
|
|
|
|
guard let eventB = eventCollectionB.findById(eventA.id) else { |
|
|
|
|
throw SyncTestError.instanceNotFound(id: eventA.id) |
|
|
|
|
} |
|
|
|
|
eventB.club = club.id |
|
|
|
|
await eventCollectionB.addOrUpdateAsync(instance: eventB) |
|
|
|
|
try await eventCollectionB.addOrUpdateAsync(instance: eventB) |
|
|
|
|
|
|
|
|
|
// Synchronize 1st StoreCenter |
|
|
|
|
let dataA = try await StoreCenter.main.testSynchronizeOnceAsync() |
|
|
|
|
@ -182,7 +182,7 @@ struct SynchronizationTests { |
|
|
|
|
|
|
|
|
|
// Setup TeamReg |
|
|
|
|
let teamRegColA: SyncedCollection<TeamRegistration> = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection() |
|
|
|
|
await teamRegColA.deleteAsync(contentOfs: Array(teamRegColA)) |
|
|
|
|
try await teamRegColA.deleteAsync(contentOfs: Array(teamRegColA)) |
|
|
|
|
let teamRegColB: SyncedCollection<TeamRegistration> = await self.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection() |
|
|
|
|
teamRegColB.clear() |
|
|
|
|
|
|
|
|
|
@ -191,8 +191,8 @@ struct SynchronizationTests { |
|
|
|
|
|
|
|
|
|
// Create |
|
|
|
|
let trA = TeamRegistration(tournament: tournament.id) |
|
|
|
|
await teamRegColA.addOrUpdateAsync(instance: trA) |
|
|
|
|
await teamRegColA.deleteAsync(instance: trA) |
|
|
|
|
try await teamRegColA.addOrUpdateAsync(instance: trA) |
|
|
|
|
try await teamRegColA.deleteAsync(instance: trA) |
|
|
|
|
#expect(teamRegColA.count == 0) |
|
|
|
|
|
|
|
|
|
let _ = try await self.secondStoreCenter.testSynchronizeOnceAsync() |
|
|
|
|
@ -207,8 +207,10 @@ struct SynchronizationTests { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Setup events collections |
|
|
|
|
let eventCollectionA: SyncedCollection<Event> = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection() |
|
|
|
|
await eventCollectionA.deleteAsync(contentOfs: Array(eventCollectionA)) |
|
|
|
|
let eventCollectionA: SyncedCollection<Event> = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection(inMemory: true) |
|
|
|
|
try await eventCollectionA.loadOnceAsync() |
|
|
|
|
|
|
|
|
|
try await eventCollectionA.deleteAsync(contentOfs: Array(eventCollectionA)) |
|
|
|
|
let eventCollectionB: SyncedCollection<Event> = await self.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection() |
|
|
|
|
eventCollectionB.clear() |
|
|
|
|
|
|
|
|
|
@ -220,7 +222,7 @@ struct SynchronizationTests { |
|
|
|
|
|
|
|
|
|
// Create |
|
|
|
|
let eventA: Event = Event(creator: userId, club: nil, name: "test-b") |
|
|
|
|
await eventCollectionA.addOrUpdateAsync(instance: eventA) |
|
|
|
|
try await eventCollectionA.addOrUpdateAsync(instance: eventA) |
|
|
|
|
#expect(eventCollectionA.count == 1) |
|
|
|
|
|
|
|
|
|
let serverEvents: [Event] = try await StoreCenter.main.service().get() |
|
|
|
|
@ -236,12 +238,12 @@ struct SynchronizationTests { |
|
|
|
|
throw SyncTestError.instanceNotFound(id: eventA.id) |
|
|
|
|
} |
|
|
|
|
eventA.name = "my event is nice" |
|
|
|
|
await eventCollectionA.addOrUpdateAsync(instance: eventA) |
|
|
|
|
try await eventCollectionA.addOrUpdateAsync(instance: eventA) |
|
|
|
|
|
|
|
|
|
eventB.name = "my event is better" |
|
|
|
|
await eventCollectionB.addOrUpdateAsync(instance: eventB) |
|
|
|
|
try await eventCollectionB.addOrUpdateAsync(instance: eventB) |
|
|
|
|
|
|
|
|
|
await eventCollectionA.addOrUpdateAsync(instance: eventA) |
|
|
|
|
try await eventCollectionA.addOrUpdateAsync(instance: eventA) |
|
|
|
|
|
|
|
|
|
let _ = try await StoreCenter.main.testSynchronizeOnceAsync() |
|
|
|
|
let _ = try await self.secondStoreCenter.testSynchronizeOnceAsync() |
|
|
|
|
@ -253,5 +255,58 @@ struct SynchronizationTests { |
|
|
|
|
#expect(eventCollectionB.first?.name == "my event is nice") |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// In this test, the first user: |
|
|
|
|
/// - creates one event and 2 clubs |
|
|
|
|
/// - shares the event with a second user |
|
|
|
|
/// - changes the club on the event |
|
|
|
|
/// Here we want to test that the first Club is removed and the second one is received |
|
|
|
|
@Test func testRelationshipChange() async throws { |
|
|
|
|
|
|
|
|
|
guard let userId = StoreCenter.main.userId else { |
|
|
|
|
throw TestError.notAuthenticated |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Setup |
|
|
|
|
let eventColA: SyncedCollection<Event> = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection() |
|
|
|
|
let clubColA: SyncedCollection<Club> = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection() |
|
|
|
|
let eventColB: SyncedCollection<Event> = await self.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection() |
|
|
|
|
let clubColB: SyncedCollection<Club> = await self.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection() |
|
|
|
|
|
|
|
|
|
if let dataAccessCollection = StoreCenter.main.dataAccessCollection { |
|
|
|
|
try await dataAccessCollection.deleteAsync(contentOfs: Array(dataAccessCollection)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try await eventColA.deleteAsync(contentOfs: Array(eventColA)) |
|
|
|
|
try await clubColA.deleteAsync(contentOfs: Array(clubColA)) |
|
|
|
|
|
|
|
|
|
let _ = try await self.secondStoreCenter.testSynchronizeOnceAsync() |
|
|
|
|
|
|
|
|
|
#expect(eventColB.count == 0) |
|
|
|
|
#expect(clubColB.count == 0) |
|
|
|
|
|
|
|
|
|
// Create |
|
|
|
|
let club1A = Club(creator: userId, name: "Club 1", acronym: "C1") |
|
|
|
|
try await clubColA.addOrUpdateAsync(instance: club1A) |
|
|
|
|
let club2A = Club(creator: userId, name: "Club 2", acronym: "C2") |
|
|
|
|
try await clubColA.addOrUpdateAsync(instance: club2A) |
|
|
|
|
|
|
|
|
|
let eventA = Event(creator: userId, club: club1A.id, name: "event 1") |
|
|
|
|
try await eventColA.addOrUpdateAsync(instance: eventA) |
|
|
|
|
|
|
|
|
|
// Share with user2 |
|
|
|
|
let _ = try await self.secondStoreCenter.testSynchronizeOnceAsync() |
|
|
|
|
#expect(eventColB.count == 1) |
|
|
|
|
#expect(clubColB.count == 2) |
|
|
|
|
|
|
|
|
|
// Change the club |
|
|
|
|
eventA.club = club2A.id |
|
|
|
|
try await eventColA.addOrUpdateAsync(instance: eventA) |
|
|
|
|
let _ = try await self.secondStoreCenter.testSynchronizeOnceAsync() |
|
|
|
|
|
|
|
|
|
#expect(eventColB.first?.club == eventA.club) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|