Coverage for tests/test_decorators.py: 43%

56 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-09 05:52 +0000

1# This file is part of utils. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# Use of this source code is governed by a 3-clause BSD-style 

10# license that can be found in the LICENSE file. 

11 

12import itertools 

13import sys 

14import unittest 

15 

16import lsst.utils.tests 

17 

18numCalls = 0 # Number of times testMethodDecorator gets called 

19 

20 

21@lsst.utils.tests.classParameters(word=["one", "two", "three", "four"], length=[3, 3, 5, 4]) 

22class DecoratorTestCase(lsst.utils.tests.TestCase): 

23 """Test methodParameters and classParameters decorators""" 

24 

25 def setUp(self): 

26 self.numCalls = 0 

27 self.methodDecorator = False # testMethodDecorator fired 

28 

29 def testClassDecorator(self): 

30 self.assertEqual(len(self.word), self.length) 

31 self.assertEqual(self.__class__.__name__, f"DecoratorTestCase_{self.word}_{self.length}") 

32 

33 @lsst.utils.tests.methodParameters( 

34 xx=[1, 2, 3], 

35 yy=[9, 8, 7], 

36 ) 

37 def testMethodDecorator(self, xx, yy): 

38 self.methodDecorator = True 

39 self.assertEqual(xx + yy, 10) 

40 self.numCalls += 1 

41 

42 def tearDown(self): 

43 if self.methodDecorator: 

44 self.assertEqual(self.numCalls, 3) 

45 

46 

47@lsst.utils.tests.classParametersProduct(word=["one", "two"], number=[3, 4]) 

48class DecoratorProductTestCase(lsst.utils.tests.TestCase): 

49 """Test methodParametersProduct and classParametersProduct decorators""" 

50 

51 def setUp(self): 

52 self.combinations = set() 

53 self.methodDecorator = False # testMethodDecorator fired 

54 

55 def testClassDecorator(self): 

56 self.assertEqual(self.__class__.__name__, f"DecoratorProductTestCase_{self.word}_{self.number}") 

57 

58 @lsst.utils.tests.methodParametersProduct( 

59 xx=[1, 2, 3], 

60 yy=[9, 8], 

61 ) 

62 def testMethodDecorator(self, xx, yy): 

63 self.methodDecorator = True 

64 self.combinations.add((xx, yy)) 

65 

66 def tearDown(self): 

67 if self.methodDecorator: 

68 self.assertEqual(len(self.combinations), 6) 

69 for xx, yy in itertools.product((1, 2, 3), (9, 8)): 

70 self.assertIn((xx, yy), self.combinations) 

71 

72 

73def testDecorators(): 

74 world = globals() 

75 assert "DecoratorTestCase_one_3" in world 

76 assert "DecoratorTestCase_two_3" in world 

77 assert "DecoratorTestCase_three_5" in world 

78 assert "DecoratorTestCase_four_4" in world 

79 

80 assert "DecoratorProductTestCase_one_3" in world 

81 assert "DecoratorProductTestCase_one_4" in world 

82 assert "DecoratorProductTestCase_two_3" in world 

83 assert "DecoratorProductTestCase_two_4" in world 

84 

85 

86class TestMemory(lsst.utils.tests.MemoryTestCase): 

87 pass 

88 

89 

90def setup_module(module): 

91 lsst.utils.tests.init() 

92 

93 

94if __name__ == "__main__": 94 ↛ 95line 94 didn't jump to line 95, because the condition on line 94 was never true

95 module = sys.modules[__name__] 

96 setup_module(module) 

97 testDecorators() 

98 unittest.main()