update tests

sync3
Laurent 6 months ago
parent c30539390b
commit b5d8fe5033
  1. 186
      PadelClubDataTests/SyncDataAccessTests.swift
  2. 1
      PadelClubDataTests/SynchronizationTests.swift

@ -9,6 +9,15 @@ import Testing
@testable import PadelClubData
@testable import LeStorage
extension String {
static func random(length: Int = 10) -> String {
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
return String((0..<length).map{ _ in letters.randomElement()! })
}
}
struct SyncDataAccessTests {
let username1: String = "UserDataTests"
@ -17,35 +26,45 @@ struct SyncDataAccessTests {
let username2: String = "seconduser"
let password2: String = "MyPass1234--"
var secondStoreCenter: StoreCenter
var storeCenterA: StoreCenter
var storeCenterB: StoreCenter
init() async throws {
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.tokenKeychain = MockKeychainStore(fileName: "storage-2/token.json")
self.secondStoreCenter.deviceKeychain = MockKeychainStore(fileName: "storage-2/device.json")
try self.secondStoreCenter.deviceKeychain.add(value: UUID().uuidString)
self.secondStoreCenter.classProject = "PadelClubData"
let dirA = "storageA"
let dirB = "storageB"
let token2 = try? self.secondStoreCenter.rawTokenShouldNotBeUsed()
if token2 == nil {
try await self.login(storeCenter: self.secondStoreCenter, username: self.username2, password: self.password2)
FileManager.default.deleteDirectoryInDocuments(directoryName: dirA)
FileManager.default.deleteDirectoryInDocuments(directoryName: dirB)
self.storeCenterA = StoreCenter(directoryName: dirA)
self.storeCenterB = StoreCenter(directoryName: dirB)
// StoreCenter A
self.storeCenterA.configureURLs(secureScheme: false, domain: "127.0.0.1:8000", 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)
self.storeCenterA.classProject = "PadelClubData"
let token = try? self.storeCenterA.rawTokenShouldNotBeUsed()
if token == nil {
try await self.login(storeCenter: self.storeCenterA, username: self.username1, password: self.password1)
}
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)
StoreCenter.main.classProject = "PadelClubData"
// StoreCenter B
self.storeCenterB.configureURLs(secureScheme: false, domain: "127.0.0.1:8000", 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)
let token = try? StoreCenter.main.rawTokenShouldNotBeUsed()
if token == nil {
try await self.login(storeCenter: StoreCenter.main, username: self.username1, password: self.password1)
self.storeCenterB.classProject = "PadelClubData"
let token2 = try? self.storeCenterB.rawTokenShouldNotBeUsed()
if token2 == nil {
try await self.login(storeCenter: self.storeCenterB, username: self.username2, password: self.password2)
}
}
mutating func login(storeCenter: StoreCenter, username: String, password: String) async throws {
@ -53,13 +72,13 @@ struct SyncDataAccessTests {
}
@Test func testSetup() async throws {
#expect(StoreCenter.main.isAuthenticated)
#expect(self.secondStoreCenter.isAuthenticated)
#expect(self.storeCenterA.isAuthenticated)
#expect(self.storeCenterB.isAuthenticated)
guard let userId1 = StoreCenter.main.userId else {
guard let userId1 = self.storeCenterA.userId else {
throw TestError.notAuthenticated
}
guard let userId2 = self.secondStoreCenter.userId else {
guard let userId2 = self.storeCenterB.userId else {
throw TestError.notAuthenticated
}
#expect(userId1 != userId2)
@ -72,27 +91,30 @@ struct SyncDataAccessTests {
/// We test that the data is properly received and removed upon the sharing actions
@Test func testTournamentSharing() async throws {
guard let userId1 = StoreCenter.main.userId else {
guard let userId1 = self.storeCenterA.userId else {
throw TestError.notAuthenticated
}
guard let userId2 = self.secondStoreCenter.userId else {
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.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection()
let tournamentColB: SyncedCollection<Tournament> = await self.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection()
if let dataAccessCollection = StoreCenter.main.dataAccessCollection {
try await dataAccessCollection.deleteAsync(contentOfs: Array(dataAccessCollection))
let eventColA: SyncedCollection<Event> = await self.storeCenterA.mainStore.asyncLoadingSynchronizedCollection()
let tournamentColA: SyncedCollection<Tournament> = await self.storeCenterA.mainStore.asyncLoadingSynchronizedCollection()
let eventColB: SyncedCollection<Event> = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection()
let tournamentColB: SyncedCollection<Tournament> = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection()
var dataAccesses: [DataAccess] = try await self.storeCenterA.service().get()
if let dataAccessCollection = self.storeCenterA.dataAccessCollection {
try await dataAccessCollection.deleteAsync(contentOfs: dataAccesses)
} else {
Issue.record("dataAccessCollection should not be nil")
}
try await eventColA.deleteAsync(contentOfs: Array(eventColA))
try await tournamentColA.deleteAsync(contentOfs: Array(tournamentColA))
let _ = try await self.secondStoreCenter.testSynchronizeOnceAsync()
let _ = try await self.storeCenterB.testSynchronizeOnceAsync()
#expect(eventColB.count == 0)
#expect(tournamentColB.count == 0)
@ -104,27 +126,27 @@ struct SyncDataAccessTests {
try await tournamentColA.addOrUpdateAsync(instance: tournamentA)
// Share with user2
try await StoreCenter.main.setAuthorizedUsersAsync(for: tournamentA, users: [userId2])
try await self.storeCenterA.setAuthorizedUsersAsync(for: tournamentA, users: [userId2])
var dataB = try await self.secondStoreCenter.testSynchronizeOnceAsync()
var syncDataB = try SyncData(data: dataB, storeCenter: self.secondStoreCenter)
var dataB = try await self.storeCenterB.testSynchronizeOnceAsync()
var syncDataB = try SyncData(data: dataB, storeCenter: self.storeCenterB)
#expect(syncDataB.grants.count == 2)
#expect(eventColB.count == 1)
#expect(tournamentColB.count == 1)
// Remove sharing from user2
try await StoreCenter.main.setAuthorizedUsersAsync(for: tournamentA, users: [])
try await self.storeCenterA.setAuthorizedUsersAsync(for: tournamentA, users: [])
dataB = try await self.secondStoreCenter.testSynchronizeOnceAsync()
syncDataB = try SyncData(data: dataB, storeCenter: self.secondStoreCenter)
dataB = try await self.storeCenterB.testSynchronizeOnceAsync()
syncDataB = try SyncData(data: dataB, storeCenter: self.storeCenterB)
#expect(syncDataB.revocations.count == 1)
#expect(syncDataB.revocationParents.count == 1)
#expect(eventColB.count == 0)
#expect(tournamentColB.count == 0)
let dataAccesses: [DataAccess] = try await StoreCenter.main.service().get()
dataAccesses = try await self.storeCenterA.service().get()
#expect(dataAccesses.count == 0)
}
@ -135,27 +157,27 @@ struct SyncDataAccessTests {
/// Here we want to test that the Club instance remains even if one event is removed from the second user
@Test func testSharedRelationship() async throws {
guard let userId1 = StoreCenter.main.userId else {
guard let userId1 = self.storeCenterA.userId else {
throw TestError.notAuthenticated
}
guard let userId2 = self.secondStoreCenter.userId else {
guard let userId2 = self.storeCenterB.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()
let eventColA: SyncedCollection<Event> = await self.storeCenterA.mainStore.asyncLoadingSynchronizedCollection()
let clubColA: SyncedCollection<Club> = await self.storeCenterA.mainStore.asyncLoadingSynchronizedCollection()
let eventColB: SyncedCollection<Event> = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection()
let clubColB: SyncedCollection<Club> = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection()
if let dataAccessCollection = StoreCenter.main.dataAccessCollection {
if let dataAccessCollection = self.storeCenterA.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()
let _ = try await self.storeCenterB.testSynchronizeOnceAsync()
#expect(eventColB.count == 0)
#expect(clubColB.count == 0)
@ -169,11 +191,11 @@ struct SyncDataAccessTests {
try await eventColA.addOrUpdateAsync(contentOfs: [event1A, event2A])
// Share with user2
try await StoreCenter.main.setAuthorizedUsersAsync(for: event1A, users: [userId2])
try await StoreCenter.main.setAuthorizedUsersAsync(for: event2A, users: [userId2])
try await self.storeCenterA.setAuthorizedUsersAsync(for: event1A, users: [userId2])
try await self.storeCenterA.setAuthorizedUsersAsync(for: event2A, users: [userId2])
var dataB = try await self.secondStoreCenter.testSynchronizeOnceAsync()
var syncDataB = try SyncData(data: dataB, storeCenter: self.secondStoreCenter)
var dataB = try await self.storeCenterB.testSynchronizeOnceAsync()
var syncDataB = try SyncData(data: dataB, storeCenter: self.storeCenterB)
#expect(syncDataB.grants.count == 2)
@ -186,10 +208,10 @@ struct SyncDataAccessTests {
#expect(clubColB.count == 1)
// Remove sharing from user2
try await StoreCenter.main.setAuthorizedUsersAsync(for: event1A, users: [])
try await self.storeCenterA.setAuthorizedUsersAsync(for: event1A, users: [])
dataB = try await self.secondStoreCenter.testSynchronizeOnceAsync()
syncDataB = try SyncData(data: dataB, storeCenter: self.secondStoreCenter)
dataB = try await self.storeCenterB.testSynchronizeOnceAsync()
syncDataB = try SyncData(data: dataB, storeCenter: self.storeCenterB)
#expect(syncDataB.revocations.count == 1)
#expect(syncDataB.revocationParents.count == 1)
@ -204,27 +226,27 @@ struct SyncDataAccessTests {
/// Here we want to test that the first Club is removed and the second one is received
@Test func testRelationshipChange() async throws {
guard let userId1 = StoreCenter.main.userId else {
guard let userId1 = self.storeCenterA.userId else {
throw TestError.notAuthenticated
}
guard let userId2 = self.secondStoreCenter.userId else {
guard let userId2 = self.storeCenterB.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()
let eventColA: SyncedCollection<Event> = await self.storeCenterA.mainStore.asyncLoadingSynchronizedCollection()
let clubColA: SyncedCollection<Club> = await self.storeCenterA.mainStore.asyncLoadingSynchronizedCollection()
let eventColB: SyncedCollection<Event> = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection()
let clubColB: SyncedCollection<Club> = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection()
if let dataAccessCollection = StoreCenter.main.dataAccessCollection {
if let dataAccessCollection = self.storeCenterA.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()
let _ = try await self.storeCenterB.testSynchronizeOnceAsync()
#expect(eventColB.count == 0)
#expect(clubColB.count == 0)
@ -239,17 +261,17 @@ struct SyncDataAccessTests {
try await eventColA.addOrUpdateAsync(instance: eventA)
// Share with user2
try await StoreCenter.main.setAuthorizedUsersAsync(for: eventA, users: [userId2])
let _ = try await self.secondStoreCenter.testSynchronizeOnceAsync()
try await self.storeCenterA.setAuthorizedUsersAsync(for: eventA, users: [userId2])
let _ = try await self.storeCenterB.testSynchronizeOnceAsync()
#expect(eventColB.count == 1)
#expect(clubColB.count == 1)
// Change the club
eventA.club = club2A.id
try await eventColA.addOrUpdateAsync(instance: eventA)
let dataB = try await self.secondStoreCenter.testSynchronizeOnceAsync()
let dataB = try await self.storeCenterB.testSynchronizeOnceAsync()
let syncDataB = try SyncData(data: dataB, storeCenter: self.secondStoreCenter)
let syncDataB = try SyncData(data: dataB, storeCenter: self.storeCenterB)
#expect(syncDataB.sharedRelationshipSets.count == 1)
#expect(syncDataB.sharedRelationshipRemovals.count == 1)
@ -265,29 +287,29 @@ struct SyncDataAccessTests {
/// Here we want to test that the first Club is removed and the second one is received
@Test func testRelationshipChangesByAgent() async throws {
guard let userId1 = StoreCenter.main.userId else {
guard let userId1 = self.storeCenterA.userId else {
throw TestError.notAuthenticated
}
guard let userId2 = self.secondStoreCenter.userId else {
guard let userId2 = self.storeCenterB.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 courtsColA: SyncedCollection<Court> = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection()
let eventColB: SyncedCollection<Event> = await self.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection()
let clubColB: SyncedCollection<Club> = await self.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection()
let courtsColB: SyncedCollection<Court> = await self.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection()
let eventColA: SyncedCollection<Event> = await self.storeCenterA.mainStore.asyncLoadingSynchronizedCollection()
let clubColA: SyncedCollection<Club> = await self.storeCenterA.mainStore.asyncLoadingSynchronizedCollection()
let courtsColA: SyncedCollection<Court> = await self.storeCenterA.mainStore.asyncLoadingSynchronizedCollection()
let eventColB: SyncedCollection<Event> = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection()
let clubColB: SyncedCollection<Club> = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection()
let courtsColB: SyncedCollection<Court> = await self.storeCenterB.mainStore.asyncLoadingSynchronizedCollection()
if let dataAccessCollection = StoreCenter.main.dataAccessCollection {
if let dataAccessCollection = self.storeCenterA.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()
let _ = try await self.storeCenterB.testSynchronizeOnceAsync()
#expect(eventColB.count == 0)
#expect(clubColB.count == 0)
@ -304,8 +326,8 @@ struct SyncDataAccessTests {
try await courtsColA.addOrUpdateAsync(contentOfs: [court1A, court2A])
// Share with user2
try await StoreCenter.main.setAuthorizedUsersAsync(for: eventA, users: [userId2])
let _ = try await self.secondStoreCenter.testSynchronizeOnceAsync()
try await self.storeCenterA.setAuthorizedUsersAsync(for: eventA, users: [userId2])
let _ = try await self.storeCenterB.testSynchronizeOnceAsync()
guard let eventB = eventColB.first else {
throw TestError.missingEvent
@ -319,7 +341,7 @@ struct SyncDataAccessTests {
eventB.club = club2B.id
try await eventColB.addOrUpdateAsync(instance: eventB)
let _ = try await StoreCenter.main.testSynchronizeOnceAsync()
let _ = try await self.storeCenterA.testSynchronizeOnceAsync()
#expect(eventA.club == club2B.id)
#expect(clubColB.count == 1)

@ -73,6 +73,7 @@ struct SynchronizationTests {
// Cleanup
let eventCollection1: SyncedCollection<Event> = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection()
try await eventCollection1.loadOnceAsync()
#expect(eventCollection1.hasLoaded == true)
try await eventCollection1.deleteAsync(contentOfs: Array(eventCollection1))

Loading…
Cancel
Save