Skip to content

Commit c1fe8ac

Browse files
authored
Merge pull request #2 from ryancoyne/master
Change to keyIsEmpty function. It will now comply with optional variables.
2 parents 6c071fb + 20319cf commit c1fe8ac

File tree

3 files changed

+130
-25
lines changed

3 files changed

+130
-25
lines changed

Package.swift

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
//
2-
// Package.swift
3-
// StORM
4-
//
5-
// Created by Jonathan Guthrie on 2016-09-23.
6-
// Copyright (C) 2016 Jonathan Guthrie.
7-
//
8-
1+
// Generated automatically by Perfect Assistant Application
2+
// Date: 2017-08-13 18:42:55 +0000
93
import PackageDescription
10-
114
let package = Package(
125
name: "StORM",
136
targets: [],
147
dependencies: [
15-
.Package(url: "https://github.com/PerfectlySoft/PerfectLib.git", majorVersion: 2)
16-
],
17-
exclude: []
8+
.Package(url: "https://github.com/PerfectlySoft/PerfectLib.git", majorVersion: 2),
9+
]
1810
)

Sources/StORM/StORM.swift

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,25 @@ open class StORM {
103103
/// Returns a boolean that is true if the first property in the class contains a value.
104104
public func keyIsEmpty() -> Bool {
105105
let (_, val) = firstAsKey()
106-
if val is Int {
107-
if val as! Int == 0 {
108-
return true
109-
} else {
110-
return false
111-
}
112-
} else {
113-
if (val as! String).isEmpty {
114-
return true
115-
} else {
116-
return false
117-
}
118-
}
106+
107+
// Grab the type of value:
108+
let type = type(of: val)
109+
// Check if we are nil, we would then of course have an empty primary key.
110+
guard String(describing: val) != "nil" else {
111+
return true
112+
}
113+
114+
// For now we will be expecting String & Integer key types:
115+
switch type {
116+
case is Int.Type, is Int?.Type:
117+
return (val as! Int == 0)
118+
case is String.Type, is String?.Type:
119+
return (val as! String).isEmpty
120+
default:
121+
print("[StORM] WARNING: [\(#function)] Unexpected \(type) for PRIMARY KEY.")
122+
return false
123+
}
124+
119125
}
120126

121127
/// The create method is designed to be overridden

Tests/PerfectCRUDTests/StORMTests.swift

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,113 @@ class StORMTests: XCTestCase {
1010
override func setUp() {
1111
super.setUp()
1212
}
13+
14+
func testKeyTypeIntegerNil() {
15+
16+
let val : Int? = nil
17+
18+
// Grab the type of value:
19+
let type = type(of: val)
20+
// Check if we are nil, we would then of course have an empty primary key.
21+
22+
switch type {
23+
case is Int.Type, is Int?.Type:
24+
guard String(describing: val) != "nil" else {
25+
XCTAssert(true)
26+
return
27+
}
28+
XCTFail("Value was supposed to be nil.")
29+
// return (val as! Int == 0)
30+
case is String.Type, is String?.Type:
31+
// return (val as! String).isEmpty
32+
XCTFail("Type was supposed to be an integer.")
33+
default:
34+
XCTFail("Type should either be a String or Integer.")
35+
print("[StORM] WARNING: Unexpected type for PRIMARY KEY in function: \(#function). TYPE: \(type)")
36+
// return false
37+
}
38+
39+
}
40+
41+
func testKeyTypeOptionalInteger() {
42+
43+
44+
let val : Int? = 1
45+
46+
let anyVal : Any = val
47+
48+
// Grab the type of value:
49+
let type = type(of: val)
50+
// Check if we are nil, we would then of course have an empty primary key.
51+
guard String(describing: anyVal) != "nil" else {
52+
XCTFail("Value was not supposed to be nil.")
53+
return
54+
}
55+
56+
switch type {
57+
case is Int.Type, is Int?.Type:
58+
XCTAssert((anyVal as? Int) != nil, "Failed to cast optional Integer as Any to Int")
59+
// return (val as! Int == 0)
60+
case is String.Type, is String?.Type:
61+
// return (val as! String).isEmpty
62+
XCTFail("Type was supposed to be an integer.")
63+
default:
64+
XCTFail("Type should either be a String or Integer.")
65+
print("[StORM] WARNING: Unexpected type for PRIMARY KEY in function: \(#function). TYPE: \(type)")
66+
// return false
67+
}
68+
69+
}
70+
71+
func testKeyTypeStringNil() {
72+
73+
let val : String? = nil
74+
75+
// Grab the type of value:
76+
let type = type(of: val)
77+
78+
switch type {
79+
case is Int.Type, is Int?.Type:
80+
XCTFail("Type was supposed to be a string.")
81+
case is String.Type, is String?.Type:
82+
guard String(describing: val) != "nil" else {
83+
XCTAssert(true)
84+
return
85+
}
86+
XCTFail("Value was supposed to be nil.")
87+
default:
88+
XCTFail("Type should either be a String or Integer.")
89+
print("[StORM] WARNING: Unexpected type for PRIMARY KEY in function: \(#function). TYPE: \(type)")
90+
}
91+
92+
}
93+
94+
func testKeyTypeOptionalString() {
95+
96+
97+
let val : String? = ""
98+
99+
let anyVal : Any = val
100+
101+
// Grab the type of value:
102+
let type = type(of: val)
103+
// Check if we are nil, we would then of course have an empty primary key.
104+
guard String(describing: anyVal) != "nil" else {
105+
XCTFail("Value was not supposed to be nil.")
106+
return
107+
}
108+
109+
switch type {
110+
case is Int.Type, is Int?.Type:
111+
XCTFail("Type was supposed to be a string.")
112+
case is String.Type, is String?.Type:
113+
XCTAssert((anyVal as? String) != nil, "Failed to cast optional string as Any to String")
114+
default:
115+
XCTFail("Type should either be a String or Integer.")
116+
print("[StORM] WARNING: Unexpected type for PRIMARY KEY in function: \(#function). TYPE: \(type)")
117+
}
118+
119+
}
13120

14121
static var allTests : [(String, (StORMTests) -> () throws -> Void)] {
15122
return [

0 commit comments

Comments
 (0)