Skip to content

Commit

Permalink
Fixed tests for python 3 added six.
Browse files Browse the repository at this point in the history
  • Loading branch information
teddy-schmitz committed Jan 2, 2015
1 parent 1fb5d83 commit a2abdd9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
11 changes: 4 additions & 7 deletions pingme/pingme.py
Expand Up @@ -14,15 +14,12 @@
"""
from __future__ import print_function

from six.moves import configparser # pylint: disable=F0401,E0611
from six.moves.configparser import NoSectionError, MissingSectionHeaderError # pylint: disable=F0401,E0611
import requests
import argparse
import os
try:
import configparser # pylint: disable=W0611
from configparser import NoSectionError, MissingSectionHeaderError # pylint: disable=W0611
except ImportError:
import ConfigParser # pylint: disable=W0611
from ConfigParser import NoSectionError, MissingSectionHeaderError # pylint: disable=W0611

import json

__VERSION__ = "0.1.0"
Expand Down Expand Up @@ -91,7 +88,7 @@ def read_config_file(fpath):
:param fpath: File to read
:return: dictionary of config items
"""
config = ConfigParser.SafeConfigParser()
config = configparser.SafeConfigParser()
config.read(fpath)

try:
Expand Down
42 changes: 29 additions & 13 deletions pingme/tests/test_pingme.py
Expand Up @@ -3,53 +3,69 @@
""" Run the tests for the pingme script."""

import unittest
import io

from mock import patch
import six

from pingme import pingme
import io
from six.moves import configparser # pylint: disable=F0401


@patch('sys.argv', return_value=[''])
class PingmeTest(unittest.TestCase):
""" Setup the test cases for the pingme script."""

def __init__(self, *args, **kwargs):
super(PingmeTest, self).__init__(*args, **kwargs)
self.config_data = '[Default]\n' \
'devices=123456\n' \
'message=test message please ignore'

@patch('sys.argv', return_value=[''])
@patch('requests.post')
@patch('os.access', return_value=True)
@patch('os.path.isfile', return_value=True)
@patch('__builtin__.open', return_value=io.BytesIO('[Default]\n'
'devices=123456\n'
'message=test message please ignore'))
@patch.object(configparser, 'open', create=True,
return_value=io.StringIO(six.u('[Default]\n'
'devices=123456\n'
'message=test message please ignore')))
def test_send_ping_success(self, mock_file, mock_isfile, mock_access, mock_post, mock_argv):
mock_post.return_value.status_code = 200
with self.assertRaises(SystemExit) as excp:
pingme.main()
self.assertEqual(excp.exception.code, 0)

@patch('sys.argv', return_value=[''])
@patch('os.path.isfile', return_value=False)
def test_send_ping_noarguments(self, mock_isfile, mock_argv):
def test_send_ping_main_noarguments(self, mock_isfile, mock_argv):
with self.assertRaises(SystemExit) as excp:
pingme.main()
self.assertEqual(excp.exception.code, 1)

@patch('requests.post')
def test_send_ping_server_error(self, mock_post):
def test_send_ping_server_error(self, mock_post, mock_argv):
mock_post.return_value.status_code = 500
with self.assertRaises(SystemExit) as excp:
pingme.send_ping('123456', 'test message')
self.assertEqual(excp.exception.code, 1)

@patch('sys.argv', return_value=[''])
@patch('os.access', return_value=True)
@patch('os.path.isfile', return_value=True)
@patch('__builtin__.open', return_value=io.BytesIO('[default]\n'
'devices=123456\n'
'message=test message please ignore'))
def test_send_ping_badconfig(self, mock_file, mock_isfile, mock_access, mock_argv):
@patch.object(configparser, 'open', create=True,
return_value=io.StringIO(six.u('[garbage]\n'
'devices=123456\n'
'message=test message please ignore')))
def test_send_ping_main_badconfig(self, mock_file, mock_isfile, mock_access, mock_argv):
with self.assertRaises(SystemExit) as excp:
pingme.main()
self.assertEqual(excp.exception.code, 1)

@patch('os.access', return_value=True)
@patch('os.path.isfile', return_value=True)
@patch.object(configparser, 'open', create=True,
return_value=io.StringIO(six.u('[garbage]\n'
'devices=123456\n'
'message=test message please ignore')))
def test_send_ping_badconfig(self, mock_file, mock_isfile, mock_access, mock_argv):
with self.assertRaises(SystemExit) as excp:
pingme.read_config_file('/fake/path')
self.assertEqual(excp.exception.code, 1)
3 changes: 2 additions & 1 deletion requirements.txt
@@ -1,2 +1,3 @@
requests==2.5.1
mock==1.0.1
mock==1.0.1
six

0 comments on commit a2abdd9

Please sign in to comment.