Source

peerreach / tests / test_runner.py

Full commit

from unittest import TestCase
import mock
import json

from peerreach.runner import main
from peerreach import JSONParser, RawParser

class TestRunner(TestCase):

    @mock.patch("peerreach.runner.Api")
    def test_main_screen_name(self, Api):

        Api().user_lookup.return_value = {"a": "b"}

        main(["test_user"])

        Api().user_lookup.assert_called_once_with(screen_name="test_user")

    @mock.patch("peerreach.runner.Api")
    def test_main_user_id(self, Api):

        Api().user_lookup.return_value = {"a": "b"}

        main(["-i", "11111"])

        Api().user_lookup.assert_called_once_with(user_id='11111')

    @mock.patch("peerreach.runner.Api")
    def test_main_screen_names(self, Api):

        Api().multi_user_lookup.return_value = {"a": "b"}

        main(["test_user1", "test_user2"])

        Api().multi_user_lookup.assert_called_once_with(
                            screen_names=["test_user1", "test_user2"])

    @mock.patch("peerreach.runner.Api")
    def test_main_user_ids(self, Api):

        Api().multi_user_lookup.return_value = {"a": "b"}

        main(["-i", "11111", "22222"])

        Api().multi_user_lookup.assert_called_once_with(
                            user_ids=["11111", "22222"])

    @mock.patch("peerreach.runner.Api")
    def test_main_not_raw(self, Api):

        Api.return_value.user_lookup.return_value = {"a": "b"}

        main(["test_user"])

        Api.assert_called_once_with(parser=mock.ANY)
        args, kwargs = Api.call_args
        self.assertTrue(isinstance(kwargs['parser'], JSONParser))

    @mock.patch("peerreach.runner.Api")
    def test_main_raw(self, Api):

        Api.return_value.user_lookup.return_value = '{"a": "b"}'

        main(["-r", "test_user"])

        Api.assert_called_once_with(parser=mock.ANY)
        args, kwargs = Api.call_args
        self.assertTrue(isinstance(kwargs['parser'], RawParser))

    def test_main_args_error(self):
        self.assertRaises(SystemExit, main, [])