Ejemplo n.º 1
0
    def test_template(self):
        site_object = ChatDownloader()
        try:
            params = test['params']

            if not params.get('logging'):  # if it is not set, make it 'none'
                params['logging'] = 'none'

            expected_result = test.pop('expected_result', None)

            if not params:
                self.assertFalse('No parameters specified.')  # Invalid test

            messages_list = []
            try:
                chat = site_object.get_chat(**params)

                # Ensure the site created matches the test site
                if site is not BaseChatDownloader:
                    self.assertEqual(
                        chat.site.__class__.__name__, site.__name__)

                messages_list = list(chat)

            except Exception as e:
                error = expected_result.get('error')
                self.assertTrue(error is not None and isinstance(e, error))

            messages_condition = expected_result.get('messages_condition')

            if messages_condition:
                if callable(messages_condition):
                    self.assertTrue(messages_condition(messages_list))
                else:
                    self.assertFalse('Message check is not callable.')

            actual_result = {
                'message_types': [],
                'action_types': []
            }
            types_to_check = [
                key for key in actual_result if key in expected_result]

            if types_to_check:
                for message in messages_list:
                    message_type = message.get('message_type')
                    if message_type not in actual_result['message_types']:
                        actual_result['message_types'].append(message_type)

                    action_type = message.get('action_type')
                    if action_type not in actual_result['action_types']:
                        actual_result['action_types'].append(action_type)

                for check in types_to_check:
                    self.assertCountEqual(expected_result.get(
                        check), actual_result.get(check))

        finally:
            site_object.close()
Ejemplo n.º 2
0
    def _get_one_message(self, expected_error=None, **init_params):
        session = ChatDownloader(**init_params)

        try:
            url = 'https://www.youtube.com/watch?v=5qap5aO4i9A'
            chat = list(session.get_chat(url, max_messages=1))

            self.assertEqual(len(chat), 1)

        except Exception as e:
            self.assertTrue(expected_error is not None
                            and isinstance(e, expected_error))
        finally:
            session.close()