[Tests] Fix console tests sometimes failing due to hard coded port

The tests in ConsoleUIWithDaemonBaseTestCase could fail to the hard coded
port 58900 being busy.
Fix by using the proper port found in self.listen_port

Also convert unnecessary use of assertTrue where more appropriate
assert functions should be used, such as assertEqual and assertIn
This commit is contained in:
bendikro 2020-05-11 21:34:56 +02:00 committed by Calum Lind
parent a4da8d29f8
commit 937afd921c
1 changed files with 15 additions and 18 deletions

View File

@ -331,13 +331,11 @@ class ConsoleUIBaseTestCase(UIBaseTestCase):
) # Check command name ) # Check command name
self.assertTrue('Common Options:' in std_output) self.assertTrue('Common Options:' in std_output)
self.assertTrue('Console Options:' in std_output) self.assertTrue('Console Options:' in std_output)
self.assertTrue( self.assertIn(
'Console Commands:\n The following console commands are available:' 'Console Commands:\n The following console commands are available:',
in std_output std_output,
)
self.assertTrue(
'The following console commands are available:' in std_output
) )
self.assertIn('The following console commands are available:', std_output)
def test_console_command_info(self): def test_console_command_info(self):
self.patch(sys, 'argv', self.var['sys_arg_cmd'] + ['info']) self.patch(sys, 'argv', self.var['sys_arg_cmd'] + ['info'])
@ -355,8 +353,8 @@ class ConsoleUIBaseTestCase(UIBaseTestCase):
with mock.patch('deluge.ui.console.main.ConsoleUI'): with mock.patch('deluge.ui.console.main.ConsoleUI'):
self.assertRaises(SystemExit, self.exec_command) self.assertRaises(SystemExit, self.exec_command)
std_output = fd.out.getvalue() std_output = fd.out.getvalue()
self.assertTrue('usage: info' in std_output) self.assertIn('usage: info', std_output)
self.assertTrue('Show information about the torrents' in std_output) self.assertIn('Show information about the torrents', std_output)
def test_console_unrecognized_arguments(self): def test_console_unrecognized_arguments(self):
self.patch( self.patch(
@ -366,7 +364,7 @@ class ConsoleUIBaseTestCase(UIBaseTestCase):
self.patch(argparse._sys, 'stderr', fd) self.patch(argparse._sys, 'stderr', fd)
with mock.patch('deluge.ui.console.main.ConsoleUI'): with mock.patch('deluge.ui.console.main.ConsoleUI'):
self.assertRaises(SystemExit, self.exec_command) self.assertRaises(SystemExit, self.exec_command)
self.assertTrue('unrecognized arguments: --ui' in fd.out.getvalue()) self.assertIn('unrecognized arguments: --ui', fd.out.getvalue())
class ConsoleUIWithDaemonBaseTestCase(UIWithDaemonBaseTestCase): class ConsoleUIWithDaemonBaseTestCase(UIWithDaemonBaseTestCase):
@ -386,7 +384,7 @@ class ConsoleUIWithDaemonBaseTestCase(UIWithDaemonBaseTestCase):
'argv', 'argv',
self.var['sys_arg_cmd'] self.var['sys_arg_cmd']
+ ['--port'] + ['--port']
+ ['58900'] + [str(self.listen_port)]
+ ['--username'] + ['--username']
+ [username] + [username]
+ ['--password'] + ['--password']
@ -404,9 +402,8 @@ class ConsoleUIWithDaemonBaseTestCase(UIWithDaemonBaseTestCase):
yield self.exec_command() yield self.exec_command()
std_output = fd.out.getvalue() std_output = fd.out.getvalue()
self.assertTrue( self.assertEqual(
std_output std_output, 'Attempting to add torrent: ' + filename + '\nTorrent added!\n'
== 'Attempting to add torrent: ' + filename + '\nTorrent added!\n'
) )
@defer.inlineCallbacks @defer.inlineCallbacks
@ -441,10 +438,8 @@ class ConsoleUIWithDaemonBaseTestCase(UIWithDaemonBaseTestCase):
yield self.exec_command() yield self.exec_command()
std_output = fd.out.getvalue() std_output = fd.out.getvalue()
self.assertTrue( self.assertTrue(std_output.startswith('Total upload: '))
std_output.startswith('Total upload: ') self.assertTrue(std_output.endswith(' Moving: 0\n'))
and std_output.endswith(' Moving: 0\n')
)
@defer.inlineCallbacks @defer.inlineCallbacks
def test_console_command_config_set_download_location(self): def test_console_command_config_set_download_location(self):
@ -460,7 +455,9 @@ class ConsoleUIWithDaemonBaseTestCase(UIWithDaemonBaseTestCase):
'u' if PY2 else '' 'u' if PY2 else ''
) )
) )
and std_output.endswith('Configuration value successfully updated.\n') )
self.assertTrue(
std_output.endswith('Configuration value successfully updated.\n')
) )