diff --git a/Package.swift b/Package.swift index a4fc0a1..aa01953 100644 --- a/Package.swift +++ b/Package.swift @@ -1,18 +1,10 @@ -// -// Package.swift -// StORM -// -// Created by Jonathan Guthrie on 2016-09-23. -// Copyright (C) 2016 Jonathan Guthrie. -// - +// Generated automatically by Perfect Assistant Application +// Date: 2017-08-13 18:42:55 +0000 import PackageDescription - let package = Package( name: "StORM", targets: [], dependencies: [ - .Package(url: "https://github.com/PerfectlySoft/PerfectLib.git", majorVersion: 2) - ], - exclude: [] + .Package(url: "https://github.com/PerfectlySoft/PerfectLib.git", majorVersion: 2), + ] ) diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index ab5314e..98297d4 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -103,19 +103,25 @@ open class StORM { /// Returns a boolean that is true if the first property in the class contains a value. public func keyIsEmpty() -> Bool { let (_, val) = firstAsKey() - if val is Int { - if val as! Int == 0 { - return true - } else { - return false - } - } else { - if (val as! String).isEmpty { - return true - } else { - return false - } - } + + // Grab the type of value: + let type = type(of: val) + // Check if we are nil, we would then of course have an empty primary key. + guard String(describing: val) != "nil" else { + return true + } + + // For now we will be expecting String & Integer key types: + switch type { + case is Int.Type, is Int?.Type: + return (val as! Int == 0) + case is String.Type, is String?.Type: + return (val as! String).isEmpty + default: + print("[StORM] WARNING: [\(#function)] Unexpected \(type) for PRIMARY KEY.") + return false + } + } /// The create method is designed to be overridden diff --git a/Tests/PerfectCRUDTests/StORMTests.swift b/Tests/PerfectCRUDTests/StORMTests.swift index b3151d7..3d015bc 100644 --- a/Tests/PerfectCRUDTests/StORMTests.swift +++ b/Tests/PerfectCRUDTests/StORMTests.swift @@ -10,6 +10,113 @@ class StORMTests: XCTestCase { override func setUp() { super.setUp() } + + func testKeyTypeIntegerNil() { + + let val : Int? = nil + + // Grab the type of value: + let type = type(of: val) + // Check if we are nil, we would then of course have an empty primary key. + + switch type { + case is Int.Type, is Int?.Type: + guard String(describing: val) != "nil" else { + XCTAssert(true) + return + } + XCTFail("Value was supposed to be nil.") + // return (val as! Int == 0) + case is String.Type, is String?.Type: + // return (val as! String).isEmpty + XCTFail("Type was supposed to be an integer.") + default: + XCTFail("Type should either be a String or Integer.") + print("[StORM] WARNING: Unexpected type for PRIMARY KEY in function: \(#function). TYPE: \(type)") + // return false + } + + } + + func testKeyTypeOptionalInteger() { + + + let val : Int? = 1 + + let anyVal : Any = val + + // Grab the type of value: + let type = type(of: val) + // Check if we are nil, we would then of course have an empty primary key. + guard String(describing: anyVal) != "nil" else { + XCTFail("Value was not supposed to be nil.") + return + } + + switch type { + case is Int.Type, is Int?.Type: + XCTAssert((anyVal as? Int) != nil, "Failed to cast optional Integer as Any to Int") + // return (val as! Int == 0) + case is String.Type, is String?.Type: + // return (val as! String).isEmpty + XCTFail("Type was supposed to be an integer.") + default: + XCTFail("Type should either be a String or Integer.") + print("[StORM] WARNING: Unexpected type for PRIMARY KEY in function: \(#function). TYPE: \(type)") + // return false + } + + } + + func testKeyTypeStringNil() { + + let val : String? = nil + + // Grab the type of value: + let type = type(of: val) + + switch type { + case is Int.Type, is Int?.Type: + XCTFail("Type was supposed to be a string.") + case is String.Type, is String?.Type: + guard String(describing: val) != "nil" else { + XCTAssert(true) + return + } + XCTFail("Value was supposed to be nil.") + default: + XCTFail("Type should either be a String or Integer.") + print("[StORM] WARNING: Unexpected type for PRIMARY KEY in function: \(#function). TYPE: \(type)") + } + + } + + func testKeyTypeOptionalString() { + + + let val : String? = "" + + let anyVal : Any = val + + // Grab the type of value: + let type = type(of: val) + // Check if we are nil, we would then of course have an empty primary key. + guard String(describing: anyVal) != "nil" else { + XCTFail("Value was not supposed to be nil.") + return + } + + switch type { + case is Int.Type, is Int?.Type: + XCTFail("Type was supposed to be a string.") + case is String.Type, is String?.Type: + XCTAssert((anyVal as? String) != nil, "Failed to cast optional string as Any to String") + default: + XCTFail("Type should either be a String or Integer.") + print("[StORM] WARNING: Unexpected type for PRIMARY KEY in function: \(#function). TYPE: \(type)") + } + + } static var allTests : [(String, (StORMTests) -> () throws -> Void)] { return [