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.
107 lines
2.8 KiB
107 lines
2.8 KiB
//
|
|
// LeStorageTests.swift
|
|
// LeStorageTests
|
|
//
|
|
// Created by Laurent Morvillier on 18/09/2024.
|
|
//
|
|
|
|
import Testing
|
|
@testable import LeStorage
|
|
|
|
class IntObject: ModelObject, Storable {
|
|
|
|
static func resourceName() -> String { "int" }
|
|
static func tokenExemptedMethods() -> [LeStorage.HTTPMethod] { [] }
|
|
|
|
var id: Int
|
|
var name: String
|
|
|
|
init(id: Int, name: String) {
|
|
self.id = id
|
|
self.name = name
|
|
}
|
|
func copy(from other: any LeStorage.Storable) {
|
|
}
|
|
|
|
static func relationships() -> [LeStorage.Relationship] {
|
|
return []
|
|
}
|
|
|
|
}
|
|
|
|
class StringObject: ModelObject, Storable {
|
|
static func resourceName() -> String { "string" }
|
|
static func tokenExemptedMethods() -> [LeStorage.HTTPMethod] { [] }
|
|
|
|
var id: String
|
|
var name: String
|
|
|
|
init(id: String, name: String) {
|
|
self.id = id
|
|
self.name = name
|
|
}
|
|
func copy(from other: any LeStorage.Storable) {
|
|
}
|
|
|
|
static func relationships() -> [LeStorage.Relationship] {
|
|
return []
|
|
}
|
|
|
|
}
|
|
|
|
struct IdentifiableTests {
|
|
|
|
let intObjects: StoredCollection<IntObject>
|
|
let stringObjects: StoredCollection<StringObject>
|
|
|
|
init() {
|
|
let dir = "test_" + String.random()
|
|
let storeCenter: StoreCenter = StoreCenter(directoryName:dir)
|
|
intObjects = storeCenter.mainStore.registerCollection()
|
|
stringObjects = storeCenter.mainStore.registerCollection()
|
|
}
|
|
|
|
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 testIntIds() async throws {
|
|
|
|
try await ensureCollectionLoaded(self.intObjects)
|
|
|
|
let int = IntObject(id: 12, name: "test")
|
|
self.intObjects.addOrUpdate(instance: int)
|
|
|
|
if let search = intObjects.findById(12) {
|
|
#expect(search.id == 12)
|
|
} else {
|
|
Issue.record("object is missing")
|
|
}
|
|
}
|
|
|
|
@Test func testStringIds() async throws {
|
|
|
|
try await ensureCollectionLoaded(self.stringObjects)
|
|
let string = StringObject(id: "coco", name: "name")
|
|
self.stringObjects.addOrUpdate(instance: string)
|
|
|
|
if let search = stringObjects.findById("coco") {
|
|
#expect(search.id == "coco")
|
|
} else {
|
|
Issue.record("object is missing")
|
|
}
|
|
}
|
|
}
|
|
|