Skip to content

Instantly share code, notes, and snippets.

@vanvoorden
Created July 30, 2024 23:53
Show Gist options
  • Save vanvoorden/31aa5fe38b1c333a36ec9b37998f122f to your computer and use it in GitHub Desktop.
Save vanvoorden/31aa5fe38b1c333a36ec9b37998f122f to your computer and use it in GitHub Desktop.
2024-07-30-4.swift
final class ContextTests : XCTestCase {
func testDelete() throws {
let schema = Schema([Item.self])
let configuration = ModelConfiguration(isStoredInMemoryOnly: true)
let container = try ModelContainer(
for: schema,
configurations: configuration
)
let modelContext = ModelContext(container)
modelContext.insert(Item())
XCTAssert(try modelContext.fetchCount(FetchDescriptor<Item>()) == 1)
try modelContext.delete(model: Item.self)
XCTAssert(try modelContext.fetchCount(FetchDescriptor<Item>()) == 0)
}
func testDeleteWithIteration() throws {
let schema = Schema([Item.self])
let configuration = ModelConfiguration(isStoredInMemoryOnly: true)
let container = try ModelContainer(
for: schema,
configurations: configuration
)
let modelContext = ModelContext(container)
modelContext.insert(Item())
XCTAssert(try modelContext.fetchCount(FetchDescriptor<Item>()) == 1)
try modelContext.delete(model: Item.self)
for model in try modelContext.fetch(FetchDescriptor<Item>()) {
modelContext.delete(model)
}
XCTAssert(try modelContext.fetchCount(FetchDescriptor<Item>()) == 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment