Coverage for tests/test_sasquatchDatastore.py: 36%

48 statements  

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

1# This file is part of analysis_tools. 

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 datetime 

23import os 

24import unittest 

25from unittest.mock import patch 

26 

27import astropy.units as u 

28import lsst.daf.butler.tests as butlerTests 

29from lsst.analysis.tools.interfaces import MetricMeasurementBundle 

30from lsst.analysis.tools.interfaces.datastore import SasquatchDispatcher 

31from lsst.daf.butler import CollectionType, Config 

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

33from lsst.verify import Measurement 

34 

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

36CONFIG_FILE = os.path.join(TESTDIR, "config", "butler-sasquatch.yaml") 

37 

38 

39class SasquatchDatastoreTest(unittest.TestCase): 

40 def setUp(self): 

41 self.root = makeTestTempDir(TESTDIR) 

42 

43 config = Config() 

44 config["datastore", "cls"] = "lsst.analysis.tools.interfaces.datastore.SasquatchDatastore" 

45 config["datastore", "restProxyUrl"] = "https://example.com/sasquatch-rest-proxy" 

46 

47 dataIds = { 

48 "instrument": ["DummyCam"], 

49 "physical_filter": ["d-r"], 

50 "visit": [42, 43, 44], 

51 "detector": [1, 2, 3], 

52 } 

53 self.butler = butlerTests.makeTestRepo(self.root, dataIds, config=config) 

54 

55 butlerTests.addDatasetType( 

56 self.butler, "Metrics", {"instrument", "visit", "detector"}, "MetricMeasurementBundle" 

57 ) 

58 self.butler.registry.registerCollection("run1", CollectionType.RUN) 

59 

60 def tearDown(self): 

61 removeTestTempDir(self.root) 

62 

63 def test_put(self): 

64 """Simple test for put method.""" 

65 m = Measurement("nopackage.fancyMetric", 42.2 * u.s) 

66 bundle = MetricMeasurementBundle({"m": [m]}) 

67 

68 # Patch dispatcher method to check parameters. 

69 with patch.object(SasquatchDispatcher, "dispatchRef") as mock_method: 

70 self.butler.put(bundle, "Metrics", run="run1", instrument="DummyCam", visit=42, detector=2) 

71 

72 mock_method.assert_called() 

73 self.assertIs(mock_method.call_args[0][0], bundle) 

74 

75 def test_explicit_timestamp_version(self): 

76 dispatcher = SasquatchDispatcher("http://test.local", "na") 

77 bundle = MetricMeasurementBundle() 

78 bundle.timestamp_version = "explicit_timestamp" 

79 # verify this raises with no specified time 

80 with self.assertRaises(ValueError): 

81 dispatcher._handleTimes({}, bundle, "localRun") 

82 # verify this raise with a date that can't be parsed 

83 bundle.timestamp_version = "explicit_timestamp:123233" 

84 with self.assertRaises(ValueError): 

85 dispatcher._handleTimes({}, bundle, "localRun") 

86 # verify that a correct time gets parsed 

87 bundle.timestamp_version = "explicit_timestamp:20230728T165102Z" 

88 meta = {} 

89 dispatcher._handleTimes(meta, bundle, "localRun") 

90 dt = datetime.datetime(2023, 7, 28, 16, 51, 2, tzinfo=datetime.timezone.utc) 

91 self.assertEqual(meta["timestamp"], dt.timestamp()) 

92 

93 

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

95 unittest.main()