@@ -22,7 +22,11 @@ import XCTest
2222import PerfectCRUD
2323
2424let testDBRowCount = 5
25+ #if os(macOS)
2526let testHost = " 127.0.0.1 "
27+ #else
28+ let testHost = " host.docker.internal "
29+ #endif
2630let testUser = " root "
2731let testPassword = " "
2832let testDB = " test "
@@ -280,8 +284,7 @@ class PerfectMySQLTests: XCTestCase {
280284 func testQueryStmt2( ) {
281285 let mysql = rawMySQL
282286 XCTAssert ( mysql. query ( statement: " DROP TABLE IF EXISTS all_data_types " ) )
283-
284- let qres = mysql. query ( statement: " CREATE TABLE `all_data_types` (`varchar` VARCHAR( 32 ), \n `tinyint` TINYINT, \n `text` TEXT, \n `date` DATE, \n `smallint` SMALLINT, \n `mediumint` MEDIUMINT, \n `int` INT, \n `bigint` BIGINT, \n `ubigint` BIGINT UNSIGNED, \n `float` FLOAT( 10, 2 ), \n `double` DOUBLE, \n `decimal` DECIMAL( 10, 2 ), \n `datetime` DATETIME, \n `timestamp` TIMESTAMP, \n `time` TIME, \n `year` YEAR, \n `char` CHAR( 10 ), \n `tinyblob` TINYBLOB, \n `tinytext` TINYTEXT, \n `blob` BLOB, \n `mediumblob` MEDIUMBLOB, \n `mediumtext` MEDIUMTEXT, \n `longblob` LONGBLOB, \n `longtext` LONGTEXT, \n `enum` ENUM( '1', '2', '3' ), \n `set` SET( '1', '2', '3' ), \n `bool` BOOL, \n `binary` BINARY( 20 ), \n `varbinary` VARBINARY( 20 ) ) ENGINE = MYISAM " )
287+ let qres = mysql. query ( statement: " CREATE TABLE `all_data_types` (`varchar` VARCHAR( 22 ), \n `tinyint` TINYINT, \n `text` TEXT, \n `date` DATE, \n `smallint` SMALLINT, \n `mediumint` MEDIUMINT, \n `int` INT, \n `bigint` BIGINT, \n `ubigint` BIGINT UNSIGNED, \n `float` FLOAT( 10, 2 ), \n `double` DOUBLE, \n `decimal` DECIMAL( 10, 2 ), \n `datetime` DATETIME, \n `timestamp` TIMESTAMP, \n `time` TIME, \n `year` YEAR, \n `char` CHAR( 10 ), \n `tinyblob` TINYBLOB, \n `tinytext` TINYTEXT, \n `blob` BLOB, \n `mediumblob` MEDIUMBLOB, \n `mediumtext` MEDIUMTEXT, \n `longblob` LONGBLOB, \n `longtext` LONGTEXT, \n `enum` ENUM( '1', '2', '3' ), \n `set` SET( '1', '2', '3' ), \n `bool` BOOL, \n `binary` BINARY( 20 ), \n `varbinary` VARBINARY( 20 ) ) ENGINE = MYISAM " )
285288 XCTAssert ( qres == true , mysql. errorMessage ( ) )
286289
287290 for _ in 1 ... 2 {
@@ -290,7 +293,7 @@ class PerfectMySQLTests: XCTestCase {
290293 XCTAssert ( prepRes, stmt1. errorMessage ( ) )
291294 XCTAssert ( stmt1. paramCount ( ) == 29 )
292295
293- stmt1. bindParam ( " varchar 20 string 👻 " )
296+ stmt1. bindParam ( " varchar ’22’ string 👻 " )
294297 stmt1. bindParam ( 1 )
295298 stmt1. bindParam ( " text string " )
296299 stmt1. bindParam ( " 2015-10-21 " )
@@ -341,7 +344,7 @@ class PerfectMySQLTests: XCTestCase {
341344 let ok = results. forEachRow {
342345 e in
343346
344- XCTAssertEqual ( e [ 0 ] as? String , " varchar 20 string 👻 " )
347+ XCTAssertEqual ( e [ 0 ] as? String , " varchar ’22’ string 👻 " )
345348 XCTAssertEqual ( e [ 1 ] as? Int8 , 1 )
346349 XCTAssertEqual ( e [ 2 ] as? String , " text string " )
347350 XCTAssertEqual ( e [ 3 ] as? String , " 2015-10-21 " )
@@ -1016,12 +1019,13 @@ class PerfectMySQLTests: XCTestCase {
10161019 do {
10171020 let db = try getTestDB ( )
10181021 let t1 = db. table ( TestTable1 . self)
1019- let newOne = TestTable1 ( id: 2000 , name: " New One " , integer: 40 )
1022+ let newOne = TestTable1 ( id: 2000 , name: " New ` One " , integer: 40 )
10201023 try t1. insert ( newOne)
10211024 let j1 = t1. where ( \TestTable1 . id == newOne. id)
10221025 let j2 = try j1. select ( ) . map { $0}
10231026 XCTAssertEqual ( try j1. count ( ) , 1 )
10241027 XCTAssertEqual ( j2 [ 0 ] . id, 2000 )
1028+ XCTAssertEqual ( j2 [ 0 ] . name, " New ` One " )
10251029 } catch {
10261030 XCTFail ( " \( error) " )
10271031 }
@@ -1671,12 +1675,109 @@ class PerfectMySQLTests: XCTestCase {
16711675 XCTFail ( " \( error) " )
16721676 }
16731677 }
1678+
1679+ func testIntConversion( ) {
1680+ do {
1681+ let db = try getTestDB ( )
1682+ struct IntTest : Codable {
1683+ let id : Int
1684+ }
1685+ try db. sql ( " CREATE TABLE IntTest(id tinyint PRIMARY KEY) " )
1686+ let table = db. table ( IntTest . self)
1687+ let inserted = IntTest ( id: 1 )
1688+ try table. insert ( inserted)
1689+ guard let selected = try db. table ( IntTest . self) . where ( \IntTest . id == 1 ) . first ( ) else {
1690+ return XCTFail ( " Unable to find IntTest. " )
1691+ }
1692+ XCTAssertEqual ( selected. id, inserted. id)
1693+ } catch {
1694+ XCTFail ( " \( error) " )
1695+ }
1696+ }
16741697
16751698 func testBespokeSQL( ) {
16761699 do {
16771700 let db = try getTestDB ( )
1678- let r = try db. sql ( " SELECT * FROM \( TestTable1 . CRUDTableName) WHERE id = 2 " , TestTable1 . self)
1679- XCTAssertEqual ( r. count, 1 )
1701+ do {
1702+ let r = try db. sql ( " SELECT * FROM \( TestTable1 . CRUDTableName) WHERE id = 2 " , TestTable1 . self)
1703+ XCTAssertEqual ( r. count, 1 )
1704+ }
1705+ do {
1706+ let r = try db. sql ( " SELECT * FROM \( TestTable1 . CRUDTableName) " , TestTable1 . self)
1707+ XCTAssertEqual ( r. count, 5 )
1708+ }
1709+ } catch {
1710+ XCTFail ( " \( error) " )
1711+ }
1712+ }
1713+
1714+ func testURL( ) {
1715+ do {
1716+ let db = try getTestDB ( )
1717+ struct TableWithURL : Codable {
1718+ let id : Int
1719+ let url : URL
1720+ }
1721+ try db. create ( TableWithURL . self)
1722+ let t1 = db. table ( TableWithURL . self)
1723+ let newOne = TableWithURL ( id: 2000 , url: URL ( string: " http://localhost/ " ) !)
1724+ try t1. insert ( newOne)
1725+ let j1 = t1. where ( \TableWithURL . id == newOne. id)
1726+ let j2 = try j1. select ( ) . map { $0}
1727+ XCTAssertEqual ( try j1. count ( ) , 1 )
1728+ XCTAssertEqual ( j2 [ 0 ] . id, 2000 )
1729+ XCTAssertEqual ( j2 [ 0 ] . url. absoluteString, " http://localhost/ " )
1730+ } catch {
1731+ XCTFail ( " \( error) " )
1732+ }
1733+ }
1734+
1735+ func testLastInsertId( ) {
1736+ do {
1737+ let db = try getTestDB ( )
1738+ struct ReturningItem : Codable , Equatable {
1739+ let id : UInt64 ?
1740+ var def : Int ?
1741+ init ( id: UInt64 , def: Int ? = nil ) {
1742+ self . id = id
1743+ self . def = def
1744+ }
1745+ }
1746+ try db. sql ( " DROP TABLE IF EXISTS \( ReturningItem . CRUDTableName) " )
1747+ try db. sql ( " CREATE TABLE \( ReturningItem . CRUDTableName) (id INT PRIMARY KEY AUTO_INCREMENT, def INT DEFAULT 42) " )
1748+ let table = db. table ( ReturningItem . self)
1749+ let id = try table
1750+ . insert ( ReturningItem ( id: 0 , def: 0 ) ,
1751+ ignoreKeys: \ReturningItem . id) //, \ReturningItem.def)
1752+ . lastInsertId ( )
1753+ XCTAssertEqual ( id, 1 )
1754+
1755+ } catch {
1756+ XCTFail ( " \( error) " )
1757+ }
1758+ }
1759+
1760+ func testEmptyInsert( ) {
1761+ do {
1762+ let db = try getTestDB ( )
1763+ struct ReturningItem : Codable , Equatable {
1764+ let id : Int ?
1765+ var def : Int ?
1766+ init ( id: Int , def: Int ? = nil ) {
1767+ self . id = id
1768+ self . def = def
1769+ }
1770+ }
1771+ try db. sql ( " DROP TABLE IF EXISTS \( ReturningItem . CRUDTableName) " )
1772+ try db. sql ( " CREATE TABLE \( ReturningItem . CRUDTableName) (id INT PRIMARY KEY AUTO_INCREMENT, def INT DEFAULT 42) " )
1773+ let table = db. table ( ReturningItem . self)
1774+
1775+ let id = try table
1776+ . insert ( ReturningItem ( id: 0 , def: 0 ) ,
1777+ ignoreKeys: \ReturningItem . id, \ReturningItem . def)
1778+ . lastInsertId ( )
1779+ XCTAssertEqual ( id, 1 )
1780+
16801781 } catch {
16811782 XCTFail ( " \( error) " )
16821783 }
@@ -1729,7 +1830,10 @@ class PerfectMySQLTests: XCTestCase {
17291830 ( " testBadDecoding " , testBadDecoding) ,
17301831 ( " testAllPrimTypes1 " , testAllPrimTypes1) ,
17311832 ( " testAllPrimTypes2 " , testAllPrimTypes2) ,
1732- ( " testBespokeSQL " , testBespokeSQL)
1833+ ( " testBespokeSQL " , testBespokeSQL) ,
1834+ ( " testURL " , testURL) ,
1835+ ( " testLastInsertId " , testLastInsertId) ,
1836+ ( " testEmptyInsert " , testEmptyInsert)
17331837 ]
17341838}
17351839
0 commit comments