Skip to content

Commit

Permalink
Merge pull request #2 from ryancoyne/master
Browse files Browse the repository at this point in the history
Change to keyIsEmpty function.  It will now comply with optional variables.
  • Loading branch information
iamcam authored Aug 30, 2017
2 parents 6c071fb + 20319cf commit c1fe8ac
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 25 deletions.
16 changes: 4 additions & 12 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -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),
]
)
32 changes: 19 additions & 13 deletions Sources/StORM/StORM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
107 changes: 107 additions & 0 deletions Tests/PerfectCRUDTests/StORMTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down

0 comments on commit c1fe8ac

Please sign in to comment.