You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.7 KiB
103 lines
2.7 KiB
//
|
|
// CollectionsTests.swift
|
|
// LeStorageTests
|
|
//
|
|
// Created by Laurent Morvillier on 15/10/2024.
|
|
//
|
|
|
|
import Testing
|
|
@testable import LeStorage
|
|
|
|
class Car: ModelObject, Storable {
|
|
|
|
var id: String = Store.randomId()
|
|
|
|
static func resourceName() -> String { return "car" }
|
|
func copy(from other: any LeStorage.Storable) {
|
|
|
|
}
|
|
static func relationships() -> [LeStorage.Relationship] { return [] }
|
|
|
|
}
|
|
|
|
class Boat: ModelObject, SyncedStorable {
|
|
|
|
var id: String = Store.randomId()
|
|
var lastUpdate: Date = Date()
|
|
var shared: Bool? = false
|
|
|
|
override required init() {
|
|
super.init()
|
|
}
|
|
|
|
static func tokenExemptedMethods() -> [LeStorage.HTTPMethod] { return [] }
|
|
static func resourceName() -> String { return "boat" }
|
|
|
|
var storeId: String? { return nil }
|
|
func copy(from other: any LeStorage.Storable) {
|
|
|
|
}
|
|
static func relationships() -> [LeStorage.Relationship] { return [] }
|
|
|
|
}
|
|
|
|
struct CollectionsTests {
|
|
|
|
var cars: StoredCollection<Car>
|
|
var boats: SyncedCollection<Boat>
|
|
|
|
init() {
|
|
let storeCenter = StoreCenter.main
|
|
cars = storeCenter.mainStore.registerCollection(inMemory: true)
|
|
boats = storeCenter.mainStore.registerSynchronizedCollection(inMemory: true)
|
|
}
|
|
|
|
func ensureCollectionLoaded(_ collection: any SomeCollection) async throws {
|
|
// Wait for the collection to finish loading
|
|
// Adjust the timeout as needed
|
|
let timeout = 5.0 // seconds
|
|
let startTime = Date()
|
|
|
|
while !collection.hasLoaded {
|
|
// Check for timeout
|
|
if Date().timeIntervalSince(startTime) > timeout {
|
|
throw Error("Collection loading timed out")
|
|
}
|
|
// Wait a bit before checking again
|
|
try await Task.sleep(for: .milliseconds(100))
|
|
}
|
|
collection.reset()
|
|
}
|
|
|
|
@Test func differentiationTest() async throws {
|
|
|
|
try await ensureCollectionLoaded(cars)
|
|
try await ensureCollectionLoaded(boats)
|
|
|
|
// Cars
|
|
#expect(cars.count == 0)
|
|
cars.addOrUpdate(instance: Car())
|
|
#expect(cars.count == 1)
|
|
|
|
// Boats
|
|
|
|
#expect(boats.count == 0)
|
|
let oldApiCallCount = await StoreCenter.main.apiCallCount(type: Boat.self)
|
|
|
|
boats.addOrUpdate(instance: Boat())
|
|
#expect(boats.count == 1)
|
|
|
|
let newApiCallCount = await StoreCenter.main.apiCallCount(type: Boat.self)
|
|
|
|
#expect(oldApiCallCount == newApiCallCount - 1)
|
|
|
|
// Cars and boats
|
|
cars.reset()
|
|
boats.reset()
|
|
#expect(cars.count == 0)
|
|
#expect(boats.count == 0)
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|