|
| 1 | +"""Test t.info format=json output. |
| 2 | +
|
| 3 | +(C) 2025 by the GRASS Development Team |
| 4 | +This program is free software under the GNU General Public |
| 5 | +License (>=v2). Read the file COPYING that comes with GRASS |
| 6 | +for details. |
| 7 | +
|
| 8 | +Validates that t.info format=json returns valid JSON and that |
| 9 | +parsed output contains correct metadata values (not only keys). |
| 10 | +""" |
| 11 | + |
| 12 | +import json |
| 13 | + |
| 14 | +import grass.script as gs |
| 15 | + |
| 16 | +from grass.gunittest.case import TestCase |
| 17 | +from grass.gunittest.gmodules import SimpleModule |
| 18 | + |
| 19 | + |
| 20 | +class TestTinfoJson(TestCase): |
| 21 | + """Value-based tests for t.info format=json.""" |
| 22 | + |
| 23 | + @classmethod |
| 24 | + def setUpClass(cls): |
| 25 | + """Set region and create a space time raster dataset with one map.""" |
| 26 | + cls.use_temp_region() |
| 27 | + cls.runModule( |
| 28 | + "g.region", s=0, n=80, w=0, e=120, b=0, t=50, res=10, res3=10, flags="p3" |
| 29 | + ) |
| 30 | + cls.runModule( |
| 31 | + "r.mapcalc", expression="prec_1 = 1", overwrite=True |
| 32 | + ) |
| 33 | + cls.runModule( |
| 34 | + "t.create", |
| 35 | + type="strds", |
| 36 | + temporaltype="absolute", |
| 37 | + output="precip_abs1", |
| 38 | + title="A test", |
| 39 | + description="A test", |
| 40 | + overwrite=True, |
| 41 | + ) |
| 42 | + cls.runModule( |
| 43 | + "t.register", |
| 44 | + type="raster", |
| 45 | + flags="i", |
| 46 | + input="precip_abs1", |
| 47 | + maps="prec_1", |
| 48 | + start="2001-01-01", |
| 49 | + increment="20 years", |
| 50 | + overwrite=True, |
| 51 | + ) |
| 52 | + cls.mapset = gs.gisenv()["MAPSET"] |
| 53 | + |
| 54 | + @classmethod |
| 55 | + def tearDownClass(cls): |
| 56 | + """Remove dataset and temporary region.""" |
| 57 | + cls.runModule("t.remove", flags="df", type="strds", inputs="precip_abs1") |
| 58 | + cls.del_temp_region() |
| 59 | + |
| 60 | + def test_t_info_format_json_returns_valid_json(self): |
| 61 | + """t.info format=json outputs valid JSON (parseable with json.loads).""" |
| 62 | + info = SimpleModule("t.info", format="json", type="strds", input="precip_abs1") |
| 63 | + self.assertModule(info) |
| 64 | + out = info.outputs.stdout |
| 65 | + self.assertIsNotNone(out) |
| 66 | + parsed = json.loads(out) |
| 67 | + self.assertIsInstance(parsed, dict) |
| 68 | + |
| 69 | + def test_t_info_format_json_metadata_values(self): |
| 70 | + """Parsed JSON from t.info format=json has correct metadata values.""" |
| 71 | + info = SimpleModule("t.info", format="json", type="strds", input="precip_abs1") |
| 72 | + self.assertModule(info) |
| 73 | + parsed = json.loads(info.outputs.stdout) |
| 74 | + |
| 75 | + self.assertEqual(parsed["name"], "precip_abs1") |
| 76 | + self.assertEqual(parsed["mapset"], self.mapset) |
| 77 | + self.assertEqual(parsed["id"], f"precip_abs1@{self.mapset}") |
| 78 | + self.assertEqual(parsed["temporal_type"], "absolute") |
| 79 | + |
| 80 | + self.assertEqual(parsed["north"], 80) |
| 81 | + self.assertEqual(parsed["south"], 0) |
| 82 | + self.assertEqual(parsed["east"], 120) |
| 83 | + self.assertEqual(parsed["west"], 0) |
| 84 | + |
| 85 | + self.assertEqual(parsed["number_of_maps"], 1) |
| 86 | + self.assertEqual(parsed["title"], "A test") |
| 87 | + self.assertEqual(parsed["description"], "A test") |
| 88 | + |
| 89 | + self.assertEqual(parsed["start_time"], "2001-01-01T00:00:00") |
| 90 | + self.assertEqual(parsed["end_time"], "2021-01-01T00:00:00") |
0 commit comments