From 63aaf80e5b3d27ded34f557df2c689c9d9854e97 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sat, 20 Jul 2019 09:42:26 -0700 Subject: [PATCH] Use hash_id in less places I was getting import errors when duplicate hash_id was generated. --- healthkit_to_sqlite/utils.py | 8 ++++---- tests/test_healthkit_to_sqlite.py | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/healthkit_to_sqlite/utils.py b/healthkit_to_sqlite/utils.py index e33d3f9..51c9848 100644 --- a/healthkit_to_sqlite/utils.py +++ b/healthkit_to_sqlite/utils.py @@ -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): @@ -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") diff --git a/tests/test_healthkit_to_sqlite.py b/tests/test_healthkit_to_sqlite.py index e71569a..2cdba12 100644 --- a/tests/test_healthkit_to_sqlite.py +++ b/tests/test_healthkit_to_sqlite.py @@ -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", @@ -50,7 +50,6 @@ def test_converted_activity_summaries(converted): "appleStandHoursGoal": "12", }, { - "id": "5aeb50d13bf455c2485ad008177c3f105949dee1", "dateComponents": "2016-11-16", "activeEnergyBurned": "323.513", "activeEnergyBurnedGoal": "630", @@ -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", @@ -84,7 +84,7 @@ def test_converted_workouts(converted): "metadata_HKWeatherHumidity": "96 %", "workout_events": "[]", } - ] == list(converted["workouts"].rows) + ] == actual assert [ ForeignKey( table="workout_points", @@ -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", @@ -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", @@ -140,7 +141,6 @@ def test_converted_records(converted): "metadata_HKMetadataKeyHeartRateMotionContext": None, }, { - "id": "3d9d67197be1bbf15ff156f126788e946184acc6", "type": "HKQuantityTypeIdentifierHeartRate", "sourceName": "Apple\xa0Watch", "sourceVersion": "4.3.1", @@ -157,4 +157,4 @@ def test_converted_records(converted): "device": "<, name:Apple Watch, manufacturer:Apple, model:Watch, hardware:Watch2,4, software:4.3.1>", "metadata_HKMetadataKeyHeartRateMotionContext": "0", }, - ] == list(converted["records"].rows) + ] == actual