@@ -22,7 +22,11 @@ import XCTest
22
22
import PerfectCRUD
23
23
24
24
let testDBRowCount = 5
25
+ #if os(macOS)
25
26
let testHost = " 127.0.0.1 "
27
+ #else
28
+ let testHost = " host.docker.internal "
29
+ #endif
26
30
let testUser = " root "
27
31
let testPassword = " "
28
32
let testDB = " test "
@@ -280,8 +284,7 @@ class PerfectMySQLTests: XCTestCase {
280
284
func testQueryStmt2( ) {
281
285
let mysql = rawMySQL
282
286
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 " )
285
288
XCTAssert ( qres == true , mysql. errorMessage ( ) )
286
289
287
290
for _ in 1 ... 2 {
@@ -290,7 +293,7 @@ class PerfectMySQLTests: XCTestCase {
290
293
XCTAssert ( prepRes, stmt1. errorMessage ( ) )
291
294
XCTAssert ( stmt1. paramCount ( ) == 29 )
292
295
293
- stmt1. bindParam ( " varchar 20 string 👻 " )
296
+ stmt1. bindParam ( " varchar ’22’ string 👻 " )
294
297
stmt1. bindParam ( 1 )
295
298
stmt1. bindParam ( " text string " )
296
299
stmt1. bindParam ( " 2015-10-21 " )
@@ -341,7 +344,7 @@ class PerfectMySQLTests: XCTestCase {
341
344
let ok = results. forEachRow {
342
345
e in
343
346
344
- XCTAssertEqual ( e [ 0 ] as? String , " varchar 20 string 👻 " )
347
+ XCTAssertEqual ( e [ 0 ] as? String , " varchar ’22’ string 👻 " )
345
348
XCTAssertEqual ( e [ 1 ] as? Int8 , 1 )
346
349
XCTAssertEqual ( e [ 2 ] as? String , " text string " )
347
350
XCTAssertEqual ( e [ 3 ] as? String , " 2015-10-21 " )
@@ -1016,12 +1019,13 @@ class PerfectMySQLTests: XCTestCase {
1016
1019
do {
1017
1020
let db = try getTestDB ( )
1018
1021
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 )
1020
1023
try t1. insert ( newOne)
1021
1024
let j1 = t1. where ( \TestTable1 . id == newOne. id)
1022
1025
let j2 = try j1. select ( ) . map { $0}
1023
1026
XCTAssertEqual ( try j1. count ( ) , 1 )
1024
1027
XCTAssertEqual ( j2 [ 0 ] . id, 2000 )
1028
+ XCTAssertEqual ( j2 [ 0 ] . name, " New ` One " )
1025
1029
} catch {
1026
1030
XCTFail ( " \( error) " )
1027
1031
}
@@ -1671,12 +1675,109 @@ class PerfectMySQLTests: XCTestCase {
1671
1675
XCTFail ( " \( error) " )
1672
1676
}
1673
1677
}
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
+ }
1674
1697
1675
1698
func testBespokeSQL( ) {
1676
1699
do {
1677
1700
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
+
1680
1781
} catch {
1681
1782
XCTFail ( " \( error) " )
1682
1783
}
@@ -1729,7 +1830,10 @@ class PerfectMySQLTests: XCTestCase {
1729
1830
( " testBadDecoding " , testBadDecoding) ,
1730
1831
( " testAllPrimTypes1 " , testAllPrimTypes1) ,
1731
1832
( " testAllPrimTypes2 " , testAllPrimTypes2) ,
1732
- ( " testBespokeSQL " , testBespokeSQL)
1833
+ ( " testBespokeSQL " , testBespokeSQL) ,
1834
+ ( " testURL " , testURL) ,
1835
+ ( " testLastInsertId " , testLastInsertId) ,
1836
+ ( " testEmptyInsert " , testEmptyInsert)
1733
1837
]
1734
1838
}
1735
1839
0 commit comments