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

View file

@ -57,8 +57,8 @@ class TestHttp(unittest.TestCase):
self.assertEqual(self._last_request().querystring["spam"], ["eggs"])
self.assertEqual(response, self.test_data)
self.assertEqual(self.client.last_url, self.test_uri)
self.assertEqual(self.client.last_params, {"foo": "bar",
"spam": "eggs"})
self.assertEqual(self.client.last_params, {"foo": b"bar",
"spam": b"eggs"})
self.assertEqual(self.client.last_response.json(), self.test_data)
@httpretty.activate
@ -67,11 +67,12 @@ class TestHttp(unittest.TestCase):
self._register_uri(httpretty.POST)
response = self.client.post(self.test_endpoint,
foo="bar", spam="eggs")
self.assertEqual(self._last_request().body, "foo=bar&spam=eggs")
self.assertIn(b"spam=eggs", self._last_request().body)
self.assertIn(b"foo=bar", self._last_request().body)
self.assertEqual(response, self.test_data)
self.assertEqual(self.client.last_url, self.test_uri)
self.assertEqual(self.client.last_params, {"foo": "bar",
"spam": "eggs"})
self.assertEqual(self.client.last_params, {"foo": b"bar",
"spam": b"eggs"})
self.assertEqual(self.client.last_response.json(), self.test_data)
@httpretty.activate
@ -123,7 +124,7 @@ class TestHttp(unittest.TestCase):
self.assertEqual(params["tag"], ["tag_id"])
self.assertEqual(params["list_"], ["photo_id,album_id,tag_id"])
self.assertEqual(params["boolean"], ["1"])
self.assertEqual(params["unicode_"], ["\xc3\xbcmlaut"])
self.assertIn(params["unicode_"], [["\xc3\xbcmlaut"], ["\xfcmlaut"]])
@httpretty.activate
def test_get_with_api_version(self):
@ -152,10 +153,10 @@ class TestHttp(unittest.TestCase):
response = self.client.post(self.test_endpoint,
files={"file": in_file})
self.assertEqual(response, self.test_data)
body = self._last_request().body
body = str(self._last_request().body)
self.assertIn("Content-Disposition: form-data; "+
"name=\"file\"; filename=\"test_file.txt\"", body)
self.assertIn("Test File", body)
self.assertIn("Test File", str(body))
@httpretty.activate