Coverage for tests/test_timer.py: 26%

56 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-10 03:37 -0700

1# This file is part of verify. 

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 time 

23import unittest 

24 

25import astropy.units as u 

26 

27import lsst.utils.tests 

28 

29from lsst.verify import Measurement, Metric, Name 

30from lsst.verify.timer import time_this_to_measurement 

31 

32 

33class TimeThisTestSuite(unittest.TestCase): 

34 def setUp(self): 

35 super().setUp() 

36 self.metric_name = Name("verify.DummyTime") 

37 

38 def test_basic(self): 

39 duration = 0.2 

40 meas = Measurement(self.metric_name) 

41 with time_this_to_measurement(meas): 

42 time.sleep(duration) 

43 

44 self.assertEqual(meas.metric_name, self.metric_name) # Should not have changed 

45 self.assertIsNotNone(meas.quantity) 

46 self.assertGreater(meas.quantity, duration * u.second) 

47 self.assertLess(meas.quantity, 2 * duration * u.second) 

48 

49 def test_unit_checking_ok(self): 

50 duration = 0.2 

51 metric = Metric(self.metric_name, "Unconventional metric", u.nanosecond) 

52 meas = Measurement(metric) 

53 with time_this_to_measurement(meas): 

54 time.sleep(duration) 

55 

56 self.assertEqual(meas.metric_name, self.metric_name) # Should not have changed 

57 self.assertIsNotNone(meas.quantity) 

58 self.assertGreater(meas.quantity, duration * u.second) 

59 self.assertLess(meas.quantity, 2 * duration * u.second) 

60 

61 def test_unit_checking_bad(self): 

62 duration = 0.2 

63 metric = Metric(self.metric_name, "Non-temporal metric", u.meter / u.second) 

64 meas = Measurement(metric) 

65 with self.assertRaises(TypeError): 

66 with time_this_to_measurement(meas): 

67 time.sleep(duration) 

68 

69 def test_exception(self): 

70 duration = 0.2 

71 meas = Measurement(self.metric_name) 

72 try: 

73 with time_this_to_measurement(meas): 

74 time.sleep(duration) 

75 raise RuntimeError("Something went wrong!") 

76 except RuntimeError: 

77 pass 

78 

79 self.assertEqual(meas.metric_name, self.metric_name) # Should not have changed 

80 self.assertIsNotNone(meas.quantity) 

81 self.assertGreater(meas.quantity, duration * u.second) 

82 self.assertLess(meas.quantity, 2 * duration * u.second) 

83 

84 

85class MemoryTester(lsst.utils.tests.MemoryTestCase): 

86 pass 

87 

88 

89def setup_module(module): 

90 lsst.utils.tests.init() 

91 

92 

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

94 lsst.utils.tests.init() 

95 unittest.main()