refactoring test - factor out common operations of calc position,

add to queue and verify result into common helper.
This commit is contained in:
orionlee 2018-05-23 22:13:01 -07:00
parent fb824b541d
commit ce5aa26878
1 changed files with 26 additions and 21 deletions

View File

@ -86,11 +86,10 @@ public class DBWriterTest {
// shallow copy to which the test will add items // shallow copy to which the test will add items
List<FeedItem> queue = new ArrayList<>(curQueue); List<FeedItem> queue = new ArrayList<>(curQueue);
FeedItem tFI = tFI(TFI_ID); FeedItem tFI = tFI(TFI_ID);
int posActual = calculator.calcPosition(posAmongAdded, tFI, queue); doAddToQueueAndAssertResult(message,
queue.add(posActual, tFI); calculator, posAmongAdded, tFI, queue,
assertEquals(message, idsExpected, toIDs(queue)); idsExpected);
} }
} }
@ -201,32 +200,27 @@ public class DBWriterTest {
// User clicks download on feed item 101 // User clicks download on feed item 101
FeedItem tFI101 = tFI_isDownloading(101, mockDownloadRequester); FeedItem tFI101 = tFI_isDownloading(101, mockDownloadRequester);
doAddToQueueAndAssertResult(message + " (1st download)",
int pos101Actual = calculator.calcPosition(0, tFI101, queue); calculator, 0, tFI101, queue,
queue.add(pos101Actual, tFI101); idsExpectedAfter101);
assertEquals(message + " (1st download)",
idsExpectedAfter101, toIDs(queue));
// Then user clicks download on feed item 102 // Then user clicks download on feed item 102
FeedItem tFI102 = tFI_isDownloading(102, mockDownloadRequester); FeedItem tFI102 = tFI_isDownloading(102, mockDownloadRequester);
int pos102Actual = calculator.calcPosition(0, tFI102, queue); doAddToQueueAndAssertResult(message + " (2nd download, it should preserve order of download)",
queue.add(pos102Actual, tFI102); calculator, 0, tFI102, queue,
assertEquals(message + " (2nd download, it should preserve order of download)", idsExpectedAfter102);
idsExpectedAfter102, toIDs(queue));
// Items 201 and 202 are added as part of a single DBWriter.addQueueItem() calls // Items 201 and 202 are added as part of a single DBWriter.addQueueItem() calls
FeedItem tFI201 = tFI_isDownloading(201, mockDownloadRequester); FeedItem tFI201 = tFI_isDownloading(201, mockDownloadRequester);
int pos201Actual = calculator.calcPosition(0, tFI201, queue); doAddToQueueAndAssertResult(message + " (bulk insertion, 1st item)",
queue.add(pos201Actual, tFI201); calculator, 0, tFI201, queue,
assertEquals(message + " (bulk insertion, 1st item)", idsExpectedAfter201);
idsExpectedAfter201, toIDs(queue));
FeedItem tFI202 = tFI_isDownloading(202, mockDownloadRequester); FeedItem tFI202 = tFI_isDownloading(202, mockDownloadRequester);
int pos202Actual = calculator.calcPosition(1, tFI202, queue); doAddToQueueAndAssertResult(message + " (bulk insertion, 2nd item)",
queue.add(pos202Actual, tFI202); calculator, 0, tFI202, queue,
assertEquals(message + " (bulk insertion, 2nd item)", idsExpectedAfter202);
idsExpectedAfter202, toIDs(queue));
// TODO: simulate download failure cases. // TODO: simulate download failure cases.
} }
@ -267,6 +261,17 @@ public class DBWriterTest {
// - common queue (of items) for tests // - common queue (of items) for tests
// - construct FeedItems for tests // - construct FeedItems for tests
static void doAddToQueueAndAssertResult(String message,
ItemEnqueuePositionCalculator calculator,
int positionAmongAdd,
FeedItem itemToAdd,
List<FeedItem> queue,
List<Long> idsExpected) {
int posActual = calculator.calcPosition(positionAmongAdd, itemToAdd, queue);
queue.add(posActual, itemToAdd);
assertEquals(message, idsExpected, toIDs(queue));
}
static final List<FeedItem> QUEUE_EMPTY = Collections.unmodifiableList(Arrays.asList()); static final List<FeedItem> QUEUE_EMPTY = Collections.unmodifiableList(Arrays.asList());
static final List<FeedItem> QUEUE_DEFAULT = Collections.unmodifiableList(Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14))); static final List<FeedItem> QUEUE_DEFAULT = Collections.unmodifiableList(Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14)));