Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/VOLTTRON/volttron into d…
Browse files Browse the repository at this point in the history
…evelop
  • Loading branch information
craig8 committed Sep 26, 2020
2 parents dd588b6 + 1edfe46 commit 85e8e82
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
8 changes: 6 additions & 2 deletions volttron/platform/agent/base_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,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"
Expand All @@ -102,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,
Expand All @@ -113,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
Expand Down Expand Up @@ -426,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")

Expand Down
27 changes: 16 additions & 11 deletions volttrontesting/services/weather/test_base_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
"name": "fake4"}
}

DATABASE_FILE = None


@pytest.fixture(scope="module")
def query_agent(request, volttron_instance):
Expand Down Expand Up @@ -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)
Expand All @@ -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()

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -962,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:
Expand Down Expand Up @@ -1039,9 +1045,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
Expand All @@ -1057,8 +1062,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",
Expand Down Expand Up @@ -1105,8 +1110,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
Expand Down Expand Up @@ -1155,4 +1160,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)

0 comments on commit 85e8e82

Please sign in to comment.