Skip to content

Commit 020acfe

Browse files
committed
Fixed tests and added new ones
1 parent b310922 commit 020acfe

1 file changed

Lines changed: 67 additions & 4 deletions

File tree

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeCommentsExtractorTest.java

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
import org.schabi.newpipe.extractor.comments.CommentsInfo;
1010
import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
1111
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
12+
import org.schabi.newpipe.extractor.localization.Localization;
1213
import org.schabi.newpipe.extractor.services.DefaultTests;
1314
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeCommentsExtractor;
1415
import org.schabi.newpipe.extractor.utils.Utils;
1516

1617
import java.io.IOException;
1718
import java.util.List;
19+
import java.util.Locale;
1820
import java.util.Random;
1921

2022
import static org.junit.Assert.assertEquals;
@@ -99,7 +101,6 @@ public void testGetCommentsAllData() throws IOException, ExtractionException {
99101
assertFalse(Utils.isBlank(c.getThumbnailUrl()));
100102
assertFalse(Utils.isBlank(c.getUrl()));
101103
assertTrue(c.getLikeCount() >= 0);
102-
assertFalse(Utils.isBlank(c.getTextualVoteCount()));
103104
}
104105
}
105106

@@ -150,7 +151,6 @@ public void testGetCommentsAllData() throws IOException, ExtractionException {
150151
assertFalse(Utils.isBlank(c.getThumbnailUrl()));
151152
assertFalse(Utils.isBlank(c.getUrl()));
152153
assertTrue(c.getLikeCount() >= 0);
153-
assertFalse(Utils.isBlank(c.getTextualVoteCount()));
154154
if (c.getCommentId().equals("Ugga_h1-EXdHB3gCoAEC")) { // comment without text
155155
assertTrue(Utils.isBlank(c.getCommentText()));
156156
} else {
@@ -194,7 +194,6 @@ public void testGetCommentsAllData() throws IOException, ExtractionException {
194194
assertFalse(Utils.isBlank(c.getThumbnailUrl()));
195195
assertFalse(Utils.isBlank(c.getUrl()));
196196
assertTrue(c.getLikeCount() >= 0);
197-
assertFalse(Utils.isBlank(c.getTextualVoteCount()));
198197
assertFalse(Utils.isBlank(c.getCommentText()));
199198
if (c.isHeartedByUploader()) {
200199
heartedByUploader = true;
@@ -235,12 +234,76 @@ public void testGetCommentsAllData() throws IOException, ExtractionException {
235234
assertNotNull(c.getUploadDate());
236235
assertFalse(Utils.isBlank(c.getThumbnailUrl()));
237236
assertFalse(Utils.isBlank(c.getUrl()));
238-
assertFalse(Utils.isBlank(c.getTextualVoteCount()));
239237
assertTrue(c.getLikeCount() >= 0);
240238
assertFalse(Utils.isBlank(c.getCommentText()));
241239
}
242240

243241
assertTrue("First comment isn't pinned", comments.getItems().get(0).isPinned());
244242
}
245243
}
244+
245+
/**
246+
* Checks if the likes/votes are handled correctly<br/>
247+
* A pinned comment with >15K likes is used for the test
248+
*/
249+
public static class LikesVotes {
250+
private final static String url = "https://www.youtube.com/watch?v=QqsLTNkzvaY";
251+
private static YoutubeCommentsExtractor extractor;
252+
253+
@BeforeClass
254+
public static void setUp() throws Exception {
255+
YoutubeParsingHelper.resetClientVersionAndKey();
256+
YoutubeParsingHelper.setNumberGenerator(new Random(1));
257+
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "likes"));
258+
extractor = (YoutubeCommentsExtractor) YouTube
259+
.getCommentsExtractor(url);
260+
extractor.fetchPage();
261+
}
262+
263+
@Test
264+
public void testGetCommentsFirst() throws IOException, ExtractionException {
265+
final InfoItemsPage<CommentsInfoItem> comments = extractor.getInitialPage();
266+
267+
DefaultTests.defaultTestListOfItems(YouTube, comments.getItems(), comments.getErrors());
268+
269+
CommentsInfoItem pinnedComment = comments.getItems().get(0);
270+
271+
assertTrue("First comment isn't pinned", pinnedComment.isPinned());
272+
assertTrue("The first pinned comment has no likes", pinnedComment.getLikeCount() > 0);
273+
assertTrue("The first pinned comment has no vote count", !Utils.isBlank(pinnedComment.getTextualVoteCount()));
274+
}
275+
}
276+
277+
/**
278+
* Checks if the vote count works localized<br/>
279+
* A pinned comment with >15K likes is used for the test
280+
*/
281+
public static class LocalizedVoteCount {
282+
private final static String url = "https://www.youtube.com/watch?v=QqsLTNkzvaY";
283+
private static YoutubeCommentsExtractor extractor;
284+
285+
@BeforeClass
286+
public static void setUp() throws Exception {
287+
YoutubeParsingHelper.resetClientVersionAndKey();
288+
YoutubeParsingHelper.setNumberGenerator(new Random(1));
289+
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "localized_vote_count"));
290+
extractor = (YoutubeCommentsExtractor) YouTube
291+
.getCommentsExtractor(url);
292+
// Force non english local here
293+
extractor.forceLocalization(Localization.fromLocale(Locale.GERMANY));
294+
extractor.fetchPage();
295+
}
296+
297+
@Test
298+
public void testGetCommentsFirst() throws IOException, ExtractionException {
299+
final InfoItemsPage<CommentsInfoItem> comments = extractor.getInitialPage();
300+
301+
DefaultTests.defaultTestListOfItems(YouTube, comments.getItems(), comments.getErrors());
302+
303+
CommentsInfoItem pinnedComment = comments.getItems().get(0);
304+
305+
assertTrue("First comment isn't pinned", pinnedComment.isPinned());
306+
assertTrue("The first pinned comment has no vote count", !Utils.isBlank(pinnedComment.getTextualVoteCount()));
307+
}
308+
}
246309
}

0 commit comments

Comments
 (0)