// // ApiCallTests.swift // LeStorageTests // // Created by Laurent Morvillier on 15/02/2025. // import Testing @testable import LeStorage class Thing: SyncedModelObject, SyncedStorable, URLParameterConvertible { override required init() { super.init() } static func resourceName() -> String { return "thing" } static func tokenExemptedMethods() -> [LeStorage.HTTPMethod] { return [] } static func filterByStoreIdentifier() -> Bool { return false } static var copyServerResponse: Bool = false static func storeParent() -> Bool { return false } var id: String = Store.randomId() var name: String = "" init(name: String) { self.name = name super.init() } required init(from decoder: any Decoder) throws { fatalError("init(from:) has not been implemented") } func copy(from other: any LeStorage.Storable) { } static func relationships() -> [LeStorage.Relationship] { return [] } func queryParameters(storeCenter: StoreCenter) -> [String : String] { return ["yeah?" : "god!"] } } struct ApiCallTests { @Test func testApiCallProvisioning1() async throws { let collection = ApiCallCollection(storeCenter: StoreCenter.main) let thing = Thing(name: "yeah") let _ = try await collection.sendInsertion(thing) await #expect(collection.items.count == 1) if let apiCall = await collection.items.first { #expect(apiCall.method == .post) } thing.name = "woo" let _ = try await collection.sendUpdate(thing) await #expect(collection.items.count == 2) // one post and one put if let apiCall = await collection.items.first { #expect(apiCall.method == .post) } if let apiCall = await collection.items.last { #expect(apiCall.method == .put) } let _ = try await collection.sendDeletion(thing) await #expect(collection.items.count == 1) } @Test func testApiCallProvisioning2() async throws { let collection = ApiCallCollection(storeCenter: StoreCenter.main) let thing = Thing(name: "yeah") let _ = try await collection.sendUpdate(thing) await #expect(collection.items.count == 1) if let apiCall = await collection.items.first { #expect(apiCall.method == .put) } thing.name = "woo" let _ = try await collection.sendUpdate(thing) let _ = try await collection.sendUpdate(thing) let _ = try await collection.sendUpdate(thing) await #expect(collection.items.count == 1) if let apiCall = await collection.items.first { #expect(apiCall.method == .put) } let _ = try await collection.sendDeletion(thing) await #expect(collection.items.count == 1) } @Test func testApiCallProvisioning3() async throws { let collection = ApiCallCollection(storeCenter: StoreCenter.main) let thing = Thing(name: "yeah") let _ = try await collection.sendDeletion(thing) await #expect(collection.items.count == 1) let _ = try await collection.sendDeletion(thing) await #expect(collection.items.count == 1) let _ = try await collection.sendDeletion(thing) await #expect(collection.items.count == 1) } @Test func testGetProvisioning() async throws { let collection = ApiCallCollection(storeCenter: StoreCenter.main) try await collection.sendGetRequest(storeId: "1") await #expect(collection.items.count == 1) try await collection.sendGetRequest(storeId: "1") await #expect(collection.items.count == 1) try await collection.sendGetRequest(storeId: "2") await #expect(collection.items.count == 2) try await collection.sendGetRequest(instance: Thing(name: "man!")) await #expect(collection.items.count == 3) try await collection.sendGetRequest(storeId: nil) await #expect(collection.items.count == 4) try await collection.sendGetRequest(storeId: nil) await #expect(collection.items.count == 4) } }