Fix testing issues

sync3
Laurent 6 months ago
parent 002cec1634
commit 9c5ddf30fa
  1. 6
      LeStorage/BaseCollection.swift
  2. 10
      LeStorage/Store.swift
  3. 2
      LeStorageTests/ApiCallTests.swift
  4. 36
      LeStorageTests/CollectionsTests.swift
  5. 29
      LeStorageTests/StoredCollectionTests.swift

@ -65,9 +65,11 @@ public class BaseCollection<T: Storable>: SomeCollection, CollectionHolder {
/// Sets a max number of items inside the collection
fileprivate(set) var limit: Int? = nil
init(store: Store) async {
init(store: Store, inMemory: Bool = false) async {
self.store = store
await self.loadFromFile()
if self.inMemory == false {
await self.loadFromFile()
}
}
init(store: Store, indexed: Bool = false, inMemory: Bool = false, limit: Int? = nil, synchronousLoading: Bool = false) {

@ -112,13 +112,19 @@ final public class Store {
return collection
}
func synchronizedCollectionWithFileLoading<T : SyncedStorable>() async -> SyncedCollection<T> {
let collection = await SyncedCollection<T>(store: self)
func asyncLoadingSynchronizedCollection<T : SyncedStorable>(inMemory: Bool = false) async -> SyncedCollection<T> {
let collection = await SyncedCollection<T>(store: self, inMemory: inMemory)
self._collections[T.resourceName()] = collection
self.storeCenter.loadApiCallCollection(type: T.self)
return collection
}
func asyncLoadingStoredCollection<T : Storable>(inMemory: Bool = false) async -> StoredCollection<T> {
let collection = await StoredCollection<T>(store: self, inMemory: inMemory)
self._collections[T.resourceName()] = collection
return collection
}
/// Registers a singleton object
/// - Parameters:
/// - synchronized: indicates if the data is synchronized with the server

@ -9,6 +9,7 @@ import Testing
@testable import LeStorage
class Thing: SyncedModelObject, SyncedStorable, URLParameterConvertible {
override required init() {
super.init()
}
@ -16,6 +17,7 @@ class Thing: SyncedModelObject, SyncedStorable, URLParameterConvertible {
static func resourceName() -> String { return "thing" }
static func tokenExemptedMethods() -> [LeStorage.HTTPMethod] { return [] }
static func filterByStoreIdentifier() -> Bool { return false }
static var copyServerResponse: Bool = false
var id: String = Store.randomId()
var name: String = ""

@ -32,6 +32,7 @@ class Boat: ModelObject, SyncedStorable {
static func tokenExemptedMethods() -> [LeStorage.HTTPMethod] { return [] }
static func resourceName() -> String { return "boat" }
static var copyServerResponse: Bool = false
var storeId: String? { return nil }
func copy(from other: any LeStorage.Storable) {
@ -46,34 +47,19 @@ 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)
init() async {
cars = await StoreCenter.main.mainStore.asyncLoadingStoredCollection(inMemory: true)
boats = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection(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 testLoading() async {
#expect(self.cars.hasLoaded)
#expect(self.boats.hasLoaded)
}
@Test func differentiationTest() async throws {
try await ensureCollectionLoaded(cars)
try await ensureCollectionLoaded(boats)
// Cars
#expect(cars.count == 0)
cars.addOrUpdate(instance: Car())
@ -83,21 +69,17 @@ struct CollectionsTests {
#expect(boats.count == 0)
let oldApiCallCount = await StoreCenter.main.apiCallCount(type: Boat.self)
#expect(oldApiCallCount == 0)
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)
}
}

@ -20,34 +20,17 @@ struct StoredCollectionTests {
var collection: StoredCollection<MockStorable>
init() {
collection = StoreCenter.main.mainStore.registerCollection()
}
func ensureCollectionLoaded() 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))
}
init() async {
collection = await StoreCenter.main.mainStore.asyncLoadingStoredCollection(inMemory: true)
collection.reset()
}
@Test func testInitialization() async throws {
try await ensureCollectionLoaded()
#expect(self.collection.hasLoaded)
#expect(collection.items.count == 0)
}
@Test func testAddOrUpdate() async throws {
try await ensureCollectionLoaded()
let item = MockStorable(id: "1", name: "Test")
collection.addOrUpdate(instance: item)
@ -61,7 +44,6 @@ struct StoredCollectionTests {
}
@Test func testDelete() async throws {
try await ensureCollectionLoaded()
let item = MockStorable(id: "1", name: "Test")
collection.addOrUpdate(instance: item)
#expect(collection.items.count == 1)
@ -71,7 +53,6 @@ struct StoredCollectionTests {
}
@Test func testFindById() async throws {
try await ensureCollectionLoaded()
let item = MockStorable(id: "1", name: "Test")
collection.addOrUpdate(instance: item)
@ -83,7 +64,6 @@ struct StoredCollectionTests {
}
@Test func testDeleteById() async throws {
try await ensureCollectionLoaded()
let item = MockStorable(id: "1", name: "Test")
collection.addOrUpdate(instance: item)
@ -93,7 +73,6 @@ struct StoredCollectionTests {
}
@Test func testAddOrUpdateMultiple() async throws {
try await ensureCollectionLoaded()
let items = [
MockStorable(id: "1", name: "Test1"),
MockStorable(id: "2", name: "Test2"),
@ -104,7 +83,6 @@ struct StoredCollectionTests {
}
@Test func testDeleteAll() async throws {
try await ensureCollectionLoaded()
let items = [
MockStorable(id: "1", name: "Test1"),
MockStorable(id: "2", name: "Test2"),
@ -118,7 +96,6 @@ struct StoredCollectionTests {
}
@Test func testRandomAccessCollection() async throws {
try await ensureCollectionLoaded()
let items = [
MockStorable(id: "1", name: "Test1"),
MockStorable(id: "2", name: "Test2"),

Loading…
Cancel
Save