Add better tests for youtube search items

This commit is contained in:
Coffeemakr 2018-01-04 18:24:09 +01:00
parent 0ffd4d9743
commit 06ea74cbb8
No known key found for this signature in database
GPG Key ID: 3F35676D8FF6E743
5 changed files with 64 additions and 72 deletions

View File

@ -0,0 +1,55 @@
package org.schabi.newpipe.extractor.services.youtube;
import org.junit.Test;
import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.channel.ChannelInfoItem;
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem;
import org.schabi.newpipe.extractor.search.SearchResult;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
public abstract class BaseYoutubeSearchTest {
protected static SearchResult result;
@Test
public void testResultList() {
assertFalse("Got empty result list", result.resultList.isEmpty());
for(InfoItem infoItem: result.resultList) {
assertIsSecureUrl(infoItem.getUrl());
assertIsSecureUrl(infoItem.getThumbnailUrl());
assertFalse(infoItem.getName().isEmpty());
assertFalse("Name is probably a URI: " + infoItem.getName(),
infoItem.getName().contains("://"));
if(infoItem instanceof StreamInfoItem) {
// test stream item
StreamInfoItem streamInfoItem = (StreamInfoItem) infoItem;
assertIsSecureUrl(streamInfoItem.getUploaderUrl());
assertFalse(streamInfoItem.getUploadDate().isEmpty());
assertFalse(streamInfoItem.getUploaderName().isEmpty());
} else if(infoItem instanceof ChannelInfoItem) {
// Nothing special to check?
} else if(infoItem instanceof PlaylistInfoItem) {
// test playlist item
long streamCount = ((PlaylistInfoItem) infoItem).getStreamCount();
assertTrue(streamCount > 0);
} else {
fail("Unknown infoItem type: " + infoItem);
}
}
}
@Test
public void testResultErrors() {
assertNotNull(result.errors);
if (!result.errors.isEmpty()) {
for (Throwable error : result.errors) {
error.printStackTrace();
}
}
assertTrue(result.errors.isEmpty());
}
}

View File

