<commit_before># -*- coding: utf-8 -*-
from unittest import TestCase

from httmock import HTTMock, all_requests

from plivo import RestClient
from plivo.rest.phlo_client import PhloClient as PhloRestClient


@all_requests
def plivo_mock(url, request):
    return request


class PlivoTestClient(RestClient):
    def __init__(self,
                 auth_id=None,
                 auth_token=None,
                 proxies=None,
                 timeout=None):
        super(PlivoTestClient, self).__init__(auth_id, auth_token, proxies,
                                              timeout)
        self.set_expected_response(500, {})

    def set_expected_response(self, status_code, data_to_return):
        self.expected_response = {
            'status_code': status_code,
            'content': data_to_return
        }

    def mock_request(self, url, request):
        self.current_request = request
        return self.expected_response

    def send_request(self, req, **kwargs):
        with HTTMock(self.mock_request):
            return super(PlivoTestClient, self).send_request(req, **kwargs)


try:
    import urlparse
    from urllib import urlencode, unquote_plus, quote_plus
except ImportError:
    from urllib import parse as urlparse
    from urllib.parse import urlencode, unquote_plus, quote_plus


class PlivoResourceTestCase(TestCase):
    def setUp(self):
        self.client = PlivoTestClient(
            auth_id='MA' + 'X' * 18, auth_token='<KEY>')

    def assertUrlEqual(self, expected, actual, message=None):
        expected_url, actual_url = (urlparse.urlparse(x)._asdict()
                                    for x in (expected, actual))
        expected_qs, actual_qs = map(
            lambda x: sorted(urlparse.parse_qsl(x['query'])), (expected_url,
                                                               actual_url))
        expected_url['query'], actual_url['query'] = '', ''
        expected_url['path'], actual_url['path'] = map(
            unquote_plus, [expected_url['path'], actual_url['path']])

        self.assertEqual(expected_url, actual_url, message)
        self.assertEqual(expected_qs, actual_qs, message)

    def assertResponseMatches(self, obj, expected_response=None):
        expected_response = expected_response or self.expected_response
        id_str = getattr(obj, '_identifier_string', None)
        if id_str:
            self.assertEqual(expected_response[id_str], obj.id)

    def get_url(self, *args, **kwargs):
        return 'https://api.plivo.com/v1/Account/' + \
               self.client.session.auth[0] + \
               '/' + '/'.join([quote_plus(arg)
                               for arg in args]) + '/?' + urlencode(kwargs)

    def get_voice_url(self, *args, **kwargs):
        return 'https://voice.plivo.com/v1/Account/' + \
               self.client.session.auth[0] + \
               '/' + '/'.join([quote_plus(arg)
                               for arg in args]) + '/?' + urlencode(kwargs)

    def get_voice_fallback1_url(self, *args, **kwargs):
        return 'https://voice-usw1.plivo.com/v1/Account/' + \
               self.client.session.auth[0] + \
               '/' + '/'.join([quote_plus(arg)
                               for arg in args]) + '/?' + urlencode(kwargs)

    def get_voice_fallback2_url(self, *args, **kwargs):
        return 'https://voice-use1.plivo.com/v1/Account/' + \
               self.client.session.auth[0] + \
               '/' + '/'.join([quote_plus(arg)
                               for arg in args]) + '/?' + urlencode(kwargs)


class PlivoRequestTest(TestCase):
    def setUp(self):
        self.timeout = 1
        self.proxies = {
            'http': 'http://0.0.0.0:8888',
            'https': 'http://0.0.0.0:8080',
        }
        self.client = PlivoTestClient(
            auth_id='MA' + 'X' * 18,
            auth_token='<PASSWORD>',
            proxies=self.proxies,
            timeout=self.timeout)

    def test_timeout_value(self):
        self.assertEqual(self.client.timeout, self.timeout)

    def test_proxy_value(self):
        self.assertEqual(self.client.proxies, self.proxies)


