Source

peerreach / tests / test_peerreach.py

Full commit

from unittest import TestCase
import mock
import json

from peerreach import Api
from peerreach import JSONParser, RawParser, ObjectParser
from peerreach import TooMuchArgsException, NoArgsException, HttpError


class TestParser(TestCase):

    raw = '{"some_raw_data": 1234}'

    def test_raw_parse(self):
        parser = RawParser()

        parsed = parser.parse(self.raw)
        self.assertEqual(parsed, self.raw)

    def test_json_parse(self):
        parser = JSONParser()

        parsed = parser.parse(self.raw)
        self.assertEqual(json.dumps(parsed), self.raw)

    def test_object_parse(self):
        parser = ObjectParser()

        parsed = parser.parse(self.raw)

        data = json.loads(self.raw)

        self.assertEqual(parsed.some_raw_data, data['some_raw_data'])


SAMPLE_RETURN = object()


class DummyApi(Api):
    def get_data(self, url):
        self.called_url = url
        return SAMPLE_RETURN


class TestApi(TestCase):

    def test_init_default_url(self):
        api = Api()
        self.assertEqual(api.api_url, 'http://api.peerreach.com/v1')

    def test_init_lookup_url(self):
        api = Api()
        self.assertEqual(api.api_user_url, api.api_url + '/user/lookup.json')
        self.assertEqual(api.api_multi_user_url,
                        api.api_url + '/multi-user/lookup.json')

    def test_init_set_url(self):
        api = Api(url='http://test')
        self.assertEqual(api.api_url, 'http://test')

    def test_init_default_parser(self):
        api = Api()
        self.assertTrue(isinstance(api.parser, JSONParser))

    def test_init_set_parser(self):
        class DummyParser:
            pass
        parser = DummyParser()
        api = Api(parser=parser)
        self.assertEqual(parser, api.parser)

    def test_parser(self):
        api = Api()

        raw = '{"aaa": "bbbb", "cccc": 123}'
        data = api.parse(raw)

        self.assertEqual(data, json.loads(raw))

    @mock.patch("requests.get", autospec=True)
    def test_get_data(self, get):

        class Response:
            status_code = 200
            text = '{"data": "test response"}'

        get.return_value = Response()

        api = Api()

        test_url = "http://test"

        data = api.get_data(test_url)

        self.assertEqual(data, json.loads(Response().text))
        get.assert_called_once_with(test_url)

    @mock.patch("requests.get", autospec=True)
    def test_get_data_error(self, get):

        class Response:
            status_code = 500
            text = '{"data": "test response"}'

        get.return_value = Response()

        api = Api()

        test_url = "http://test"

        self.assertRaises(HttpError, api.get_data, test_url)

    def test_user_lookup_user_id(self):
        api = DummyApi()

        data = api.user_lookup(user_id=1111)
        self.assertEqual(data, SAMPLE_RETURN)
        expected = 'http://api.peerreach.com/v1/user/lookup.json?user_id=1111'
        self.assertEqual(api.called_url, expected)

    def test_user_lookup_screen_name(self):
        api = DummyApi()

        data = api.user_lookup(screen_name='test_user')
        self.assertEqual(data, SAMPLE_RETURN)
        expected = 'http://api.peerreach.com/' + \
                   'v1/user/lookup.json?screen_name=test_user'
        self.assertEqual(api.called_url, expected)

    def test_user_lookup_no_data(self):
        api = Api()

        self.assertRaises(NoArgsException, api.user_lookup)

    def test_user_lookup_too_data(self):
        api = Api()

        self.assertRaises(TooMuchArgsException, api.user_lookup,
                                user_id=1111, screen_name='test')

    def test_multi_user_lookup_user_ids(self):
        api = DummyApi()

        data = api.multi_user_lookup(user_ids=[1111, 2222])
        self.assertEqual(data, SAMPLE_RETURN)
        expected = 'http://api.peerreach.com/' + \
                   'v1/multi-user/lookup.json?user_id=1111%2C2222'
        self.assertEqual(api.called_url, expected)

    def test_multi_user_lookup_screen_names(self):
        api = DummyApi()

        data = api.multi_user_lookup(screen_names=['test1', 'test2'])
        self.assertEqual(data, SAMPLE_RETURN)
        expected = 'http://api.peerreach.com/v1/' + \
                   'multi-user/lookup.json?screen_name=test1%2Ctest2'
        self.assertEqual(api.called_url, expected)

    def test_multi_user_lookup_no_data(self):
        api = Api()

        self.assertRaises(NoArgsException, api.multi_user_lookup)

    def test_multi_user_lookup_too_data(self):
        api = Api()

        self.assertRaises(TooMuchArgsException, api.multi_user_lookup,
                    user_ids=1111, screen_names='test')