Skip to content

Commit ba878e2

Browse files
authored
Merge pull request #662 from vapor/beta-5-updates
Fluent Beta 5
2 parents 74c64be + 6709156 commit ba878e2

File tree

6 files changed

+58
-31
lines changed

6 files changed

+58
-31
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ on:
44
jobs:
55
xenial:
66
container:
7-
image: vapor/swift:5.1-xenial
7+
image: vapor/swift:5.2-xenial
88
runs-on: ubuntu-latest
99
steps:
1010
- uses: actions/checkout@v1
1111
- run: swift test --enable-test-discovery --sanitize=thread
1212
bionic:
1313
container:
14-
image: vapor/swift:5.1-bionic
14+
image: vapor/swift:5.2-bionic
1515
runs-on: ubuntu-latest
1616
steps:
1717
- uses: actions/checkout@v1

Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import PackageDescription
44
let package = Package(
55
name: "fluent",
66
platforms: [
7-
.macOS(.v10_14)
7+
.macOS(.v10_15)
88
],
99
products: [
1010
.library(name: "Fluent", targets: ["Fluent"]),
1111
],
1212
dependencies: [
13-
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.0.0-beta.2"),
14-
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-beta.2"),
13+
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.0.0-beta.5"),
14+
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-beta.4"),
1515
],
1616
targets: [
1717
.target(name: "Fluent", dependencies: ["FluentKit", "Vapor"]),

Tests/FluentTests/FluentOperatorTests.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class FluentOperatorTests: XCTestCase {
3232
private final class Planet: Model {
3333
static let schema = "planets"
3434

35-
@ID(key: "id")
35+
@ID(custom: .id)
3636
var id: Int?
3737

3838
@Field(key: "name")
@@ -44,15 +44,23 @@ private struct DummyDatabase: Database {
4444
fatalError()
4545
}
4646

47-
func execute(query: DatabaseQuery, onRow: @escaping (DatabaseRow) -> ()) -> EventLoopFuture<Void> {
47+
func execute(query: DatabaseQuery, onOutput: @escaping (DatabaseOutput) -> ()) -> EventLoopFuture<Void> {
4848
fatalError()
4949
}
5050

5151
func execute(schema: DatabaseSchema) -> EventLoopFuture<Void> {
5252
fatalError()
5353
}
5454

55+
func execute(enum: DatabaseEnum) -> EventLoopFuture<Void> {
56+
fatalError()
57+
}
58+
5559
func withConnection<T>(_ closure: @escaping (Database) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
5660
fatalError()
5761
}
62+
63+
func transaction<T>(_ closure: @escaping (Database) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
64+
fatalError()
65+
}
5866
}

Tests/FluentTests/FluentPaginationTests.swift

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,22 @@ final class FluentPaginationTests: XCTestCase {
1111
for i in 1...1_000 {
1212
rows.append(TestRow(data: ["id": i, "title": "Todo #\(i)"]))
1313
}
14-
15-
app.databases.use(TestDatabaseDriver { query in
14+
15+
app.databases.use(TestDatabaseConfiguration { query in
1616
XCTAssertEqual(query.schema, "todos")
1717
let result: [TestRow]
1818
if let limit = query.limits.first?.value, let offset = query.offsets.first?.value {
1919
result = [TestRow](rows[min(offset, rows.count - 1)..<min(offset + limit, rows.count)])
2020
} else {
2121
result = rows
2222
}
23-
24-
if query.fields.count == 1 {
25-
// aggregate
26-
return [
27-
TestRow(data: ["fluentAggregate": rows.count])
28-
]
29-
} else {
30-
return result
23+
24+
switch query.action {
25+
case .aggregate(_):
26+
return [TestRow(data: [.aggregate: rows.count])]
27+
default:
28+
return result
3129
}
32-
3330
}, as: .test)
3431

3532
app.get("todos") { req -> EventLoopFuture<Page<Todo>> in
@@ -90,7 +87,7 @@ private extension DatabaseQuery.Offset {
9087
private final class Todo: Model, Content {
9188
static let schema = "todos"
9289

93-
@ID(key: "id")
90+
@ID(custom: .id)
9491
var id: Int?
9592

9693
@Field(key: "title")

Tests/FluentTests/FluentRepositoryTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ final class FluentRepositoryTests: XCTestCase {
3737
let app = Application(.testing)
3838
defer { app.shutdown() }
3939

40-
app.databases.use(TestDatabaseDriver { query in
40+
app.databases.use(TestDatabaseConfiguration { query in
4141
XCTAssertEqual(query.schema, "posts")
4242
return [
4343
TestRow(data: ["id": 1, "content": "a"]),
@@ -102,7 +102,7 @@ private final class Post: Model, Content, Equatable {
102102

103103
static var schema: String { "posts" }
104104

105-
@ID(key: "id")
105+
@ID(custom: .id)
106106
var id: Int?
107107

108108
@Field(key: "content")

Tests/FluentTests/TestDatabase.swift

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,52 @@ struct TestDatabase: Database {
88
let driver: TestDatabaseDriver
99
let context: DatabaseContext
1010

11-
func execute(query: DatabaseQuery, onRow: @escaping (DatabaseRow) -> ()) -> EventLoopFuture<Void> {
12-
self.driver.handler(query).forEach(onRow)
11+
func execute(query: DatabaseQuery, onOutput: @escaping (DatabaseOutput) -> ()) -> EventLoopFuture<Void> {
12+
self.driver.handler(query).forEach(onOutput)
1313
return self.eventLoop.makeSucceededFuture(())
1414
}
1515

1616
func execute(schema: DatabaseSchema) -> EventLoopFuture<Void> {
1717
fatalError()
1818
}
19-
19+
20+
func execute(enum: DatabaseEnum) -> EventLoopFuture<Void> {
21+
fatalError()
22+
}
23+
2024
func withConnection<T>(_ closure: @escaping (Database) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
2125
closure(self)
2226
}
27+
28+
func transaction<T>(_ closure: @escaping (Database) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
29+
closure(self)
30+
}
2331
}
2432

25-
struct TestRow: DatabaseRow {
26-
var data: [String: Any]
33+
struct TestRow: DatabaseOutput {
34+
var data: [FieldKey: Any]
2735

2836
var description: String {
2937
self.data.description
3038
}
39+
40+
func schema(_ schema: String) -> DatabaseOutput {
41+
self
42+
}
3143

32-
func contains(field: String) -> Bool {
44+
func contains(_ field: FieldKey) -> Bool {
3345
self.data.keys.contains(field)
3446
}
3547

36-
func decode<T>(field: String, as type: T.Type, for database: Database) throws -> T where T : Decodable {
37-
return self.data[field] as! T
48+
func decode<T>(_ field: FieldKey, as type: T.Type) throws -> T where T : Decodable {
49+
self.data[field]! as! T
3850
}
3951
}
4052

4153
final class TestDatabaseDriver: DatabaseDriver {
42-
let handler: (DatabaseQuery) -> [DatabaseRow]
54+
let handler: (DatabaseQuery) -> [DatabaseOutput]
4355

44-
init(_ handler: @escaping (DatabaseQuery) -> [DatabaseRow]) {
56+
init(_ handler: @escaping (DatabaseQuery) -> [DatabaseOutput]) {
4557
self.handler = handler
4658
}
4759

@@ -53,3 +65,13 @@ final class TestDatabaseDriver: DatabaseDriver {
5365
// nothing
5466
}
5567
}
68+
69+
struct TestDatabaseConfiguration: DatabaseConfiguration {
70+
let handler: (DatabaseQuery) -> [DatabaseOutput]
71+
72+
var middleware: [AnyModelMiddleware] = []
73+
74+
func makeDriver(for databases: Databases) -> DatabaseDriver {
75+
TestDatabaseDriver(handler)
76+
}
77+
}

0 commit comments

Comments
 (0)