youtube_crawler.test.js
2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* YouTube Trending Crawler Unit Tests
*/
const { YouTubeTrendingCrawler, getMockTrendingVideos, validateRegionCode } = require('./youtube_crawler');
describe('YouTubeTrendingCrawler', () => {
test('should create crawler instance', () => {
const crawler = new YouTubeTrendingCrawler({ useMock: true });
expect(crawler).toBeDefined();
expect(crawler.getRemainingQuota()).toBe(10000);
});
test('validateRegionCode should accept valid regions', () => {
expect(validateRegionCode('US')).toBe(true);
expect(validateRegionCode('JP')).toBe(true);
expect(validateRegionCode('KR')).toBe(true);
expect(validateRegionCode('XX')).toBe(false);
});
test('getMockTrendingVideos should return mock data', () => {
const videos = getMockTrendingVideos({ regionCode: 'US', maxResults: 3 });
expect(videos.length).toBe(3);
expect(videos[0].video_id).toBeDefined();
expect(videos[0].view_count).toBeDefined();
});
test('get_trending mock should return videos with required fields', async () => {
const crawler = new YouTubeTrendingCrawler({ useMock: true });
const videos = await crawler.get_trending('US', 2);
expect(videos.length).toBe(2);
const v = videos[0];
expect(v.video_id).toBeDefined();
expect(v.title).toBeDefined();
expect(v.channel_id).toBeDefined();
expect(v.channel_title).toBeDefined();
expect(v.published_at).toBeDefined();
expect(v.view_count).toBeDefined();
expect(v.category_id).toBeDefined();
expect(v.source_url).toBeDefined();
expect(v.crawl_time).toBeDefined();
});
test('schema validation should pass for valid video', () => {
const crawler = new YouTubeTrendingCrawler({ useMock: true });
const result = crawler.validateSchema({
video_id: 'AbCdEfGhIjK',
title: 'Test',
channel_id: 'UC_123',
channel_title: 'Channel',
published_at: '2024-01-01T00:00:00Z',
view_count: 1000,
category_id: '10',
source_url: 'https://www.youtube.com/watch?v=AbCdEfGhIjK',
crawl_time: '2024-01-01T00:00:00Z'
});
expect(result.valid).toBe(true);
});
test('save_to_json and save_to_csv should create files', () => {
const fs = require('fs');
const path = require('path');
const crawler = new YouTubeTrendingCrawler({ useMock: true });
const videos = crawler._getMockData('US', 1);
const jsonPath = path.join('/tmp', 'yt_test.json');
const csvPath = path.join('/tmp', 'yt_test.csv');
crawler.save_to_json(videos, jsonPath);
expect(fs.existsSync(jsonPath)).toBe(true);
crawler.save_to_csv(videos, csvPath);
expect(fs.existsSync(csvPath)).toBe(true);
fs.unlinkSync(jsonPath);
fs.unlinkSync(csvPath);
});
});