class PlivoPhlosTestClient(PhloRestClient):
    """
        Plivo Phlos rest client test case
    """
    def __init__(self,
                 auth_id=None,
                 auth_token=None,
                 proxies=None,
                 timeout=None):
        super(PlivoPhlosTestClient, self).__init__(auth_id, auth_token, proxies,
                                                   timeout)
        self.set_expected_response(500, {})

    def set_expected_response(self, status_code, data_to_return):
        self.expected_response = {
            'status_code': status_code,
            'content': data_to_return
        }

    def mock_request(self, url, request):
        self.current_request = request
        return self.expected_response

    def send_request(self, req, **kwargs):
        with HTTMock(self.mock_request):
            return super(PlivoPhlosTestClient, self).send_request(req, **kwargs)


class PlivoPhlosResourceTestCase(TestCase):
    def setUp(self):
        self.client = PlivoPhlosTestClient(
            auth_id='MA' + 'X' * 18, auth_token='<PASSWORD>'
        )

    def assertUrlEqual(self, expected, actual, message=None):
        expected_url, actual_url = (urlparse.urlparse(x)._asdict()
                                    for x in (expected, actual))
        expected_qs, actual_qs = map(
            lambda x: sorted(urlparse.parse_qsl(x['query'])), (expected_url,
                                                               actual_url))
        expected_url['query'], actual_url['query'] = '', ''
        expected_url['path'], actual_url['path'] = map(
            unquote_plus, [expected_url['path'], actual_url['path']])

        self.assertEqual(expected_url, actual_url, message)
        self.assertEqual(expected_qs, actual_qs, message)

    def assertResponseMatches(self, obj, expected_response=None):
        expected_response = expected_response or self.expected_response
        id_str = getattr(obj, '_identifier_string', None)
        if id_str:
            self.assertEqual(expected_response[id_str], obj.id)

    def get_url(self, *args, **kwargs):
        return 'https://phlorunner.plivo.com/v1/account/' + \
               self.client.session.auth[0] + \
               '/' + '/'.join([quote_plus(arg)
                               for arg in args]) + '/?' + urlencode(kwargs)
<commit_msg>tests: add assertXmlEqual<commit_after># -*- coding: utf-8 -*-
from unittest import TestCase
from doctest import Example
from lxml.doctestcompare import LXMLOutputChecker

from httmock import HTTMock, all_requests

from plivo import RestClient
from plivo.rest.phlo_client import PhloClient as PhloRestClient


@all_requests
def plivo_mock(url, request):
    return request


class PlivoTestClient(RestClient):
    def __init__(self,
                 auth_id=None,
                 auth_token=None,
                 proxies=None,
                 timeout=None):
        super(PlivoTestClient, self).__init__(auth_id, auth_token, proxies,
                                              timeout)
        self.set_expected_response(500, {})

    def set_expected_response(self, status_code, data_to_return):
        self.expected_response = {
            'status_code': status_code,
            'content': data_to_return
        }

    def mock_request(self, url, request):
        self.current_request = request
        return self.expected_response

    def send_request(self, req, **kwargs):
        with HTTMock(self.mock_request):
            return super(PlivoTestClient, self).send_request(req, **kwargs)


try:
    import urlparse
    from urllib import urlencode, unquote_plus, quote_plus
except ImportError:
    from urllib import parse as urlparse
    from urllib.parse import urlencode, unquote_plus, quote_plus


class PlivoResourceTestCase(TestCase):
    def setUp(self):
        self.client = PlivoTestClient(
            auth_id='MA' + 'X' * 18, auth_token='<KEY>')

    def assertUrlEqual(self, expected, actual, message=None):
        expected_url, actual_url = (urlparse.urlparse(x)._asdict()
                                    for x in (expected, actual))
        expected_qs, actual_qs = map(
            lambda x: sorted(urlparse.parse_qsl(x['query'])), (expected_url,
                                                               actual_url))
        expected_url['query'], actual_url['query'] = '', ''
        expected_url['path'], actual_url['path'] = map(
            unquote_plus, [expected_url['path'], actual_url['path']])

        self.assertEqual(expected_url, actual_url, message)
        self.assertEqual(expected_qs, actual_qs, message)

    def assertResponseMatches(self, obj, expected_response=None):
        expected_response = expected_response or self.expected_response
        id_str = getattr(obj, '_identifier_string', None)
        if id_str:
            self.assertEqual(expected_response[id_str], obj.id)

    def get_url(self, *args, **kwargs):
        return 'https://api.plivo.com/v1/Account/' + \
               self.client.session.auth[0] + \
               '/' + '/'.join([quote_plus(arg)
                               for arg in args]) + '/?' + urlencode(kwargs)

    def get_voice_url(self, *args, **kwargs):
        return 'https://voice.plivo.com/v1/Account/' + \
               self.client.session.auth[0] + \
               '/' + '/'.join([quote_plus(arg)
                               for arg in args]) + '/?' + urlencode(kwargs)

    def get_voice_fallback1_url(self, *args, **kwargs):
        return 'https://voice-usw1.plivo.com/v1/Account/' + \
               self.client.session.auth[0] + \
               '/' + '/'.join([quote_plus(arg)
                               for arg in args]) + '/?' + urlencode(kwargs)

    def get_voice_fallback2_url(self, *args, **kwargs):
        return 'https://voice-use1.plivo.com/v1/Account/' + \
               self.client.session.auth[0] + \
               '/' + '/'.join([quote_plus(arg)
                               for arg in args]) + '/?' + urlencode(kwargs)


