From 37c44399efce9681ca429c1f4ef9afbe8decc8dc Mon Sep 17 00:00:00 2001 From: jklarson Date: Thu, 2 Apr 2020 13:16:00 -0700 Subject: [PATCH 1/4] fixed base weather test timing and pint unit conversion approximation --- volttrontesting/services/weather/test_base_weather.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/volttrontesting/services/weather/test_base_weather.py b/volttrontesting/services/weather/test_base_weather.py index cd5bc51c55..37deb6c45a 100644 --- a/volttrontesting/services/weather/test_base_weather.py +++ b/volttrontesting/services/weather/test_base_weather.py @@ -486,7 +486,7 @@ def test_set_update_interval_fail(weather, service_name, interval): def test_manage_unit_conversion_success(weather, from_units, start, to_units, end): output = weather.manage_unit_conversion(from_units, start, to_units) - assert str(output).startswith(str(end)) + assert pytest.approx(output, 0.0001) == end @pytest.mark.weather2 @@ -541,7 +541,7 @@ def test_get_current_valid_locations(weather, fake_locations): test_value = test_points[fake_point["name"]] assert test_value if test_value: - assert str(test_value).startswith(str(fake_point["value"])) + assert pytest.approx(test_value, 0.0001) == fake_point["value"] assert results1[0]["location"].startswith("fake_location") # Check data got cached @@ -934,8 +934,7 @@ def test_poll_location(volttron_instance, query_agent): test_value = test_points[fake_point["name"]] assert test_value if test_value: - assert str(test_value).startswith(str(fake_point["value"])) - assert results1[0]["location"].startswith("fake_location") + assert pytest.approx(test_value, 0.0001) == fake_point["value"] assert query_agent.vip.rpc.call( "test_poll_basic", "health.get_status").get(timeout=10).get( @@ -961,6 +960,7 @@ def test_poll_location(volttron_instance, query_agent): ]) def test_poll_multiple_locations(volttron_instance, query_agent, config, result_topics): + gevent.sleep(1) agent = None query_agent.poll_callback.reset_mock() try: From abac250153984a2800cd9c7eff254878bf1d6300 Mon Sep 17 00:00:00 2001 From: jklarson Date: Fri, 24 Apr 2020 14:00:02 -0700 Subject: [PATCH 2/4] Fixed base weather default database file being created outside agent-data dir. --- volttron/platform/agent/base_weather.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/volttron/platform/agent/base_weather.py b/volttron/platform/agent/base_weather.py index 960175710a..c61d59797c 100644 --- a/volttron/platform/agent/base_weather.py +++ b/volttron/platform/agent/base_weather.py @@ -41,7 +41,6 @@ import csv import sqlite3 import datetime -import pkg_resources from functools import wraps from abc import abstractmethod from gevent import get_hub @@ -80,6 +79,8 @@ FORECAST_TIME TIMESTAMP NOT NULL, POINTS TEXT NOT NULL);""" +AGENT_DATA_DIR = os.path.basename(os.getcwd()) + ".agent-data" + CACHE_READ_ERROR = "Cache read failed" CACHE_WRITE_ERROR = "Cache write failed" CACHE_FULL = "cache_full" @@ -103,7 +104,7 @@ class BaseWeatherAgent(Agent): """ def __init__(self, - database_file="weather.sqlite", + database_file=os.path.join(AGENT_DATA_DIR, "weather.sqlite"), api_key=None, max_size_gb=None, poll_locations=None, @@ -114,6 +115,8 @@ def __init__(self, # Initial agent configuration try: super(BaseWeatherAgent, self).__init__(**kwargs) + if os.path.dirname(database_file) == AGENT_DATA_DIR and not os.path.isdir(AGENT_DATA_DIR): + os.mkdir(AGENT_DATA_DIR) self._database_file = database_file self._async_call = AsyncCall() self._api_key = api_key @@ -427,7 +430,7 @@ def _configure(self, config_name, actions, contents): "Configuration of weather agent " "successful") except sqlite3.OperationalError as error: - _log.error("Error initializing cache {}".format(error)) + _log.error("Error initializing cache: {}".format(error)) self.vip.health.set_status(STATUS_BAD, "Cache failed to start " "during configuration") From c2f5ae5c30d139f533b9eb36de5b1fb56f14e6d7 Mon Sep 17 00:00:00 2001 From: jklarson Date: Mon, 27 Apr 2020 13:31:57 -0700 Subject: [PATCH 3/4] Changed base weather tests to use default database file --- .../services/weather/test_base_weather.py | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/volttrontesting/services/weather/test_base_weather.py b/volttrontesting/services/weather/test_base_weather.py index 37deb6c45a..f84e158b2d 100644 --- a/volttrontesting/services/weather/test_base_weather.py +++ b/volttrontesting/services/weather/test_base_weather.py @@ -71,6 +71,8 @@ "name": "fake4"} } +DATABASE_FILE = None + @pytest.fixture(scope="module") def query_agent(request, volttron_instance): @@ -205,6 +207,10 @@ def weather(request, volttron_instance): ) gevent.sleep(2) + global DATABASE_FILE + DATABASE_FILE = agent._database_file + assert DATABASE_FILE.endswith("weather.sqlite") + yield agent agent.core.stop() request.addfinalizer(remove_temp_file) @@ -228,7 +234,7 @@ def test_create_tables(weather): connection = weather._cache._sqlite_conn cursor = connection.cursor() - assert os.path.isfile(weather._database_file) + assert os.path.isfile(DATABASE_FILE) weather._cache.create_tables() @@ -282,7 +288,8 @@ def test_manage_cache_size(volttron_instance): connection = weather._cache._sqlite_conn cursor = connection.cursor() - assert os.path.isfile("weather.sqlite") + database_file = weather._cache._db_file_path + assert os.path.isfile(database_file) for service_name in weather._api_services: query = "DELETE FROM {};".format(service_name) @@ -1037,9 +1044,8 @@ def test_poll_errors(volttron_instance, query_agent, config, def delete_database_file(): - db_path = "weather.sqlite" - if os.path.isfile(db_path): - os.remove(db_path) + if os.path.isfile(DATABASE_FILE): + os.remove(DATABASE_FILE) @pytest.mark.weather2 @@ -1055,8 +1061,8 @@ def test_unhandled_cache_store_exception(volttron_instance, weather, conn.commit() # workaround to open the file in read only mode weather._cache._sqlite_conn.close() - os.chmod(weather._database_file, 0o444) - weather._cache._sqlite_conn = sqlite3.connect(weather._database_file) + os.chmod(DATABASE_FILE, 0o444) + weather._cache._sqlite_conn = sqlite3.connect(DATABASE_FILE) query_agent.alert_callback.reset_mock() results1 = query_agent.vip.rpc.call(identity, "get_current_weather", @@ -1103,8 +1109,8 @@ def test_unhandled_cache_store_exception(volttron_instance, weather, assert results1["observation_time"] != results2["observation_time"] finally: weather._cache._sqlite_conn.close() - os.chmod(weather._database_file, 0o666) - weather._cache._sqlite_conn = sqlite3.connect(weather._database_file) + os.chmod(DATABASE_FILE, 0o666) + weather._cache._sqlite_conn = sqlite3.connect(DATABASE_FILE) @pytest.mark.weather2 @@ -1153,4 +1159,4 @@ def test_unhandled_cache_read_exception(volttron_instance, weather, assert read_warning finally: # make sure the cache is ready to be used again - weather._cache._sqlite_conn = sqlite3.connect(weather._database_file) + weather._cache._sqlite_conn = sqlite3.connect(DATABASE_FILE) From 8730b48168a5dfaace552a333ae69c3fcbe31f71 Mon Sep 17 00:00:00 2001 From: jklarson Date: Mon, 27 Apr 2020 13:47:16 -0700 Subject: [PATCH 4/4] Minor test file cleanup --- volttrontesting/services/weather/test_base_weather.py | 1 - 1 file changed, 1 deletion(-) diff --git a/volttrontesting/services/weather/test_base_weather.py b/volttrontesting/services/weather/test_base_weather.py index 47c3d95d3c..b371f26b0f 100644 --- a/volttrontesting/services/weather/test_base_weather.py +++ b/volttrontesting/services/weather/test_base_weather.py @@ -969,7 +969,6 @@ def test_poll_location(volttron_instance, query_agent): def test_poll_multiple_locations(volttron_instance, query_agent, config, result_topics): gevent.sleep(1) - agent = None query_agent.poll_callback.reset_mock() try: