Implement database access (with migrations)

This commit is contained in:
2019-01-26 22:58:53 -06:00
parent e19339d808
commit 7cab0a39e5
67 changed files with 1826 additions and 136 deletions

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>

View File

@@ -0,0 +1,44 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import XCTest
@testable import uhabits
class IosFilesTest: XCTestCase {
func testResourceFiles() {
let fileOpener = IosFileOpener()
let file = fileOpener.openResourceFile(filename: "migrations/010.sql")
let lines = file.readLines()
XCTAssertEqual(lines[0], "delete from Score")
}
func testUserFiles() throws {
let fm = FileManager.default
let root = try fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).path
let path = "\(root)/test.txt"
fm.createFile(atPath: path, contents: "Hello world\nThis is line 2".data(using: .utf8), attributes: nil)
let fileOpener = IosFileOpener()
let file = fileOpener.openUserFile(filename: "test.txt")
XCTAssertTrue(file.exists())
file.delete()
XCTAssertFalse(file.exists())
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import XCTest
@testable import uhabits
class IosDatabaseTest: XCTestCase {
func testUsage() {
let databaseOpener = IosDatabaseOpener()
let fileOpener = IosFileOpener()
let dbFile = fileOpener.openUserFile(filename: "test.sqlite3")
if dbFile.exists() {
dbFile.delete()
}
let db = databaseOpener.open(file: dbFile)
var stmt = db.prepareStatement(sql: "drop table if exists demo")
stmt.step()
stmt.finalize()
stmt = db.prepareStatement(sql: "begin")
stmt.step()
stmt.finalize()
stmt = db.prepareStatement(sql: "create table if not exists demo(key int, value text)")
stmt.step()
stmt.finalize()
stmt = db.prepareStatement(sql: "insert into demo(key, value) values (?1, ?2)")
stmt.bindInt(index: 1, value: 42)
stmt.bindText(index: 2, value: "Hello World")
stmt.step()
stmt.finalize()
stmt = db.prepareStatement(sql: "select * from demo where key > ?1")
stmt.bindInt(index: 1, value: 10)
var result = stmt.step()
XCTAssertEqual(result, StepResult.row)
XCTAssertEqual(stmt.getInt(index: 0), 42)
XCTAssertEqual(stmt.getText(index: 1), "Hello World")
result = stmt.step()
XCTAssertEqual(result, StepResult.done)
stmt.finalize()
stmt = db.prepareStatement(sql: "drop table demo")
stmt.step()
stmt.finalize()
stmt = db.prepareStatement(sql: "commit")
stmt.step()
stmt.finalize()
db.close()
dbFile.delete()
}
}