Coverage for tests/test_consdbClient.py: 37%
61 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-16 05:26 -0700
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-16 05:26 -0700
1# This file is part of summit_utils.
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/>.
22import pytest
23import responses
24from requests import HTTPError
26from lsst.summit.utils import ConsDbClient, FlexibleMetadataInfo
29@pytest.fixture
30def client():
31 return ConsDbClient("http://example.com/consdb")
34def test_table_name(client):
35 instrument = "latiss"
36 obs_type = "exposure"
37 assert client.compute_flexible_metadata_table_name(instrument, obs_type) == "cdb_latiss.exposure_flexdata"
40@responses.activate
41def test_add_flexible_metadata_key(client):
42 instrument = "latiss"
43 obs_type = "exposure"
44 responses.post(
45 "http://example.com/consdb/flex/latiss/exposure/addkey",
46 json={
47 "message": "Key added to flexible metadata",
48 "key": "foo",
49 "instrument": "latiss",
50 "obs_type": "exposure",
51 },
52 match=[
53 responses.matchers.json_params_matcher({"key": "foo", "dtype": "bool", "doc": "bool key"}),
54 ],
55 )
56 responses.post(
57 "http://example.com/consdb/flex/latiss/exposure/addkey",
58 json={
59 "message": "Key added to flexible metadata",
60 "key": "bar",
61 "instrument": "latiss",
62 "obs_type": "exposure",
63 },
64 match=[
65 responses.matchers.json_params_matcher({"key": "bar", "dtype": "int", "doc": "int key"}),
66 ],
67 )
68 responses.post(
69 "http://example.com/consdb/flex/bad_instrument/exposure/addkey",
70 status=404,
71 json={"message": "Unknown instrument", "value": "bad_instrument", "valid": ["latiss"]},
72 )
73 responses.post(
74 "http://example.com/consdb/flex/latiss/bad_obs_type/addkey",
75 status=404,
76 json={"message": "Unknown observation type", "value": "bad_obs_type", "valid": ["exposure"]},
77 )
79 assert (
80 client.add_flexible_metadata_key(instrument, obs_type, "foo", "bool", "bool key").json()["key"]
81 == "foo"
82 )
83 assert (
84 client.add_flexible_metadata_key(instrument, obs_type, "bar", "int", "int key").json()["instrument"]
85 == "latiss"
86 )
87 with pytest.raises(HTTPError, match="404") as e:
88 client.add_flexible_metadata_key("bad_instrument", obs_type, "error", "int", "instrument error")
89 json_data = e.value.response.json()
90 assert json_data["message"] == "Unknown instrument"
91 assert json_data["value"] == "bad_instrument"
92 assert json_data["valid"] == ["latiss"]
93 with pytest.raises(HTTPError, match="404"):
94 client.add_flexible_metadata_key(instrument, "bad_obs_type", "error", "int", "obs_type error")
97@responses.activate
98def test_get_flexible_metadata_keys(client):
99 description = {"foo": ["bool", "a", None, None], "bar": ["float", "b", "deg", "pos.eq.ra"]}
100 responses.get(
101 "http://example.com/consdb/flex/latiss/exposure/schema",
102 json=description,
103 )
104 instrument = "latiss"
105 obs_type = "exposure"
106 assert client.get_flexible_metadata_keys(instrument, obs_type) == {
107 "foo": FlexibleMetadataInfo("bool", "a"),
108 "bar": FlexibleMetadataInfo("float", "b", "deg", "pos.eq.ra"),
109 }
112@responses.activate
113def test_get_flexible_metadata(client):
114 results = {"bool_key": True, "int_key": 42, "float_key": 3.14159, "str_key": "foo"}
115 responses.get(
116 "http://example.com/consdb/flex/latiss/exposure/obs/271828",
117 json=results,
118 )
119 responses.get(
120 "http://example.com/consdb/flex/latiss/exposure/obs/271828?k=float_key", json={"float_key": 3.14159}
121 )
122 responses.get(
123 "http://example.com/consdb/flex/latiss/exposure/obs/271828?k=int_key&k=float_key",
124 json={"float_key": 3.14159, "int_key": 42},
125 )
126 instrument = "latiss"
127 obs_type = "exposure"
128 obs_id = 271828
129 assert client.get_flexible_metadata(instrument, obs_type, obs_id) == results
130 assert client.get_flexible_metadata(instrument, obs_type, obs_id, ["float_key"]) == {
131 "float_key": results["float_key"]
132 }
133 assert client.get_flexible_metadata(instrument, obs_type, obs_id, ["int_key", "float_key"]) == {
134 "int_key": results["int_key"],
135 "float_key": results["float_key"],
136 }
139@responses.activate
140def test_insert_flexible_metadata(client):
141 instrument = "latiss"
142 obs_type = "exposure"
143 with pytest.raises(ValueError):
144 client.insert_flexible_metadata(instrument, obs_type, 271828)
145 # TODO: more POST tests
148@responses.activate
149def test_schema(client):
150 description = {"foo": ("bool", "a"), "bar": ("int", "b")}
151 responses.get(
152 "http://example.com/consdb/schema/latiss/misc_table",
153 json=description,
154 )
155 instrument = "latiss"
156 table = "misc_table"
157 assert client.schema(instrument, table) == description
160# TODO: more POST tests
161# client.insert(instrument, table, obs_id, values, allow_update)
162# client.query(query)