SwifterTestsSQLite.swift 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // SwifterTests.swift
  3. // SwifterTests
  4. //
  5. // Copyright © 2016 Damian Kołakowski. All rights reserved.
  6. //
  7. import XCTest
  8. class SwifterTestsSQLite: XCTestCase {
  9. func testSQLite() {
  10. print(try? File.currentWorkingDirectory())
  11. guard let databsePath = try? File.currentWorkingDirectory() + "/" + "test_\(Int64(Date().timeIntervalSince1970*1000)).db" else {
  12. XCTAssert(false, "Could not find a path for a database file.")
  13. return
  14. }
  15. do {
  16. let database = try SQLite.open(databsePath)
  17. XCTAssert(true, "Opening the database should not throw any exceptions.")
  18. database.close()
  19. } catch {
  20. XCTAssert(false, "Opening the database should not throw any exceptions.")
  21. }
  22. do {
  23. let database = try SQLite.open(databsePath)
  24. try database.exec("CREATE TABLE swifter_tests (title TEXT, description TEXT);")
  25. try database.exec("INSERT INTO swifter_tests VALUES (\"Test1\", \"Test1 Description\");")
  26. try database.exec("INSERT INTO swifter_tests VALUES (\"Test2\", \"Test2 Description\");")
  27. database.close()
  28. } catch {
  29. XCTAssert(false, "Database manipulation should not throw any exceptions: \(error).")
  30. }
  31. do {
  32. let database = try SQLite.open(databsePath)
  33. var counter = 0
  34. for _ in try database.enumerate("SELECT * FROM swifter_tests;") {
  35. counter = counter + 1
  36. }
  37. XCTAssertEqual(counter, 2, "Database should have two rows.")
  38. counter = 0
  39. try database.exec("SELECT * FROM swifter_tests;") { content in
  40. counter = counter + 1
  41. }
  42. XCTAssertEqual(counter, 2, "Database should have two rows.")
  43. database.close()
  44. } catch {
  45. XCTAssert(false, "Database manipulation should not throw any exceptions.")
  46. }
  47. }
  48. }