forked from vapor-community/sqlite-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a703f07
commit 65fdfb0
Showing
6 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import XCTest | ||
@testable import FluentSQLite | ||
import Fluent | ||
|
||
class JoinTests: XCTestCase { | ||
static let allTests = [ | ||
("testBasic", testBasic) | ||
] | ||
|
||
var database: Fluent.Database! | ||
var driver: SQLiteDriver! | ||
|
||
override func setUp() { | ||
driver = SQLiteDriver.makeTestConnection() | ||
database = Database(driver) | ||
} | ||
|
||
func testBasic() throws { | ||
try Atom.revert(database) | ||
try Compound.revert(database) | ||
try Pivot<Atom, Compound>.revert(database) | ||
|
||
try Atom.prepare(database) | ||
try Compound.prepare(database) | ||
try Pivot<Atom, Compound>.prepare(database) | ||
|
||
Atom.database = database | ||
Compound.database = database | ||
Pivot<Atom, Compound>.database = database | ||
|
||
var hydrogen = Atom(name: "Hydrogen", protons: 1) | ||
try hydrogen.save() | ||
|
||
var water = Compound(name: "Water") | ||
try water.save() | ||
var hydrogenWater = Pivot<Atom, Compound>(hydrogen, water) | ||
try hydrogenWater.save() | ||
|
||
var sugar = Compound(name: "Sugar") | ||
try sugar.save() | ||
var hydrogenSugar = Pivot<Atom, Compound>(hydrogen, sugar) | ||
try hydrogenSugar.save() | ||
|
||
|
||
let compounds = try hydrogen.compounds().all() | ||
XCTAssertEqual(compounds.count, 2) | ||
XCTAssertEqual(compounds.first?.name.string, water.name.string) | ||
XCTAssertEqual(compounds.last?.id?.int, sugar.id?.int) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import XCTest | ||
@testable import FluentSQLite | ||
@testable import Fluent | ||
|
||
|
||
class SQLite3Tests: XCTestCase { | ||
static var allTests: [(String, (SQLite3Tests) -> () throws -> Void)] { | ||
return [ | ||
("testSaveAndFind", testSaveAndFind) | ||
] | ||
} | ||
|
||
var driver:SQLiteDriver! | ||
var database:Fluent.Database! | ||
|
||
|
||
override func setUp() { | ||
driver = SQLiteDriver.makeTestConnection() | ||
database = Database(driver) | ||
} | ||
|
||
|
||
func testSaveAndFind() { | ||
_ = try! driver.raw("DROP TABLE IF EXISTS `posts`") | ||
do { | ||
_ = try driver.raw("CREATE TABLE IF NOT EXISTS `posts` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `title` CHAR(255), `text` CHAR(255))") | ||
} catch { | ||
XCTFail("Could not create table \(error)") | ||
} | ||
// try! database.create("posts") { creator in | ||
// creator.id() | ||
// creator.string("title") | ||
// creator.string("text") | ||
// } | ||
|
||
var post = Post(id: nil, title: "Vapor & Tests", text: "Lorem ipsum etc...") | ||
Post.database = database | ||
|
||
do { | ||
try post.save() | ||
print("Just saved") | ||
} catch { | ||
XCTFail("Could not save : \(error)") | ||
} | ||
|
||
do { | ||
let fetched = try Post.find(1) | ||
XCTAssertEqual(fetched?.title, post.title) | ||
XCTAssertEqual(fetched?.text, post.text) | ||
} catch { | ||
XCTFail("Could not fetch user : \(error)") | ||
} | ||
|
||
do { | ||
let post = try Post.find(2) | ||
XCTAssertNil(post) | ||
} catch { | ||
XCTFail("Could not find post: \(error)") | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Fluent | ||
|
||
final class Atom: Entity { | ||
var id: Node? | ||
var name: String | ||
var protons: Int | ||
|
||
init(name: String, protons: Int) { | ||
self.name = name | ||
self.protons = protons | ||
} | ||
|
||
init(node: Node, in context: Context) throws { | ||
id = try node.extract("id") | ||
name = try node.extract("name") | ||
protons = try node.extract("protons") | ||
} | ||
|
||
func makeNode() throws -> Node { | ||
return try Node(node: [ | ||
"id": id, | ||
"name": name, | ||
"protons": protons | ||
]) | ||
} | ||
|
||
func compounds() throws -> Siblings<Compound> { | ||
return try siblings() | ||
} | ||
|
||
static func prepare(_ database: Fluent.Database) throws { | ||
try database.create(entity) { builder in | ||
builder.id() | ||
builder.string("name") | ||
builder.int("protons") | ||
} | ||
} | ||
static func revert(_ database: Fluent.Database) throws { | ||
try database.delete(entity) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import Fluent | ||
|
||
final class Compound: Entity { | ||
var id: Node? | ||
var name: String | ||
|
||
init(name: String) { | ||
self.name = name | ||
} | ||
|
||
init(node: Node, in context: Context) throws { | ||
id = try node.extract("id") | ||
name = try node.extract("name") | ||
} | ||
|
||
func makeNode() throws -> Node { | ||
return try Node(node: [ | ||
"id": id, | ||
"name": name | ||
]) | ||
} | ||
|
||
static func prepare(_ database: Fluent.Database) throws { | ||
try database.create(entity) { builder in | ||
builder.id() | ||
builder.string("name") | ||
} | ||
} | ||
static func revert(_ database: Fluent.Database) throws { | ||
try database.delete(entity) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Fluent | ||
|
||
final class Post: Entity { | ||
var id: Fluent.Node? | ||
var title: String | ||
var text: String | ||
|
||
init(id: Node?, title: String, text: String) { | ||
self.id = id | ||
self.title = title | ||
self.text = text | ||
} | ||
|
||
func makeNode() throws -> Node { | ||
return try Node(node: [ | ||
"id": id, | ||
"title": title, | ||
"text": text | ||
]) | ||
} | ||
|
||
init(node: Node, in context: Context) throws { | ||
id = try node.extract("id") | ||
title = try node.extract("title") | ||
text = try node.extract("text") | ||
} | ||
|
||
static func prepare(_ database: Fluent.Database) throws { | ||
try database.create(entity) { builder in | ||
builder.id() | ||
builder.string("title") | ||
builder.string("text") | ||
} | ||
} | ||
static func revert(_ database: Fluent.Database) throws { | ||
try database.delete(entity) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Tests/FluentSQLiteTests/Utilities/SQLiteDriver+Tests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
import FluentSQLite | ||
import XCTest | ||
import Foundation | ||
|
||
extension SQLiteDriver { | ||
static func makeTestConnection() -> SQLiteDriver { | ||
do { | ||
let driver = try SQLiteDriver(path:"database.db") | ||
return driver | ||
} catch { | ||
XCTFail("Could not create driver") | ||
fatalError("Could not create driver : \(error)") | ||
} | ||
} | ||
} |