Coverage for tests/test_matplotlibFormatter.py: 41%
41 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-04-01 02:05 -0700
« prev ^ index » next coverage.py v6.5.0, created at 2023-04-01 02:05 -0700
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/>.
22"""Tests for MatplotlibFormatter.
23"""
25import os
26import tempfile
27import unittest
28from random import Random
30try:
31 import matplotlib
33 matplotlib.use("Agg") # noqa:E402
34 from matplotlib import pyplot
35except ImportError:
36 pyplot = None
38import filecmp
40from lsst.daf.butler import Butler, DatasetType
41from lsst.daf.butler.tests.utils import makeTestTempDir, removeTestTempDir
43TESTDIR = os.path.abspath(os.path.dirname(__file__))
46@unittest.skipIf(pyplot is None, "skipping test because matplotlib import failed")
47class MatplotlibFormatterTestCase(unittest.TestCase):
48 """Test for MatplotlibFormatter."""
50 RANDOM_SEED = 10
52 def setUp(self):
53 self.root = makeTestTempDir(TESTDIR)
54 Butler.makeRepo(self.root)
55 # Create a random image for testing
56 self.rng = Random(self.RANDOM_SEED)
58 def tearDown(self):
59 removeTestTempDir(self.root)
61 def testMatplotlibFormatter(self):
62 butler = Butler(self.root, run="testrun")
63 datasetType = DatasetType("test_plot", [], "Plot", universe=butler.registry.dimensions)
64 butler.registry.registerDatasetType(datasetType)
65 # Does not have to be a random image
66 pyplot.imshow(
67 [
68 self.rng.sample(range(50), 10),
69 self.rng.sample(range(50), 10),
70 self.rng.sample(range(50), 10),
71 ]
72 )
73 ref = butler.put(pyplot.gcf(), datasetType)
74 uri = butler.getURI(ref)
76 # Following test needs a local file
77 with uri.as_local() as local:
78 with tempfile.NamedTemporaryFile(suffix=".png") as file:
79 pyplot.gcf().savefig(file.name)
80 self.assertTrue(filecmp.cmp(local.ospath, file.name, shallow=True))
81 self.assertTrue(butler.datasetExists(ref))
82 with self.assertRaises(ValueError):
83 butler.get(ref)
84 butler.pruneDatasets([ref], unstore=True, purge=True)
85 with self.assertRaises(LookupError):
86 butler.datasetExists(ref)
89if __name__ == "__main__":
90 unittest.main()