import os
import sys
from unittest import TestCase

sys.path.append(os.path.abspath('.'))


class BaiduSpiderOtherTestCase(TestCase):
    def setUp(self) -> None:
        from baiduspider import BaiduSpider
        self.spider = BaiduSpider()

    def test_calc(self):
        result = self.spider.search_web('12345679*9')
        flag = False
        for i in result['results']:
            if i['type'] == 'calc':
                print(i['process'])
                print(i['result'])
                flag = True
        self.assertTrue(flag)

    def test_tieba(self):
        result = self.spider.search_web('python吧')
        for i in result['results']:
            if i['type'] == 'tieba':
                self.are_in(['title', 'des', 'cover', 'url', 'followers', 'hot', 'total'], i['result'])
            elif i['type'] == 'result':
                self.are_in(['des', 'origin', 'title', 'url'], i)

    def test_video(self):
        result = self.spider.search_web('视频')
        print(result)
        for i in result['results']:
            if i['type'] == 'result':
                self.are_in(['des', 'origin', 'title', 'url'], i)
            elif i['type'] == 'tieba':
                self.are_in(['title', 'des', 'cover', 'url', 'followers', 'hot', 'total'], i['results'])
            elif i['type'] == 'video':
                self.are_in(['length', 'origin', 'title', 'url'], i['results'][0])

    def test_exclude_all(self):
        result = self.spider.search_web('python', exclude=['all'])
        for i in result['results']:
            self.assertIn(i['type'], ['result', 'total'])

    def are_in(self, members: list, container: list):
        for i in members:
            self.assertIn(i, container)

