Skip to content

Commit 9704fc9

Browse files
committed
Improve search extractor tests for services
1 parent d72130e commit 9704fc9

21 files changed

Lines changed: 653 additions & 900 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.schabi.newpipe.extractor.services;
2+
3+
@SuppressWarnings("unused")
4+
public interface BaseSearchExtractorTest extends BaseListExtractorTest {
5+
void testSearchString() throws Exception;
6+
void testSearchSuggestion() throws Exception;
7+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.schabi.newpipe.extractor.services;
2+
3+
import org.junit.Test;
4+
import org.schabi.newpipe.extractor.Extractor;
5+
import org.schabi.newpipe.extractor.StreamingService;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertTrue;
9+
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
10+
11+
public abstract class DefaultExtractorTest<T extends Extractor> implements BaseExtractorTest {
12+
public abstract T extractor() throws Exception;
13+
14+
public abstract StreamingService expectedService() throws Exception;
15+
public abstract String expectedName() throws Exception;
16+
public abstract String expectedId() throws Exception;
17+
public abstract String expectedUrlContains() throws Exception;
18+
public abstract String expectedOriginalUrlContains() throws Exception;
19+
20+
@Test
21+
@Override
22+
public void testServiceId() throws Exception {
23+
assertEquals(expectedService().getServiceId(), extractor().getServiceId());
24+
}
25+
26+
@Test
27+
@Override
28+
public void testName() throws Exception {
29+
assertEquals(expectedName(), extractor().getName());
30+
}
31+
32+
@Test
33+
@Override
34+
public void testId() throws Exception {
35+
assertEquals(expectedId(), extractor().getId());
36+
}
37+
38+
@Test
39+
@Override
40+
public void testUrl() throws Exception {
41+
final String url = extractor().getUrl();
42+
final String expectedContains = expectedUrlContains();
43+
44+
assertIsSecureUrl(url);
45+
assertTrue("Url \"" + url + "\" doesn't contains \"" + expectedContains + "\"",
46+
url.contains(expectedContains));
47+
}
48+
49+
@Test
50+
@Override
51+
public void testOriginalUrl() throws Exception {
52+
final String originalUrl = extractor().getOriginalUrl();
53+
final String expectedContains = expectedOriginalUrlContains();
54+
55+
assertIsSecureUrl(originalUrl);
56+
assertTrue("Original url \"" + originalUrl + "\" doesn't contains \"" + expectedContains + "\"",
57+
originalUrl.contains(expectedContains));
58+
}
59+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.schabi.newpipe.extractor.services;
2+
3+
import org.junit.Test;
4+
import org.schabi.newpipe.extractor.InfoItem;
5+
import org.schabi.newpipe.extractor.ListExtractor;
6+
7+
import javax.annotation.Nullable;
8+
9+
import static org.schabi.newpipe.extractor.services.DefaultTests.*;
10+
11+
public abstract class DefaultListExtractorTest<T extends ListExtractor<? extends InfoItem>> extends DefaultExtractorTest<T>
12+
implements BaseListExtractorTest {
13+
14+
@Nullable
15+
public InfoItem.InfoType expectedInfoItemType() {
16+
return null;
17+
}
18+
19+
public boolean expectedHasMoreItems() {
20+
return true;
21+
}
22+
23+
@Test
24+
@Override
25+
public void testRelatedItems() throws Exception {
26+
final ListExtractor<? extends InfoItem> extractor = extractor();
27+
28+
final InfoItem.InfoType expectedType = expectedInfoItemType();
29+
final ListExtractor.InfoItemsPage<? extends InfoItem> items = defaultTestRelatedItems(extractor);
30+
if (expectedType != null) {
31+
assertOnlyContainsType(items, expectedType);
32+
}
33+
}
34+
35+
@Test
36+
@Override
37+
public void testMoreRelatedItems() throws Exception {
38+
final ListExtractor<? extends InfoItem> extractor = extractor();
39+
40+
if (expectedHasMoreItems()) {
41+
final InfoItem.InfoType expectedType = expectedInfoItemType();
42+
final ListExtractor.InfoItemsPage<? extends InfoItem> items = defaultTestMoreItems(extractor);
43+
if (expectedType != null) {
44+
assertOnlyContainsType(items, expectedType);
45+
}
46+
} else {
47+
assertNoMoreItems(extractor);
48+
}
49+
}
50+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.schabi.newpipe.extractor.services;
2+
3+
import org.junit.Test;
4+
import org.schabi.newpipe.extractor.search.SearchExtractor;
5+
6+
7+
import javax.annotation.Nullable;
8+
9+
import static org.junit.Assert.assertEquals;
10+
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertEmpty;
11+
12+
public abstract class DefaultSearchExtractorTest extends DefaultListExtractorTest<SearchExtractor>
13+
implements BaseSearchExtractorTest {
14+
15+
public abstract String expectedSearchString();
16+
@Nullable public abstract String expectedSearchSuggestion();
17+
18+
@Test
19+
@Override
20+
public void testSearchString() throws Exception {
21+
assertEquals(expectedSearchString(), extractor().getSearchString());
22+
}
23+
24+
@Test
25+
@Override
26+
public void testSearchSuggestion() throws Exception {
27+
final String expectedSearchSuggestion = expectedSearchSuggestion();
28+
if (expectedSearchSuggestion == null || expectedSearchSuggestion.isEmpty()) {
29+
assertEmpty("Suggestion was expected to be empty", extractor().getSearchSuggestion());
30+
} else {
31+
assertEquals(expectedSearchSuggestion, extractor().getSearchSuggestion());
32+
}
33+
}
34+
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@
22

33
import org.schabi.newpipe.extractor.InfoItem;
44
import org.schabi.newpipe.extractor.ListExtractor;
5-
import org.schabi.newpipe.extractor.NewPipe;
65
import org.schabi.newpipe.extractor.StreamingService;
76
import org.schabi.newpipe.extractor.channel.ChannelInfoItem;
87
import org.schabi.newpipe.extractor.exceptions.ParsingException;
9-
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
10-
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
118
import org.schabi.newpipe.extractor.localization.DateWrapper;
129
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem;
1310
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
1411

1512
import java.util.Calendar;
13+
import java.util.HashSet;
1614
import java.util.List;
15+
import java.util.Set;
1716

1817
import static junit.framework.TestCase.assertFalse;
1918
import static org.junit.Assert.*;
2019
import static org.schabi.newpipe.extractor.ExtractorAsserts.*;
21-
import static org.schabi.newpipe.extractor.StreamingService.*;
20+
import static org.schabi.newpipe.extractor.StreamingService.LinkType;
2221

2322
public final class DefaultTests {
2423
public static void defaultTestListOfItems(StreamingService expectedService, List<? extends InfoItem> itemsList, List<Throwable> errors) throws ParsingException {
@@ -71,12 +70,38 @@ private static void assertExpectedLinkType(StreamingService expectedService, Str
7170
expectedLinkType, linkTypeByUrl);
7271
}
7372

73+
public static void assertOnlyContainsType(ListExtractor.InfoItemsPage<? extends InfoItem> items, InfoItem.InfoType expectedType) {
74+
for (InfoItem item : items.getItems()) {
75+
assertEquals("Item list contains unexpected info types",
76+
expectedType, item.getInfoType());
77+
}
78+
}
79+
7480
public static <T extends InfoItem> void assertNoMoreItems(ListExtractor<T> extractor) throws Exception {
7581
assertFalse("More items available when it shouldn't", extractor.hasNextPage());
7682
final String nextPageUrl = extractor.getNextPageUrl();
7783
assertTrue("Next page is not empty or null", nextPageUrl == null || nextPageUrl.isEmpty());
7884
}
7985

86+
public static void assertNoDuplicatedItems(StreamingService expectedService,
87+
ListExtractor.InfoItemsPage<InfoItem> page1,
88+
ListExtractor.InfoItemsPage<InfoItem> page2) throws Exception {
89+
defaultTestListOfItems(expectedService, page1.getItems(), page1.getErrors());
90+
defaultTestListOfItems(expectedService, page2.getItems(), page2.getErrors());
91+
92+
final Set<String> urlsSet = new HashSet<>();
93+
for (InfoItem item : page1.getItems()) {
94+
urlsSet.add(item.getUrl());
95+
}
96+
97+
for (InfoItem item : page2.getItems()) {
98+
final boolean wasAdded = urlsSet.add(item.getUrl());
99+
if (!wasAdded) {
100+
fail("Same item was on the first and second page item list");
101+
}
102+
}
103+
}
104+
80105
public static <T extends InfoItem> ListExtractor.InfoItemsPage<T> defaultTestRelatedItems(ListExtractor<T> extractor) throws Exception {
81106
final ListExtractor.InfoItemsPage<T> page = extractor.getInitialPage();
82107
final List<T> itemsList = page.getItems();

extractor/src/test/java/org/schabi/newpipe/extractor/services/media_ccc/MediaCCCSearchExtractorAllTest.java

Lines changed: 0 additions & 58 deletions
This file was deleted.

extractor/src/test/java/org/schabi/newpipe/extractor/services/media_ccc/MediaCCCSearchExtractorConferencesTest.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)