Permalink
Browse files

tests.test_commands: use special assertion for call args matching

  • Loading branch information...
1 parent 2e6a72a commit f38af4d344da65b23497740ca3f4d74a54d7ed6a @andreypopp andreypopp committed Aug 7, 2012
Showing with 32 additions and 16 deletions.
  1. +32 −16 pgxnclient/tests/test_commands.py
View
48 pgxnclient/tests/test_commands.py
@@ -307,8 +307,23 @@ def test_version(self):
self.assertEqual(res, cmd.get_best_version(data, spec))
+class Assertions(object):
+
+ make = object()
+
+ def assertCallArgs(self, pattern, args):
+ if len(pattern) != len(args):
+ self.fail('args and pattern have different lengths')
+ for p, a in zip(pattern, args):
+ if p is self.make:
+ if not a.endswith('make'):
+ self.fail('%s is not a make in %s' % (a, args))
+ else:
+ if not a == p:
+ self.fail('%s is not a %s in %s' % (a, p, args))
+
+class InstallTestCase(unittest.TestCase, Assertions):
-class InstallTestCase(unittest.TestCase):
def setUp(self):
self._p1 = patch('pgxnclient.api.get_file')
self.mock_get = self._p1.start()
@@ -333,8 +348,8 @@ def test_install_latest(self):
main(['install', '--sudo', '--', 'foobar'])
self.assertEquals(self.mock_popen.call_count, 2)
- self.assertEquals(['make'], self.mock_popen.call_args_list[0][0][0][:1])
- self.assertEquals(['sudo', 'make'], self.mock_popen.call_args_list[1][0][0][:2])
+ self.assertCallArgs([self.make], self.mock_popen.call_args_list[0][0][0][:1])
+ self.assertCallArgs(['sudo', self.make], self.mock_popen.call_args_list[1][0][0][:2])
def test_install_missing_sudo(self):
from pgxnclient.cli import main
@@ -348,8 +363,8 @@ def test_install_local(self):
main(['install', 'foobar'])
self.assertEquals(self.mock_popen.call_count, 2)
- self.assertEquals(['make'], self.mock_popen.call_args_list[0][0][0][:1])
- self.assertEquals(['make'], self.mock_popen.call_args_list[1][0][0][:1])
+ self.assertCallArgs([self.make], self.mock_popen.call_args_list[0][0][0][:1])
+ self.assertCallArgs([self.make], self.mock_popen.call_args_list[1][0][0][:1])
def test_install_fails(self):
self.mock_popen.return_value.returncode = 1
@@ -381,16 +396,17 @@ def test_install_nosudo(self):
main(['install', '--nosudo', 'foobar'])
self.assertEquals(self.mock_popen.call_count, 2)
- self.assertEquals(['make'], self.mock_popen.call_args_list[0][0][0][:1])
- self.assertEquals(['make'], self.mock_popen.call_args_list[1][0][0][:1])
+ self.assertCallArgs([self.make], self.mock_popen.call_args_list[0][0][0][:1])
+ self.assertCallArgs([self.make], self.mock_popen.call_args_list[1][0][0][:1])
def test_install_sudo(self):
from pgxnclient.cli import main
main(['install', '--sudo', 'gksudo -d "hello world"', 'foobar'])
self.assertEquals(self.mock_popen.call_count, 2)
- self.assertEquals(['make'], self.mock_popen.call_args_list[0][0][0][:1])
- self.assertEquals(['gksudo', '-d', 'hello world', 'make'],
+ self.assertCallArgs([self.make],
+ self.mock_popen.call_args_list[0][0][0][:1])
+ self.assertCallArgs(['gksudo', '-d', 'hello world', self.make],
self.mock_popen.call_args_list[1][0][0][:4])
@patch('pgxnclient.commands.unpack')
@@ -402,8 +418,8 @@ def test_install_local_zip(self, mock_unpack):
main(['install', '--sudo', '--', get_test_filename('foobar-0.42.1.zip')])
self.assertEquals(self.mock_popen.call_count, 2)
- self.assertEquals(['make'], self.mock_popen.call_args_list[0][0][0][:1])
- self.assertEquals(['sudo', 'make'],
+ self.assertCallArgs([self.make], self.mock_popen.call_args_list[0][0][0][:1])
+ self.assertCallArgs(['sudo', self.make],
self.mock_popen.call_args_list[1][0][0][:2])
make_cwd = self.mock_popen.call_args_list[1][1]['cwd']
@@ -427,14 +443,14 @@ def test_install_local_dir(self):
shutil.rmtree(tdir)
self.assertEquals(self.mock_popen.call_count, 2)
- self.assertEquals(['make'], self.mock_popen.call_args_list[0][0][0][:1])
- self.assertEquals(dir, self.mock_popen.call_args_list[0][1]['cwd'])
- self.assertEquals(['sudo', 'make'],
+ self.assertCallArgs([self.make], self.mock_popen.call_args_list[0][0][0][:1])
+ self.assertCallArgs(dir, self.mock_popen.call_args_list[0][1]['cwd'])
+ self.assertCallArgs(['sudo', self.make],
self.mock_popen.call_args_list[1][0][0][:2])
self.assertEquals(dir, self.mock_popen.call_args_list[1][1]['cwd'])
-class CheckTestCase(unittest.TestCase):
+class CheckTestCase(unittest.TestCase, Assertions):
def setUp(self):
self._p1 = patch('pgxnclient.api.get_file')
self.mock_get = self._p1.start()
@@ -459,7 +475,7 @@ def test_check_latest(self):
main(['check', 'foobar'])
self.assertEquals(self.mock_popen.call_count, 1)
- self.assertEquals(['make'], self.mock_popen.call_args_list[0][0][0][:1])
+ self.assertCallArgs([self.make], self.mock_popen.call_args_list[0][0][0][:1])
def test_check_fails(self):
self.mock_popen.return_value.returncode = 1

0 comments on commit f38af4d

Please sign in to comment.