import XCTest
import GRDB

private let insertedRowCount = 50_000

// Here we insert records.
class InsertRecordStructTests: XCTestCase {
    
    func testGRDB() {
        struct Item: PersistableRecord {
            var i0: Int
            var i1: Int
            var i2: Int
            var i3: Int
            var i4: Int
            var i5: Int
            var i6: Int
            var i7: Int
            var i8: Int
            var i9: Int
            
            func encode(to container: inout PersistenceContainer) {
                container["i0"] = i0
                container["i1"] = i1
                container["i2"] = i2
                container["i3"] = i3
                container["i4"] = i4
                container["i5"] = i5
                container["i6"] = i6
                container["i7"] = i7
                container["i8"] = i8
                container["i9"] = i9
            }
        }

        let databaseFileName = "GRDBPerformanceTests-\(ProcessInfo.processInfo.globallyUniqueString).sqlite"
        let databasePath = (NSTemporaryDirectory() as NSString).appendingPathComponent(databaseFileName)
        _ = try? FileManager.default.removeItem(atPath: databasePath)
        defer {
            let dbQueue = try! DatabaseQueue(path: databasePath)
            try! dbQueue.inDatabase { db in
                XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM item")!, insertedRowCount)
                XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MIN(i0) FROM item")!, 0)
                XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT MAX(i9) FROM item")!, insertedRowCount - 1)
            }
            try! FileManager.default.removeItem(atPath: databasePath)
        }
        
        measure {
            _ = try? FileManager.default.removeItem(atPath: databasePath)
            
            let dbQueue = try! DatabaseQueue(path: databasePath)
            try! dbQueue.inDatabase { db in
                try db.execute(sql: "CREATE TABLE item (i0 INT, i1 INT, i2 INT, i3 INT, i4 INT, i5 INT, i6 INT, i7 INT, i8 INT, i9 INT)")
            }
            
            try! dbQueue.inTransaction { db in
                for i in 0..<insertedRowCount {
                    try Item(i0: i, i1: i, i2: i, i3: i, i4: i, i5: i, i6: i, i7: i, i8: i, i9: i).insert(db)
                }
                return .commit
            }
        }
    }
}
