Coverage for tests/test_decorators.py: 47%

50 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-01 15:14 -0700

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# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22import itertools 

23import sys 

24import unittest 

25 

26import lsst.utils.tests 

27 

28numCalls = 0 # Number of times testMethodDecorator gets called 

29 

30 

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

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

33 """Test methodParameters and classParameters decorators.""" 

34 

35 def setUp(self): 

36 self.numCalls = 0 

37 self.methodDecorator = False # testMethodDecorator fired 

38 

39 def testClassDecorator(self): 

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

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

42 

43 @lsst.utils.tests.methodParameters( 

44 xx=[1, 2, 3], 

45 yy=[9, 8, 7], 

46 ) 

47 def testMethodDecorator(self, xx, yy): 

48 self.methodDecorator = True 

49 self.assertEqual(xx + yy, 10) 

50 self.numCalls += 1 

51 

52 def tearDown(self): 

53 if self.methodDecorator: 

54 self.assertEqual(self.numCalls, 3) 

55 

56 

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

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

59 """Test methodParametersProduct and classParametersProduct decorators.""" 

60 

61 def setUp(self): 

62 self.combinations = set() 

63 self.methodDecorator = False # testMethodDecorator fired 

64 

65 def testClassDecorator(self): 

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

67 

68 @lsst.utils.tests.methodParametersProduct( 

69 xx=[1, 2, 3], 

70 yy=[9, 8], 

71 ) 

72 def testMethodDecorator(self, xx, yy): 

73 self.methodDecorator = True 

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

75 

76 def tearDown(self): 

77 if self.methodDecorator: 

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

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

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

81 

82 

83def testDecorators(): 

84 """Test that the decorators have been applied.""" 

85 world = globals() 

86 assert "DecoratorTestCase_one_3" in world 

87 assert "DecoratorTestCase_two_3" in world 

88 assert "DecoratorTestCase_three_5" in world 

89 assert "DecoratorTestCase_four_4" in world 

90 

91 assert "DecoratorProductTestCase_one_3" in world 

92 assert "DecoratorProductTestCase_one_4" in world 

93 assert "DecoratorProductTestCase_two_3" in world 

94 assert "DecoratorProductTestCase_two_4" in world 

95 

96 

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

98 """Test for file descriptor leaks.""" 

99 

100 

101def setup_module(module): 

102 """Initialize the pytest environment.""" 

103 lsst.utils.tests.init() 

104 

105 

106if __name__ == "__main__": 

107 module = sys.modules[__name__] 

108 setup_module(module) 

109 testDecorators() 

110 unittest.main()