Coverage for tests/test_matplotlibFormatter.py: 49%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

43 statements  

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://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 <http://www.gnu.org/licenses/>. 

21 

22"""Tests for MatplotlibFormatter. 

23""" 

24 

25import unittest 

26import os 

27from random import Random 

28import tempfile 

29 

30try: 

31 import matplotlib 

32 matplotlib.use("Agg") # noqa:E402 

33 from matplotlib import pyplot 

34except ImportError: 

35 pyplot = None 

36 

37from lsst.daf.butler import Butler, DatasetType 

38from lsst.daf.butler.tests.utils import makeTestTempDir, removeTestTempDir 

39import filecmp 

40 

41TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

42 

43 

44@unittest.skipIf(pyplot is None, 

45 "skipping test because matplotlib import failed") 

46class MatplotlibFormatterTestCase(unittest.TestCase): 

47 """Test for MatplotlibFormatter. 

48 """ 

49 RANDOM_SEED = 10 

50 

51 def setUp(self): 

52 self.root = makeTestTempDir(TESTDIR) 

53 Butler.makeRepo(self.root) 

54 # Create a random image for testing 

55 self.rng = Random(self.RANDOM_SEED) 

56 

57 def tearDown(self): 

58 removeTestTempDir(self.root) 

59 

60 def testMatplotlibFormatter(self): 

61 butler = Butler(self.root, run="testrun") 

62 datasetType = DatasetType("test_plot", [], "Plot", 

63 universe=butler.registry.dimensions) 

64 butler.registry.registerDatasetType(datasetType) 

65 # Does not have to be a random image 

66 pyplot.imshow([self.rng.sample(range(50), 10), 

67 self.rng.sample(range(50), 10), 

68 self.rng.sample(range(50), 10), 

69 ]) 

70 ref = butler.put(pyplot.gcf(), datasetType) 

71 uri = butler.getURI(ref) 

72 

73 # Following test needs a local file 

74 with uri.as_local() as local: 

75 with tempfile.NamedTemporaryFile(suffix=".png") as file: 

76 pyplot.gcf().savefig(file.name) 

77 self.assertTrue( 

78 filecmp.cmp( 

79 local.ospath, 

80 file.name, 

81 shallow=True 

82 ) 

83 ) 

84 self.assertTrue(butler.datasetExists(ref)) 

85 with self.assertRaises(ValueError): 

86 butler.get(ref) 

87 butler.pruneDatasets([ref], unstore=True, purge=True) 

88 with self.assertRaises(LookupError): 

89 butler.datasetExists(ref) 

90 

91 

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

93 unittest.main()