Skip to content

Commit 8866dd8

Browse files
committed
refactored Storage.getAll tests
1 parent 45d0653 commit 8866dd8

14 files changed

+349
-174
lines changed

Sources/SQLiteORM/AnyColumn.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,16 @@ public class AnyColumn: NSObject {
7575
//..
7676
}
7777
}
78+
79+
extension AnyColumn: Serializable {
80+
func serialize() -> String {
81+
let typeString = self.sqliteTypeName
82+
var res = "\(self.name) \(typeString)"
83+
for constraint in self.constraints {
84+
let constraintString = constraint.serialize()
85+
res += " "
86+
res += constraintString
87+
}
88+
return res
89+
}
90+
}

Sources/SQLiteORM/ColumnConstraint.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,45 @@ public enum ColumnConstraint {
66
case unique(conflictClause: ConflictClause?)
77
}
88

9+
extension ColumnConstraint: Serializable {
10+
func serialize() -> String {
11+
switch self {
12+
case .primaryKey(let orderMaybe, let conflictClauseMaybe, let autoincrement):
13+
var res = "PRIMARY KEY"
14+
if let order = orderMaybe {
15+
let orderString = order.serialize()
16+
res += " "
17+
res += orderString
18+
}
19+
if let conflictClause = conflictClauseMaybe {
20+
let conflictClauseString = conflictClause.serialize()
21+
res += " "
22+
res += conflictClauseString
23+
}
24+
if autoincrement {
25+
res += " AUTOINCREMENT"
26+
}
27+
return res
28+
case .notNull(let conflictClauseMaybe):
29+
var res = "NOT NULL"
30+
if let conflictClause = conflictClauseMaybe {
31+
let conflictClauseString = conflictClause.serialize()
32+
res += " "
33+
res += conflictClauseString
34+
}
35+
return res
36+
case .unique(let conflictClauseMaybe):
37+
var res = "UNIQUE"
38+
if let conflictClause = conflictClauseMaybe {
39+
let conflictClauseString = conflictClause.serialize()
40+
res += " "
41+
res += conflictClauseString
42+
}
43+
return res
44+
}
45+
}
46+
}
47+
948
extension ColumnConstraint: Equatable {
1049
public static func == (lhs: Self, rhs: Self) -> Bool {
1150
switch (lhs, rhs) {

Sources/SQLiteORM/ConflictClause.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,17 @@ public enum ConflictClause {
77
case ignore
88
case replace
99
}
10+
11+
extension ConflictClause: Serializable {
12+
func serialize() -> String {
13+
var res = "ON CONFLICT "
14+
switch self {
15+
case .rollback: res += "ROLLBACK"
16+
case .abort: res += "ABORT"
17+
case .fail: res += "FAIL"
18+
case .ignore: res += "IGNORE"
19+
case .replace: res += "REPLACE"
20+
}
21+
return res
22+
}
23+
}

Sources/SQLiteORM/Expression.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@ import Foundation
33
protocol Expression: Any {
44

55
}
6+
7+
extension Int: Expression {
8+
9+
}
10+
11+
extension Bool: Expression {
12+
13+
}

Sources/SQLiteORM/Order.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,12 @@ public enum Order {
44
case asc
55
case desc
66
}
7+
8+
extension Order: Serializable {
9+
func serialize() -> String {
10+
switch self {
11+
case .asc: return "ASC"
12+
case .desc: return "DESC"
13+
}
14+
}
15+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import Foundation
2+
3+
public protocol SelectConstraintBuilder: AnyObject {
4+
5+
}
6+
7+
class WhereBuilder: SelectConstraintBuilder {
8+
var expression: Expression
9+
10+
init(expression: Expression) {
11+
self.expression = expression
12+
}
13+
}
14+
15+
func `where`(expression: Expression) -> WhereBuilder {
16+
return WhereBuilder(expression: expression)
17+
}
18+
19+
enum BinaryOperatorType {
20+
// case add
21+
// case sub
22+
// case mul
23+
// case div
24+
case equal
25+
case notEqual
26+
case lesserThan
27+
case lesserOrEqual
28+
case greaterThan
29+
case greaterOrEqual
30+
}
31+
32+
class BinaryOperator: Expression {
33+
var lhs: Expression
34+
var rhs: Expression
35+
var operatorType: BinaryOperatorType
36+
37+
init(lhs: Expression, rhs: Expression, operatorType: BinaryOperatorType) {
38+
self.lhs = lhs
39+
self.rhs = rhs
40+
self.operatorType = operatorType
41+
}
42+
}
43+
44+
func isEqual(lhs: Expression, rhs: Expression) -> BinaryOperator {
45+
return BinaryOperator(lhs: lhs, rhs: rhs, operatorType: .equal)
46+
}
47+
48+
func isNotEqual(lhs: Expression, rhs: Expression) -> BinaryOperator {
49+
return BinaryOperator(lhs: lhs, rhs: rhs, operatorType: .notEqual)
50+
}
51+
52+
func lesserThan(lhs: Expression, rhs: Expression) -> BinaryOperator {
53+
return BinaryOperator(lhs: lhs, rhs: rhs, operatorType: .lesserThan)
54+
}
55+
56+
func lesserOrEqual(lhs: Expression, rhs: Expression) -> BinaryOperator {
57+
return BinaryOperator(lhs: lhs, rhs: rhs, operatorType: .lesserOrEqual)
58+
}
59+
60+
func greaterThan(lhs: Expression, rhs: Expression) -> BinaryOperator {
61+
return BinaryOperator(lhs: lhs, rhs: rhs, operatorType: .greaterThan)
62+
}
63+
64+
func greaterOrEqual(lhs: Expression, rhs: Expression) -> BinaryOperator {
65+
return BinaryOperator(lhs: lhs, rhs: rhs, operatorType: .greaterOrEqual)
66+
}
Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,5 @@
11
import Foundation
22

3-
func serialize(order: Order) -> String {
4-
switch order {
5-
case .asc: return "ASC"
6-
case .desc: return "DESC"
7-
}
8-
}
9-
10-
func serialize(conflictClause: ConflictClause) -> String {
11-
var res = "ON CONFLICT "
12-
switch conflictClause {
13-
case .rollback: res += "ROLLBACK"
14-
case .abort: res += "ABORT"
15-
case .fail: res += "FAIL"
16-
case .ignore: res += "IGNORE"
17-
case .replace: res += "REPLACE"
18-
}
19-
return res
20-
}
21-
22-
func serialize(columnConstraint: ColumnConstraint) -> String {
23-
switch columnConstraint {
24-
case .primaryKey(let orderMaybe, let conflictClauseMaybe, let autoincrement):
25-
var res = "PRIMARY KEY"
26-
if let order = orderMaybe {
27-
let orderString = serialize(order: order)
28-
res += " "
29-
res += orderString
30-
}
31-
if let conflictClause = conflictClauseMaybe {
32-
let conflictClauseString = serialize(conflictClause: conflictClause)
33-
res += " "
34-
res += conflictClauseString
35-
}
36-
if autoincrement {
37-
res += " AUTOINCREMENT"
38-
}
39-
return res
40-
case .notNull(let conflictClauseMaybe):
41-
var res = "NOT NULL"
42-
if let conflictClause = conflictClauseMaybe {
43-
let conflictClauseString = serialize(conflictClause: conflictClause)
44-
res += " "
45-
res += conflictClauseString
46-
}
47-
return res
48-
case .unique(let conflictClauseMaybe):
49-
var res = "UNIQUE"
50-
if let conflictClause = conflictClauseMaybe {
51-
let conflictClauseString = serialize(conflictClause: conflictClause)
52-
res += " "
53-
res += conflictClauseString
54-
}
55-
return res
56-
}
57-
}
58-
59-
func serialize(column: AnyColumn) -> String {
60-
let typeString = column.sqliteTypeName
61-
var res = "\(column.name) \(typeString)"
62-
for constraint in column.constraints {
63-
let constraintString = serialize(columnConstraint: constraint)
64-
res += " "
65-
res += constraintString
66-
}
67-
return res
3+
protocol Serializable: Any {
4+
func serialize() -> String
685
}

Sources/SQLiteORM/Storage.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public class Storage: NSObject {
185185
var sql = "CREATE TABLE '\(name)' ("
186186
let columnsCount = columns.count
187187
for (columnIndex, column) in columns.enumerated() {
188-
let columnString = serialize(column: column)
188+
let columnString = column.serialize()
189189
sql += "\(columnString)"
190190
if columnIndex < columnsCount - 1 {
191191
sql += ", "
@@ -333,7 +333,7 @@ public class Storage: NSObject {
333333
return res
334334
}
335335

336-
public func getAll<T>() throws -> [T] where T: Initializable {
336+
public func getAll<T>(_ constraints: SelectConstraintBuilder...) throws -> [T] where T: Initializable {
337337
guard let anyTable = self.tables.first(where: { $0.type == T.self }) else {
338338
throw Error.typeIsNotMapped
339339
}
@@ -363,11 +363,11 @@ public class Storage: NSObject {
363363
let errorString = connectionRef.errorMessage
364364
throw Error.sqliteError(code: resultCode, text: errorString)
365365
}
366-
} while resultCode != apiProvider.SQLITE_DONE
366+
} while resultCode != self.apiProvider.SQLITE_DONE
367367
return result
368368
}
369369

370-
public func delete<T>(object: T) throws {
370+
public func delete<T>(_ object: T) throws {
371371
guard let anyTable = self.tables.first(where: { $0.type == T.self }) else {
372372
throw Error.typeIsNotMapped
373373
}
@@ -404,7 +404,7 @@ public class Storage: NSObject {
404404
}
405405
}
406406

407-
public func update<T>(object: T) throws {
407+
public func update<T>(_ object: T) throws {
408408
guard let anyTable = self.tables.first(where: { $0.type == T.self }) else {
409409
throw Error.typeIsNotMapped
410410
}
@@ -520,7 +520,7 @@ public class Storage: NSObject {
520520
}
521521
}
522522

523-
public func insert<T>(object: T) throws -> Int64 {
523+
public func insert<T>(_ object: T) throws -> Int64 {
524524
guard let anyTable = self.tables.first(where: { $0.type == T.self }) else {
525525
throw Error.typeIsNotMapped
526526
}
@@ -556,7 +556,7 @@ public class Storage: NSObject {
556556
return connectionRef.lastInsertRowid
557557
}
558558

559-
public func replace<T>(object: T) throws {
559+
public func replace<T>(_ object: T) throws {
560560
guard let anyTable = self.tables.first(where: { $0.type == T.self }) else {
561561
throw Error.typeIsNotMapped
562562
}

Tests/SQLiteORMTests/SerializeTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class SerializeTests: XCTestCase {
4343
expected: "rating REAL"),
4444
]
4545
for testCase in testCases {
46-
let value = serialize(column: testCase.anyColumn)
46+
let value = testCase.anyColumn.serialize()
4747
XCTAssertEqual(value, testCase.expected)
4848
}
4949
}
@@ -58,7 +58,7 @@ class SerializeTests: XCTestCase {
5858
TestCase(order: .desc, expected: "DESC"),
5959
]
6060
for testCase in testCases {
61-
let value = serialize(order: testCase.order)
61+
let value = testCase.order.serialize()
6262
XCTAssertEqual(value, testCase.expected)
6363
}
6464
}
@@ -76,7 +76,7 @@ class SerializeTests: XCTestCase {
7676
TestCase(conflictClause: .replace, expected: "ON CONFLICT REPLACE"),
7777
]
7878
for testCase in testCases {
79-
let value = serialize(conflictClause: testCase.conflictClause)
79+
let value = testCase.conflictClause.serialize()
8080
XCTAssertEqual(value, testCase.expected)
8181
}
8282
}
@@ -110,7 +110,7 @@ class SerializeTests: XCTestCase {
110110
expected: "NOT NULL ON CONFLICT REPLACE"),
111111
]
112112
for testCase in testCases {
113-
let result = serialize(columnConstraint: testCase.columnConstraint)
113+
let result = testCase.columnConstraint.serialize()
114114
XCTAssertEqual(result, testCase.expected)
115115
}
116116
}

Tests/SQLiteORMTests/StatementTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class StatementTests: XCTestCase {
132132
func testColumnCount() {
133133
XCTAssertEqual(self.apiProvider.calls, [])
134134
_ = self.statement.columnCount()
135-
XCTAssertEqual(self.apiProvider.calls, [SQLiteApiProviderMock.Call(id: 0, callType: .sqlite3ColumnCount(self.pointer))])
135+
XCTAssertEqual(self.apiProvider.calls, [SQLiteApiProviderMock.Call(id: 0, callType: .sqlite3ColumnCount(.value(self.pointer)))])
136136
}
137137

138138
func testStep() {

0 commit comments

Comments
 (0)