Skip to content

Commit

Permalink
Simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Oct 18, 2024
1 parent ec3de27 commit 6b4ded0
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 376 deletions.
31 changes: 0 additions & 31 deletions Tests/OrdersTests/EncryptedOrdersDelegate.swift

This file was deleted.

92 changes: 0 additions & 92 deletions Tests/OrdersTests/EncryptedOrdersTests.swift

This file was deleted.

3 changes: 2 additions & 1 deletion Tests/OrdersTests/OrderData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ struct OrderDataMiddleware: AsyncModelMiddleware {
func create(model: OrderData, on db: any Database, next: any AnyAsyncModelResponder) async throws {
let order = Order(
orderTypeIdentifier: "order.com.example.pet-store",
authenticationToken: Data([UInt8].random(count: 12)).base64EncodedString())
authenticationToken: Data([UInt8].random(count: 12)).base64EncodedString()
)
try await order.save(on: db)
model.$order.id = try order.requireID()
try await next.create(model, on: db)
Expand Down
38 changes: 18 additions & 20 deletions Tests/OrdersTests/OrdersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import Zip

@Suite("Orders Tests")
struct OrdersTests {
let delegate = TestOrdersDelegate()
let ordersURI = "/api/orders/v1/"

@Test("Order Generation")
func orderGeneration() async throws {
try await withApp(delegate: delegate) { app, ordersService in
@Test("Order Generation", arguments: [true, false])
func orderGeneration(useEncryptedKey: Bool) async throws {
try await withApp(useEncryptedKey: useEncryptedKey) { app, ordersService in
let orderData = OrderData(title: "Test Order")
try await orderData.create(on: app.db)
let order = try await orderData.$order.get(on: app.db)
Expand Down Expand Up @@ -41,7 +40,7 @@ struct OrdersTests {

@Test("Getting Order from Apple Wallet API")
func getOrderFromAPI() async throws {
try await withApp(delegate: delegate) { app, ordersService in
try await withApp { app, ordersService in
let orderData = OrderData(title: "Test Order")
try await orderData.create(on: app.db)
let order = try await orderData.$order.get(on: app.db)
Expand Down Expand Up @@ -117,7 +116,7 @@ struct OrdersTests {

@Test("Device Registration API")
func apiDeviceRegistration() async throws {
try await withApp(delegate: delegate) { app, ordersService in
try await withApp { app, ordersService in
let orderData = OrderData(title: "Test Order")
try await orderData.create(on: app.db)
let order = try await orderData.$order.get(on: app.db)
Expand Down Expand Up @@ -270,7 +269,7 @@ struct OrdersTests {

@Test("Error Logging")
func errorLog() async throws {
try await withApp(delegate: delegate) { app, ordersService in
try await withApp { app, ordersService in
let log1 = "Error 1"
let log2 = "Error 2"

Expand Down Expand Up @@ -313,9 +312,9 @@ struct OrdersTests {
}
}

@Test("APNS Client")
func apnsClient() async throws {
try await withApp(delegate: delegate) { app, ordersService in
@Test("APNS Client", arguments: [true, false])
func apnsClient(useEncryptedKey: Bool) async throws {
try await withApp(useEncryptedKey: useEncryptedKey) { app, ordersService in
#expect(app.apns.client(.init(string: "orders")) != nil)

let orderData = OrderData(title: "Test Order")
Expand Down Expand Up @@ -397,16 +396,15 @@ struct OrdersTests {

@Test("Default OrdersDelegate Properties")
func defaultDelegate() {
#expect(!DefaultOrdersDelegate().generateSignatureFile(in: URL(fileURLWithPath: "")))
}
}
final class DefaultOrdersDelegate: OrdersDelegate {
func template<O: OrderModel>(for order: O, db: any Database) async throws -> URL {
URL(fileURLWithPath: "")
}
func encode<O: OrderModel>(order: O, db: any Database, encoder: JSONEncoder) async throws -> Data {
Data()
}
}

final class DefaultOrdersDelegate: OrdersDelegate {
let sslSigningFilesDirectory = URL(fileURLWithPath: "", isDirectory: true)
func template<O: OrderModel>(for order: O, db: any Database) async throws -> URL {
URL(fileURLWithPath: "")
}
func encode<O: OrderModel>(order: O, db: any Database, encoder: JSONEncoder) async throws -> Data {
Data()
#expect(!DefaultOrdersDelegate().generateSignatureFile(in: URL(fileURLWithPath: "")))
}
}
13 changes: 3 additions & 10 deletions Tests/OrdersTests/TestOrdersDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import Orders
import Vapor

final class TestOrdersDelegate: OrdersDelegate {
func encode<O: OrderModel>(
order: O, db: any Database, encoder: JSONEncoder
) async throws -> Data {
func encode<O: OrderModel>(order: O, db: any Database, encoder: JSONEncoder) async throws -> Data {
guard
let orderData = try await OrderData.query(on: db)
.filter(\.$order.$id == order.requireID())
Expand All @@ -14,18 +12,13 @@ final class TestOrdersDelegate: OrdersDelegate {
else {
throw Abort(.internalServerError)
}
guard let data = try? encoder.encode(OrderJSONData(data: orderData, order: orderData.order))
else {
guard let data = try? encoder.encode(OrderJSONData(data: orderData, order: orderData.order)) else {
throw Abort(.internalServerError)
}
return data
}

func template<O: OrderModel>(for: O, db: any Database) async throws -> URL {
URL(
fileURLWithPath:
"\(FileManager.default.currentDirectoryPath)/Tests/OrdersTests/Templates/",
isDirectory: true
)
URL(fileURLWithPath: "\(FileManager.default.currentDirectoryPath)/Tests/OrdersTests/Templates/", isDirectory: true)
}
}
3 changes: 2 additions & 1 deletion Tests/OrdersTests/withApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Vapor
import Zip

func withApp(
delegate: some OrdersDelegate,
useEncryptedKey: Bool = false,
_ body: (Application, OrdersService) async throws -> Void
) async throws {
Expand All @@ -17,6 +16,8 @@ func withApp(

app.databases.use(.sqlite(.memory), as: .sqlite)

let delegate = TestOrdersDelegate()

OrdersService.register(migrations: app.migrations)
app.migrations.add(CreateOrderData())
let ordersService = try OrdersService(
Expand Down
49 changes: 0 additions & 49 deletions Tests/PassesTests/EncryptedPassesDelegate.swift

This file was deleted.

Loading

0 comments on commit 6b4ded0

Please sign in to comment.