62 lines
2.1 KiB
Swift
62 lines
2.1 KiB
Swift
import Testing
|
|
import Foundation
|
|
import SwiftData
|
|
@testable import Storage
|
|
|
|
@Suite("BookStore")
|
|
struct BookStoreTests {
|
|
@MainActor
|
|
func makeStore() throws -> (BookStore, URL) {
|
|
let config = ModelConfiguration(isStoredInMemoryOnly: true)
|
|
let container = try ModelContainer(for: StoredBook.self, configurations: config)
|
|
let tmpDir = URL(fileURLWithPath: NSTemporaryDirectory())
|
|
.appendingPathComponent(UUID().uuidString)
|
|
try FileManager.default.createDirectory(at: tmpDir, withIntermediateDirectories: true)
|
|
return (BookStore(modelContainer: container, documentsDirectory: tmpDir), tmpDir)
|
|
}
|
|
|
|
@Test @MainActor func importAndList() throws {
|
|
let (store, tmpDir) = try makeStore()
|
|
defer { try? FileManager.default.removeItem(at: tmpDir) }
|
|
|
|
let sourceFile = tmpDir.appendingPathComponent("source.epub")
|
|
try "fake epub".write(to: sourceFile, atomically: true, encoding: .utf8)
|
|
|
|
let stored = try store.importBook(from: sourceFile, title: "Test Book", author: "Author")
|
|
#expect(stored.title == "Test Book")
|
|
#expect(stored.lastPosition == 0)
|
|
|
|
let books = try store.allBooks()
|
|
#expect(books.count == 1)
|
|
#expect(store.fileExists(for: books[0]))
|
|
}
|
|
|
|
@Test @MainActor func updatePosition() throws {
|
|
let (store, tmpDir) = try makeStore()
|
|
defer { try? FileManager.default.removeItem(at: tmpDir) }
|
|
|
|
let sourceFile = tmpDir.appendingPathComponent("source.txt")
|
|
try "text".write(to: sourceFile, atomically: true, encoding: .utf8)
|
|
|
|
let stored = try store.importBook(from: sourceFile, title: "Book", author: nil)
|
|
try store.updatePosition(stored, position: 500)
|
|
#expect(stored.lastPosition == 500)
|
|
#expect(stored.lastRead != nil)
|
|
}
|
|
|
|
@Test @MainActor func deleteBook() throws {
|
|
let (store, tmpDir) = try makeStore()
|
|
defer { try? FileManager.default.removeItem(at: tmpDir) }
|
|
|
|
let sourceFile = tmpDir.appendingPathComponent("source.txt")
|
|
try "text".write(to: sourceFile, atomically: true, encoding: .utf8)
|
|
|
|
let stored = try store.importBook(from: sourceFile, title: "Book", author: nil)
|
|
#expect(store.fileExists(for: stored))
|
|
|
|
try store.deleteBook(stored)
|
|
let books = try store.allBooks()
|
|
#expect(books.isEmpty)
|
|
}
|
|
}
|