Skip to content

Commit ac10f12

Browse files
Merge pull request #2 from fred-sch/main
Insert does not fail if model is entirely empty
2 parents f646213 + a236e5e commit ac10f12

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

Sources/ClickHouseVapor/ClickHouseModel.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ extension ClickHouseModel {
6464
}
6565
}
6666

67+
/// Insert data into table. Insert will fail if columns differ in the amount of elements.
6768
public func insert(on connection: ClickHouseConnectionProtocol, engine: ClickHouseEngine? = nil) throws -> EventLoopFuture<Void> {
6869
let fields = properties
6970
let engine = engine ?? Self.engine
7071

71-
guard let rowCount = fields.first?.count else {
72-
// no columns -> nothing to do
72+
let rowCount = self.count
73+
if rowCount == 0 {
74+
// no rows or no columns -> nothing to do
7375
return connection.eventLoop.makeSucceededFuture(())
7476
}
7577

@@ -80,6 +82,7 @@ extension ClickHouseModel {
8082
}
8183
return rowCount == 0 ? nil : ClickHouseColumn(field.key, field.getClickHouseArray())
8284
}
85+
8386
return connection.insert(into: engine.tableWithDatabase, data: data)
8487
}
8588

Sources/ClickHouseVapor/ClickHouseVaporError.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Created by Patrick Zippenfenig on 2020-11-11.
66
//
77

8-
public enum ClickHouseVaporError: Error {
8+
public enum ClickHouseVaporError: Error, Equatable {
99
case mismatchingDataType(columnName: String)
1010
case mismatchingRowCount(count: Int, expected: Int)
1111
}

Tests/ClickHouseVaporTests/ClickHouseVaporTests.swift

+35-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ final class ClickHouseVaporTests: XCTestCase {
5252
static var allTests = [
5353
("testPing", testPing),
5454
("testModel", testModel),
55-
("testModelMissesColumns", testModelMissesColumns)
55+
("testDifferingRowsInsert", testDifferingRowsInsert),
56+
("testEmptyModelInsert", testEmptyModelInsert)
5657
]
5758

5859
func testPing() {
@@ -111,8 +112,8 @@ final class ClickHouseVaporTests: XCTestCase {
111112
XCTAssertEqual(model3.timestamp, model2.timestamp)
112113
}
113114

114-
/// model should fail if insert with empty columns is tried
115-
public func testModelMissesColumns() {
115+
/// insert should fail if some columns are not set, but others are
116+
public func testDifferingRowsInsert() {
116117
let app = Application(.testing)
117118
defer { app.shutdown() }
118119
try! app.configureClickHouseDatabases()
@@ -125,11 +126,40 @@ final class ClickHouseVaporTests: XCTestCase {
125126

126127

127128
model.id = [ "x010", "ax51", "cd22" ]
128-
model.fixed = [ "", "123456", "12345678901234" ]
129+
model.fixed = [ "", "12345678901234" ]
130+
model.timestamp = [ 100, 200, 300 ]
129131
model.temperature = [ 11.1, 10.4, 8.9 ]
130132

131133
try! TestModel.createTable(on: app.clickHouse).wait()
132-
XCTAssertThrowsError(try model.insert(on: app.clickHouse).wait(), "\(ClickHouseVaporError.mismatchingRowCount(count: 0, expected: 3))")
134+
135+
var thrownError : Error?
136+
// insert should fail if one tries to insert columns with different amount of rows
137+
XCTAssertThrowsError(try model.insert(on: app.clickHouse).wait()) {
138+
thrownError = $0
139+
}
140+
// check that we get correct error message
141+
XCTAssertEqual(thrownError as! ClickHouseVaporError, ClickHouseVaporError.mismatchingRowCount(count: 2, expected: 3))
142+
}
143+
144+
145+
/// insert should not fail if the complete model is empty
146+
public func testEmptyModelInsert() {
147+
let app = Application(.testing)
148+
defer { app.shutdown() }
149+
try! app.configureClickHouseDatabases()
150+
app.logger.logLevel = .trace
151+
152+
let model = TestModel()
153+
154+
// drop table to ensure unit test
155+
try! TestModel.deleteTable(on: app.clickHouse).wait()
156+
157+
try! TestModel.createTable(on: app.clickHouse).wait()
158+
159+
// insert should not fail if all columns are empty
160+
XCTAssertNoThrow(try model.insert(on: app.clickHouse).wait())
133161
}
162+
163+
134164

135165
}

0 commit comments

Comments
 (0)