test_youtube_stub.py 2.43 KB
Newer Older
1 2 3 4 5 6
"""
Unit test for stub YouTube implementation.
"""

import unittest
import requests
7
from ..youtube import StubYouTubeService
8 9 10 11 12 13 14


class StubYouTubeServiceTest(unittest.TestCase):

    def setUp(self):
        self.server = StubYouTubeService()
        self.url = "http://127.0.0.1:{0}/".format(self.server.port)
15
        self.server.config['time_to_response'] = 0.0
16 17 18 19 20 21 22 23 24 25 26
        self.addCleanup(self.server.shutdown)

    def test_unused_url(self):
        response = requests.get(self.url + 'unused_url')
        self.assertEqual("Unused url", response.content)

    def test_video_url(self):
        response = requests.get(
            self.url + 'test_youtube/OEoXaMPEzfM?v=2&alt=jsonc&callback=callback_func'
        )

27 28 29 30 31
        # YouTube metadata for video `OEoXaMPEzfM` states that duration is 116.
        self.assertEqual(
            'callback_func({"data": {"duration": 116, "message": "I\'m youtube.", "id": "OEoXaMPEzfM"}})',
            response.content
        )
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

    def test_transcript_url_equal(self):
        response = requests.get(
            self.url + 'test_transcripts_youtube/t__eq_exist'
        )

        self.assertEqual(
            "".join([
                '<?xml version="1.0" encoding="utf-8" ?>',
                '<transcript><text start="1.0" dur="1.0">',
                'Equal transcripts</text></transcript>'
            ]), response.content
        )

    def test_transcript_url_not_equal(self):
        response = requests.get(
            self.url + 'test_transcripts_youtube/t_neq_exist',
        )

        self.assertEqual(
            "".join([
                '<?xml version="1.0" encoding="utf-8" ?>',
                '<transcript><text start="1.1" dur="5.5">',
                'Transcripts sample, different that on server',
                '</text></transcript>'
            ]), response.content
        )

    def test_transcript_not_found(self):
        response = requests.get(self.url + 'test_transcripts_youtube/some_id')
        self.assertEqual(404, response.status_code)
63 64 65 66 67 68 69 70 71 72 73 74 75 76

    def test_reset_configuration(self):

        reset_config_url = self.url + 'del_config'

        # add some configuration data
        self.server.config['test_reset'] = 'This is a reset config test'

        # reset server configuration
        response = requests.delete(reset_config_url)
        self.assertEqual(response.status_code, 200)

        # ensure that server config dict is empty after successful reset
        self.assertEqual(self.server.config, {})