Test cases readability: change expected format from position to the actual queue

(list of IDs), to make the test case more readable.
This commit is contained in:
orionlee 2018-05-23 16:56:46 -07:00
parent 97905e5ed4
commit fb824b541d

View File

@ -15,6 +15,7 @@ import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import de.danoeh.antennapod.core.feed.FeedFile;
import de.danoeh.antennapod.core.feed.FeedItem;
@ -38,17 +39,23 @@ public class DBWriterTest {
return Arrays.asList(new Object[][]{
{"case default, i.e., add to the end",
QUEUE_DEFAULT.size(), optDefault, 0, QUEUE_DEFAULT},
concat(QUEUE_DEFAULT_IDS, TFI_ID),
optDefault, 0, QUEUE_DEFAULT},
{"case default (2nd item)",
QUEUE_DEFAULT.size(), optDefault, 1, QUEUE_DEFAULT},
concat(QUEUE_DEFAULT_IDS, TFI_ID),
optDefault, 1, QUEUE_DEFAULT},
{"case option enqueue at front",
0, optEnqAtFront, 0, QUEUE_DEFAULT},
concat(TFI_ID, QUEUE_DEFAULT_IDS),
optEnqAtFront, 0, QUEUE_DEFAULT},
{"case option enqueue at front (2nd item)",
1, optEnqAtFront, 1, QUEUE_DEFAULT},
list(11L, TFI_ID, 12L, 13L, 14L),
optEnqAtFront, 1, QUEUE_DEFAULT},
{"case empty queue, option default",
0, optDefault, 0, QUEUE_EMPTY},
list(TFI_ID),
optDefault, 0, QUEUE_EMPTY},
{"case empty queue, option enqueue at front",
0, optEnqAtFront, 0, QUEUE_EMPTY},
list(TFI_ID),
optEnqAtFront, 0, QUEUE_EMPTY},
});
}
@ -56,7 +63,7 @@ public class DBWriterTest {
public String message;
@Parameter(1)
public int posExpected;
public List<Long> idsExpected;
@Parameter(2)
public Options options;
@ -68,17 +75,22 @@ public class DBWriterTest {
public List<FeedItem> curQueue;
public static final int TFI_TO_ADD_ID = 101;
public static final long TFI_ID = 101;
/**
* Add a FeedItem with ID {@link #TFI_TO_ADD_ID} with the setup
* Add a FeedItem with ID {@link #TFI_ID} with the setup
*/
@Test
public void test() {
ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options);
int posActual = calculator.calcPosition(posAmongAdded, tFI(TFI_TO_ADD_ID), curQueue);
assertEquals(message, posExpected, posActual);
// shallow copy to which the test will add items
List<FeedItem> queue = new ArrayList<>(curQueue);
FeedItem tFI = tFI(TFI_ID);
int posActual = calculator.calcPosition(posAmongAdded, tFI, queue);
queue.add(posActual, tFI);
assertEquals(message, idsExpected, toIDs(queue));
}
}
@ -95,23 +107,31 @@ public class DBWriterTest {
return Arrays.asList(new Object[][]{
{"case option keep in progress at front",
1, optKeepInProgressAtFront, 0, QUEUE_FRONT_IN_PROGRESS},
list(11L, TFI_ID, 12L, 13L),
optKeepInProgressAtFront, 0, QUEUE_FRONT_IN_PROGRESS},
{"case option keep in progress at front (2nd item)",
2, optKeepInProgressAtFront, 1, QUEUE_FRONT_IN_PROGRESS},
list(11L, 12L, TFI_ID, 13L),
optKeepInProgressAtFront, 1, QUEUE_FRONT_IN_PROGRESS},
{"case option keep in progress at front, front item not in progress",
0, optKeepInProgressAtFront, 0, QUEUE_DEFAULT},
concat(TFI_ID, QUEUE_DEFAULT_IDS),
optKeepInProgressAtFront, 0, QUEUE_DEFAULT},
{"case option keep in progress at front, front item no media at all",
0, optKeepInProgressAtFront, 0, QUEUE_FRONT_NO_MEDIA}, // No media should not cause any exception
concat(TFI_ID, QUEUE_FRONT_NO_MEDIA_IDS),
optKeepInProgressAtFront, 0, QUEUE_FRONT_NO_MEDIA}, // No media should not cause any exception
{"case option keep in progress at front, but enqueue at front is disabled",
QUEUE_FRONT_IN_PROGRESS.size(), optKeepInProgressAtFrontWithNoEnqueueAtFront, 0, QUEUE_FRONT_IN_PROGRESS},
concat(QUEUE_FRONT_IN_PROGRESS_IDS, TFI_ID),
optKeepInProgressAtFrontWithNoEnqueueAtFront, 0, QUEUE_FRONT_IN_PROGRESS},
{"case empty queue, option keep in progress at front",
0, optKeepInProgressAtFront, 0, QUEUE_EMPTY},
list(TFI_ID),
optKeepInProgressAtFront, 0, QUEUE_EMPTY},
});
}
private static final List<FeedItem> QUEUE_FRONT_IN_PROGRESS = Arrays.asList(tFI(11, 60000), tFI(12), tFI(13));
private static final List<Long> QUEUE_FRONT_IN_PROGRESS_IDS = toIDs(QUEUE_FRONT_IN_PROGRESS);
private static final List<FeedItem> QUEUE_FRONT_NO_MEDIA = Arrays.asList(tFINoMedia(11), tFI(12), tFI(13));
private static final List<Long> QUEUE_FRONT_NO_MEDIA_IDS = toIDs(QUEUE_FRONT_NO_MEDIA);
}
@ -123,14 +143,20 @@ public class DBWriterTest {
Options optDefault = new Options();
Options optEnqAtFront = new Options().setEnqueueAtFront(true);
// Attempts to make test more readable by showing the expected list of ids
// (rather than the expected positions)
return Arrays.asList(new Object[][] {
{"download order test, enqueue default",
QUEUE_DEFAULT.size(), QUEUE_DEFAULT.size() + 1,
QUEUE_DEFAULT.size() + 2, QUEUE_DEFAULT.size() + 3,
concat(QUEUE_DEFAULT_IDS, 101L),
concat(QUEUE_DEFAULT_IDS, list(101L, 102L)),
concat(QUEUE_DEFAULT_IDS, list(101L, 102L, 201L)),
concat(QUEUE_DEFAULT_IDS, list(101L, 102L, 201L, 202L)),
optDefault, QUEUE_DEFAULT},
{"download order test, enqueue at front",
0, 1,
2, 3,
concat(101L, QUEUE_DEFAULT_IDS),
concat(list(101L, 102L), QUEUE_DEFAULT_IDS),
concat(list(101L, 102L, 201L), QUEUE_DEFAULT_IDS),
concat(list(101L, 102L, 201L, 202L), QUEUE_DEFAULT_IDS),
optEnqAtFront, QUEUE_DEFAULT},
});
}
@ -139,17 +165,17 @@ public class DBWriterTest {
public String message;
@Parameter(1)
public int pos101Expected;
public List<Long> idsExpectedAfter101;
@Parameter(2)
public int pos102Expected;
public List<Long> idsExpectedAfter102;
// 2XX are for testing bulk insertion cases
@Parameter(3)
public int pos201Expected;
public List<Long> idsExpectedAfter201;
@Parameter(4)
public int pos202Expected;
public List<Long> idsExpectedAfter202;
@Parameter(5)
public Options options;
@ -179,26 +205,28 @@ public class DBWriterTest {
int pos101Actual = calculator.calcPosition(0, tFI101, queue);
queue.add(pos101Actual, tFI101);
assertEquals(message + " (1st download)",
pos101Expected, pos101Actual);
idsExpectedAfter101, toIDs(queue));
// Then user clicks download on feed item 102
FeedItem tFI102 = tFI_isDownloading(102, mockDownloadRequester);
int pos102Actual = calculator.calcPosition(0, tFI102, queue);
queue.add(pos102Actual, tFI102);
assertEquals(message + " (2nd download, it should preserve order of download)",
pos102Expected, pos102Actual);
idsExpectedAfter102, toIDs(queue));
// Items 201 and 202 are added as part of a single DBWriter.addQueueItem() calls
FeedItem tFI201 = tFI_isDownloading(201, mockDownloadRequester);
int pos201Actual = calculator.calcPosition(0, tFI201, queue);
queue.add(pos201Actual, tFI201);
assertEquals(message + " (bulk insertion, 1st item)", pos201Expected, pos201Actual);
assertEquals(message + " (bulk insertion, 1st item)",
idsExpectedAfter201, toIDs(queue));
FeedItem tFI202 = tFI_isDownloading(202, mockDownloadRequester);
int pos202Actual = calculator.calcPosition(1, tFI202, queue);
queue.add(pos202Actual, tFI202);
assertEquals(message + " (bulk insertion, 2nd item)", pos202Expected, pos202Actual);
assertEquals(message + " (bulk insertion, 2nd item)",
idsExpectedAfter202, toIDs(queue));
// TODO: simulate download failure cases.
}
@ -242,13 +270,14 @@ public class DBWriterTest {
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<Long> QUEUE_DEFAULT_IDS = QUEUE_DEFAULT.stream().map(fi -> fi.getId()).collect(Collectors.toList());
static FeedItem tFI(int id) {
static FeedItem tFI(long id) {
return tFI(id, -1);
}
static FeedItem tFI(int id, int position) {
static FeedItem tFI(long id, int position) {
FeedItem item = tFINoMedia(id);
FeedMedia media = new FeedMedia(item, "download_url", 1234567, "audio/mpeg");
media.setId(item.getId());
@ -261,11 +290,41 @@ public class DBWriterTest {
return item;
}
static FeedItem tFINoMedia(int id) {
static FeedItem tFINoMedia(long id) {
FeedItem item = new FeedItem(id, "Item" + id, "ItemId" + id, "url",
new Date(), FeedItem.PLAYED, FeedMother.anyFeed());
return item;
}
// Collections helpers
static <T> List<? extends T> concat(T item, List<? extends T> list) {
List<T> res = new ArrayList<>(list);
res.add(0, item);
return res;
}
static <T> List<? extends T> concat(List<? extends T> list, T item) {
List<T> res = new ArrayList<>(list);
res.add(item);
return res;
}
static <T> List<? extends T> concat(List<? extends T> list1, List<? extends T> list2) {
List<T> res = new ArrayList<>(list1);
res.addAll(list2);
return res;
}
public static <T> List<T> list(T... a) {
return Arrays.asList(a);
}
static List<Long> toIDs(List<FeedItem> items) {
return items.stream().map(i->i.getId()).collect(Collectors.toList());
}
}
}