-
Notifications
You must be signed in to change notification settings - Fork 496
/
OffsetQueryPagingSourceTest.kt
758 lines (651 loc) · 22.9 KB
/
OffsetQueryPagingSourceTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/*
* Copyright (C) 2016 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.sqldelight.paging3
import androidx.paging.LoadType
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingSource
import androidx.paging.PagingSource.LoadResult
import androidx.paging.PagingState
import androidx.recyclerview.widget.DiffUtil
import app.cash.sqldelight.Query
import app.cash.sqldelight.Transacter
import app.cash.sqldelight.TransacterImpl
import app.cash.sqldelight.db.SqlCursor
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.After
import org.junit.Before
import org.junit.Test
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue
@ExperimentalCoroutinesApi
class OffsetQueryPagingSourceTest {
private lateinit var driver: SqlDriver
private lateinit var transacter: Transacter
@Before
fun init() {
Dispatchers.setMain(StandardTestDispatcher())
driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY)
driver.execute(null, "CREATE TABLE TestItem(id INTEGER NOT NULL PRIMARY KEY);", 0)
transacter = object : TransacterImpl(driver) {}
}
@After
fun tearDown() {
Dispatchers.resetMain()
}
@Test
fun test_itemCount() = runTest {
insertItems(ITEMS_LIST)
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
pagingSource.refresh()
Pager(CONFIG, pagingSourceFactory = { pagingSource })
.flow
.first()
.withPagingDataDiffer(this, testItemDiffCallback) {
assertThat(itemCount).isEqualTo(100)
}
}
@Test
fun invalidDbQuery_pagingSourceDoesNotInvalidate() = runTest {
insertItems(ITEMS_LIST)
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
// load once to register db observers
pagingSource.refresh()
assertThat(pagingSource.invalid).isFalse()
val result = deleteItem(TestItem(1000))
// invalid delete. Should have 0 items deleted and paging source remains valid
assertThat(result).isEqualTo(0)
assertThat(pagingSource.invalid).isFalse()
}
@Test
fun load_initialLoad() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
insertItems(ITEMS_LIST)
val result = pagingSource.refresh() as LoadResult.Page
assertThat(result.data).containsExactlyElementsIn(ITEMS_LIST.subList(0, 15))
}
@Test
fun load_initialEmptyLoad() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
val result = pagingSource.refresh() as LoadResult.Page
assertTrue(result.data.isEmpty())
// now add items
insertItems(ITEMS_LIST)
// invalidate pagingSource to imitate invalidation from running refreshVersionSync
pagingSource.invalidate()
assertTrue(pagingSource.invalid)
// this refresh should check pagingSource's invalid status, realize it is invalid, and
// return a LoadResult.Invalid
assertThat(pagingSource.refresh()).isInstanceOf(LoadResult.Invalid::class.java)
}
@Test
fun load_initialLoadWithInitialKey() = runTest {
insertItems(ITEMS_LIST)
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
// refresh with initial key = 20
val result = pagingSource.refresh(key = 20) as LoadResult.Page
// item in pos 21-35 (TestItemId 20-34) loaded
assertThat(result.data).containsExactlyElementsIn(ITEMS_LIST.subList(20, 35))
}
@Test
fun invalidInitialKey_dbEmpty_returnsEmpty() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
val result = pagingSource.refresh(key = 101) as LoadResult.Page
assertThat(result.data).isEmpty()
}
@Test
fun invalidInitialKey_keyTooLarge_returnsLastPage() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
insertItems(ITEMS_LIST)
val result = pagingSource.refresh(key = 101) as LoadResult.Page
// should load the last page
assertThat(result.data).containsExactlyElementsIn(ITEMS_LIST.subList(85, 100))
}
@Test
fun invalidInitialKey_negativeKey() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
insertItems(ITEMS_LIST)
// should throw error when initial key is negative
val expectedException = assertFailsWith<IllegalArgumentException> {
pagingSource.refresh(key = -1)
}
// default message from Paging 3 for negative initial key
assertThat(expectedException.message).isEqualTo("itemsBefore cannot be negative")
}
@Test
fun append_middleOfList() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
insertItems(ITEMS_LIST)
val result = pagingSource.append(key = 20) as LoadResult.Page
// item in pos 21-25 (TestItemId 20-24) loaded
assertThat(result.data).containsExactlyElementsIn(ITEMS_LIST.subList(20, 25))
assertThat(result.nextKey).isEqualTo(25)
assertThat(result.prevKey).isEqualTo(20)
}
@Test
fun append_availableItemsLessThanLoadSize() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
insertItems(ITEMS_LIST)
val result = pagingSource.append(key = 97) as LoadResult.Page
// item in pos 98-100 (TestItemId 97-99) loaded
assertThat(result.data).containsExactlyElementsIn(ITEMS_LIST.subList(97, 100))
assertThat(result.nextKey).isNull()
assertThat(result.prevKey).isEqualTo(97)
}
@Test
fun load_consecutiveAppend() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
insertItems(ITEMS_LIST)
// first append
val result = pagingSource.append(key = 30) as LoadResult.Page
// TestItemId 30-34 loaded
assertThat(result.data).containsExactlyElementsIn(ITEMS_LIST.subList(30, 35))
// second append using nextKey from previous load
val result2 = pagingSource.append(key = result.nextKey) as LoadResult.Page
// TestItemId 35 - 39 loaded
assertThat(result2.data).containsExactlyElementsIn(ITEMS_LIST.subList(35, 40))
}
@Test
fun append_invalidResult() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
insertItems(ITEMS_LIST)
// first append
val result = pagingSource.append(key = 30) as LoadResult.Page
// TestItemId 30-34 loaded
assertThat(result.data).containsExactlyElementsIn(ITEMS_LIST.subList(30, 35))
// invalidate pagingSource to imitate invalidation from running refreshVersionSync
pagingSource.invalidate()
// this append should check pagingSource's invalid status, realize it is invalid, and
// return a LoadResult.Invalid
val result2 = pagingSource.append(key = result.nextKey)
assertThat(result2).isInstanceOf(LoadResult.Invalid::class.java)
}
@Test
fun prepend_middleOfList() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
insertItems(ITEMS_LIST)
val result = pagingSource.prepend(key = 30) as LoadResult.Page
assertThat(result.data).containsExactlyElementsIn(ITEMS_LIST.subList(25, 30))
assertThat(result.nextKey).isEqualTo(30)
assertThat(result.prevKey).isEqualTo(25)
}
@Test
fun prepend_availableItemsLessThanLoadSize() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
insertItems(ITEMS_LIST)
val result = pagingSource.prepend(key = 3) as LoadResult.Page
// items in pos 0 - 2 (TestItemId 0 - 2) loaded
assertThat(result.data).containsExactlyElementsIn(ITEMS_LIST.subList(0, 3))
assertThat(result.nextKey).isEqualTo(3)
assertThat(result.prevKey).isNull()
}
@Test
fun load_consecutivePrepend() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
insertItems(ITEMS_LIST)
// first prepend
val result = pagingSource.prepend(key = 20) as LoadResult.Page
// items pos 16-20 (TestItemId 15-19) loaded
assertThat(result.data).containsExactlyElementsIn(ITEMS_LIST.subList(15, 20))
// second prepend using prevKey from previous load
val result2 = pagingSource.prepend(key = result.prevKey) as LoadResult.Page
// items pos 11-15 (TestItemId 10 - 14) loaded
assertThat(result2.data).containsExactlyElementsIn(ITEMS_LIST.subList(10, 15))
}
@Test
fun prepend_invalidResult() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
insertItems(ITEMS_LIST)
// first prepend
val result = pagingSource.prepend(key = 20) as LoadResult.Page
// items pos 16-20 (TestItemId 15-19) loaded
assertThat(result.data).containsExactlyElementsIn(ITEMS_LIST.subList(15, 20))
// invalidate pagingSource to imitate invalidation from running refreshVersionSync
pagingSource.invalidate()
// this prepend should check pagingSource's invalid status, realize it is invalid, and
// return LoadResult.Invalid
val result2 = pagingSource.prepend(key = result.prevKey)
assertThat(result2).isInstanceOf(LoadResult.Invalid::class.java)
}
@Test
fun test_itemsBefore() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
insertItems(ITEMS_LIST)
// for initial load
val result = pagingSource.refresh(key = 50) as LoadResult.Page
// initial loads items in pos 51 - 65, should have 50 items before
assertThat(result.itemsBefore).isEqualTo(50)
// prepend from initial load
val result2 = pagingSource.prepend(key = result.prevKey) as LoadResult.Page
// prepend loads items in pos 46 - 50, should have 45 item before
assertThat(result2.itemsBefore).isEqualTo(45)
// append from initial load
val result3 = pagingSource.append(key = result.nextKey) as LoadResult.Page
// append loads items in position 66 - 70 , should have 65 item before
assertThat(result3.itemsBefore).isEqualTo(65)
}
@Test
fun test_itemsAfter() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
insertItems(ITEMS_LIST)
// for initial load
val result = pagingSource.refresh(key = 30) as LoadResult.Page
// initial loads items in position 31 - 45, should have 55 items after
assertThat(result.itemsAfter).isEqualTo(55)
// prepend from initial load
val result2 = pagingSource.prepend(key = result.prevKey) as LoadResult.Page
// prepend loads items in position 26 - 30, should have 70 item after
assertThat(result2.itemsAfter).isEqualTo(70)
// append from initial load
val result3 = pagingSource.append(result.nextKey) as LoadResult.Page
// append loads items in position 46 - 50 , should have 50 item after
assertThat(result3.itemsAfter).isEqualTo(50)
}
@Test
fun test_getRefreshKey() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
insertItems(ITEMS_LIST)
// initial load
val result = pagingSource.refresh() as LoadResult.Page
// 15 items loaded, assuming anchorPosition = 14 as the last item loaded
var refreshKey = pagingSource.getRefreshKey(
PagingState(
pages = listOf(result),
anchorPosition = 14,
config = CONFIG,
leadingPlaceholderCount = 0,
),
)
// should load around anchor position
// Initial load size = 15, refresh key should be (15/2 = 7) items
// before anchorPosition (14 - 7 = 7)
assertThat(refreshKey).isEqualTo(7)
// append after refresh
val result2 = pagingSource.append(key = result.nextKey) as LoadResult.Page
assertThat(result2.data).isEqualTo(ITEMS_LIST.subList(15, 20))
refreshKey = pagingSource.getRefreshKey(
PagingState(
pages = listOf(result, result2),
// 20 items loaded, assume anchorPosition = 19 as the last item loaded
anchorPosition = 19,
config = CONFIG,
leadingPlaceholderCount = 0,
),
)
// initial load size 15. Refresh key should be (15/2 = 7) items before anchorPosition
// (19 - 7 = 12)
assertThat(refreshKey).isEqualTo(12)
}
@Test
fun load_refreshKeyGreaterThanItemCount_lastPage() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
insertItems(ITEMS_LIST)
pagingSource.refresh(key = 70)
deleteItems(40..100)
// assume user was viewing last item of the refresh load with anchorPosition = 85,
// initialLoadSize = 15. This mimics how getRefreshKey() calculates refresh key.
val refreshKey = 85 - (15 / 2)
assertThat(refreshKey).isEqualTo(78)
val pagingSource2 = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
val result2 = pagingSource2.refresh(key = refreshKey) as LoadResult.Page
// database should only have 40 items left. Refresh key is invalid at this point
// (greater than item count after deletion)
Pager(CONFIG, pagingSourceFactory = { pagingSource2 })
.flow
.first()
.withPagingDataDiffer(this, testItemDiffCallback) {
assertThat(itemCount).isEqualTo(40)
}
// ensure that paging source can handle invalid refresh key properly
// should load last page with items 25 - 40
assertThat(result2.data).containsExactlyElementsIn(ITEMS_LIST.subList(25, 40))
// should account for updated item count to return correct itemsBefore, itemsAfter,
// prevKey, nextKey
assertThat(result2.itemsBefore).isEqualTo(25)
assertThat(result2.itemsAfter).isEqualTo(0)
// no append can be triggered
assertThat(result2.prevKey).isEqualTo(25)
assertThat(result2.nextKey).isNull()
}
/**
* Tests the behavior if user was viewing items in the top of the database and those items
* were deleted.
*
* Currently, if anchorPosition is small enough (within bounds of 0 to loadSize/2), then on
* invalidation from dropped items at the top, refresh will load with offset = 0. If
* anchorPosition is larger than loadsize/2, then the refresh load's offset will
* be 0 to (anchorPosition - loadSize/2).
*
* Ideally, in the future Paging will be able to handle this case better.
*/
@Test
fun load_refreshKeyGreaterThanItemCount_firstPage() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
insertItems(ITEMS_LIST)
pagingSource.refresh()
Pager(CONFIG, pagingSourceFactory = { pagingSource })
.flow
.first()
.withPagingDataDiffer(this, testItemDiffCallback) {
assertThat(itemCount).isEqualTo(100)
}
// items id 0 - 29 deleted (30 items removed)
deleteItems(0..29)
val pagingSource2 = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
// assume user was viewing first few items with anchorPosition = 0 and refresh key
// clips to 0
val refreshKey = 0
val result2 = pagingSource2.refresh(key = refreshKey) as LoadResult.Page
// database should only have 70 items left
Pager(CONFIG, pagingSourceFactory = { pagingSource2 })
.flow
.first()
.withPagingDataDiffer(this, testItemDiffCallback) {
assertThat(itemCount).isEqualTo(70)
}
// first 30 items deleted, refresh should load starting from pos 31 (item id 30 - 45)
assertThat(result2.data).containsExactlyElementsIn(ITEMS_LIST.subList(30, 45))
// should account for updated item count to return correct itemsBefore, itemsAfter,
// prevKey, nextKey
assertThat(result2.itemsBefore).isEqualTo(0)
assertThat(result2.itemsAfter).isEqualTo(55)
// no prepend can be triggered
assertThat(result2.prevKey).isNull()
assertThat(result2.nextKey).isEqualTo(15)
}
@Test
fun load_loadSizeAndRefreshKeyGreaterThanItemCount() = runTest {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
insertItems(ITEMS_LIST)
pagingSource.refresh(key = 30)
Pager(CONFIG, pagingSourceFactory = { pagingSource })
.flow
.first()
.withPagingDataDiffer(this, testItemDiffCallback) {
assertThat(itemCount).isEqualTo(100)
}
// items id 0 - 94 deleted (95 items removed)
deleteItems(0..94)
val pagingSource2 = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
// assume user was viewing first few items with anchorPosition = 0 and refresh key
// clips to 0
val refreshKey = 0
val result2 = pagingSource2.refresh(key = refreshKey) as LoadResult.Page
// database should only have 5 items left
Pager(CONFIG, pagingSourceFactory = { pagingSource2 })
.flow
.first()
.withPagingDataDiffer(this, testItemDiffCallback) {
assertThat(itemCount).isEqualTo(5)
}
// only 5 items should be loaded with offset = 0
assertThat(result2.data).containsExactlyElementsIn(ITEMS_LIST.subList(95, 100))
// should recognize that this is a terminal load
assertThat(result2.itemsBefore).isEqualTo(0)
assertThat(result2.itemsAfter).isEqualTo(0)
assertThat(result2.prevKey).isNull()
assertThat(result2.nextKey).isNull()
}
@Test
fun test_jumpSupport() {
val pagingSource = QueryPagingSource(
countQuery(),
transacter,
EmptyCoroutineContext,
::query,
)
assertTrue(pagingSource.jumpingSupported)
}
@Test
fun load_initialEmptyLoad_QueryPagingSourceLong() = runTest {
val pagingSource = QueryPagingSource(
countQueryLong(),
transacter,
EmptyCoroutineContext,
::queryLong,
)
val result = pagingSource.refresh() as LoadResult.Page
assertTrue(result.data.isEmpty())
// now add items
insertItems(ITEMS_LIST)
// invalidate pagingSource to imitate invalidation from running refreshVersionSync
pagingSource.invalidate()
assertTrue(pagingSource.invalid)
// this refresh should check pagingSource's invalid status, realize it is invalid, and
// return a LoadResult.Invalid
assertThat(pagingSource.refresh()).isInstanceOf(LoadResult.Invalid::class.java)
}
private fun query(limit: Int, offset: Int) = queryLong(limit.toLong(), offset.toLong())
private fun queryLong(limit: Long, offset: Long) = object : Query<TestItem>(
{ cursor ->
TestItem(cursor.getLong(0)!!)
},
) {
override fun <R> execute(mapper: (SqlCursor) -> R) = driver.executeQuery(1, "SELECT id FROM TestItem LIMIT ? OFFSET ?", mapper, 2) {
bindLong(0, limit)
bindLong(1, offset)
}
override fun addListener(listener: Listener) = driver.addListener(listener, arrayOf("TestItem"))
override fun removeListener(listener: Listener) = driver.removeListener(listener, arrayOf("TestItem"))
}
private fun countQuery() = Query(
2,
arrayOf("TestItem"),
driver,
"Test.sq",
"count",
"SELECT count(*) FROM TestItem",
{ it.getLong(0)!!.toInt() },
)
private fun countQueryLong() = Query(
2,
arrayOf("TestItem"),
driver,
"Test.sq",
"count",
"SELECT count(*) FROM TestItem",
{ it.getLong(0)!! },
)
private fun insertItems(items: List<TestItem>) {
items.forEach {
driver.execute(0, "INSERT INTO TestItem (id) VALUES (?)", 1) {
bindLong(0, it.id)
}
}
}
private fun deleteItem(item: TestItem): Long =
driver
.execute(0, "DELETE FROM TestItem WHERE id = ?;", 1) {
bindLong(0, item.id)
}
.value
private fun deleteItems(range: IntRange): Long =
driver
.execute(0, "DELETE FROM TestItem WHERE id >= ? AND id <= ?", 2) {
bindLong(0, range.first.toLong())
bindLong(1, range.last.toLong())
}
.value
}
private val CONFIG = PagingConfig(
pageSize = 5,
enablePlaceholders = true,
initialLoadSize = 15,
)
private val ITEMS_LIST = List(100) { TestItem(id = it.toLong()) }
private val testItemDiffCallback = object : DiffUtil.ItemCallback<TestItem>() {
override fun areItemsTheSame(oldItem: TestItem, newItem: TestItem): Boolean = oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: TestItem, newItem: TestItem): Boolean = oldItem == newItem
}
data class TestItem(val id: Long)
private fun createLoadParam(loadType: LoadType, key: Int?): PagingSource.LoadParams<Int> = when (loadType) {
LoadType.REFRESH -> PagingSource.LoadParams.Refresh(
key = key,
loadSize = CONFIG.initialLoadSize,
placeholdersEnabled = CONFIG.enablePlaceholders,
)
LoadType.APPEND -> PagingSource.LoadParams.Append(
key = key ?: -1,
loadSize = CONFIG.pageSize,
placeholdersEnabled = CONFIG.enablePlaceholders,
)
LoadType.PREPEND -> PagingSource.LoadParams.Prepend(
key = key ?: -1,
loadSize = CONFIG.pageSize,
placeholdersEnabled = CONFIG.enablePlaceholders,
)
}
private suspend fun PagingSource<Int, TestItem>.refresh(key: Int? = null): LoadResult<Int, TestItem> =
load(createLoadParam(LoadType.REFRESH, key))
private suspend fun PagingSource<Int, TestItem>.append(key: Int?): LoadResult<Int, TestItem> =
load(createLoadParam(LoadType.APPEND, key))
private suspend fun PagingSource<Int, TestItem>.prepend(key: Int?): LoadResult<Int, TestItem> =
load(createLoadParam(LoadType.PREPEND, key))