SwifterTestsSQLite.swift 2.0 KB

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