Best Python code snippet using locust
test_case.py
Source:test_case.py  
1import difflib2import pprint3import pickle4import re5import sys67from copy import deepcopy8from test import test_support910import unittest1112from unittest.test.support import (13    TestEquality, TestHashing, LoggingResult, ResultWithNoStartTestRunStopTestRun14)151617class Test(object):18    "Keep these TestCase classes out of the main namespace"1920    class Foo(unittest.TestCase):21        def runTest(self): pass22        def test1(self): pass2324    class Bar(Foo):25        def test2(self): pass2627    class LoggingTestCase(unittest.TestCase):28        """A test case which logs its calls."""2930        def __init__(self, events):31            super(Test.LoggingTestCase, self).__init__('test')32            self.events = events3334        def setUp(self):35            self.events.append('setUp')3637        def test(self):38            self.events.append('test')3940        def tearDown(self):41            self.events.append('tearDown')424344class Test_TestCase(unittest.TestCase, TestEquality, TestHashing):4546    ### Set up attributes used by inherited tests47    ################################################################4849    # Used by TestHashing.test_hash and TestEquality.test_eq50    eq_pairs = [(Test.Foo('test1'), Test.Foo('test1'))]5152    # Used by TestEquality.test_ne53    ne_pairs = [(Test.Foo('test1'), Test.Foo('runTest'))54               ,(Test.Foo('test1'), Test.Bar('test1'))55               ,(Test.Foo('test1'), Test.Bar('test2'))]5657    ################################################################58    ### /Set up attributes used by inherited tests596061    # "class TestCase([methodName])"62    # ...63    # "Each instance of TestCase will run a single test method: the64    # method named methodName."65    # ...66    # "methodName defaults to "runTest"."67    #68    # Make sure it really is optional, and that it defaults to the proper69    # thing.70    def test_init__no_test_name(self):71        class Test(unittest.TestCase):72            def runTest(self): raise TypeError()73            def test(self): pass7475        self.assertEqual(Test().id()[-13:], '.Test.runTest')7677    # "class TestCase([methodName])"78    # ...79    # "Each instance of TestCase will run a single test method: the80    # method named methodName."81    def test_init__test_name__valid(self):82        class Test(unittest.TestCase):83            def runTest(self): raise TypeError()84            def test(self): pass8586        self.assertEqual(Test('test').id()[-10:], '.Test.test')8788    # "class TestCase([methodName])"89    # ...90    # "Each instance of TestCase will run a single test method: the91    # method named methodName."92    def test_init__test_name__invalid(self):93        class Test(unittest.TestCase):94            def runTest(self): raise TypeError()95            def test(self): pass9697        try:98            Test('testfoo')99        except ValueError:100            pass101        else:102            self.fail("Failed to raise ValueError")103104    # "Return the number of tests represented by the this test object. For105    # TestCase instances, this will always be 1"106    def test_countTestCases(self):107        class Foo(unittest.TestCase):108            def test(self): pass109110        self.assertEqual(Foo('test').countTestCases(), 1)111112    # "Return the default type of test result object to be used to run this113    # test. For TestCase instances, this will always be114    # unittest.TestResult;  subclasses of TestCase should115    # override this as necessary."116    def test_defaultTestResult(self):117        class Foo(unittest.TestCase):118            def runTest(self):119                pass120121        result = Foo().defaultTestResult()122        self.assertEqual(type(result), unittest.TestResult)123124    # "When a setUp() method is defined, the test runner will run that method125    # prior to each test. Likewise, if a tearDown() method is defined, the126    # test runner will invoke that method after each test. In the example,127    # setUp() was used to create a fresh sequence for each test."128    #129    # Make sure the proper call order is maintained, even if setUp() raises130    # an exception.131    def test_run_call_order__error_in_setUp(self):132        events = []133        result = LoggingResult(events)134135        class Foo(Test.LoggingTestCase):136            def setUp(self):137                super(Foo, self).setUp()138                raise RuntimeError('raised by Foo.setUp')139140        Foo(events).run(result)141        expected = ['startTest', 'setUp', 'addError', 'stopTest']142        self.assertEqual(events, expected)143144    # "With a temporary result stopTestRun is called when setUp errors.145    def test_run_call_order__error_in_setUp_default_result(self):146        events = []147148        class Foo(Test.LoggingTestCase):149            def defaultTestResult(self):150                return LoggingResult(self.events)151152            def setUp(self):153                super(Foo, self).setUp()154                raise RuntimeError('raised by Foo.setUp')155156        Foo(events).run()157        expected = ['startTestRun', 'startTest', 'setUp', 'addError',158                    'stopTest', 'stopTestRun']159        self.assertEqual(events, expected)160161    # "When a setUp() method is defined, the test runner will run that method162    # prior to each test. Likewise, if a tearDown() method is defined, the163    # test runner will invoke that method after each test. In the example,164    # setUp() was used to create a fresh sequence for each test."165    #166    # Make sure the proper call order is maintained, even if the test raises167    # an error (as opposed to a failure).168    def test_run_call_order__error_in_test(self):169        events = []170        result = LoggingResult(events)171172        class Foo(Test.LoggingTestCase):173            def test(self):174                super(Foo, self).test()175                raise RuntimeError('raised by Foo.test')176177        expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown',178                    'stopTest']179        Foo(events).run(result)180        self.assertEqual(events, expected)181182    # "With a default result, an error in the test still results in stopTestRun183    # being called."184    def test_run_call_order__error_in_test_default_result(self):185        events = []186187        class Foo(Test.LoggingTestCase):188            def defaultTestResult(self):189                return LoggingResult(self.events)190191            def test(self):192                super(Foo, self).test()193                raise RuntimeError('raised by Foo.test')194195        expected = ['startTestRun', 'startTest', 'setUp', 'test', 'addError',196                    'tearDown', 'stopTest', 'stopTestRun']197        Foo(events).run()198        self.assertEqual(events, expected)199200    # "When a setUp() method is defined, the test runner will run that method201    # prior to each test. Likewise, if a tearDown() method is defined, the202    # test runner will invoke that method after each test. In the example,203    # setUp() was used to create a fresh sequence for each test."204    #205    # Make sure the proper call order is maintained, even if the test signals206    # a failure (as opposed to an error).207    def test_run_call_order__failure_in_test(self):208        events = []209        result = LoggingResult(events)210211        class Foo(Test.LoggingTestCase):212            def test(self):213                super(Foo, self).test()214                self.fail('raised by Foo.test')215216        expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown',217                    'stopTest']218        Foo(events).run(result)219        self.assertEqual(events, expected)220221    # "When a test fails with a default result stopTestRun is still called."222    def test_run_call_order__failure_in_test_default_result(self):223224        class Foo(Test.LoggingTestCase):225            def defaultTestResult(self):226                return LoggingResult(self.events)227            def test(self):228                super(Foo, self).test()229                self.fail('raised by Foo.test')230231        expected = ['startTestRun', 'startTest', 'setUp', 'test', 'addFailure',232                    'tearDown', 'stopTest', 'stopTestRun']233        events = []234        Foo(events).run()235        self.assertEqual(events, expected)236237    # "When a setUp() method is defined, the test runner will run that method238    # prior to each test. Likewise, if a tearDown() method is defined, the239    # test runner will invoke that method after each test. In the example,240    # setUp() was used to create a fresh sequence for each test."241    #242    # Make sure the proper call order is maintained, even if tearDown() raises243    # an exception.244    def test_run_call_order__error_in_tearDown(self):245        events = []246        result = LoggingResult(events)247248        class Foo(Test.LoggingTestCase):249            def tearDown(self):250                super(Foo, self).tearDown()251                raise RuntimeError('raised by Foo.tearDown')252253        Foo(events).run(result)254        expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',255                    'stopTest']256        self.assertEqual(events, expected)257258    # "When tearDown errors with a default result stopTestRun is still called."259    def test_run_call_order__error_in_tearDown_default_result(self):260261        class Foo(Test.LoggingTestCase):262            def defaultTestResult(self):263                return LoggingResult(self.events)264            def tearDown(self):265                super(Foo, self).tearDown()266                raise RuntimeError('raised by Foo.tearDown')267268        events = []269        Foo(events).run()270        expected = ['startTestRun', 'startTest', 'setUp', 'test', 'tearDown',271                    'addError', 'stopTest', 'stopTestRun']272        self.assertEqual(events, expected)273274    # "TestCase.run() still works when the defaultTestResult is a TestResult275    # that does not support startTestRun and stopTestRun.276    def test_run_call_order_default_result(self):277278        class Foo(unittest.TestCase):279            def defaultTestResult(self):280                return ResultWithNoStartTestRunStopTestRun()281            def test(self):282                pass283284        Foo('test').run()285286    # "This class attribute gives the exception raised by the test() method.287    # If a test framework needs to use a specialized exception, possibly to288    # carry additional information, it must subclass this exception in289    # order to ``play fair'' with the framework.  The initial value of this290    # attribute is AssertionError"291    def test_failureException__default(self):292        class Foo(unittest.TestCase):293            def test(self):294                pass295296        self.assertIs(Foo('test').failureException, AssertionError)297298    # "This class attribute gives the exception raised by the test() method.299    # If a test framework needs to use a specialized exception, possibly to300    # carry additional information, it must subclass this exception in301    # order to ``play fair'' with the framework."302    #303    # Make sure TestCase.run() respects the designated failureException304    def test_failureException__subclassing__explicit_raise(self):305        events = []306        result = LoggingResult(events)307308        class Foo(unittest.TestCase):309            def test(self):310                raise RuntimeError()311312            failureException = RuntimeError313314        self.assertIs(Foo('test').failureException, RuntimeError)315316317        Foo('test').run(result)318        expected = ['startTest', 'addFailure', 'stopTest']319        self.assertEqual(events, expected)320321    # "This class attribute gives the exception raised by the test() method.322    # If a test framework needs to use a specialized exception, possibly to323    # carry additional information, it must subclass this exception in324    # order to ``play fair'' with the framework."325    #326    # Make sure TestCase.run() respects the designated failureException327    def test_failureException__subclassing__implicit_raise(self):328        events = []329        result = LoggingResult(events)330331        class Foo(unittest.TestCase):332            def test(self):333                self.fail("foo")334335            failureException = RuntimeError336337        self.assertIs(Foo('test').failureException, RuntimeError)338339340        Foo('test').run(result)341        expected = ['startTest', 'addFailure', 'stopTest']342        self.assertEqual(events, expected)343344    # "The default implementation does nothing."345    def test_setUp(self):346        class Foo(unittest.TestCase):347            def runTest(self):348                pass349350        # ... and nothing should happen351        Foo().setUp()352353    # "The default implementation does nothing."354    def test_tearDown(self):355        class Foo(unittest.TestCase):356            def runTest(self):357                pass358359        # ... and nothing should happen360        Foo().tearDown()361362    # "Return a string identifying the specific test case."363    #364    # Because of the vague nature of the docs, I'm not going to lock this365    # test down too much. Really all that can be asserted is that the id()366    # will be a string (either 8-byte or unicode -- again, because the docs367    # just say "string")368    def test_id(self):369        class Foo(unittest.TestCase):370            def runTest(self):371                pass372373        self.assertIsInstance(Foo().id(), basestring)374375    # "If result is omitted or None, a temporary result object is created376    # and used, but is not made available to the caller. As TestCase owns the377    # temporary result startTestRun and stopTestRun are called.378379    def test_run__uses_defaultTestResult(self):380        events = []381382        class Foo(unittest.TestCase):383            def test(self):384                events.append('test')385386            def defaultTestResult(self):387                return LoggingResult(events)388389        # Make run() find a result object on its own390        Foo('test').run()391392        expected = ['startTestRun', 'startTest', 'test', 'addSuccess',393            'stopTest', 'stopTestRun']394        self.assertEqual(events, expected)395396    def testShortDescriptionWithoutDocstring(self):397        self.assertIsNone(self.shortDescription())398399    @unittest.skipIf(sys.flags.optimize >= 2,400                     "Docstrings are omitted with -O2 and above")401    def testShortDescriptionWithOneLineDocstring(self):402        """Tests shortDescription() for a method with a docstring."""403        self.assertEqual(404                self.shortDescription(),405                'Tests shortDescription() for a method with a docstring.')406407    @unittest.skipIf(sys.flags.optimize >= 2,408                     "Docstrings are omitted with -O2 and above")409    def testShortDescriptionWithMultiLineDocstring(self):410        """Tests shortDescription() for a method with a longer docstring.411412        This method ensures that only the first line of a docstring is413        returned used in the short description, no matter how long the414        whole thing is.415        """416        self.assertEqual(417                self.shortDescription(),418                 'Tests shortDescription() for a method with a longer '419                 'docstring.')420421    def testAddTypeEqualityFunc(self):422        class SadSnake(object):423            """Dummy class for test_addTypeEqualityFunc."""424        s1, s2 = SadSnake(), SadSnake()425        self.assertNotEqual(s1, s2)426        def AllSnakesCreatedEqual(a, b, msg=None):427            return type(a) is type(b) is SadSnake428        self.addTypeEqualityFunc(SadSnake, AllSnakesCreatedEqual)429        self.assertEqual(s1, s2)430        # No this doesn't clean up and remove the SadSnake equality func431        # from this TestCase instance but since its a local nothing else432        # will ever notice that.433434    def testAssertIs(self):435        thing = object()436        self.assertIs(thing, thing)437        self.assertRaises(self.failureException, self.assertIs, thing, object())438439    def testAssertIsNot(self):440        thing = object()441        self.assertIsNot(thing, object())442        self.assertRaises(self.failureException, self.assertIsNot, thing, thing)443444    def testAssertIsInstance(self):445        thing = []446        self.assertIsInstance(thing, list)447        self.assertRaises(self.failureException, self.assertIsInstance,448                          thing, dict)449450    def testAssertNotIsInstance(self):451        thing = []452        self.assertNotIsInstance(thing, dict)453        self.assertRaises(self.failureException, self.assertNotIsInstance,454                          thing, list)455456    def testAssertIn(self):457        animals = {'monkey': 'banana', 'cow': 'grass', 'seal': 'fish'}458459        self.assertIn('a', 'abc')460        self.assertIn(2, [1, 2, 3])461        self.assertIn('monkey', animals)462463        self.assertNotIn('d', 'abc')464        self.assertNotIn(0, [1, 2, 3])465        self.assertNotIn('otter', animals)466467        self.assertRaises(self.failureException, self.assertIn, 'x', 'abc')468        self.assertRaises(self.failureException, self.assertIn, 4, [1, 2, 3])469        self.assertRaises(self.failureException, self.assertIn, 'elephant',470                          animals)471472        self.assertRaises(self.failureException, self.assertNotIn, 'c', 'abc')473        self.assertRaises(self.failureException, self.assertNotIn, 1, [1, 2, 3])474        self.assertRaises(self.failureException, self.assertNotIn, 'cow',475                          animals)476477    def testAssertDictContainsSubset(self):478        self.assertDictContainsSubset({}, {})479        self.assertDictContainsSubset({}, {'a': 1})480        self.assertDictContainsSubset({'a': 1}, {'a': 1})481        self.assertDictContainsSubset({'a': 1}, {'a': 1, 'b': 2})482        self.assertDictContainsSubset({'a': 1, 'b': 2}, {'a': 1, 'b': 2})483484        with self.assertRaises(self.failureException):485            self.assertDictContainsSubset({1: "one"}, {})486487        with self.assertRaises(self.failureException):488            self.assertDictContainsSubset({'a': 2}, {'a': 1})489490        with self.assertRaises(self.failureException):491            self.assertDictContainsSubset({'c': 1}, {'a': 1})492493        with self.assertRaises(self.failureException):494            self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1})495496        with self.assertRaises(self.failureException):497            self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1})498499        with test_support.check_warnings(("", UnicodeWarning)):500            one = ''.join(chr(i) for i in range(255))501            # this used to cause a UnicodeDecodeError constructing the failure msg502            with self.assertRaises(self.failureException):503                self.assertDictContainsSubset({'foo': one}, {'foo': u'\uFFFD'})504505    def testAssertEqual(self):506        equal_pairs = [507                ((), ()),508                ({}, {}),509                ([], []),510                (set(), set()),511                (frozenset(), frozenset())]512        for a, b in equal_pairs:513            # This mess of try excepts is to test the assertEqual behavior514            # itself.515            try:516                self.assertEqual(a, b)517            except self.failureException:518                self.fail('assertEqual(%r, %r) failed' % (a, b))519            try:520                self.assertEqual(a, b, msg='foo')521            except self.failureException:522                self.fail('assertEqual(%r, %r) with msg= failed' % (a, b))523            try:524                self.assertEqual(a, b, 'foo')525            except self.failureException:526                self.fail('assertEqual(%r, %r) with third parameter failed' %527                          (a, b))528529        unequal_pairs = [530               ((), []),531               ({}, set()),532               (set([4,1]), frozenset([4,2])),533               (frozenset([4,5]), set([2,3])),534               (set([3,4]), set([5,4]))]535        for a, b in unequal_pairs:536            self.assertRaises(self.failureException, self.assertEqual, a, b)537            self.assertRaises(self.failureException, self.assertEqual, a, b,538                              'foo')539            self.assertRaises(self.failureException, self.assertEqual, a, b,540                              msg='foo')541542    def testEquality(self):543        self.assertListEqual([], [])544        self.assertTupleEqual((), ())545        self.assertSequenceEqual([], ())546547        a = [0, 'a', []]548        b = []549        self.assertRaises(unittest.TestCase.failureException,550                          self.assertListEqual, a, b)551        self.assertRaises(unittest.TestCase.failureException,552                          self.assertListEqual, tuple(a), tuple(b))553        self.assertRaises(unittest.TestCase.failureException,554                          self.assertSequenceEqual, a, tuple(b))555556        b.extend(a)557        self.assertListEqual(a, b)558        self.assertTupleEqual(tuple(a), tuple(b))559        self.assertSequenceEqual(a, tuple(b))560        self.assertSequenceEqual(tuple(a), b)561562        self.assertRaises(self.failureException, self.assertListEqual,563                          a, tuple(b))564        self.assertRaises(self.failureException, self.assertTupleEqual,565                          tuple(a), b)566        self.assertRaises(self.failureException, self.assertListEqual, None, b)567        self.assertRaises(self.failureException, self.assertTupleEqual, None,568                          tuple(b))569        self.assertRaises(self.failureException, self.assertSequenceEqual,570                          None, tuple(b))571        self.assertRaises(self.failureException, self.assertListEqual, 1, 1)572        self.assertRaises(self.failureException, self.assertTupleEqual, 1, 1)573        self.assertRaises(self.failureException, self.assertSequenceEqual,574                          1, 1)575576        self.assertDictEqual({}, {})577578        c = { 'x': 1 }579        d = {}580        self.assertRaises(unittest.TestCase.failureException,581                          self.assertDictEqual, c, d)582583        d.update(c)584        self.assertDictEqual(c, d)585586        d['x'] = 0587        self.assertRaises(unittest.TestCase.failureException,588                          self.assertDictEqual, c, d, 'These are unequal')589590        self.assertRaises(self.failureException, self.assertDictEqual, None, d)591        self.assertRaises(self.failureException, self.assertDictEqual, [], d)592        self.assertRaises(self.failureException, self.assertDictEqual, 1, 1)593594    def testAssertSequenceEqualMaxDiff(self):595        self.assertEqual(self.maxDiff, 80*8)596        seq1 = 'a' + 'x' * 80**2597        seq2 = 'b' + 'x' * 80**2598        diff = '\n'.join(difflib.ndiff(pprint.pformat(seq1).splitlines(),599                                       pprint.pformat(seq2).splitlines()))600        # the +1 is the leading \n added by assertSequenceEqual601        omitted = unittest.case.DIFF_OMITTED % (len(diff) + 1,)602603        self.maxDiff = len(diff)//2604        try:605            self.assertSequenceEqual(seq1, seq2)606        except self.failureException as e:607            msg = e.args[0]608        else:609            self.fail('assertSequenceEqual did not fail.')610        self.assertLess(len(msg), len(diff))611        self.assertIn(omitted, msg)612613        self.maxDiff = len(diff) * 2614        try:615            self.assertSequenceEqual(seq1, seq2)616        except self.failureException as e:617            msg = e.args[0]618        else:619            self.fail('assertSequenceEqual did not fail.')620        self.assertGreater(len(msg), len(diff))621        self.assertNotIn(omitted, msg)622623        self.maxDiff = None624        try:625            self.assertSequenceEqual(seq1, seq2)626        except self.failureException as e:627            msg = e.args[0]628        else:629            self.fail('assertSequenceEqual did not fail.')630        self.assertGreater(len(msg), len(diff))631        self.assertNotIn(omitted, msg)632633    def testTruncateMessage(self):634        self.maxDiff = 1635        message = self._truncateMessage('foo', 'bar')636        omitted = unittest.case.DIFF_OMITTED % len('bar')637        self.assertEqual(message, 'foo' + omitted)638639        self.maxDiff = None640        message = self._truncateMessage('foo', 'bar')641        self.assertEqual(message, 'foobar')642643        self.maxDiff = 4644        message = self._truncateMessage('foo', 'bar')645        self.assertEqual(message, 'foobar')646647    def testAssertDictEqualTruncates(self):648        test = unittest.TestCase('assertEqual')649        def truncate(msg, diff):650            return 'foo'651        test._truncateMessage = truncate652        try:653            test.assertDictEqual({}, {1: 0})654        except self.failureException as e:655            self.assertEqual(str(e), 'foo')656        else:657            self.fail('assertDictEqual did not fail')658659    def testAssertMultiLineEqualTruncates(self):660        test = unittest.TestCase('assertEqual')661        def truncate(msg, diff):662            return 'foo'663        test._truncateMessage = truncate664        try:665            test.assertMultiLineEqual('foo', 'bar')666        except self.failureException as e:667            self.assertEqual(str(e), 'foo')668        else:669            self.fail('assertMultiLineEqual did not fail')670671    def testAssertEqual_diffThreshold(self):672        # check threshold value673        self.assertEqual(self._diffThreshold, 2**16)674        # disable madDiff to get diff markers675        self.maxDiff = None676677        # set a lower threshold value and add a cleanup to restore it678        old_threshold = self._diffThreshold679        self._diffThreshold = 2**8680        self.addCleanup(lambda: setattr(self, '_diffThreshold', old_threshold))681682        # under the threshold: diff marker (^) in error message683        s = u'x' * (2**7)684        with self.assertRaises(self.failureException) as cm:685            self.assertEqual(s + 'a', s + 'b')686        self.assertIn('^', str(cm.exception))687        self.assertEqual(s + 'a', s + 'a')688689        # over the threshold: diff not used and marker (^) not in error message690        s = u'x' * (2**9)691        # if the path that uses difflib is taken, _truncateMessage will be692        # called -- replace it with explodingTruncation to verify that this693        # doesn't happen694        def explodingTruncation(message, diff):695            raise SystemError('this should not be raised')696        old_truncate = self._truncateMessage697        self._truncateMessage = explodingTruncation698        self.addCleanup(lambda: setattr(self, '_truncateMessage', old_truncate))699700        s1, s2 = s + 'a', s + 'b'701        with self.assertRaises(self.failureException) as cm:702            self.assertEqual(s1, s2)703        self.assertNotIn('^', str(cm.exception))704        self.assertEqual(str(cm.exception), '%r != %r' % (s1, s2))705        self.assertEqual(s + 'a', s + 'a')706707    def testAssertItemsEqual(self):708        a = object()709        self.assertItemsEqual([1, 2, 3], [3, 2, 1])710        self.assertItemsEqual(['foo', 'bar', 'baz'], ['bar', 'baz', 'foo'])711        self.assertItemsEqual([a, a, 2, 2, 3], (a, 2, 3, a, 2))712        self.assertItemsEqual([1, "2", "a", "a"], ["a", "2", True, "a"])713        self.assertRaises(self.failureException, self.assertItemsEqual,714                          [1, 2] + [3] * 100, [1] * 100 + [2, 3])715        self.assertRaises(self.failureException, self.assertItemsEqual,716                          [1, "2", "a", "a"], ["a", "2", True, 1])717        self.assertRaises(self.failureException, self.assertItemsEqual,718                          [10], [10, 11])719        self.assertRaises(self.failureException, self.assertItemsEqual,720                          [10, 11], [10])721        self.assertRaises(self.failureException, self.assertItemsEqual,722                          [10, 11, 10], [10, 11])723724        # Test that sequences of unhashable objects can be tested for sameness:725        self.assertItemsEqual([[1, 2], [3, 4], 0], [False, [3, 4], [1, 2]])726        # Test that iterator of unhashable objects can be tested for sameness:727        self.assertItemsEqual(iter([1, 2, [], 3, 4]),728                              iter([1, 2, [], 3, 4]))729730        # hashable types, but not orderable731        self.assertRaises(self.failureException, self.assertItemsEqual,732                          [], [divmod, 'x', 1, 5j, 2j, frozenset()])733        # comparing dicts734        self.assertItemsEqual([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}])735        # comparing heterogenous non-hashable sequences736        self.assertItemsEqual([1, 'x', divmod, []], [divmod, [], 'x', 1])737        self.assertRaises(self.failureException, self.assertItemsEqual,738                          [], [divmod, [], 'x', 1, 5j, 2j, set()])739        self.assertRaises(self.failureException, self.assertItemsEqual,740                          [[1]], [[2]])741742        # Same elements, but not same sequence length743        self.assertRaises(self.failureException, self.assertItemsEqual,744                          [1, 1, 2], [2, 1])745        self.assertRaises(self.failureException, self.assertItemsEqual,746                          [1, 1, "2", "a", "a"], ["2", "2", True, "a"])747        self.assertRaises(self.failureException, self.assertItemsEqual,748                          [1, {'b': 2}, None, True], [{'b': 2}, True, None])749750        # Same elements which don't reliably compare, in751        # different order, see issue 10242752        a = [{2,4}, {1,2}]753        b = a[::-1]754        self.assertItemsEqual(a, b)755756        # test utility functions supporting assertItemsEqual()757758        diffs = set(unittest.util._count_diff_all_purpose('aaabccd', 'abbbcce'))759        expected = {(3,1,'a'), (1,3,'b'), (1,0,'d'), (0,1,'e')}760        self.assertEqual(diffs, expected)761762        diffs = unittest.util._count_diff_all_purpose([[]], [])763        self.assertEqual(diffs, [(1, 0, [])])764765        diffs = set(unittest.util._count_diff_hashable('aaabccd', 'abbbcce'))766        expected = {(3,1,'a'), (1,3,'b'), (1,0,'d'), (0,1,'e')}767        self.assertEqual(diffs, expected)768769    def testAssertSetEqual(self):770        set1 = set()771        set2 = set()772        self.assertSetEqual(set1, set2)773774        self.assertRaises(self.failureException, self.assertSetEqual, None, set2)775        self.assertRaises(self.failureException, self.assertSetEqual, [], set2)776        self.assertRaises(self.failureException, self.assertSetEqual, set1, None)777        self.assertRaises(self.failureException, self.assertSetEqual, set1, [])778779        set1 = set(['a'])780        set2 = set()781        self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)782783        set1 = set(['a'])784        set2 = set(['a'])785        self.assertSetEqual(set1, set2)786787        set1 = set(['a'])788        set2 = set(['a', 'b'])789        self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)790791        set1 = set(['a'])792        set2 = frozenset(['a', 'b'])793        self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)794795        set1 = set(['a', 'b'])796        set2 = frozenset(['a', 'b'])797        self.assertSetEqual(set1, set2)798799        set1 = set()800        set2 = "foo"801        self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)802        self.assertRaises(self.failureException, self.assertSetEqual, set2, set1)803804        # make sure any string formatting is tuple-safe805        set1 = set([(0, 1), (2, 3)])806        set2 = set([(4, 5)])807        self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)808809    def testInequality(self):810        # Try ints811        self.assertGreater(2, 1)812        self.assertGreaterEqual(2, 1)813        self.assertGreaterEqual(1, 1)814        self.assertLess(1, 2)815        self.assertLessEqual(1, 2)816        self.assertLessEqual(1, 1)817        self.assertRaises(self.failureException, self.assertGreater, 1, 2)818        self.assertRaises(self.failureException, self.assertGreater, 1, 1)819        self.assertRaises(self.failureException, self.assertGreaterEqual, 1, 2)820        self.assertRaises(self.failureException, self.assertLess, 2, 1)821        self.assertRaises(self.failureException, self.assertLess, 1, 1)822        self.assertRaises(self.failureException, self.assertLessEqual, 2, 1)823824        # Try Floats825        self.assertGreater(1.1, 1.0)826        self.assertGreaterEqual(1.1, 1.0)827        self.assertGreaterEqual(1.0, 1.0)828        self.assertLess(1.0, 1.1)829        self.assertLessEqual(1.0, 1.1)830        self.assertLessEqual(1.0, 1.0)831        self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.1)832        self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.0)833        self.assertRaises(self.failureException, self.assertGreaterEqual, 1.0, 1.1)834        self.assertRaises(self.failureException, self.assertLess, 1.1, 1.0)835        self.assertRaises(self.failureException, self.assertLess, 1.0, 1.0)836        self.assertRaises(self.failureException, self.assertLessEqual, 1.1, 1.0)837838        # Try Strings839        self.assertGreater('bug', 'ant')840        self.assertGreaterEqual('bug', 'ant')841        self.assertGreaterEqual('ant', 'ant')842        self.assertLess('ant', 'bug')843        self.assertLessEqual('ant', 'bug')844        self.assertLessEqual('ant', 'ant')845        self.assertRaises(self.failureException, self.assertGreater, 'ant', 'bug')846        self.assertRaises(self.failureException, self.assertGreater, 'ant', 'ant')847        self.assertRaises(self.failureException, self.assertGreaterEqual, 'ant', 'bug')848        self.assertRaises(self.failureException, self.assertLess, 'bug', 'ant')849        self.assertRaises(self.failureException, self.assertLess, 'ant', 'ant')850        self.assertRaises(self.failureException, self.assertLessEqual, 'bug', 'ant')851852        # Try Unicode853        self.assertGreater(u'bug', u'ant')854        self.assertGreaterEqual(u'bug', u'ant')855        self.assertGreaterEqual(u'ant', u'ant')856        self.assertLess(u'ant', u'bug')857        self.assertLessEqual(u'ant', u'bug')858        self.assertLessEqual(u'ant', u'ant')859        self.assertRaises(self.failureException, self.assertGreater, u'ant', u'bug')860        self.assertRaises(self.failureException, self.assertGreater, u'ant', u'ant')861        self.assertRaises(self.failureException, self.assertGreaterEqual, u'ant',862                          u'bug')863        self.assertRaises(self.failureException, self.assertLess, u'bug', u'ant')864        self.assertRaises(self.failureException, self.assertLess, u'ant', u'ant')865        self.assertRaises(self.failureException, self.assertLessEqual, u'bug', u'ant')866867        # Try Mixed String/Unicode868        self.assertGreater('bug', u'ant')869        self.assertGreater(u'bug', 'ant')870        self.assertGreaterEqual('bug', u'ant')871        self.assertGreaterEqual(u'bug', 'ant')872        self.assertGreaterEqual('ant', u'ant')873        self.assertGreaterEqual(u'ant', 'ant')874        self.assertLess('ant', u'bug')875        self.assertLess(u'ant', 'bug')876        self.assertLessEqual('ant', u'bug')877        self.assertLessEqual(u'ant', 'bug')878        self.assertLessEqual('ant', u'ant')879        self.assertLessEqual(u'ant', 'ant')880        self.assertRaises(self.failureException, self.assertGreater, 'ant', u'bug')881        self.assertRaises(self.failureException, self.assertGreater, u'ant', 'bug')882        self.assertRaises(self.failureException, self.assertGreater, 'ant', u'ant')883        self.assertRaises(self.failureException, self.assertGreater, u'ant', 'ant')884        self.assertRaises(self.failureException, self.assertGreaterEqual, 'ant',885                          u'bug')886        self.assertRaises(self.failureException, self.assertGreaterEqual, u'ant',887                          'bug')888        self.assertRaises(self.failureException, self.assertLess, 'bug', u'ant')889        self.assertRaises(self.failureException, self.assertLess, u'bug', 'ant')890        self.assertRaises(self.failureException, self.assertLess, 'ant', u'ant')891        self.assertRaises(self.failureException, self.assertLess, u'ant', 'ant')892        self.assertRaises(self.failureException, self.assertLessEqual, 'bug', u'ant')893        self.assertRaises(self.failureException, self.assertLessEqual, u'bug', 'ant')894895    def testAssertMultiLineEqual(self):896        sample_text = b"""\897http://www.python.org/doc/2.3/lib/module-unittest.html898test case899    A test case is the smallest unit of testing. [...]900"""901        revised_sample_text = b"""\902http://www.python.org/doc/2.4.1/lib/module-unittest.html903test case904    A test case is the smallest unit of testing. [...] You may provide your905    own implementation that does not subclass from TestCase, of course.906"""907        sample_text_error = b"""\908- http://www.python.org/doc/2.3/lib/module-unittest.html909?                             ^910+ http://www.python.org/doc/2.4.1/lib/module-unittest.html911?                             ^^^912  test case913-     A test case is the smallest unit of testing. [...]914+     A test case is the smallest unit of testing. [...] You may provide your915?                                                       +++++++++++++++++++++916+     own implementation that does not subclass from TestCase, of course.917"""918        self.maxDiff = None919        for type_changer in (lambda x: x, lambda x: x.decode('utf8')):920            try:921                self.assertMultiLineEqual(type_changer(sample_text),922                                          type_changer(revised_sample_text))923            except self.failureException, e:924                # need to remove the first line of the error message925                error = str(e).encode('utf8').split('\n', 1)[1]926927                # assertMultiLineEqual is hooked up as the default for928                # unicode strings - so we can't use it for this check929                self.assertTrue(sample_text_error == error)930931    def testAsertEqualSingleLine(self):932        sample_text = u"laden swallows fly slowly"933        revised_sample_text = u"unladen swallows fly quickly"934        sample_text_error = """\935- laden swallows fly slowly936?                    ^^^^937+ unladen swallows fly quickly938? ++                   ^^^^^939"""940        try:941            self.assertEqual(sample_text, revised_sample_text)942        except self.failureException as e:943            error = str(e).split('\n', 1)[1]944            self.assertTrue(sample_text_error == error)945946    def testAssertIsNone(self):947        self.assertIsNone(None)948        self.assertRaises(self.failureException, self.assertIsNone, False)949        self.assertIsNotNone('DjZoPloGears on Rails')950        self.assertRaises(self.failureException, self.assertIsNotNone, None)951952    def testAssertRegexpMatches(self):953        self.assertRegexpMatches('asdfabasdf', r'ab+')954        self.assertRaises(self.failureException, self.assertRegexpMatches,955                          'saaas', r'aaaa')956957    def testAssertRaisesCallable(self):958        class ExceptionMock(Exception):959            pass960        def Stub():961            raise ExceptionMock('We expect')962        self.assertRaises(ExceptionMock, Stub)963        # A tuple of exception classes is accepted964        self.assertRaises((ValueError, ExceptionMock), Stub)965        # *args and **kwargs also work966        self.assertRaises(ValueError, int, '19', base=8)967        # Failure when no exception is raised968        with self.assertRaises(self.failureException):969            self.assertRaises(ExceptionMock, lambda: 0)970        # Failure when another exception is raised971        with self.assertRaises(ExceptionMock):972            self.assertRaises(ValueError, Stub)973974    def testAssertRaisesContext(self):975        class ExceptionMock(Exception):976            pass977        def Stub():978            raise ExceptionMock('We expect')979        with self.assertRaises(ExceptionMock):980            Stub()981        # A tuple of exception classes is accepted982        with self.assertRaises((ValueError, ExceptionMock)) as cm:983            Stub()984        # The context manager exposes caught exception985        self.assertIsInstance(cm.exception, ExceptionMock)986        self.assertEqual(cm.exception.args[0], 'We expect')987        # *args and **kwargs also work988        with self.assertRaises(ValueError):989            int('19', base=8)990        # Failure when no exception is raised991        with self.assertRaises(self.failureException):992            with self.assertRaises(ExceptionMock):993                pass994        # Failure when another exception is raised995        with self.assertRaises(ExceptionMock):996            self.assertRaises(ValueError, Stub)997998    def testAssertRaisesRegexp(self):999        class ExceptionMock(Exception):1000            pass10011002        def Stub():1003            raise ExceptionMock('We expect')10041005        self.assertRaisesRegexp(ExceptionMock, re.compile('expect$'), Stub)1006        self.assertRaisesRegexp(ExceptionMock, 'expect$', Stub)1007        self.assertRaisesRegexp(ExceptionMock, u'expect$', Stub)10081009    def testAssertNotRaisesRegexp(self):1010        self.assertRaisesRegexp(1011                self.failureException, '^Exception not raised$',1012                self.assertRaisesRegexp, Exception, re.compile('x'),1013                lambda: None)1014        self.assertRaisesRegexp(1015                self.failureException, '^Exception not raised$',1016                self.assertRaisesRegexp, Exception, 'x',1017                lambda: None)1018        self.assertRaisesRegexp(1019                self.failureException, '^Exception not raised$',1020                self.assertRaisesRegexp, Exception, u'x',1021                lambda: None)10221023    def testAssertRaisesRegexpInvalidRegexp(self):1024        # Issue 20145.1025        class MyExc(Exception):1026            pass1027        self.assertRaises(TypeError, self.assertRaisesRegexp, MyExc, lambda: True)10281029    def testAssertRaisesRegexpMismatch(self):1030        def Stub():1031            raise Exception('Unexpected')10321033        self.assertRaisesRegexp(1034                self.failureException,1035                r'"\^Expected\$" does not match "Unexpected"',1036                self.assertRaisesRegexp, Exception, '^Expected$',1037                Stub)1038        self.assertRaisesRegexp(1039                self.failureException,1040                r'"\^Expected\$" does not match "Unexpected"',1041                self.assertRaisesRegexp, Exception, u'^Expected$',1042                Stub)1043        self.assertRaisesRegexp(1044                self.failureException,1045                r'"\^Expected\$" does not match "Unexpected"',1046                self.assertRaisesRegexp, Exception,1047                re.compile('^Expected$'), Stub)10481049    def testAssertRaisesExcValue(self):1050        class ExceptionMock(Exception):1051            pass10521053        def Stub(foo):1054            raise ExceptionMock(foo)1055        v = "particular value"10561057        ctx = self.assertRaises(ExceptionMock)1058        with ctx:1059            Stub(v)1060        e = ctx.exception1061        self.assertIsInstance(e, ExceptionMock)1062        self.assertEqual(e.args[0], v)10631064    def testSynonymAssertMethodNames(self):1065        """Test undocumented method name synonyms.10661067        Please do not use these methods names in your own code.10681069        This test confirms their continued existence and functionality1070        in order to avoid breaking existing code.1071        """1072        self.assertNotEquals(3, 5)1073        self.assertEquals(3, 3)1074        self.assertAlmostEquals(2.0, 2.0)1075        self.assertNotAlmostEquals(3.0, 5.0)1076        self.assert_(True)10771078    def testPendingDeprecationMethodNames(self):1079        """Test fail* methods pending deprecation, they will warn in 3.2.10801081        Do not use these methods.  They will go away in 3.3.1082        """1083        with test_support.check_warnings():1084            self.failIfEqual(3, 5)1085            self.failUnlessEqual(3, 3)1086            self.failUnlessAlmostEqual(2.0, 2.0)1087            self.failIfAlmostEqual(3.0, 5.0)1088            self.failUnless(True)1089            self.failUnlessRaises(TypeError, lambda _: 3.14 + u'spam')1090            self.failIf(False)10911092    def testDeepcopy(self):1093        # Issue: 56601094        class TestableTest(unittest.TestCase):1095            def testNothing(self):1096                pass10971098        test = TestableTest('testNothing')10991100        # This shouldn't blow up1101        deepcopy(test)11021103    def testKeyboardInterrupt(self):1104        def _raise(self=None):1105            raise KeyboardInterrupt1106        def nothing(self):1107            pass11081109        class Test1(unittest.TestCase):1110            test_something = _raise11111112        class Test2(unittest.TestCase):1113            setUp = _raise1114            test_something = nothing11151116        class Test3(unittest.TestCase):1117            test_something = nothing1118            tearDown = _raise11191120        class Test4(unittest.TestCase):1121            def test_something(self):1122                self.addCleanup(_raise)11231124        for klass in (Test1, Test2, Test3, Test4):1125            with self.assertRaises(KeyboardInterrupt):1126                klass('test_something').run()11271128    def testSystemExit(self):1129        def _raise(self=None):1130            raise SystemExit1131        def nothing(self):1132            pass11331134        class Test1(unittest.TestCase):1135            test_something = _raise11361137        class Test2(unittest.TestCase):1138            setUp = _raise1139            test_something = nothing11401141        class Test3(unittest.TestCase):1142            test_something = nothing1143            tearDown = _raise11441145        class Test4(unittest.TestCase):1146            def test_something(self):1147                self.addCleanup(_raise)11481149        for klass in (Test1, Test2, Test3, Test4):1150            result = unittest.TestResult()1151            klass('test_something').run(result)1152            self.assertEqual(len(result.errors), 1)1153            self.assertEqual(result.testsRun, 1)11541155    def testPickle(self):1156        # Issue 1032611571158        # Can't use TestCase classes defined in Test class as1159        # pickle does not work with inner classes1160        test = unittest.TestCase('run')1161        for protocol in range(pickle.HIGHEST_PROTOCOL + 1):11621163            # blew up prior to fix1164            pickled_test = pickle.dumps(test, protocol=protocol)11651166            unpickled_test = pickle.loads(pickled_test)1167            self.assertEqual(test, unpickled_test)116811691170if __name__ == '__main__':
...failurereason.py
Source:failurereason.py  
1# -*- coding: utf-8 -*-2# $Id: failurereason.py $3"""4Test Manager - Failure Reasons.5"""6__copyright__ = \7"""8Copyright (C) 2012-2015 Oracle Corporation9This file is part of VirtualBox Open Source Edition (OSE), as10available from http://www.virtualbox.org. This file is free software;11you can redistribute it and/or modify it under the terms of the GNU12General Public License (GPL) as published by the Free Software13Foundation, in version 2 as it comes in the "COPYING" file of the14VirtualBox OSE distribution. VirtualBox OSE is distributed in the15hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.16The contents of this file may alternatively be used under the terms17of the Common Development and Distribution License Version 1.018(CDDL) only, as it comes in the "COPYING.CDDL" file of the19VirtualBox OSE distribution, in which case the provisions of the20CDDL are applicable instead of those of the GPL.21You may elect to license modified versions of this file under the22terms and conditions of either the GPL or the CDDL or both.23"""24__version__ = "$Revision: 100880 $"25# Validation Kit imports.26from testmanager.core.base          import ModelDataBase, ModelLogicBase, TMExceptionBase27class FailureReasonData(ModelDataBase):28    """29    Failure Reason Data.30    """31    ksIdAttr = 'idFailureReason';32    ksParam_idFailureReason   = 'FailureReasonData_idFailureReason'33    ksParam_tsEffective       = 'FailureReasonData_tsEffective'34    ksParam_tsExpire          = 'FailureReasonData_tsExpire'35    ksParam_uidAuthor         = 'FailureReasonData_uidAuthor'36    ksParam_idFailureCategory = 'FailureReasonData_idFailureCategory'37    ksParam_sShort            = 'FailureReasonData_sShort'38    ksParam_sFull             = 'FailureReasonData_sFull'39    ksParam_iTicket           = 'FailureReasonData_iTicket'40    ksParam_asUrls            = 'FailureReasonData_asUrls'41    kasAllowNullAttributes    = [ 'idFailureReason', 'tsEffective', 'tsExpire',42                                  'uidAuthor',       'iTicket',      'asUrls' ]43    def __init__(self):44        ModelDataBase.__init__(self);45        #46        # Initialize with defaults.47        # See the database for explanations of each of these fields.48        #49        self.idFailureReason   = None50        self.tsEffective       = None51        self.tsExpire          = None52        self.uidAuthor         = None53        self.idFailureCategory = None54        self.sShort            = None55        self.sFull             = None56        self.iTicket           = None57        self.asUrls            = None58    def initFromDbRow(self, aoRow):59        """60        Re-initializes the data with a row from a SELECT * FROM FailureReasons.61        Returns self. Raises exception if the row is None or otherwise invalid.62        """63        if aoRow is None:64            raise TMExceptionBase('Failure Reason not found.');65        self.idFailureReason   = aoRow[0]66        self.tsEffective       = aoRow[1]67        self.tsExpire          = aoRow[2]68        self.uidAuthor         = aoRow[3]69        self.idFailureCategory = aoRow[4]70        self.sShort            = aoRow[5]71        self.sFull             = aoRow[6]72        self.iTicket           = aoRow[7]73        self.asUrls            = aoRow[8]74        return self75class FailureReasonLogic(ModelLogicBase): # pylint: disable=R090376    """77    Failure Reason logic.78    """79    def fetchForListing(self, iStart, cMaxRows, tsNow):80        """81        Fetches Failure Category records.82        Returns an array (list) of FailureReasonData items, empty list if none.83        Raises exception on error.84        """85        if tsNow is None:86            self._oDb.execute('SELECT   *\n'87                              'FROM     FailureReasons\n'88                              'WHERE    tsExpire = \'infinity\'::TIMESTAMP\n'89                              'ORDER BY idFailureReason DESC\n'90                              'LIMIT %s OFFSET %s\n'91                              , (cMaxRows, iStart,));92        else:93            self._oDb.execute('SELECT   *\n'94                              'FROM     FailureReasons\n'95                              'WHERE    tsExpire     > %s\n'96                              '     AND tsEffective <= %s\n'97                              'ORDER BY idFailureReason DESC\n'98                              'LIMIT %s OFFSET %s\n'99                              , (tsNow, tsNow, cMaxRows, iStart,));100        aoRows = []101        for aoRow in self._oDb.fetchAll():102            aoRows.append(FailureReasonData().initFromDbRow(aoRow))103        return aoRows104    def fetchForCombo(self, tsEffective = None):105        """106        Gets the list of Failure Reasons for a combo box.107        Returns an array of (value [idFailureReason], drop-down-name [sShort],108        hover-text [sFull]) tuples.109        """110        if tsEffective is None:111            self._oDb.execute('SELECT   idFailureReason, sShort, sFull\n'112                              'FROM     FailureReasons\n'113                              'WHERE    tsExpire = \'infinity\'::TIMESTAMP\n'114                              'ORDER BY sShort')115        else:116            self._oDb.execute('SELECT   idFailureReason, sShort, sFull\n'117                              'FROM     FailureReasons\n'118                              'WHERE    tsExpire     > %s\n'119                              '     AND tsEffective <= %s\n'120                              'ORDER BY sShort'121                              , (tsEffective, tsEffective))122        return self._oDb.fetchAll()123    def getById(self, idFailureReason):124        """Get Failure Reason data by idFailureReason"""125        self._oDb.execute('SELECT   *\n'126                          'FROM     FailureReasons\n'127                          'WHERE    tsExpire   = \'infinity\'::timestamp\n'128                          '  AND    idFailureReason = %s;', (idFailureReason,))129        aRows = self._oDb.fetchAll()130        if len(aRows) not in (0, 1):131            raise self._oDb.integrityException(132                'Found more than one failure reasons with the same credentials. Database structure is corrupted.')133        try:134            return FailureReasonData().initFromDbRow(aRows[0])135        except IndexError:136            return None137    def getIdsByCategory(self, idFailureCategory, tsEffective=None):138        """139        Gets the list of Failure Ressons IDs,140        all the items belong to @param idFailureCategory141        """142        if tsEffective is None:143            self._oDb.execute('SELECT   idFailureReason\n'144                              'FROM     FailureReasons\n'145                              'WHERE    tsExpire = \'infinity\'::TIMESTAMP\n'146                              '     AND idFailureCategory = %s\n'147                              'ORDER BY idFailureReason DESC'148                              , (idFailureCategory,))149        else:150            self._oDb.execute('SELECT   idFailureReason\n'151                              'FROM     FailureReasons\n'152                              'WHERE    tsExpire     > %s\n'153                              '     AND tsEffective <= %s\n'154                              '     AND idFailureCategory = %s\n'155                              'ORDER BY idFailureReason DESC'156                              , (tsEffective, tsEffective, idFailureCategory))157        return self._oDb.fetchAll()158    def getAll(self, tsEffective=None):159        """160        Gets the list of all Failure Reasons.161        Returns an array of FailureReasonData instances.162        """163        if tsEffective is None:164            self._oDb.execute('SELECT   *\n'165                              'FROM     FailureReasons\n'166                              'WHERE    tsExpire = \'infinity\'::TIMESTAMP\n'167                              'ORDER BY idFailureReason DESC')168        else:169            self._oDb.execute('SELECT   *\n'170                              'FROM     FailureReasons\n'171                              'WHERE    tsExpire     > %s\n'172                              '     AND tsEffective <= %s\n'173                              'ORDER BY idFailureReason DESC'174                              , (tsEffective, tsEffective))175        aoRet = []176        for aoRow in self._oDb.fetchAll():177            aoRet.append(FailureReasonData().initFromDbRow(aoRow))178        return aoRet179    def addEntry(self, oFailureReasonData, uidAuthor, fCommit=True):180        """Add record to database"""181        # Check if record with the same sShort fiels is already exists182        self._oDb.execute('SELECT *\n'183                          'FROM   FailureReasons\n'184                          'WHERE  tsExpire   = \'infinity\'::TIMESTAMP\n'185                          '   AND sShort = %s\n',186                          (oFailureReasonData.sShort,))187        if len(self._oDb.fetchAll()) != 0:188            raise Exception('Record already exist')189        # Add record190        self._oDb.execute('INSERT INTO FailureReasons (\n'191                          '  uidAuthor, idFailureCategory,'192                          '  sShort, sFull, iTicket, asUrls'193                          ')\n'194                          'VALUES (%s, %s, %s, %s, %s, %s)',195                          (uidAuthor,196                           oFailureReasonData.idFailureCategory,197                           oFailureReasonData.sShort,198                           oFailureReasonData.sFull,199                           oFailureReasonData.iTicket,200                           oFailureReasonData.asUrls))201        if fCommit:202            self._oDb.commit()203        return True204    def remove(self, uidAuthor, idFailureReason, fNeedCommit=True):205        """206        Historize record207        """208        self._oDb.execute('UPDATE FailureReasons\n'209                          'SET    tsExpire    = CURRENT_TIMESTAMP,\n'210                          '       uidAuthor   = %s\n'211                          'WHERE  idFailureReason = %s\n'212                          '   AND tsExpire    = \'infinity\'::TIMESTAMP\n',213                          (uidAuthor, idFailureReason))214        # Also historize Black List records215        self._oDb.execute('UPDATE BuildBlackList\n'216                          'SET    tsExpire    = CURRENT_TIMESTAMP,\n'217                          '       uidAuthor   = %s\n'218                          'WHERE  idFailureReason = %s\n'219                          '   AND tsExpire    = \'infinity\'::TIMESTAMP\n',220                          (uidAuthor, idFailureReason))221        if fNeedCommit:222            self._oDb.commit()223        return True224    def editEntry(self, oFailureReasonData, uidAuthor, fCommit=True):225        """Modify database record"""226        # Check if record exists227        oFailureReasonDataOld = self.getById(oFailureReasonData.idFailureReason)228        if oFailureReasonDataOld is None:229            raise TMExceptionBase(230                'Failure Reason (id: %d) does not exist'231                % oFailureReasonData.idFailureReason)232        # Check if anything has been changed233        if oFailureReasonData.isEqual(oFailureReasonDataOld):234            return True235        # Historize record236        self.remove(237            uidAuthor, oFailureReasonData.idFailureReason, fNeedCommit=False)238        # Add new record (keeping its ID)239        self._oDb.execute('INSERT INTO FailureReasons (\n'240                          '  idFailureReason,'241                          '  uidAuthor,'242                          '  idFailureCategory,'243                          '  sShort,'244                          '  sFull,'245                          '  iTicket,'246                          '  asUrls'247                          ')\n'248                          'VALUES (%s, %s, %s, %s, %s, %s, %s)',249                          (oFailureReasonData.idFailureReason,250                           uidAuthor,251                           oFailureReasonData.idFailureCategory,252                           oFailureReasonData.sShort,253                           oFailureReasonData.sFull,254                           oFailureReasonData.iTicket,255                           oFailureReasonData.asUrls256                           ))257        if fCommit:258            self._oDb.commit()...failurecategory.py
Source:failurecategory.py  
1# -*- coding: utf-8 -*-2# $Id: failurecategory.py $3"""4Test Manager - Failure Categories.5"""6__copyright__ = \7"""8Copyright (C) 2012-2015 Oracle Corporation9This file is part of VirtualBox Open Source Edition (OSE), as10available from http://www.virtualbox.org. This file is free software;11you can redistribute it and/or modify it under the terms of the GNU12General Public License (GPL) as published by the Free Software13Foundation, in version 2 as it comes in the "COPYING" file of the14VirtualBox OSE distribution. VirtualBox OSE is distributed in the15hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.16The contents of this file may alternatively be used under the terms17of the Common Development and Distribution License Version 1.018(CDDL) only, as it comes in the "COPYING.CDDL" file of the19VirtualBox OSE distribution, in which case the provisions of the20CDDL are applicable instead of those of the GPL.21You may elect to license modified versions of this file under the22terms and conditions of either the GPL or the CDDL or both.23"""24__version__ = "$Revision: 100880 $"25# Validation Kit imports.26from testmanager.core.base          import ModelDataBase, ModelLogicBase, TMExceptionBase27from testmanager.core.failurereason import FailureReasonLogic28class FailureCategoryData(ModelDataBase):29    """30    Failure Category Data.31    """32    ksParam_idFailureCategory = 'FailureCategory_idFailureCategory'33    ksParam_tsEffective       = 'FailureCategory_tsEffective'34    ksParam_tsExpire          = 'FailureCategory_tsExpire'35    ksParam_uidAuthor         = 'FailureCategory_uidAuthor'36    ksParam_sShort            = 'FailureCategory_sShort'37    ksParam_sFull             = 'FailureCategory_sFull'38    kasAllowNullAttributes    = [ 'idFailureCategory', 'tsEffective', 'tsExpire', 'uidAuthor' ]39    def __init__(self):40        ModelDataBase.__init__(self);41        #42        # Initialize with defaults.43        # See the database for explanations of each of these fields.44        #45        self.idFailureCategory = None46        self.tsEffective       = None47        self.tsExpire          = None48        self.uidAuthor         = None49        self.sShort            = None50        self.sFull             = None51    def initFromDbRow(self, aoRow):52        """53        Re-initializes the data with a row from a SELECT * FROM FailureCategoryes.54        Returns self. Raises exception if the row is None or otherwise invalid.55        """56        if aoRow is None:57            raise TMExceptionBase('Failure Category not found.');58        self.idFailureCategory = aoRow[0]59        self.tsEffective       = aoRow[1]60        self.tsExpire          = aoRow[2]61        self.uidAuthor         = aoRow[3]62        self.sShort            = aoRow[4]63        self.sFull             = aoRow[5]64        return self65class FailureCategoryLogic(ModelLogicBase): # pylint: disable=R090366    """67    Failure Category logic.68    """69    def fetchForListing(self, iStart, cMaxRows, tsNow):70        """71        Fetches Failure Category records.72        Returns an array (list) of FailureCategoryData items, empty list if none.73        Raises exception on error.74        """75        if tsNow is None:76            self._oDb.execute('SELECT   *\n'77                              'FROM     FailureCategories\n'78                              'WHERE    tsExpire = \'infinity\'::TIMESTAMP\n'79                              'ORDER BY idFailureCategory ASC\n'80                              'LIMIT %s OFFSET %s\n'81                              , (cMaxRows, iStart,));82        else:83            self._oDb.execute('SELECT   *\n'84                              'FROM     FailureCategories\n'85                              'WHERE    tsExpire     > %s\n'86                              '     AND tsEffective <= %s\n'87                              'ORDER BY idFailureCategory ASC\n'88                              'LIMIT %s OFFSET %s\n'89                              , (tsNow, tsNow, cMaxRows, iStart,));90        aoRows = []91        for aoRow in self._oDb.fetchAll():92            aoRows.append(FailureCategoryData().initFromDbRow(aoRow))93        return aoRows94    def getFailureCategoriesForCombo(self, tsEffective = None):95        """96        Gets the list of Failure Categories for a combo box.97        Returns an array of (value [idFailureCategory], drop-down-name [sShort],98        hover-text [sFull]) tuples.99        """100        if tsEffective is None:101            self._oDb.execute('SELECT   idFailureCategory, sShort, sFull\n'102                              'FROM     FailureCategories\n'103                              'WHERE    tsExpire = \'infinity\'::TIMESTAMP\n'104                              'ORDER BY sShort')105        else:106            self._oDb.execute('SELECT   idFailureCategory, sShort, sFull\n'107                              'FROM     FailureCategories\n'108                              'WHERE    tsExpire     > %s\n'109                              '     AND tsEffective <= %s\n'110                              'ORDER BY sShort'111                              , (tsEffective, tsEffective))112        return self._oDb.fetchAll()113    def getById(self, idFailureCategory):114        """Get Failure Category data by idFailureCategory"""115        self._oDb.execute('SELECT   *\n'116                          'FROM     FailureCategories\n'117                          'WHERE    tsExpire   = \'infinity\'::timestamp\n'118                          '  AND    idFailureCategory = %s;', (idFailureCategory,))119        aRows = self._oDb.fetchAll()120        if len(aRows) not in (0, 1):121            raise self._oDb.integrityException(122                'Found more than one failure categories with the same credentials. Database structure is corrupted.')123        try:124            return FailureCategoryData().initFromDbRow(aRows[0])125        except IndexError:126            return None127    def addEntry(self, oFailureCategoryData, uidAuthor, fCommit=True):128        """129        Add Failure Category record130        """131        # Check if record with the same sShort fiels is already exists132        self._oDb.execute('SELECT *\n'133                          'FROM   FailureCategories\n'134                          'WHERE  tsExpire   = \'infinity\'::TIMESTAMP\n'135                          '   AND sShort = %s\n',136                          (oFailureCategoryData.sShort,))137        if len(self._oDb.fetchAll()) != 0:138            raise Exception('Record already exist')139        # Add record140        self._oDb.execute('INSERT INTO FailureCategories (\n'141                          '  uidAuthor, sShort, sFull'142                          ')\n'143                          'VALUES (%s, %s, %s)',144                          (uidAuthor,145                           oFailureCategoryData.sShort,146                           oFailureCategoryData.sFull))147        if fCommit:148            self._oDb.commit()149        return True150    def remove(self, uidAuthor, idFailureCategory, fNeedCommit=True):151        """152        Historize record153        """154        # Historize Failure Reasons records first155        self._oDb.execute('SELECT idFailureReason\n'156                          'FROM   FailureReasons\n'157                          'WHERE  idFailureCategory = %s\n'158                          '   AND tsExpire    = \'infinity\'::TIMESTAMP\n',159                          (idFailureCategory,))160        for iFailureReasonId in self._oDb.fetchAll():161            FailureReasonLogic(self._oDb).remove(162                uidAuthor, iFailureReasonId, fNeedCommit=False)163        self._oDb.execute('UPDATE FailureCategories\n'164                          'SET    tsExpire    = CURRENT_TIMESTAMP,\n'165                          '       uidAuthor   = %s\n'166                          'WHERE  idFailureCategory = %s\n'167                          '   AND tsExpire    = \'infinity\'::TIMESTAMP\n',168                          (uidAuthor, idFailureCategory))169        if fNeedCommit:170            self._oDb.commit()171        return True172    def editEntry(self, oFailureCategoryData, uidAuthor, fCommit=True):173        """Modify database record"""174        # Check if record exists175        oFailureCategoryDataOld = self.getById(oFailureCategoryData.idFailureCategory)176        if oFailureCategoryDataOld is None:177            raise TMExceptionBase(178                'Failure Category (id: %d) does not exist'179                % oFailureCategoryData.idFailureCategory)180        # Check if anything has been changed181        if oFailureCategoryData.isEqual(oFailureCategoryDataOld):182            return True183        # Historize record184        self.remove(185            uidAuthor, oFailureCategoryData.idFailureCategory, fNeedCommit=False)186        self._oDb.execute('INSERT INTO FailureCategories (\n'187                          '  idFailureCategory, uidAuthor, sShort, sFull'188                          ')\n'189                          'VALUES (%s, %s, %s, %s)',190                          (oFailureCategoryData.idFailureCategory,191                           uidAuthor,192                           oFailureCategoryData.sShort,193                           oFailureCategoryData.sFull))194        if fCommit:195            self._oDb.commit()...test_failures.py
Source:test_failures.py  
...26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.28import cPickle29from webkitpy.layout_tests.models import test_expectations30def is_reftest_failure(failure_list):31    failure_types = [type(f) for f in failure_list]32    return set((FailureReftestMismatch, FailureReftestMismatchDidNotOccur, FailureReftestNoImagesGenerated)).intersection(failure_types)33# FIXME: This is backwards.  Each TestFailure subclass should know what34# test_expectation type it corresponds too.  Then this method just35# collects them all from the failure list and returns the worst one.36def determine_result_type(failure_list):37    """Takes a set of test_failures and returns which result type best fits38    the list of failures. "Best fits" means we use the worst type of failure.39    Returns:40      one of the test_expectations result types - PASS, FAIL, CRASH, etc."""41    if not failure_list or len(failure_list) == 0:42        return test_expectations.PASS43    failure_types = [type(f) for f in failure_list]44    if FailureCrash in failure_types:45        return test_expectations.CRASH46    elif FailureLeak in failure_types:47        return test_expectations.LEAK48    elif FailureTimeout in failure_types:49        return test_expectations.TIMEOUT50    elif FailureEarlyExit in failure_types:51        return test_expectations.SKIP52    elif (FailureMissingResult in failure_types or53          FailureMissingImage in failure_types or54          FailureMissingImageHash in failure_types or55          FailureMissingAudio in failure_types):56        return test_expectations.MISSING57    else:58        is_text_failure = (FailureTextMismatch in failure_types or59                           FailureTestHarnessAssertion in failure_types)60        is_image_failure = (FailureImageHashIncorrect in failure_types or61                            FailureImageHashMismatch in failure_types)62        is_audio_failure = (FailureAudioMismatch in failure_types)63        if is_text_failure and is_image_failure:64            return test_expectations.IMAGE_PLUS_TEXT65        elif is_text_failure:66            return test_expectations.TEXT67        elif is_image_failure or is_reftest_failure(failure_list):68            return test_expectations.IMAGE69        elif is_audio_failure:70            return test_expectations.AUDIO71        else:72            raise ValueError("unclassifiable set of failures: "73                             + str(failure_types))74class TestFailure(object):75    """Abstract base class that defines the failure interface."""76    @staticmethod77    def loads(s):78        """Creates a TestFailure object from the specified string."""79        return cPickle.loads(s)80    def message(self):81        """Returns a string describing the failure in more detail."""...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
