Skip to content

Commit

Permalink
handle schema difference, id casing, closes #6 (#7)
Browse files Browse the repository at this point in the history
* handle casing of id, closes #6

remove unused fixture

* add tests for alternate schema,

id field is lowercase in habits table
  • Loading branch information
ConorSheehan1 authored Apr 24, 2022
1 parent bc93aa2 commit bdc07d9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 11 deletions.
13 changes: 10 additions & 3 deletions src/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ def convert_bool_habit_to_num(
if not data:
return f"Could not find habit with name '{habit_name}'"
habit = dict(data)
# some variance in schema, can be capitalized or not. see issue #6
if "Id" in habit:
habit_id = habit["Id"]
where_habit_str = f"Id IS {habit['Id']}"
else:
habit_id = habit["id"]
where_habit_str = f"id IS {habit['id']}"
if habit["type"] != self.bool_habit_type:
return f"Could not find habit with type ${self.bool_habit_type} and name '{habit_name}'"

Expand All @@ -90,20 +97,20 @@ def convert_bool_habit_to_num(
f"found {print_dict}{' ':<{75 - len(str(print_dict))}} updating {update_dict} ..."
)
self.cursor.execute(
f"UPDATE Habits Set {update_str} where Id IS {habit['Id']} AND TYPE IS {self.bool_habit_type};"
f"UPDATE Habits Set {update_str} where {where_habit_str} AND TYPE IS {self.bool_habit_type};"
)

# update habit type
self.cursor.execute(
f"UPDATE Habits Set type = 1 where Id IS {habit['Id']} AND TYPE IS {self.bool_habit_type};"
f"UPDATE Habits Set type = 1 where {where_habit_str} AND TYPE IS {self.bool_habit_type};"
)

# update reps
self.cursor.execute(
f"""
UPDATE Repetitions
SET value = {new_value}
WHERE habit is {habit['Id']} AND value IS {old_value};
WHERE habit is {habit_id} AND value IS {old_value};
"""
)

Expand Down
7 changes: 7 additions & 0 deletions tests/data/Habits_alt.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
id,archived,color,description,freq_den,freq_num,highlight,name,position,reminder_days,reminder_hour,reminder_min,type,target_type,target_value,unit,question,uuid
1,0,7,,7,3,0,Gym,4,84,19,0,0,0,0.0,,Did you lift today?,e7b2eace076600d63ef95be46fc5912a31
2,0,15,,7,2,0,Drink,5,0,,,0,0,1.0,,,1a29a84792d1d1fa7371ee8036ccb10a32
3,0,7,,1,1,0,Program,6,0,,,0,0,0.0,,,eda88bbe6d96c4962bf9f6974c212b9f37
4,1,15,,1,1,0,Sweets,3,0,,,0,0,0.0,,,1a3dad7a2c27fd08d9599292775c0ed43136
5,0,15,,7,5,0,Coffee,2,0,,,0,0,0.0,,,34458704a3658719926fae6f647cd0ae3338
6,0,15,,1,1,0,Tea,1,0,,,1,0,1.0,cups,How many cups of tea did you drink?,43a1e0375b9f498180c82b4156048c8b
14 changes: 14 additions & 0 deletions tests/data/Repetitions_alt.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
id,habit,timestamp,value
1,1,1471219200000,2
2,1,1471305600000,2
3,6,1538576000000,1000
4,6,1638576000000,1000
5,4,1471219200000,2
6,4,1471319200000,2
7,2,1481319200000,2
8,2,1491319200000,2
9,3,1491519200000,2
10,3,1491619200000,2
11,5,1591619200000,2
12,5,1691619200000,2
13,5,1791619200000,2
2 changes: 0 additions & 2 deletions tests/data/android_metadata.csv

This file was deleted.

34 changes: 28 additions & 6 deletions tests/unit/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

class TestConverter(unittest.TestCase):
def setUp(self):
self.id_field = "Id"
cwd = os.path.abspath(os.path.dirname(__file__))
data_dir = os.path.join(cwd, "..", "data")
csv_glob = os.path.join(data_dir, "*.csv")
Expand Down Expand Up @@ -70,7 +71,7 @@ def test_convert_bool_habit_to_num_wrong_type(self):
def test_convert_bool_habit_to_num(self):
# assert habit and reps are boolean
habit_before = self.get_habit_by_name("Coffee")
reps_before = self.get_entries_by_id(habit_before["Id"])
reps_before = self.get_entries_by_id(habit_before[self.id_field])
assert habit_before["type"] == 0
assert habit_before["freq_den"] == 7
assert habit_before["freq_num"] == 5
Expand All @@ -89,7 +90,7 @@ def test_convert_bool_habit_to_num(self):

# assert habit and reps are numeric
habit_after = self.get_habit_by_name("Coffee")
reps_after = self.get_entries_by_id(habit_after["Id"])
reps_after = self.get_entries_by_id(habit_after[self.id_field])
assert habit_after["type"] == 1
assert habit_after["freq_den"] == 7
assert habit_after["freq_num"] == 1
Expand All @@ -99,7 +100,7 @@ def test_convert_bool_habit_to_num(self):
def test_convert_bool_habit_to_num_preserve_graph(self):
# assert habit and reps are boolean
habit_before = self.get_habit_by_name("Coffee")
reps_before = self.get_entries_by_id(habit_before["Id"])
reps_before = self.get_entries_by_id(habit_before[self.id_field])
assert habit_before["type"] == 0
assert habit_before["freq_den"] == 7
assert habit_before["freq_num"] == 5
Expand All @@ -118,7 +119,7 @@ def test_convert_bool_habit_to_num_preserve_graph(self):

# assert habit and reps are numeric
habit_after = self.get_habit_by_name("Coffee")
reps_after = self.get_entries_by_id(habit_after["Id"])
reps_after = self.get_entries_by_id(habit_after[self.id_field])
assert habit_after["type"] == 1
assert habit_after["freq_den"] == 7
assert habit_after["freq_num"] == 5 # different to default preserve=logic
Expand All @@ -128,7 +129,7 @@ def test_convert_bool_habit_to_num_preserve_graph(self):
def test_convert_bool_habit_to_num_archived(self):
# assert habit and reps are boolean
habit_before = self.get_habit_by_name("Sweets")
reps_before = self.get_entries_by_id(habit_before["Id"])
reps_before = self.get_entries_by_id(habit_before[self.id_field])
assert habit_before["type"] == 0
assert habit_before["freq_den"] == 1
assert habit_before["freq_num"] == 1
Expand All @@ -151,9 +152,30 @@ def test_convert_bool_habit_to_num_archived(self):

# assert habit and reps are numeric
habit_after = self.get_habit_by_name("Sweets")
reps_after = self.get_entries_by_id(habit_after["Id"])
reps_after = self.get_entries_by_id(habit_after[self.id_field])
assert habit_after["type"] == 1
assert habit_after["freq_den"] == 1
assert habit_after["freq_num"] == 1
assert habit_after["target_value"] == 1
assert all([rep["value"] == 1000 for rep in reps_after])


class TestConverterAltSchema(TestConverter):
"""
same tests but with alternate schema
"""

def setUp(self):
self.id_field = "id" # lowercase id, see issues #6
cwd = os.path.abspath(os.path.dirname(__file__))
data_dir = os.path.join(cwd, "..", "data")
csv_glob = os.path.join(data_dir, "*_alt.csv")
# create sql db in memory for test
self.converter = Converter(inputdb="", outputdb=":memory:", create_outputdb=False)
for csv_file in glob.glob(csv_glob):
self.load_csv_to_table(csv_file)

def load_csv_to_table(self, csv_path: str):
table = os.path.basename(csv_path).replace("_alt.csv", "")
df = pandas.read_csv(csv_path)
df.to_sql(table, self.converter.con)

0 comments on commit bdc07d9

Please sign in to comment.