Skip to content

Commit

Permalink
Merge pull request #662 from vapor/beta-5-updates
Browse files Browse the repository at this point in the history
Fluent Beta 5
  • Loading branch information
gwynne committed Feb 26, 2020
2 parents 74c64be + 6709156 commit ba878e2
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ on:
jobs:
xenial:
container:
image: vapor/swift:5.1-xenial
image: vapor/swift:5.2-xenial
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- run: swift test --enable-test-discovery --sanitize=thread
bionic:
container:
image: vapor/swift:5.1-bionic
image: vapor/swift:5.2-bionic
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
Expand Down
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import PackageDescription
let package = Package(
name: "fluent",
platforms: [
.macOS(.v10_14)
.macOS(.v10_15)
],
products: [
.library(name: "Fluent", targets: ["Fluent"]),
],
dependencies: [
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.0.0-beta.2"),
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-beta.2"),
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.0.0-beta.5"),
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-beta.4"),
],
targets: [
.target(name: "Fluent", dependencies: ["FluentKit", "Vapor"]),
Expand Down
12 changes: 10 additions & 2 deletions Tests/FluentTests/FluentOperatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class FluentOperatorTests: XCTestCase {
private final class Planet: Model {
static let schema = "planets"

@ID(key: "id")
@ID(custom: .id)
var id: Int?

@Field(key: "name")
Expand All @@ -44,15 +44,23 @@ private struct DummyDatabase: Database {
fatalError()
}

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

func execute(schema: DatabaseSchema) -> EventLoopFuture<Void> {
fatalError()
}

func execute(enum: DatabaseEnum) -> EventLoopFuture<Void> {
fatalError()
}

func withConnection<T>(_ closure: @escaping (Database) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
fatalError()
}

func transaction<T>(_ closure: @escaping (Database) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
fatalError()
}
}
21 changes: 9 additions & 12 deletions Tests/FluentTests/FluentPaginationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,22 @@ final class FluentPaginationTests: XCTestCase {
for i in 1...1_000 {
rows.append(TestRow(data: ["id": i, "title": "Todo #\(i)"]))
}

app.databases.use(TestDatabaseDriver { query in
app.databases.use(TestDatabaseConfiguration { query in
XCTAssertEqual(query.schema, "todos")
let result: [TestRow]
if let limit = query.limits.first?.value, let offset = query.offsets.first?.value {
result = [TestRow](rows[min(offset, rows.count - 1)..<min(offset + limit, rows.count)])
} else {
result = rows
}

if query.fields.count == 1 {
// aggregate
return [
TestRow(data: ["fluentAggregate": rows.count])
]
} else {
return result

switch query.action {
case .aggregate(_):
return [TestRow(data: [.aggregate: rows.count])]
default:
return result
}

}, as: .test)

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

@ID(key: "id")
@ID(custom: .id)
var id: Int?

@Field(key: "title")
Expand Down
4 changes: 2 additions & 2 deletions Tests/FluentTests/FluentRepositoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class FluentRepositoryTests: XCTestCase {
let app = Application(.testing)
defer { app.shutdown() }

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

static var schema: String { "posts" }

@ID(key: "id")
@ID(custom: .id)
var id: Int?

@Field(key: "content")
Expand Down
42 changes: 32 additions & 10 deletions Tests/FluentTests/TestDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,52 @@ struct TestDatabase: Database {
let driver: TestDatabaseDriver
let context: DatabaseContext

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

func execute(schema: DatabaseSchema) -> EventLoopFuture<Void> {
fatalError()
}


func execute(enum: DatabaseEnum) -> EventLoopFuture<Void> {
fatalError()
}

func withConnection<T>(_ closure: @escaping (Database) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
closure(self)
}

func transaction<T>(_ closure: @escaping (Database) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
closure(self)
}
}

struct TestRow: DatabaseRow {
var data: [String: Any]
struct TestRow: DatabaseOutput {
var data: [FieldKey: Any]

var description: String {
self.data.description
}

func schema(_ schema: String) -> DatabaseOutput {
self
}

func contains(field: String) -> Bool {
func contains(_ field: FieldKey) -> Bool {
self.data.keys.contains(field)
}

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

final class TestDatabaseDriver: DatabaseDriver {
let handler: (DatabaseQuery) -> [DatabaseRow]
let handler: (DatabaseQuery) -> [DatabaseOutput]

init(_ handler: @escaping (DatabaseQuery) -> [DatabaseRow]) {
init(_ handler: @escaping (DatabaseQuery) -> [DatabaseOutput]) {
self.handler = handler
}

Expand All @@ -53,3 +65,13 @@ final class TestDatabaseDriver: DatabaseDriver {
// nothing
}
}

struct TestDatabaseConfiguration: DatabaseConfiguration {
let handler: (DatabaseQuery) -> [DatabaseOutput]

var middleware: [AnyModelMiddleware] = []

func makeDriver(for databases: Databases) -> DatabaseDriver {
TestDatabaseDriver(handler)
}
}

0 comments on commit ba878e2

Please sign in to comment.