@@ -46,6 +46,8 @@ public final class FluentBenchmarker {
4646 try self . testFieldFilter ( )
4747 try self . testJoinedFieldFilter ( )
4848 try self . testSameChildrenFromKey ( )
49+ try self . testArray ( )
50+ try self . testPerformance ( )
4951 try self . testSoftDeleteWithQuery ( )
5052 }
5153
@@ -1524,6 +1526,179 @@ public final class FluentBenchmarker {
15241526 }
15251527 }
15261528
1529+ public func testArray( ) throws {
1530+ struct Qux : Codable {
1531+ var foo : String
1532+ }
1533+
1534+ final class Foo : Model {
1535+ static let schema = " foos "
1536+
1537+ struct _Migration : Migration {
1538+ func prepare( on database: Database ) -> EventLoopFuture < Void > {
1539+ return database. schema ( " foos " )
1540+ . field ( " id " , . uuid, . identifier( auto: false ) )
1541+ . field ( " bar " , . array( of: . int) , . required)
1542+ . field ( " baz " , . array( of: . string) )
1543+ . field ( " qux " , . json, . required)
1544+ . create ( )
1545+ }
1546+
1547+ func revert( on database: Database ) -> EventLoopFuture < Void > {
1548+ return database. schema ( " foos " ) . delete ( )
1549+ }
1550+ }
1551+
1552+ @ID ( key: " id " )
1553+ var id : UUID ?
1554+
1555+ @Field ( key: " bar " )
1556+ var bar : [ Int ]
1557+
1558+ @Field ( key: " baz " )
1559+ var baz : [ String ] ?
1560+
1561+ @Field ( key: " qux " )
1562+ var qux : [ Qux ]
1563+
1564+ init ( ) { }
1565+
1566+ init ( id: UUID ? = nil , bar: [ Int ] , baz: [ String ] ? , qux: [ Qux ] ) {
1567+ self . id = id
1568+ self . bar = bar
1569+ self . baz = baz
1570+ self . qux = qux
1571+ }
1572+ }
1573+
1574+ try runTest ( #function, [
1575+ Foo . _Migration ( ) ,
1576+ ] ) {
1577+ let new = Foo (
1578+ bar: [ 1 , 2 , 3 ] ,
1579+ baz: [ " 4 " , " 5 " , " 6 " ] ,
1580+ qux: [ . init( foo: " 7 " ) , . init( foo: " 8 " ) , . init( foo: " 9 " ) ]
1581+ )
1582+ try new. create ( on: self . database) . wait ( )
1583+
1584+ guard let fetched = try Foo . find ( new. id, on: self . database) . wait ( ) else {
1585+ throw Failure ( " foo didnt save " )
1586+ }
1587+ XCTAssertEqual ( fetched. bar, [ 1 , 2 , 3 ] )
1588+ XCTAssertEqual ( fetched. baz, [ " 4 " , " 5 " , " 6 " ] )
1589+ XCTAssertEqual ( fetched. qux. map { $0. foo } , [ " 7 " , " 8 " , " 9 " ] )
1590+ }
1591+ }
1592+
1593+ public func testPerformance( ) throws {
1594+ final class Foo : Model {
1595+ static let schema = " foos "
1596+
1597+ struct _Migration : Migration {
1598+ func prepare( on database: Database ) -> EventLoopFuture < Void > {
1599+ return database. schema ( " foos " )
1600+ . field ( " id " , . uuid, . identifier( auto: false ) )
1601+ . field ( " bar " , . int, . required)
1602+ . field ( " baz " , . double, . required)
1603+ . field ( " qux " , . string, . required)
1604+ . field ( " quux " , . datetime, . required)
1605+ . field ( " quuz " , . float, . required)
1606+ . field ( " corge " , . array( of: . int) , . required)
1607+ . field ( " grault " , . array( of: . double) , . required)
1608+ . field ( " garply " , . array( of: . string) , . required)
1609+ . field ( " fred " , . string, . required)
1610+ . field ( " plugh " , . int)
1611+ . field ( " xyzzy " , . double)
1612+ . field ( " thud " , . json, . required)
1613+ . create ( )
1614+ }
1615+
1616+ func revert( on database: Database ) -> EventLoopFuture < Void > {
1617+ return database. schema ( " foos " ) . delete ( )
1618+ }
1619+ }
1620+
1621+ struct Thud : Codable {
1622+ var foo : Int
1623+ var bar : Double
1624+ var baz : String
1625+ }
1626+
1627+ @ID ( key: " id " ) var id : UUID ?
1628+ @Field ( key: " bar " ) var bar : Int
1629+ @Field ( key: " baz " ) var baz : Double
1630+ @Field ( key: " qux " ) var qux : String
1631+ @Field ( key: " quux " ) var quux : Date
1632+ @Field ( key: " quuz " ) var quuz : Float
1633+ @Field ( key: " corge " ) var corge : [ Int ]
1634+ @Field ( key: " grault " ) var grault : [ Double ]
1635+ @Field ( key: " garply " ) var garply : [ String ]
1636+ @Field ( key: " fred " ) var fred : Decimal
1637+ @Field ( key: " plugh " ) var plugh : Int ?
1638+ @Field ( key: " xyzzy " ) var xyzzy : Double ?
1639+ @Field ( key: " thud " ) var thud : Thud
1640+
1641+ init ( ) { }
1642+
1643+ init (
1644+ id: UUID ? = nil ,
1645+ bar: Int ,
1646+ baz: Double ,
1647+ qux: String ,
1648+ quux: Date ,
1649+ quuz: Float ,
1650+ corge: [ Int ] ,
1651+ grault: [ Double ] ,
1652+ garply: [ String ] ,
1653+ fred: Decimal ,
1654+ plugh: Int ? ,
1655+ xyzzy: Double ? ,
1656+ thud: Thud
1657+ ) {
1658+ self . id = id
1659+ self . bar = bar
1660+ self . baz = baz
1661+ self . qux = qux
1662+ self . quux = quux
1663+ self . quuz = quuz
1664+ self . corge = corge
1665+ self . grault = grault
1666+ self . garply = garply
1667+ self . fred = fred
1668+ self . plugh = plugh
1669+ self . xyzzy = xyzzy
1670+ self . thud = thud
1671+ }
1672+ }
1673+
1674+ try runTest ( #function, [
1675+ Foo . _Migration ( )
1676+ ] ) {
1677+ for _ in 0 ..< 100 {
1678+ let foo = Foo (
1679+ bar: 42 ,
1680+ baz: 3.14159 ,
1681+ qux: " foobar " ,
1682+ quux: . init( ) ,
1683+ quuz: 2.71828 ,
1684+ corge: [ 1 , 2 , 3 ] ,
1685+ grault: [ 4 , 5 , 6 ] ,
1686+ garply: [ " foo " , " bar " , " baz " ] ,
1687+ fred: 1.4142135623730950 ,
1688+ plugh: 1337 ,
1689+ xyzzy: 9.94987437106 ,
1690+ thud: . init( foo: 5 , bar: 23 , baz: " 1994 " )
1691+ )
1692+ try foo. save ( on: self . database) . wait ( )
1693+ }
1694+ let foos = try Foo . query ( on: self . database) . all ( ) . wait ( )
1695+ for foo in foos {
1696+ XCTAssertNotNil ( foo. id)
1697+ }
1698+ XCTAssertEqual ( foos. count, 100 )
1699+ }
1700+ }
1701+
15271702 public func testSoftDeleteWithQuery( ) throws {
15281703 try runTest ( #function, [
15291704 TrashMigration ( )
0 commit comments