@@ -434,6 +434,21 @@ class RecordPrimaryKeyHiddenRowIDTests : GRDBTestCase {
434
434
}
435
435
}
436
436
437
+ func testFindWithKey( ) throws {
438
+ let dbQueue = try makeDatabaseQueue ( )
439
+ try dbQueue. inDatabase { db in
440
+ let record = Person ( name: " Arthur " )
441
+ try record. insert ( db)
442
+
443
+ let fetchedRecord = try Person . find ( db, key: [ " rowid " : record. id] )
444
+ XCTAssertTrue ( fetchedRecord. id == record. id)
445
+ XCTAssertTrue ( fetchedRecord. name == record. name)
446
+ XCTAssertTrue ( fetchedRecord. age == record. age)
447
+ XCTAssertTrue ( abs ( fetchedRecord. creationDate. timeIntervalSince ( record. creationDate) ) < 1e-3 ) // ISO-8601 is precise to the millisecond.
448
+ XCTAssertEqual ( lastSQLQuery, " SELECT *, \" rowid \" FROM \" persons \" WHERE \" rowid \" = \( record. id!) " )
449
+ }
450
+ }
451
+
437
452
438
453
// MARK: - Fetch With Key Request
439
454
@@ -702,6 +717,48 @@ class RecordPrimaryKeyHiddenRowIDTests : GRDBTestCase {
702
717
}
703
718
}
704
719
720
+ func testFindWithPrimaryKey( ) throws {
721
+ let dbQueue = try makeDatabaseQueue ( )
722
+ try dbQueue. inDatabase { db in
723
+ let record = Person ( name: " Arthur " )
724
+ try record. insert ( db)
725
+
726
+ do {
727
+ let id : Int64 ? = nil
728
+ _ = try Person . find ( db, key: id)
729
+ XCTFail ( " Expected RecordError " )
730
+ } catch RecordError . recordNotFound( databaseTableName: " persons " , key: [ " rowid " : . null] ) { }
731
+
732
+ do {
733
+ let fetchedRecord = try Person . find ( db, key: record. id)
734
+ XCTAssertTrue ( fetchedRecord. id == record. id)
735
+ XCTAssertTrue ( fetchedRecord. name == record. name)
736
+ XCTAssertTrue ( fetchedRecord. age == record. age)
737
+ XCTAssertTrue ( abs ( fetchedRecord. creationDate. timeIntervalSince ( record. creationDate) ) < 1e-3 ) // ISO-8601 is precise to the millisecond.
738
+ XCTAssertEqual ( lastSQLQuery, " SELECT *, \" rowid \" FROM \" persons \" WHERE \" rowid \" = \( record. id!) " )
739
+ }
740
+
741
+ if #available( OSX 10 . 15 , iOS 13 . 0 , tvOS 13 . 0 , watchOS 6 , * ) {
742
+ do {
743
+ _ = try Person . find ( db, id: - 1 )
744
+ XCTFail ( " Expected RecordError " )
745
+ } catch RecordError . recordNotFound( databaseTableName: " persons " , key: [ " rowid " : ( - 1 ) . databaseValue] ) { }
746
+
747
+ do {
748
+ let fetchedRecord = try Person . find ( db, id: record. id!)
749
+ XCTAssertTrue ( fetchedRecord. id == record. id)
750
+ XCTAssertTrue ( fetchedRecord. name == record. name)
751
+ XCTAssertTrue ( fetchedRecord. age == record. age)
752
+ XCTAssertTrue ( abs ( fetchedRecord. creationDate. timeIntervalSince ( record. creationDate) ) < 1e-3 ) // ISO-8601 is precise to the millisecond.
753
+ XCTAssertEqual ( lastSQLQuery, " SELECT *, \" rowid \" FROM \" persons \" WHERE \" rowid \" = \( record. id!) " )
754
+ }
755
+ do {
756
+ try XCTAssertNil ( Person . fetchOne ( db, id: nil ) )
757
+ }
758
+ }
759
+ }
760
+ }
761
+
705
762
706
763
// MARK: - Fetch With Primary Key Request
707
764
@@ -915,6 +972,8 @@ class RecordPrimaryKeyHiddenRowIDTests : GRDBTestCase {
915
972
XCTAssertTrue ( try Person . fetchOne ( db) !. id != nil )
916
973
XCTAssertTrue ( try Person . fetchOne ( db, key: 1 ) !. id != nil )
917
974
XCTAssertTrue ( try Person . fetchOne ( db, key: [ " rowid " : 1 ] ) !. id != nil )
975
+ XCTAssertTrue ( try Person . find ( db, key: 1 ) . id != nil )
976
+ XCTAssertTrue ( try Person . find ( db, key: [ " rowid " : 1 ] ) . id != nil )
918
977
XCTAssertTrue ( try Person . fetchAll ( db) . first!. id != nil )
919
978
XCTAssertTrue ( try Person . fetchAll ( db, keys: [ 1 ] ) . first!. id != nil )
920
979
XCTAssertTrue ( try Person . fetchAll ( db, keys: [ [ " rowid " : 1 ] ] ) . first!. id != nil )
0 commit comments