Coverage for tests / test_sasquatchDatastore.py: 40%

48 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-06 09:07 +0000

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 

28 

29import lsst.daf.butler.tests as butlerTests 

30from lsst.analysis.tools.interfaces import MetricMeasurementBundle 

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

32from lsst.daf.butler import CollectionType, Config 

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

34from lsst.verify import Measurement 

35 

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

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

38 

39 

40class SasquatchDatastoreTest(unittest.TestCase): 

41 def setUp(self): 

42 self.root = makeTestTempDir(TESTDIR) 

43 

44 config = Config() 

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

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

47 

48 dataIds = { 

49 "instrument": ["DummyCam"], 

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

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

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

53 } 

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

55 

56 butlerTests.addDatasetType( 

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

58 ) 

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

60 

61 def tearDown(self): 

62 removeTestTempDir(self.root) 

63 

64 def test_put(self): 

65 """Simple test for put method.""" 

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

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

68 

69 # Patch dispatcher method to check parameters. 

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

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

72 

73 mock_method.assert_called() 

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

75 

76 def test_explicit_timestamp_version(self): 

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

78 bundle = MetricMeasurementBundle() 

79 bundle.timestamp_version = "explicit_timestamp" 

80 # verify this raises with no specified time 

81 with self.assertRaises(ValueError): 

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

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

84 bundle.timestamp_version = "explicit_timestamp:123233" 

85 with self.assertRaises(ValueError): 

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

87 # verify that a correct time gets parsed 

88 bundle.timestamp_version = "explicit_timestamp:20230728T165102Z" 

89 meta = {} 

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

91 dt = datetime.datetime(2023, 7, 28, 16, 51, 2, tzinfo=datetime.UTC) 

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

93 

94 

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

96 unittest.main()