Skip to content

Commit

Permalink
Use hash_id in less places
Browse files Browse the repository at this point in the history
I was getting import errors when duplicate hash_id was generated.
  • Loading branch information
simonw committed Jul 20, 2019
1 parent d642997 commit 63aaf80
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions healthkit_to_sqlite/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ def convert_xml_to_sqlite(fp, db):
record["metadata_" + child.attrib["key"]] = child.attrib["value"]
records.append(record)
if len(records) >= 100:
db["records"].insert_all(records, alter=True, hash_id="id")
db["records"].insert_all(records, alter=True)
records = []
if records:
db["records"].insert_all(records, alter=True, hash_id="id")
db["activity_summary"].upsert_all(activity_summaries, hash_id="id")
db["records"].insert_all(records, alter=True)
db["activity_summary"].insert_all(activity_summaries)


def workout_to_db(workout, db):
Expand All @@ -41,7 +41,7 @@ def workout_to_db(workout, db):
record["metadata_" + el.attrib["key"]] = el.attrib["value"]
# Dump any WorkoutEvent in a nested list for the moment
record["workout_events"] = [el.attrib for el in workout.findall("WorkoutEvent")]
pk = db["workouts"].insert(record, hash_id="id", alter=True).last_pk
pk = db["workouts"].insert(record, alter=True, hash_id="id").last_pk
points = [
dict(el.attrib, workout_id=pk)
for el in workout.findall("WorkoutRoute/Location")
Expand Down
16 changes: 8 additions & 8 deletions tests/test_healthkit_to_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def test_find_all_tags(xml_fp):


def test_converted_activity_summaries(converted):
actual = list(converted["activity_summary"].rows)
assert [
{
"id": "f0801853ea483cb3b80f923abdffefdd9427a940",
"dateComponents": "2016-11-15",
"activeEnergyBurned": "590.252",
"activeEnergyBurnedGoal": "630",
Expand All @@ -50,7 +50,6 @@ def test_converted_activity_summaries(converted):
"appleStandHoursGoal": "12",
},
{
"id": "5aeb50d13bf455c2485ad008177c3f105949dee1",
"dateComponents": "2016-11-16",
"activeEnergyBurned": "323.513",
"activeEnergyBurnedGoal": "630",
Expand All @@ -60,10 +59,11 @@ def test_converted_activity_summaries(converted):
"appleStandHours": "9",
"appleStandHoursGoal": "12",
},
] == list(converted["activity_summary"].rows)
] == actual


def test_converted_workouts(converted):
actual = list(converted["workouts"].rows)
assert [
{
"id": "e615a9651eab4d95debed14c2c2f7cce0c31feed",
Expand All @@ -84,7 +84,7 @@ def test_converted_workouts(converted):
"metadata_HKWeatherHumidity": "96 %",
"workout_events": "[]",
}
] == list(converted["workouts"].rows)
] == actual
assert [
ForeignKey(
table="workout_points",
Expand All @@ -93,6 +93,7 @@ def test_converted_workouts(converted):
other_column="id",
)
] == converted["workout_points"].foreign_keys
actual_points = list(converted["workout_points"].rows)
assert [
{
"date": "2016-11-14 07:25:44 -0700",
Expand All @@ -116,13 +117,13 @@ def test_converted_workouts(converted):
"speed": "2.48034",
"workout_id": "e615a9651eab4d95debed14c2c2f7cce0c31feed",
},
] == list(converted["workout_points"].rows)
] == actual_points


def test_converted_records(converted):
actual = list(converted["records"].rows)
assert [
{
"id": "8bc7fb164391c879fef1333fb9d3a3171a5fe5cf",
"type": "HKQuantityTypeIdentifierBodyMassIndex",
"sourceName": "Health Mate",
"sourceVersion": "2160040",
Expand All @@ -140,7 +141,6 @@ def test_converted_records(converted):
"metadata_HKMetadataKeyHeartRateMotionContext": None,
},
{
"id": "3d9d67197be1bbf15ff156f126788e946184acc6",
"type": "HKQuantityTypeIdentifierHeartRate",
"sourceName": "Apple\xa0Watch",
"sourceVersion": "4.3.1",
Expand All @@ -157,4 +157,4 @@ def test_converted_records(converted):
"device": "<<HKDevice: 0x282a45810>, name:Apple Watch, manufacturer:Apple, model:Watch, hardware:Watch2,4, software:4.3.1>",
"metadata_HKMetadataKeyHeartRateMotionContext": "0",
},
] == list(converted["records"].rows)
] == actual

0 comments on commit 63aaf80

Please sign in to comment.