class PlivoRequestTest(TestCase):
    def setUp(self):
        self.timeout = 1
        self.proxies = {
            'http': 'http://0.0.0.0:8888',
            'https': 'http://0.0.0.0:8080',
        }
        self.client = PlivoTestClient(
            auth_id='MA' + 'X' * 18,
            auth_token='<PASSWORD>',
            proxies=self.proxies,
            timeout=self.timeout)

    def test_timeout_value(self):
        self.assertEqual(self.client.timeout, self.timeout)

    def test_proxy_value(self):
        self.assertEqual(self.client.proxies, self.proxies)


class PlivoPhlosTestClient(PhloRestClient):
    """
        Plivo Phlos rest client test case
    """
    def __init__(self,
                 auth_id=None,
                 auth_token=None,
                 proxies=None,
                 timeout=None):
        super(PlivoPhlosTestClient, self).__init__(auth_id, auth_token, proxies,
                                                   timeout)
        self.set_expected_response(500, {})

    def set_expected_response(self, status_code, data_to_return):
        self.expected_response = {
            'status_code': status_code,
            'content': data_to_return
        }

    def mock_request(self, url, request):
        self.current_request = request
        return self.expected_response

    def send_request(self, req, **kwargs):
        with HTTMock(self.mock_request):
            return super(PlivoPhlosTestClient, self).send_request(req, **kwargs)


class PlivoPhlosResourceTestCase(TestCase):
    def setUp(self):
        self.client = PlivoPhlosTestClient(
            auth_id='MA' + 'X' * 18, auth_token='<PASSWORD>'
        )

    def assertUrlEqual(self, expected, actual, message=None):
        expected_url, actual_url = (urlparse.urlparse(x)._asdict()
                                    for x in (expected, actual))
        expected_qs, actual_qs = map(
            lambda x: sorted(urlparse.parse_qsl(x['query'])), (expected_url,
                                                               actual_url))
        expected_url['query'], actual_url['query'] = '', ''
        expected_url['path'], actual_url['path'] = map(
            unquote_plus, [expected_url['path'], actual_url['path']])

        self.assertEqual(expected_url, actual_url, message)
        self.assertEqual(expected_qs, actual_qs, message)

    def assertResponseMatches(self, obj, expected_response=None):
        expected_response = expected_response or self.expected_response
        id_str = getattr(obj, '_identifier_string', None)
        if id_str:
            self.assertEqual(expected_response[id_str], obj.id)

    def get_url(self, *args, **kwargs):
        return 'https://phlorunner.plivo.com/v1/account/' + \
               self.client.session.auth[0] + \
               '/' + '/'.join([quote_plus(arg)
                               for arg in args]) + '/?' + urlencode(kwargs)


class PlivoXmlTestCase:

    def assertXmlEqual(self, got, want):
        checker = LXMLOutputChecker()
        if not checker.check_output(want, got, 0):
            message = checker.output_difference(Example("", want), got, 0)
            raise AssertionError(message)

{'fraction_non_alphanumeric': 0.11808781657425446, 'fraction_numerical': 0.006743593586093211, 'mean_word_length': 2.1308937368050667, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5780495', 'n_tokens_mistral': 3921, 'n_tokens_neox': 3828, 'n_words': 736}