Unit test fixes for Python3 support

This commit is contained in:
sneakypete81 2013-06-29 20:24:12 +01:00
parent e662f32cc3
commit af4260937f
2 changed files with 25 additions and 20 deletions

View file

@ -1,7 +1,11 @@
from __future__ import unicode_literals
import os
import sys
from StringIO import StringIO
import mock
try:
import StringIO as io # Python2
except ImportError:
import io # Python3
try:
import unittest2 as unittest # Python2.6
except ImportError:
@ -20,7 +24,7 @@ class TestCli(unittest.TestCase):
test_file = os.path.join("tests", "unit", "data", "test_file.txt")
@mock.patch.object(openphoto.main, "OpenPhoto")
@mock.patch('sys.stdout', new_callable=StringIO)
@mock.patch('sys.stdout', new_callable=io.StringIO)
def test_defaults(self, _, mock_openphoto):
"""Check that the default behaviour is correct"""
get = mock_openphoto.return_value.get
@ -29,14 +33,14 @@ class TestCli(unittest.TestCase):
get.assert_called_with("/photos/list.json", process_response=False)
@mock.patch.object(openphoto.main, "OpenPhoto")
@mock.patch('sys.stdout', new_callable=StringIO)
@mock.patch('sys.stdout', new_callable=io.StringIO)
def test_config(self, _, mock_openphoto):
"""Check that a config file can be specified"""
main(["--config=test"])
mock_openphoto.assert_called_with(config_file="test")
@mock.patch.object(openphoto.main, "OpenPhoto")
@mock.patch('sys.stdout', new_callable=StringIO)
@mock.patch('sys.stdout', new_callable=io.StringIO)
def test_get(self, mock_stdout, mock_openphoto):
"""Check that the get operation is working"""
get = mock_openphoto.return_value.get
@ -49,7 +53,7 @@ class TestCli(unittest.TestCase):
self.assertEqual(mock_stdout.getvalue(), "Result\n")
@mock.patch.object(openphoto.main, "OpenPhoto")
@mock.patch('sys.stdout', new_callable=StringIO)
@mock.patch('sys.stdout', new_callable=io.StringIO)
def test_post(self, mock_stdout, mock_openphoto):
"""Check that the post operation is working"""
post = mock_openphoto.return_value.post
@ -62,7 +66,7 @@ class TestCli(unittest.TestCase):
self.assertEqual(mock_stdout.getvalue(), "Result\n")
@mock.patch.object(openphoto.main, "OpenPhoto")
@mock.patch('sys.stdout', new_callable=StringIO)
@mock.patch('sys.stdout', new_callable=io.StringIO)
def test_post_files(self, _, mock_openphoto):
"""Check that files are posted correctly"""
post = mock_openphoto.return_value.post
@ -70,11 +74,11 @@ class TestCli(unittest.TestCase):
# It's not possible to directly compare the file object,
# so check it manually
files = post.call_args[1]["files"]
self.assertEqual(files.keys(), ["photo"])
self.assertEqual(list(files.keys()), ["photo"])
self.assertEqual(files["photo"].name, self.test_file)
@mock.patch.object(sys, "exit", raise_exception)
@mock.patch('sys.stderr', new_callable=StringIO)
@mock.patch('sys.stderr', new_callable=io.StringIO)
def test_unknown_arg(self, mock_stderr):
"""Check that an unknown argument produces an error"""
with self.assertRaises(TestException):
@ -82,7 +86,7 @@ class TestCli(unittest.TestCase):
self.assertIn("error: Unknown argument", mock_stderr.getvalue())
@mock.patch.object(sys, "exit", raise_exception)
@mock.patch('sys.stderr', new_callable=StringIO)
@mock.patch('sys.stderr', new_callable=io.StringIO)
def test_unknown_option(self, mock_stderr):
"""Check that an unknown option produces an error"""
with self.assertRaises(TestException):
@ -90,7 +94,7 @@ class TestCli(unittest.TestCase):
self.assertIn("error: no such option", mock_stderr.getvalue())
@mock.patch.object(sys, "exit", raise_exception)
@mock.patch('sys.stdout', new_callable=StringIO)
@mock.patch('sys.stdout', new_callable=io.StringIO)
def test_unknown_config(self, mock_stdout):
"""Check that an unknown config file produces an error"""
with self.assertRaises(TestException):
@ -101,7 +105,7 @@ class TestCli(unittest.TestCase):
self.assertIn("To get your credentials", mock_stdout.getvalue())
@mock.patch.object(openphoto.main, "OpenPhoto")
@mock.patch('sys.stdout', new_callable=StringIO)
@mock.patch('sys.stdout', new_callable=io.StringIO)
def test_verbose(self, mock_stdout, _):
"""Check that the verbose option is working"""
main(["-v"])
@ -109,7 +113,7 @@ class TestCli(unittest.TestCase):
self.assertIn("Endpoint: /photos/list.json", mock_stdout.getvalue())
@mock.patch.object(openphoto.main, "OpenPhoto")
@mock.patch('sys.stdout', new_callable=StringIO)
@mock.patch('sys.stdout', new_callable=io.StringIO)
def test_pretty_print(self, mock_stdout, mock_openphoto):
"""Check that the pretty-print option is working"""
get = mock_openphoto.return_value.get