Skip to content

Commit

Permalink
Merge pull request #109 from lanl/test_fixes
Browse files Browse the repository at this point in the history
Test fixes
  • Loading branch information
jpulidojr authored Aug 21, 2024
2 parents 36d796a + 6134776 commit ab6f394
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
51 changes: 50 additions & 1 deletion dsi/backends/sqlite.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import csv
import sqlite3
import json
import re

from dsi.backends.filesystem import Filesystem

Expand All @@ -9,6 +11,7 @@
STRING = "VARCHAR"
FLOAT = "FLOAT"
INT = "INT"
JSON = "TEXT"

# Holds table name and data properties

Expand Down Expand Up @@ -83,7 +86,6 @@ def put_artifact_type(self, types, isVerbose=False):
if isVerbose:
print(str_query)

print(str_query)
self.cur.execute(str_query)
self.con.commit()

Expand Down Expand Up @@ -219,6 +221,53 @@ def put_artifacts_lgcy(self,artifacts, isVerbose=False):
self.cur.execute(str_query)
self.con.commit()

def put_artifacts_json(self, fname, tname, isVerbose=False):
"""
Function for insertion of Artifact metadata into a defined schema by using a JSON file
`fname`: filepath to the .json file to be read and inserted into the database
`tname`: String name of the table to be inserted
`return`: none
"""

json_str = None
try:
j = open(fname)
data = json.load(j)
json_str = json.dumps(data)
json_str = "'" + json_str + "'"
j.close()
except IOError as i:
print(i)
return
except ValueError as v:
print(v)
return

types = DataType()
types.properties = {}
types.name = tname

# Check if this has been defined from helper function
if self.types != None:
types.name = self.types.name

col_name = re.sub(r'.json', '', fname)
col_name = re.sub(r'.*/', '', col_name)
col_name = "'" + col_name + "'"
types.properties[col_name] = JSON

self.put_artifact_type(types)
col_names = ', '.join(types.properties.keys())
str_query = "INSERT INTO {} ({}) VALUES ({});".format(str(types.name), col_names, json_str)
if isVerbose:
print(str_query)

self.types = types
self.cur.execute(str_query)
self.con.commit()

# Adds columns and rows automaticallly based on a csv file
#[NOTE 3] This method should be deprecated in favor of put_artifacts.
def put_artifacts_csv(self, fname, tname, isVerbose=False):
Expand Down
24 changes: 23 additions & 1 deletion dsi/backends/tests/test_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ def test_wildfiredata_artifact_put():
# No error implies success
assert True

def test_wildfiredata_artifact_put_t():
valid_middleware_datastructure = OrderedDict({'foo':[1,2,3],'bar':[3,2,1]})
dbpath = 'test_wildfiredata_artifact.sqlite_data'
store = Sqlite(dbpath)
store.put_artifacts_t(valid_middleware_datastructure, tableName="Wildfire")
store.close()
# No error implies success
assert True

#Data from: https://microsoftedge.github.io/Demos/json-dummy-data/64KB.json
def test_jsondata_artifact_put():
jsonpath = '/'.join([get_git_root('.'), 'dsi/data/64KB.json'])
dbpath = "jsondata.db"
store = Sqlite(dbpath)
store.put_artifacts_json(jsonpath, tname="JSONData")
store.close()
# No error implies success
assert True

def test_yosemite_data_csv_artifact():
csvpath = '/'.join([get_git_root('.'), 'dsi/data/yosemite5.csv'])
dbpath = "yosemite.db"
Expand All @@ -55,7 +74,10 @@ def test_artifact_query():
data_type.name = "simulation"
result = store.sqlquery("SELECT *, MAX(wind_speed) AS max_windspeed FROM " +
str(data_type.name) + " GROUP BY safe_unsafe_fire_behavior")
store.export_csv(result, "query.csv")
store.export_csv(result, "TABLENAME", "query.csv")
store.close()
# No error implies success
assert True


test_jsondata_artifact_put()

0 comments on commit ab6f394

Please sign in to comment.