Skip to content

Commit 5718d5b

Browse files
committed
add tests for searchextractor
1 parent 06c6776 commit 5718d5b

11 files changed

Lines changed: 297 additions & 80 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/ListUrlIdHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ public abstract class ListUrlIdHandler extends UrlIdHandler {
1212

1313
public ListUrlIdHandler setQuery(String id,
1414
List<String> contentFilter,
15-
String softFilter) throws ParsingException {
15+
String sortFilter) throws ParsingException {
1616
setId(id);
1717
this.contentFilter = contentFilter;
18-
this.sortFilter = softFilter;
18+
this.sortFilter = sortFilter;
1919
return this;
2020
}
2121

extractor/src/main/java/org/schabi/newpipe/extractor/StreamingService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ public String toString() {
8888
public abstract PlaylistExtractor getPlaylistExtractor(ListUrlIdHandler urlIdHandler) throws ExtractionException;
8989
public abstract StreamExtractor getStreamExtractor(UrlIdHandler urlIdHandler) throws ExtractionException;
9090

91-
public SearchExtractor getSearchExtractor(String query, List<String> contentFilter, String softFilter, String contentCountry) throws ExtractionException {
92-
return getSearchExtractor(getSearchQueryHandler().setQuery(query, contentFilter, softFilter), contentCountry);
91+
public SearchExtractor getSearchExtractor(String query, List<String> contentFilter, String sortFilter, String contentCountry) throws ExtractionException {
92+
return getSearchExtractor(getSearchQueryHandler().setQuery(query, contentFilter, sortFilter), contentCountry);
9393
}
9494

9595
public ChannelExtractor getChannelExtractor(String id, List<String> contentFilter, String sortFilter) throws ExtractionException {

extractor/src/main/java/org/schabi/newpipe/extractor/search/SearchQueryUrlHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.schabi.newpipe.extractor.search;
22

33
import org.schabi.newpipe.extractor.ListUrlIdHandler;
4-
import org.schabi.newpipe.extractor.UrlIdHandler;
54
import org.schabi.newpipe.extractor.exceptions.ParsingException;
65

76
import java.util.List;

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudQueryUrlHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public String getUrl() throws ParsingException {
2121
try {
2222
String url = "https://api-v2.soundcloud.com/search";
2323

24-
if(getContentFilter().size() > 1) {
24+
if(getContentFilter().size() > 0) {
2525
switch (getContentFilter().get(0)) {
2626
case TRACKS:
2727
url += "/tracks";

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSearchExtractor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionEx
6666
}
6767

6868
@Override
69-
public String getNextPageUrl() throws IOException, ExtractionException {
70-
return getUrl() + "&page=" + Integer.toString( 1);
69+
public String getNextPageUrl() throws ExtractionException {
70+
return getUrl() + "&page=" + Integer.toString( 2);
7171
}
7272

7373
@Override

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/urlIdHandlers/YoutubeSearchQueryUrlHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ public String getUrl() throws ParsingException {
2525
final String url = "https://www.youtube.com/results"
2626
+ "?q=" + URLEncoder.encode(id, CHARSET_UTF_8);
2727

28-
if(getContentFilter().size() > 1) {
28+
if(getContentFilter().size() > 0) {
2929
switch (getContentFilter().get(0)) {
3030
case STREAM: return url + "&sp=EgIQAVAU";
3131
case CHANNEL: return url + "&sp=EgIQAlAU";
3232
case PLAYLIST: return url + "&sp=EgIQA1AU";
3333
case ANY:
34-
default: return url;
34+
default:
3535
}
3636
}
3737

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchExtractorAllTest.java

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.schabi.newpipe.extractor.services.youtube.search;
2+
3+
import org.junit.Test;
4+
import org.schabi.newpipe.extractor.InfoItem;
5+
import org.schabi.newpipe.extractor.ListExtractor;
6+
import org.schabi.newpipe.extractor.services.BaseListExtractorTest;
7+
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeSearchExtractor;
8+
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertTrue;
11+
12+
13+
/*
14+
* Created by Christian Schabesberger on 27.05.18
15+
*
16+
* Copyright (C) Christian Schabesberger 2018 <chris.schabesberger@mailbox.org>
17+
* YoutubeSearchExtractorBaseTest.java is part of NewPipe.
18+
*
19+
* NewPipe is free software: you can redistribute it and/or modify
20+
* it under the terms of the GNU General Public License as published by
21+
* the Free Software Foundation, either version 3 of the License, or
22+
* (at your option) any later version.
23+
*
24+
* NewPipe is distributed in the hope that it will be useful,
25+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27+
* GNU General Public License for more details.
28+
*
29+
* You should have received a copy of the GNU General Public License
30+
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
31+
*/
32+
33+
/**
34+
* Test for {@link YoutubeSearchExtractor}
35+
*/
36+
public abstract class YoutubeSearchExtractorBaseTest {
37+
38+
protected static YoutubeSearchExtractor extractor;
39+
protected static ListExtractor.InfoItemsPage<InfoItem> itemsPage;
40+
41+
42+
@Test
43+
public void testResultListElementsLength() {
44+
assertTrue(Integer.toString(itemsPage.getItems().size()),
45+
itemsPage.getItems().size() > 10);
46+
}
47+
48+
@Test
49+
public void testUrl() throws Exception {
50+
assertTrue(extractor.getUrl(), extractor.getUrl().startsWith("https://www.youtube.com"));
51+
}
52+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.schabi.newpipe.extractor.services.youtube.search;
2+
3+
import org.junit.BeforeClass;
4+
import org.junit.Test;
5+
import org.schabi.newpipe.Downloader;
6+
import org.schabi.newpipe.extractor.InfoItem;
7+
import org.schabi.newpipe.extractor.ListExtractor;
8+
import org.schabi.newpipe.extractor.NewPipe;
9+
import org.schabi.newpipe.extractor.channel.ChannelInfoItem;
10+
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeSearchExtractor;
11+
12+
import static java.util.Arrays.asList;
13+
import static org.junit.Assert.*;
14+
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
15+
16+
public class YoutubeSearchExtractorChannelOnlyTest extends YoutubeSearchExtractorBaseTest {
17+
18+
@BeforeClass
19+
public static void setUpClass() throws Exception {
20+
NewPipe.init(Downloader.getInstance());
21+
extractor = (YoutubeSearchExtractor) YouTube.getSearchExtractor("pewdiepie",
22+
asList(new String[]{"channel"}), null, "de");
23+
extractor.fetchPage();
24+
itemsPage = extractor.getInitialPage();
25+
}
26+
27+
@Test
28+
public void testGetSecondPage() throws Exception {
29+
YoutubeSearchExtractor secondExtractor = (YoutubeSearchExtractor) YouTube.getSearchExtractor("pewdiepie",
30+
asList(new String[]{"channel"}), null, "de");
31+
ListExtractor.InfoItemsPage<InfoItem> secondPage = secondExtractor.getPage(itemsPage.getNextPageUrl());
32+
assertTrue(Integer.toString(secondPage.getItems().size()),
33+
secondPage.getItems().size() > 10);
34+
35+
// check if its the same result
36+
boolean equals = true;
37+
for (int i = 0; i < secondPage.getItems().size()
38+
&& i < itemsPage.getItems().size(); i++) {
39+
if(!secondPage.getItems().get(i).getUrl().equals(
40+
itemsPage.getItems().get(i).getUrl())) {
41+
equals = false;
42+
}
43+
}
44+
assertFalse("First and second page are equal", equals);
45+
46+
assertEquals("https://www.youtube.com/results?q=pewdiepie&sp=EgIQAlAU&page=3", secondPage.getNextPageUrl());
47+
}
48+
49+
@Test
50+
public void testGetSecondPageUrl() throws Exception {
51+
assertEquals("https://www.youtube.com/results?q=pewdiepie&sp=EgIQAlAU&page=2", extractor.getNextPageUrl());
52+
}
53+
54+
@Test
55+
public void testOnlyContainChannels() {
56+
for(InfoItem item : itemsPage.getItems()) {
57+
if(!(item instanceof ChannelInfoItem)) {
58+
fail("The following item is no channel item: " + item.toString());
59+
}
60+
}
61+
}
62+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package org.schabi.newpipe.extractor.services.youtube.search;
2+
3+
import org.junit.BeforeClass;
4+
import org.junit.Test;
5+
import org.schabi.newpipe.Downloader;
6+
import org.schabi.newpipe.extractor.InfoItem;
7+
import org.schabi.newpipe.extractor.ListExtractor;
8+
import org.schabi.newpipe.extractor.NewPipe;
9+
import org.schabi.newpipe.extractor.channel.ChannelInfoItem;
10+
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeSearchExtractor;
11+
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
12+
13+
import static org.junit.Assert.assertEquals;
14+
import static org.junit.Assert.assertFalse;
15+
import static org.junit.Assert.assertTrue;
16+
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
17+
18+
/*
19+
* Created by Christian Schabesberger on 27.05.18
20+
*
21+
* Copyright (C) Christian Schabesberger 2018 <chris.schabesberger@mailbox.org>
22+
* YoutubeSearchExtractorStreamTest.java is part of NewPipe.
23+
*
24+
* NewPipe is free software: you can redistribute it and/or modify
25+
* it under the terms of the GNU General Public License as published by
26+
* the Free Software Foundation, either version 3 of the License, or
27+
* (at your option) any later version.
28+
*
29+
* NewPipe is distributed in the hope that it will be useful,
30+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
31+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32+
* GNU General Public License for more details.
33+
*
34+
* You should have received a copy of the GNU General Public License
35+
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
36+
*/
37+
38+
/**
39+
* Test for {@link YoutubeSearchExtractor}
40+
*/
41+
public class YoutubeSearchExtractorDefaultTest extends YoutubeSearchExtractorBaseTest {
42+
43+
@BeforeClass
44+
public static void setUpClass() throws Exception {
45+
NewPipe.init(Downloader.getInstance());
46+
extractor = (YoutubeSearchExtractor) YouTube.getSearchExtractor("pewdiepie", "de");
47+
extractor.fetchPage();
48+
itemsPage = extractor.getInitialPage();
49+
}
50+
51+
52+
53+
@Test
54+
public void testGetSecondPageUrl() throws Exception {
55+
assertEquals(extractor.getNextPageUrl(), "https://www.youtube.com/results?q=pewdiepie&page=2");
56+
}
57+
58+
@Test
59+
public void testResultList_FirstElement() {
60+
InfoItem firstInfoItem = itemsPage.getItems().get(0);
61+
62+
// THe channel should be the first item
63+
assertTrue(firstInfoItem instanceof ChannelInfoItem);
64+
assertEquals("name", "PewDiePie", firstInfoItem.getName());
65+
assertEquals("url","https://www.youtube.com/user/PewDiePie", firstInfoItem.getUrl());
66+
}
67+
68+
@Test
69+
public void testResultListCheckIfContainsStreamItems() {
70+
boolean hasStreams = false;
71+
for(InfoItem item : itemsPage.getItems()) {
72+
if(item instanceof StreamInfoItem) {
73+
hasStreams = true;
74+
}
75+
}
76+
assertTrue("Has no InfoItemStreams", hasStreams);
77+
}
78+
79+
@Test
80+
public void testGetSecondPage() throws Exception {
81+
YoutubeSearchExtractor secondExtractor =
82+
(YoutubeSearchExtractor) YouTube.getSearchExtractor("pewdiepie", "de");
83+
ListExtractor.InfoItemsPage<InfoItem> secondPage = secondExtractor.getPage(itemsPage.getNextPageUrl());
84+
assertTrue(Integer.toString(secondPage.getItems().size()),
85+
secondPage.getItems().size() > 10);
86+
87+
// check if its the same result
88+
boolean equals = true;
89+
for (int i = 0; i < secondPage.getItems().size()
90+
&& i < itemsPage.getItems().size(); i++) {
91+
if(!secondPage.getItems().get(i).getUrl().equals(
92+
itemsPage.getItems().get(i).getUrl())) {
93+
equals = false;
94+
}
95+
}
96+
assertFalse("First and second page are equal", equals);
97+
98+
assertEquals("https://www.youtube.com/results?q=pewdiepie&page=3", secondPage.getNextPageUrl());
99+
}
100+
101+
@Test
102+
public void testSuggestionNotNull() throws Exception {
103+
//todo write a real test
104+
assertTrue(extractor.getSearchSuggestion() != null);
105+
}
106+
107+
108+
@Test
109+
public void testId() throws Exception {
110+
assertEquals(extractor.getId(), "pewdiepie");
111+
}
112+
113+
@Test
114+
public void testName() {
115+
assertEquals(extractor.getName(), "pewdiepie");
116+
}
117+
}

0 commit comments

Comments
 (0)