From a983f79149e556c9ae3255c8983d980ad840c8f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Cante=CC=81rot?= Date: Thu, 29 Sep 2016 23:00:37 +0200 Subject: [PATCH] added unicode + basic tests --- Tests/SQLiteTests/SQLite3Tests.swift | 69 ++++++++++++++++++++++++++-- Tests/SQLiteTests/Utilities.swift | 15 ++++++ 2 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 Tests/SQLiteTests/Utilities.swift diff --git a/Tests/SQLiteTests/SQLite3Tests.swift b/Tests/SQLiteTests/SQLite3Tests.swift index f7d1798..b664739 100644 --- a/Tests/SQLiteTests/SQLite3Tests.swift +++ b/Tests/SQLiteTests/SQLite3Tests.swift @@ -2,11 +2,70 @@ import XCTest @testable import SQLite class SQLite3Tests: XCTestCase { - static let allTests = [ - ("testReality", testReality) - ] + static let allTests = [""] - func testReality() { - XCTAssert(2 + 2 == 4, "Something is seriously wrong.") + var database:SQLite! + + override func setUp() { + self.database = SQLite.makeTestConnection() } + + func testTables() { + do { + try _ = database.execute("DROP TABLE IF EXISTS foo") + try _ = database.execute("CREATE TABLE foo (bar INT(4), baz VARCHAR(16))") + try _ = database.execute("INSERT INTO foo VALUES (42, 'Life')") + try _ = database.execute("INSERT INTO foo VALUES (1337, 'Elite')") + try _ = database.execute("INSERT INTO foo VALUES (9, NULL)") + + if let resultBar = try database.execute("SELECT * FROM foo WHERE bar = 42").first { + XCTAssertEqual(resultBar.data["bar"], "42") + XCTAssertEqual(resultBar.data["baz"], "Life") + } else { + XCTFail("Could not get bar result") + } + + + if let resultBaz = try database.execute("SELECT * FROM foo where baz = 'Elite'").first { + XCTAssertEqual(resultBaz.data["bar"], "1337") + XCTAssertEqual(resultBaz.data["baz"], "Elite") + } else { + XCTFail("Could not get baz result") + } + + if let resultBaz = try database.execute("SELECT * FROM foo where bar = 9").first { + XCTAssertEqual(resultBaz.data["bar"], "9") + XCTAssertEqual(resultBaz.data["baz"], nil) + } else { + XCTFail("Could not get null result") + } + } catch { + XCTFail("Testing tables failed: \(error)") + } + } + + func testUnicodeStrings() { + + do { + + /** + This string includes characters from most Unicode categories + such as Latin, Latin-Extended-A/B, Cyrrilic, Greek etc. + */ + let unicode = "®¿ÐØ×ĞƋƢǂNJǕǮȐȘȢȱȵẀˍΔῴЖ♆" + try _ = database.execute("DROP TABLE IF EXISTS `foo`") + try _ = database.execute("CREATE TABLE `foo` (bar TEXT)") + try _ = database.execute("INSERT INTO `foo` VALUES(?)") { statement in + try statement.bind(unicode) + } + + if let results = try database.execute("SELECT * FROM `foo`").first { + XCTAssertEqual(results.data["bar"], unicode) + } + } catch { + XCTFail(error.localizedDescription) + } + + } + } diff --git a/Tests/SQLiteTests/Utilities.swift b/Tests/SQLiteTests/Utilities.swift new file mode 100644 index 0000000..7f5e5b2 --- /dev/null +++ b/Tests/SQLiteTests/Utilities.swift @@ -0,0 +1,15 @@ +import XCTest +@testable import SQLite + +extension SQLite { + static func makeTestConnection() -> SQLite? { + do { + let sqlite = try SQLite(path:"test_database.sqlite") + return sqlite + + } catch { + XCTFail() + } + return nil + } +}