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.
 
 
LeStorage/LeStorageTests/StoredCollectionTests.swift

119 lines
3.0 KiB

//
// StoredCollectionTests.swift
// LeStorageTests
//
// Created by Laurent Morvillier on 16/10/2024.
//
import XCTest
@testable import LeStorage
class StoredCollectionTests: XCTestCase {
var collection: StoredCollection<MockStorable>!
override func setUp() {
super.setUp()
self.collection = Store.main.registerCollection()
}
override func tearDown() {
self.collection.clear()
super.tearDown()
}
func testInitialization() {
XCTAssertEqual(collection.items.count, 0)
}
func testAddOrUpdate() throws {
let item = MockStorable(id: "1", name: "Test")
collection.addOrUpdate(instance: item)
XCTAssertEqual(collection.items.count, 1)
XCTAssertEqual(collection.items[0].id, "1")
}
func testDelete() throws {
let item = MockStorable(id: "1", name: "Test")
collection.addOrUpdate(instance: item)
XCTAssertEqual(collection.items.count, 1)
try collection.delete(instance: item)
XCTAssertEqual(collection.items.count, 0)
}
func testFindById() throws {
let item = MockStorable(id: "1", name: "Test")
collection.addOrUpdate(instance: item)
let foundItem = collection.findById("1")
XCTAssertNotNil(foundItem)
XCTAssertEqual(foundItem?.id, "1")
}
func testDeleteById() throws {
let item = MockStorable(id: "1", name: "Test")
collection.addOrUpdate(instance: item)
try collection.deleteById("1")
XCTAssertNil(collection.findById("1"))
}
func testAddOrUpdateMultiple() throws {
let items = [
MockStorable(id: "1", name: "Test1"),
MockStorable(id: "2", name: "Test2"),
]
collection.addOrUpdate(contentOfs: items)
XCTAssertEqual(collection.items.count, 2)
}
func testDeleteAll() throws {
let items = [
MockStorable(id: "1", name: "Test1"),
MockStorable(id: "2", name: "Test2"),
]
collection.addOrUpdate(contentOfs: items)
XCTAssertEqual(collection.items.count, 2)
collection.clear()
XCTAssertEqual(collection.items.count, 0)
}
func testRandomAccessCollection() {
let items = [
MockStorable(id: "1", name: "Test1"),
MockStorable(id: "2", name: "Test2"),
MockStorable(id: "3", name: "Test3"),
]
collection.addOrUpdate(contentOfs: items)
XCTAssertEqual(collection.startIndex, 0)
XCTAssertEqual(collection.endIndex, 3)
XCTAssertEqual(collection[1].name, "Test2")
}
}
// Mock Storable for testing purposes
class MockStorable: ModelObject, Storable {
static func filterByStoreIdentifier() -> Bool {
return false
}
var id: String = Store.randomId()
var name: String
init(id: String, name: String) {
self.id = id
self.name = name
}
static func resourceName() -> String {
return "mocks"
}
}