Skip to content

Commit

Permalink
Patch in modern unittest methods for Python 2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
benmwebb committed Apr 28, 2020
1 parent 4cbe557 commit 8b3c585
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/backend/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
except ImportError:
from email.MIMEText import MIMEText # python2
import re
import testutil

basic_config = """
[general]
Expand Down
1 change: 1 addition & 0 deletions test/backend/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from saliweb.backend import Job, MySQLField
import saliweb.backend
from memory_database import MemoryDatabase
import testutil

def make_test_jobs(sql):
c = sql.cursor()
Expand Down
1 change: 1 addition & 0 deletions test/backend/test_delete_all_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from io import StringIO
else:
from io import BytesIO as StringIO
import testutil


class DummyWeb(object):
Expand Down
1 change: 1 addition & 0 deletions test/backend/test_do_nothing_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
from saliweb.backend import DoNothingRunner
import saliweb.backend.events
import testutil

class DummyJob(object):
def _try_complete(self, webservice, run_exception):
Expand Down
1 change: 1 addition & 0 deletions test/backend/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import socket
import saliweb.backend.events
import testutil

class EventsTest(unittest.TestCase):
"""Check events"""
Expand Down
1 change: 1 addition & 0 deletions test/backend/test_metadata.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from saliweb.backend import _JobMetadata
import testutil

class MetadataTest(unittest.TestCase):
"""Check JobMetadata class"""
Expand Down
1 change: 1 addition & 0 deletions test/backend/test_sge.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import saliweb.backend
import saliweb.backend.events
from saliweb.backend.sge import _DRMAAJobWaiter, _SGETasks, _DRMAAWrapper
import testutil

class SGETest(unittest.TestCase):
"""Check SGE utility classes"""
Expand Down
1 change: 1 addition & 0 deletions test/backend/test_web_service_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import xml.parsers.expat
import subprocess
import io
import testutil


class MockURL(object):
Expand Down
64 changes: 64 additions & 0 deletions test/build/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,70 @@
import glob
import shutil
import contextlib
import unittest


# If we're using Python 2.6, 3.0, or 3.1, add in more modern unittest
# convenience methods
if not hasattr(unittest.TestCase, 'assertIsInstance'):
def assertIn(self, member, container, msg=None):
return self.assertTrue(member in container,
msg or '%s not found in %s' % (member, container))
def assertNotIn(self, member, container, msg=None):
return self.assertTrue(member not in container,
msg or '%s unexpectedly found in %s'
% (member, container))
def assertIs(self, a, b, msg=None):
return self.assertTrue(a is b, msg or '%s is not %s' % (a, b))
def assertIsInstance(self, obj, cls, msg=None):
return self.assertTrue(isinstance(obj, cls),
msg or '%s is not an instance of %s' % (obj, cls))
def assertLess(self, a, b, msg=None):
return self.assertTrue(a < b, msg or '%s not less than %s' % (a, b))
def assertGreater(self, a, b, msg=None):
return self.assertTrue(a > b, msg or '%s not greater than %s' % (a, b))
def assertLessEqual(self, a, b, msg=None):
return self.assertTrue(a <= b,
msg or '%s not less than or equal to %s' % (a, b))
def assertGreaterEqual(self, a, b, msg=None):
return self.assertTrue(a >= b,
msg or '%s not greater than or equal to %s' % (a, b))
def assertIsNone(self, obj, msg=None):
return self.assertTrue(obj is None, msg or '%s is not None' % obj)
def assertIsNotNone(self, obj, msg=None):
return self.assertTrue(obj is not None, msg or 'unexpectedly None')
def assertAlmostEqual(self, first, second, places=None, msg=None,
delta=None):
if first == second:
return
if delta is not None and places is not None:
raise TypeError("specify delta or places not both")
diff = abs(first - second)
if delta is not None:
if diff <= delta:
return
standard_msg = ("%s != %s within %s delta (%s difference)"
% (first, second, delta, diff))
else:
if places is None:
places = 7
if round(diff, places) == 0:
return
standard_msg = ("%s != %s within %r places (%s difference)"
% (first, second, places, diff))
raise self.failureException(msg or standard_msg)
unittest.TestCase.assertIn = assertIn
unittest.TestCase.assertNotIn = assertNotIn
unittest.TestCase.assertIs = assertIs
unittest.TestCase.assertIsInstance = assertIsInstance
unittest.TestCase.assertLess = assertLess
unittest.TestCase.assertGreater = assertGreater
unittest.TestCase.assertLessEqual = assertLessEqual
unittest.TestCase.assertGreaterEqual = assertGreaterEqual
unittest.TestCase.assertIsNone = assertIsNone
unittest.TestCase.assertIsNotNone = assertIsNotNone
unittest.TestCase.assertAlmostEqual = assertAlmostEqual


def run_catch_warnings(method, *args, **keys):
"""Run a method and return both its own return value and a list of any
Expand Down

0 comments on commit 8b3c585

Please sign in to comment.