Added the catalogue models and seed to support phase 1 of the POC prototype in the Persistence package.
This commit is contained in:
1 parent
717df5208c
commit
6d7ab4bcba
19 files changed
+1381
-151
No files matched your search
@@ -0,0 +1,104 @@
|
||||
import HummingbirdFluent
|
||||
import Logging
|
||||
import Testing
|
||||
|
||||
@testable import Persistence
|
||||
|
||||
@Suite(
|
||||
"CatalogueRepository repository",
|
||||
.tags(.repository)
|
||||
)
|
||||
struct CatalogueRepositoryTests {
|
||||
|
||||
// MARK: Methods tests
|
||||
|
||||
@Test
|
||||
func `seeds every demo shop and listing`() async throws {
|
||||
try await withCatalogue { repository in
|
||||
let listings = try await repository.listings()
|
||||
|
||||
#expect(listings.count == Seed.listings.count)
|
||||
|
||||
for shop in Seed.shops {
|
||||
let found = try await repository.shop(slug: shop.slug)
|
||||
|
||||
#expect(found == shop)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func `lists the newest listing first by default`() async throws {
|
||||
try await withCatalogue { repository in
|
||||
let listings = try await repository.listings()
|
||||
|
||||
#expect(listings.map(\.slug) == Seed.listings.map(\.slug))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func `finds a listing with its shop by slug`() async throws {
|
||||
try await withCatalogue { repository in
|
||||
let found = try await repository.listing(slug: "nightward-1-cgc-9-8")
|
||||
let missing = try await repository.listing(slug: "missing")
|
||||
let listing = try #require(found)
|
||||
|
||||
#expect(listing.title == "Nightward #1")
|
||||
#expect(listing.shop.name == "Kestrel Row Comics")
|
||||
#expect(missing == nil)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func `lists each publisher once, alphabetically`() async throws {
|
||||
try await withCatalogue { repository in
|
||||
let publishers = try await repository.publishers()
|
||||
|
||||
#expect(publishers == [
|
||||
"Blackline", "Cutwater Press", "Harbour & Vale", "Meridian Comics", "Sōkai Books",
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
func `narrows and orders the listings by the filter`() async throws {
|
||||
try await withCatalogue { repository in
|
||||
let listings = try await repository.listings(matching: .init(
|
||||
query: "nightward 1",
|
||||
sort: .priceAscending
|
||||
))
|
||||
|
||||
#expect(listings.map(\.priceCents) == [3_500, 12_000, 15_000, 24_000])
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private extension CatalogueRepositoryTests {
|
||||
|
||||
/// Runs the body against a migrated, seeded in-memory catalogue, shutting the pool down after.
|
||||
func withCatalogue(
|
||||
_ body: (CatalogueRepository) async throws -> Void
|
||||
) async throws {
|
||||
let fluent = try Service(
|
||||
driver: .inMemory,
|
||||
logger: Logger(label: "test")
|
||||
)()
|
||||
|
||||
do {
|
||||
await PrepareDB()(for: fluent)
|
||||
|
||||
try await fluent.migrate()
|
||||
try await body(.init(fluent: fluent))
|
||||
} catch {
|
||||
try? await fluent.shutdown()
|
||||
|
||||
throw error
|
||||
}
|
||||
|
||||
try await fluent.shutdown()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user