@ -40,8 +40,7 @@ import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsValidUrl;
/** /**
* Test for {@link SearchEngine} * Test for {@link SearchEngine}
*/ */
public class YoutubeSearchEngineAllTest { public class YoutubeSearchEngineAllTest extends BaseYoutubeSearchTest {
private static SearchResult result;
@BeforeClass @BeforeClass
public static void setUpClass() throws Exception { public static void setUpClass() throws Exception {
@ -53,29 +52,13 @@ public class YoutubeSearchEngineAllTest {
} }
@Test @Test
public void testResultList() { public void testResultList_FirstElement() {
final List<InfoItem> results = result.getResults(); InfoItem firstInfoItem = result.getResults().get(0);
assertFalse("Results are empty: " + results, results.isEmpty());
InfoItem firstInfoItem = results.get(0);
// THe channel should be the first item // THe channel should be the first item
assertTrue(firstInfoItem instanceof ChannelInfoItem); assertTrue(firstInfoItem instanceof ChannelInfoItem);
assertEquals("name", "PewDiePie", firstInfoItem.name); assertEquals("name", "PewDiePie", firstInfoItem.name);
assertEquals("url","https://www.youtube.com/user/PewDiePie", firstInfoItem.url); assertEquals("url","https://www.youtube.com/user/PewDiePie", firstInfoItem.url);
for(InfoItem item: results) {
assertIsValidUrl(item.url);
}
}
@Test
public void testResultErrors() {
for (Throwable error : result.getErrors()) {
error.printStackTrace();
}
assertTrue(result.getErrors().isEmpty());
} }
@Ignore @Ignore

View File

@ -37,8 +37,7 @@ import static org.schabi.newpipe.extractor.ServiceList.YouTube;
/** /**
* Test for {@link SearchEngine} * Test for {@link SearchEngine}
*/ */
public class YoutubeSearchEngineChannelTest { public class YoutubeSearchEngineChannelTest extends BaseYoutubeSearchTest {
private static SearchResult result;
@BeforeClass @BeforeClass
public static void setUp() throws Exception { public static void setUp() throws Exception {
@ -51,14 +50,6 @@ public class YoutubeSearchEngineChannelTest {
.getSearchResult(); .getSearchResult();
} }
@Test
public void testResultList() {
assertFalse(result.resultList.isEmpty());
for(InfoItem item: result.getResults()) {
assertIsValidUrl(item.url);
}
}
@Test @Test
public void testResultsItemType() { public void testResultsItemType() {
for (InfoItem infoItem : result.resultList) { for (InfoItem infoItem : result.resultList) {
@ -66,13 +57,6 @@ public class YoutubeSearchEngineChannelTest {
} }
} }
@Test
public void testResultErrors() {
assertNotNull(result.errors);
if (!result.errors.isEmpty()) for (Throwable error : result.errors) error.printStackTrace();
assertTrue(result.errors.isEmpty());
}
@Ignore @Ignore
@Test @Test
public void testSuggestion() { public void testSuggestion() {

View File

@ -6,6 +6,7 @@ import org.junit.Test;
import org.schabi.newpipe.Downloader; import org.schabi.newpipe.Downloader;
import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem;
import org.schabi.newpipe.extractor.search.SearchEngine; import org.schabi.newpipe.extractor.search.SearchEngine;
import org.schabi.newpipe.extractor.search.SearchResult; import org.schabi.newpipe.extractor.search.SearchResult;
@ -37,8 +38,7 @@ import static org.schabi.newpipe.extractor.ServiceList.YouTube;
/** /**
* Test for {@link SearchEngine} * Test for {@link SearchEngine}
*/ */
public class YoutubeSearchEnginePlaylistTest { public class YoutubeSearchEnginePlaylistTest extends BaseYoutubeSearchTest {
private static SearchResult result;
@BeforeClass @BeforeClass
public static void setUp() throws Exception { public static void setUp() throws Exception {
@ -52,27 +52,13 @@ public class YoutubeSearchEnginePlaylistTest {
} }
@Test @Test
public void testResultList() { public void testInfoItemType() {
assertFalse(result.resultList.isEmpty());
for(InfoItem item: result.getResults()) {
assertIsValidUrl(item.url);
}
}
@Test
public void testUserItemType() {
for (InfoItem infoItem : result.resultList) { for (InfoItem infoItem : result.resultList) {
assertTrue(infoItem instanceof PlaylistInfoItem);
assertEquals(InfoItem.InfoType.PLAYLIST, infoItem.info_type); assertEquals(InfoItem.InfoType.PLAYLIST, infoItem.info_type);
} }
} }
@Test
public void testResultErrors() {
assertNotNull(result.errors);
if (!result.errors.isEmpty()) for (Throwable error : result.errors) error.printStackTrace();
assertTrue(result.errors.isEmpty());
}
@Ignore @Ignore
@Test @Test
public void testSuggestion() { public void testSuggestion() {

View File

@ -37,8 +37,7 @@ import static org.schabi.newpipe.extractor.ServiceList.YouTube;
/** /**
* Test for {@link SearchEngine} * Test for {@link SearchEngine}
*/ */
public class YoutubeSearchEngineStreamTest { public class YoutubeSearchEngineStreamTest extends BaseYoutubeSearchTest {
private static SearchResult result;
@BeforeClass @BeforeClass
public static void setUp() throws Exception { public static void setUp() throws Exception {
@ -51,14 +50,6 @@ public class YoutubeSearchEngineStreamTest {
.getSearchResult(); .getSearchResult();
} }
@Test
public void testResultList() {
assertFalse(result.resultList.isEmpty());
for(InfoItem item: result.getResults()) {
assertIsValidUrl(item.url);
}
}
@Test @Test
public void testResultsItemType() { public void testResultsItemType() {
for (InfoItem infoItem : result.resultList) { for (InfoItem infoItem : result.resultList) {
@ -66,13 +57,6 @@ public class YoutubeSearchEngineStreamTest {
} }
} }
@Test
public void testResultErrors() {
assertNotNull(result.errors);
if (!result.errors.isEmpty()) for (Throwable error : result.errors) error.printStackTrace();
assertTrue(result.errors.isEmpty());
}
@Ignore @Ignore
@Test @Test
public void testSuggestion() { public void testSuggestion() {