Skip to content

Change to keyIsEmpty function. It will now comply with optional variables. #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
]
)
44 changes: 31 additions & 13 deletions Sources/StORM/StORM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,37 @@ 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
}

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: Unexpected type for PRIMARY KEY in function: \(#function). TYPE: \(type)")
return false
}

// if val is Int {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest removing unused code unless it will be needed in the near future.

// if val as! Int == 0 {
// return true
// } else {
// return false
// }
// } else {
// if (val as! String).isEmpty {
// return true
// } else {
// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor formatting for consistency
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor formatting for consistency
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