diff --git a/lifting/importers.py b/lifting/importers.py index 05a0e9f..3942c1d 100644 --- a/lifting/importers.py +++ b/lifting/importers.py @@ -17,15 +17,15 @@ def import_fitnotes_db(filename): cur = conn.cursor() for fnid, ename in cur.execute('SELECT _id, name FROM exercise WHERE exercise_type_id=0'): cleaned = _clean_name(ename) - if cleaned in exercises: - exercise_id_mapping[fnid] = exercises[cleaned] + # map to an Exercise id or str + exercise_id_mapping[fnid] = exercises[cleaned] if cleaned in exercises else cleaned for fnid, date, weight_kg, reps in cur.execute( 'SELECT exercise_id, date, metric_weight, reps FROM training_log'): # create Exercise if it wasn't found and there's a workout using it - if fnid not in exercise_id_mapping: - exercise_id_mapping[fnid] = Exercise.objects.create(name=cleaned).id + if isinstance(exercise_id_mapping[fnid], str): + exercise_id_mapping[fnid] = Exercise.objects.create(name=exercise_id_mapping[fnid]).id exercise_id = exercise_id_mapping[fnid] diff --git a/lifting/models.py b/lifting/models.py index 623b16c..734141b 100644 --- a/lifting/models.py +++ b/lifting/models.py @@ -9,6 +9,9 @@ SET_TYPES = ( class Exercise(models.Model): name = models.CharField(max_length=200) + def __str__(self): + return self.name + class Set(models.Model): date = models.DateField() diff --git a/lifting/testdata/example.fitnotes b/lifting/testdata/example.fitnotes new file mode 100644 index 0000000..14d2c9d Binary files /dev/null and b/lifting/testdata/example.fitnotes differ diff --git a/lifting/tests.py b/lifting/tests.py index d9df877..459bb5f 100644 --- a/lifting/tests.py +++ b/lifting/tests.py @@ -16,11 +16,13 @@ class TestFitnotesImport(TestCase): # squat 3 @ 135 # squat 2 @ 185 # squat 5 @ 225 - import_fitnotes_db('example.fitnotes') - #assert Exercise.objects.count() == 2 + def test_basic_import(self): + import_fitnotes_db('lifting/testdata/example.fitnotes') - bp = Exercise.objects.get(name="bench press") - squat = Exercise.objects.get(name="barbell squat") + assert Exercise.objects.count() == 2 - assert Set.objects.count() == 9 + bp = Exercise.objects.get(name="flat barbell bench press") + squat = Exercise.objects.get(name="barbell squat") + + assert Set.objects.count() == 9