diff --git a/volttron/platform/dbutils/mysqlfuncts.py b/volttron/platform/dbutils/mysqlfuncts.py index 4bbb754b33..50b4729495 100644 --- a/volttron/platform/dbutils/mysqlfuncts.py +++ b/volttron/platform/dbutils/mysqlfuncts.py @@ -414,7 +414,7 @@ def get_topic_meta_map(self): _log.debug("loading metadata from db") topic_meta_map = dict() for id, meta in rows: - topic_meta_map[id] = jsonapi.loads(meta) + topic_meta_map[id] = jsonapi.loads(meta) if meta else None return topic_meta_map def get_topic_map(self): diff --git a/volttron/platform/dbutils/postgresqlfuncts.py b/volttron/platform/dbutils/postgresqlfuncts.py index 0983611388..8e74934371 100644 --- a/volttron/platform/dbutils/postgresqlfuncts.py +++ b/volttron/platform/dbutils/postgresqlfuncts.py @@ -350,7 +350,7 @@ def get_topic_meta_map(self): 'SELECT topic_id, metadata ' 'FROM {}').format(Identifier(self.meta_table)) rows = self.select(query) - meta_map = {tid: jsonapi.loads(meta) for tid, meta in rows} + meta_map = {tid: jsonapi.loads(meta) if meta else None for tid, meta in rows} return meta_map def get_agg_topics(self): diff --git a/volttron/platform/dbutils/redshiftfuncts.py b/volttron/platform/dbutils/redshiftfuncts.py index e7c51dca1a..934b9015ae 100644 --- a/volttron/platform/dbutils/redshiftfuncts.py +++ b/volttron/platform/dbutils/redshiftfuncts.py @@ -266,7 +266,7 @@ def get_topic_meta_map(self): 'SELECT topic_id, metadata ' 'FROM {}').format(Identifier(self.meta_table)) rows = self.select(query) - meta_map = {tid: jsonapi.loads(meta) for tid, meta in rows} + meta_map = {tid: jsonapi.loads(meta) if meta else None for tid, meta in rows} return meta_map def get_agg_topics(self): diff --git a/volttron/platform/dbutils/sqlitefuncts.py b/volttron/platform/dbutils/sqlitefuncts.py index 1a48aa7c45..5c525a63cd 100644 --- a/volttron/platform/dbutils/sqlitefuncts.py +++ b/volttron/platform/dbutils/sqlitefuncts.py @@ -384,7 +384,7 @@ def get_topic_meta_map(self): _log.debug("loading metadata from db") topic_meta_map = dict() for id, meta in rows: - topic_meta_map[id] = jsonapi.loads(meta) + topic_meta_map[id] = jsonapi.loads(meta) if meta else None return topic_meta_map def get_agg_topics(self): diff --git a/volttrontesting/platform/dbutils/test_mysqlfuncts.py b/volttrontesting/platform/dbutils/test_mysqlfuncts.py index 95d5d6e0ed..13cad99927 100644 --- a/volttrontesting/platform/dbutils/test_mysqlfuncts.py +++ b/volttrontesting/platform/dbutils/test_mysqlfuncts.py @@ -263,6 +263,23 @@ def test_get_topic_map_should_succeed(get_container_func): assert actual == expected +def test_get_topic_meta_map_should_succeed(get_container_func): + container, sqlfuncts, connection_port, historian_version = get_container_func + if historian_version == "<4.0.0": + pytest.skip("method applied only to version >=4.0.0") + else: + query = """ + INSERT INTO topics (topic_name) + VALUES ('football'); + INSERT INTO topics (topic_name, metadata) + VALUES ('baseball', '{"metadata":"value"}'); + """ + seed_database(container, query) + expected = {1: None, 2: {"metadata": "value"}} + actual = sqlfuncts.get_topic_meta_map() + assert actual == expected + + # fails for image:mysql:8.0.25 historian schema version >=4.0.0 def test_get_agg_topic_map_should_return_dict(get_container_func): container, sqlfuncts, connection_port, historian_version = get_container_func diff --git a/volttrontesting/platform/dbutils/test_postgresql_timescaledb.py b/volttrontesting/platform/dbutils/test_postgresql_timescaledb.py index 62baff85f6..0904d50c01 100644 --- a/volttrontesting/platform/dbutils/test_postgresql_timescaledb.py +++ b/volttrontesting/platform/dbutils/test_postgresql_timescaledb.py @@ -331,6 +331,22 @@ def test_get_topic_map_should_return_maps(get_container_func): assert actual == expected +def test_get_topic_meta_map_should_return_maps(get_container_func): + container, sqlfuncts, connection_port, historian_version = get_container_func + if historian_version == "<4.0.0": + pytest.skip("method applied only to version >=4.0.0") + else: + query = """ + INSERT INTO topics (topic_name) + VALUES ('football'); + INSERT INTO topics (topic_name, metadata) + VALUES ('baseball', '{"meta":"value"}'); + """ + seed_database(container, query) + expected = {1: None, 2: {"meta": "value"}} + actual = sqlfuncts.get_topic_meta_map() + assert actual == expected + def test_get_agg_topics_should_return_list(get_container_func): container, postgresqlfuncts, port_on_host, historian_version = get_container_func topic = "some_agg_topic" diff --git a/volttrontesting/platform/dbutils/test_postgresqlfuncts.py b/volttrontesting/platform/dbutils/test_postgresqlfuncts.py index 26fa605c34..172f29761a 100644 --- a/volttrontesting/platform/dbutils/test_postgresqlfuncts.py +++ b/volttrontesting/platform/dbutils/test_postgresqlfuncts.py @@ -338,6 +338,23 @@ def test_get_topic_map_should_return_maps(get_container_func): assert actual == expected +def test_get_topic_meta_map_should_return_maps(get_container_func): + container, sqlfuncts, connection_port, historian_version = get_container_func + if historian_version == "<4.0.0": + pytest.skip("method applied only to version >=4.0.0") + else: + query = """ + INSERT INTO topics (topic_name) + VALUES ('football'); + INSERT INTO topics (topic_name, metadata) + VALUES ('baseball', '{"meta":"value"}'); + """ + seed_database(container, query) + expected = {1: None, 2: {"meta": "value"}} + actual = sqlfuncts.get_topic_meta_map() + assert actual == expected + + def test_get_agg_topics_should_return_list(get_container_func): container, sqlfuncts, connection_port, historian_version = get_container_func diff --git a/volttrontesting/platform/dbutils/test_sqlitefuncts.py b/volttrontesting/platform/dbutils/test_sqlitefuncts.py index acd1d49a4c..490df0fb79 100644 --- a/volttrontesting/platform/dbutils/test_sqlitefuncts.py +++ b/volttrontesting/platform/dbutils/test_sqlitefuncts.py @@ -276,6 +276,22 @@ def test_get_topic_map(get_sqlitefuncts): assert actual_topic_map == expected_topic_map +@pytest.mark.sqlitefuncts +@pytest.mark.dbutils +def test_get_topic_meta_map(get_sqlitefuncts): + sqlitefuncts, historian_version = get_sqlitefuncts + if historian_version == "<4.0.0": + pytest.skip("method applied only to version >=4.0.0") + else: + query = "INSERT INTO topics (topic_name) values ('football');" \ + "INSERT INTO topics (topic_name, metadata) values ('netball', '{\"meta\":\"value\"}');" + query_db(query) + expected_topic_map = {1: None, 2: {"meta": "value"}} + + actual_topic_meta_map = sqlitefuncts.get_topic_meta_map() + + assert actual_topic_meta_map == expected_topic_map + @pytest.mark.sqlitefuncts @pytest.mark.dbutils def test_get_agg_topics(get_sqlitefuncts):