Skip to content

Commit

Permalink
Handle errors in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Oct 22, 2024
1 parent 168b6d5 commit 839cc5d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 56 deletions.
17 changes: 9 additions & 8 deletions Tests/OrdersTests/OrdersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -377,14 +377,15 @@ struct OrdersTests {
}
)

// Test `OrderDataMiddleware` update method
// TODO: Fix this test
// orderData.title = "Test Order 2"
// do {
// try await orderData.update(on: app.db)
// } catch let error as HTTPClientError {
// #expect(error.self == .remoteConnectionClosed)
// }
if !useEncryptedKey {
// Test `OrderDataMiddleware` update method
orderData.title = "Test Order 2"
do {
try await orderData.update(on: app.db)
} catch let error as HTTPClientError {
#expect(error.self == .remoteConnectionClosed)
}
}
}
}

Expand Down
45 changes: 25 additions & 20 deletions Tests/OrdersTests/withApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,35 @@ func withApp(
_ body: (Application, OrdersService) async throws -> Void
) async throws {
let app = try await Application.make(.testing)
try #require(isLoggingConfigured)
do {
try #require(isLoggingConfigured)

app.databases.use(.sqlite(.memory), as: .sqlite)
OrdersService.register(migrations: app.migrations)
app.migrations.add(CreateOrderData())
try await app.autoMigrate()
app.databases.use(.sqlite(.memory), as: .sqlite)
OrdersService.register(migrations: app.migrations)
app.migrations.add(CreateOrderData())
try await app.autoMigrate()

let delegate = TestOrdersDelegate()
let ordersService = try OrdersService(
app: app,
delegate: delegate,
signingFilesDirectory: "\(FileManager.default.currentDirectoryPath)/Tests/Certificates/",
pemCertificate: useEncryptedKey ? "encryptedcert.pem" : "certificate.pem",
pemPrivateKey: useEncryptedKey ? "encryptedkey.pem" : "key.pem",
pemPrivateKeyPassword: useEncryptedKey ? "password" : nil,
pushRoutesMiddleware: SecretMiddleware(secret: "foo"),
logger: app.logger
)
app.databases.middleware.use(OrderDataMiddleware(service: ordersService), on: .sqlite)
let delegate = TestOrdersDelegate()
let ordersService = try OrdersService(
app: app,
delegate: delegate,
signingFilesDirectory: "\(FileManager.default.currentDirectoryPath)/Tests/Certificates/",
pemCertificate: useEncryptedKey ? "encryptedcert.pem" : "certificate.pem",
pemPrivateKey: useEncryptedKey ? "encryptedkey.pem" : "key.pem",
pemPrivateKeyPassword: useEncryptedKey ? "password" : nil,
pushRoutesMiddleware: SecretMiddleware(secret: "foo"),
logger: app.logger
)
app.databases.middleware.use(OrderDataMiddleware(service: ordersService), on: .sqlite)

Zip.addCustomFileExtension("order")
Zip.addCustomFileExtension("order")

try await body(app, ordersService)
try await body(app, ordersService)

try await app.autoRevert()
try await app.autoRevert()
} catch {
try await app.asyncShutdown()
throw error
}
try await app.asyncShutdown()
}
17 changes: 9 additions & 8 deletions Tests/PassesTests/PassesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -505,14 +505,15 @@ struct PassesTests {
}
)

// Test `PassDataMiddleware` update method
// TODO: Fix this test
// passData.title = "Test Pass 2"
// do {
// try await passData.update(on: app.db)
// } catch let error as HTTPClientError {
// #expect(error.self == .remoteConnectionClosed)
// }
if !useEncryptedKey {
// Test `PassDataMiddleware` update method
passData.title = "Test Pass 2"
do {
try await passData.update(on: app.db)
} catch let error as HTTPClientError {
#expect(error.self == .remoteConnectionClosed)
}
}
}
}

Expand Down
45 changes: 25 additions & 20 deletions Tests/PassesTests/withApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,35 @@ func withApp(
_ body: (Application, PassesService) async throws -> Void
) async throws {
let app = try await Application.make(.testing)
try #require(isLoggingConfigured)
do {
try #require(isLoggingConfigured)

app.databases.use(.sqlite(.memory), as: .sqlite)
PassesService.register(migrations: app.migrations)
app.migrations.add(CreatePassData())
try await app.autoMigrate()
app.databases.use(.sqlite(.memory), as: .sqlite)
PassesService.register(migrations: app.migrations)
app.migrations.add(CreatePassData())
try await app.autoMigrate()

let delegate = TestPassesDelegate()
let passesService = try PassesService(
app: app,
delegate: delegate,
signingFilesDirectory: "\(FileManager.default.currentDirectoryPath)/Tests/Certificates/",
pemCertificate: useEncryptedKey ? "encryptedcert.pem" : "certificate.pem",
pemPrivateKey: useEncryptedKey ? "encryptedkey.pem" : "key.pem",
pemPrivateKeyPassword: useEncryptedKey ? "password" : nil,
pushRoutesMiddleware: SecretMiddleware(secret: "foo"),
logger: app.logger
)
app.databases.middleware.use(PassDataMiddleware(service: passesService), on: .sqlite)
let delegate = TestPassesDelegate()
let passesService = try PassesService(
app: app,
delegate: delegate,
signingFilesDirectory: "\(FileManager.default.currentDirectoryPath)/Tests/Certificates/",
pemCertificate: useEncryptedKey ? "encryptedcert.pem" : "certificate.pem",
pemPrivateKey: useEncryptedKey ? "encryptedkey.pem" : "key.pem",
pemPrivateKeyPassword: useEncryptedKey ? "password" : nil,
pushRoutesMiddleware: SecretMiddleware(secret: "foo"),
logger: app.logger
)
app.databases.middleware.use(PassDataMiddleware(service: passesService), on: .sqlite)

Zip.addCustomFileExtension("pkpass")
Zip.addCustomFileExtension("pkpass")

try await body(app, passesService)
try await body(app, passesService)

try await app.autoRevert()
try await app.autoRevert()
} catch {
try await app.asyncShutdown()
throw error
}
try await app.asyncShutdown()
}

0 comments on commit 839cc5d

Please sign in to comment.