@@ -64,6 +64,78 @@ final class PaginationTests: XCTestCase {
64
64
XCTAssertEqual ( todos. items. count, 1 )
65
65
}
66
66
}
67
+
68
+ func testPaginationLimits( ) throws {
69
+ let app = Application ( . testing)
70
+ defer { app. shutdown ( ) }
71
+
72
+ let rows = [
73
+ TestOutput ( [ " id " : 1 , " title " : " a " ] ) ,
74
+ TestOutput ( [ " id " : 2 , " title " : " b " ] ) ,
75
+ TestOutput ( [ " id " : 3 , " title " : " c " ] ) ,
76
+ TestOutput ( [ " id " : 4 , " title " : " d " ] ) ,
77
+ TestOutput ( [ " id " : 5 , " title " : " e " ] ) ,
78
+ ]
79
+
80
+ let test = CallbackTestDatabase { query in
81
+ XCTAssertEqual ( query. schema, " todos " )
82
+ let result : [ TestOutput ]
83
+ if let limit = query. limits. first? . value, let offset = query. offsets. first? . value {
84
+ result = [ TestOutput] ( rows [ min ( offset, rows. count - 1 ) ..< min ( offset + limit, rows. count) ] )
85
+ } else {
86
+ result = rows
87
+ }
88
+ switch query. action {
89
+ case . aggregate( _) :
90
+ return [ TestOutput ( [ . aggregate: rows. count] ) ]
91
+ default :
92
+ return result
93
+ }
94
+ }
95
+
96
+ app. databases. use ( test. configuration, as: . test)
97
+ app. fluent. pagination. pageSizeLimit = 4
98
+
99
+ app. get ( " todos-request-limit " ) { req -> EventLoopFuture < Page < Todo > > in
100
+ req. fluent. pagination. pageSizeLimit = 2
101
+ return Todo . query ( on: req. db) . paginate ( for: req)
102
+ }
103
+
104
+ app. get ( " todos-request-no-limit " ) { req -> EventLoopFuture < Page < Todo > > in
105
+ req. fluent. pagination. pageSizeLimit = . noLimit
106
+ return Todo . query ( on: req. db) . paginate ( for: req)
107
+ }
108
+
109
+ app. get ( " todos-request-app-limit " ) { req -> EventLoopFuture < Page < Todo > > in
110
+ req. fluent. pagination. pageSizeLimit = nil
111
+ return Todo . query ( on: req. db) . paginate ( for: req)
112
+ }
113
+
114
+ app. get ( " todos-app-limit " ) { req -> EventLoopFuture < Page < Todo > > in
115
+ Todo . query ( on: req. db) . paginate ( for: req)
116
+ }
117
+
118
+ try app. test ( . GET, " todos-request-limit?page=1&per=5 " ) { response in
119
+ XCTAssertEqual ( response. status, . ok)
120
+ let todos = try response. content. decode ( Page< Todo> . self )
121
+ XCTAssertEqual ( todos. items. count, 2 , " Should be capped by request-level limit. " )
122
+ }
123
+ . test ( . GET, " todos-request-no-limit?page=1&per=5 " ) { response in
124
+ XCTAssertEqual ( response. status, . ok)
125
+ let todos = try response. content. decode ( Page< Todo> . self )
126
+ XCTAssertEqual ( todos. items. count, 5 , " Request-level override should suspend app-level limit. " )
127
+ }
128
+ . test ( . GET, " todos-request-app-limit?page=1&per=5 " ) { response in
129
+ XCTAssertEqual ( response. status, . ok)
130
+ let todos = try response. content. decode ( Page< Todo> . self )
131
+ XCTAssertEqual ( todos. items. count, 4 , " Should be capped by app-level limit. " )
132
+ }
133
+ . test ( . GET, " todos-app-limit?page=1&per=5 " ) { response in
134
+ XCTAssertEqual ( response. status, . ok)
135
+ let todos = try response. content. decode ( Page< Todo> . self )
136
+ XCTAssertEqual ( todos. items. count, 4 , " Should be capped by app-level limit. " )
137
+ }
138
+ }
67
139
}
68
140
69
141
private extension DatabaseQuery . Limit {
0 commit comments