From 9d2bf76d3d52390cdafbb134e55ef355d1c89514 Mon Sep 17 00:00:00 2001 From: James Turk Date: Wed, 1 Apr 2015 17:19:28 -0400 Subject: [PATCH] update FN import test to work w/ names --- lifting/importers.py | 8 ++++++-- lifting/models.py | 2 +- lifting/tests.py | 14 ++++++++++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lifting/importers.py b/lifting/importers.py index 3942c1d..c6787f5 100644 --- a/lifting/importers.py +++ b/lifting/importers.py @@ -8,7 +8,10 @@ def _clean_name(name): def import_fitnotes_db(filename): # exercise names to db ids - exercises = {_clean_name(e.name): e.id for e in Exercise.objects.all()} + exercises = {} + for e in Exercise.objects.all(): + for n in e.names: + exercises[_clean_name(n)] = e.id # build mapping FitNotes exercise id => our exercise id exercise_id_mapping = {} @@ -25,7 +28,8 @@ def import_fitnotes_db(filename): # create Exercise if it wasn't found and there's a workout using it if isinstance(exercise_id_mapping[fnid], str): - exercise_id_mapping[fnid] = Exercise.objects.create(name=exercise_id_mapping[fnid]).id + exercise_id_mapping[fnid] = Exercise.objects.create( + names=[exercise_id_mapping[fnid]]).id exercise_id = exercise_id_mapping[fnid] diff --git a/lifting/models.py b/lifting/models.py index dc1c82d..76aca9a 100644 --- a/lifting/models.py +++ b/lifting/models.py @@ -11,7 +11,7 @@ class Exercise(models.Model): names = ArrayField(models.CharField(max_length=200)) def __str__(self): - return self.name + return self.names class Set(models.Model): diff --git a/lifting/tests.py b/lifting/tests.py index 459bb5f..1892aa8 100644 --- a/lifting/tests.py +++ b/lifting/tests.py @@ -18,11 +18,17 @@ class TestFitnotesImport(TestCase): # squat 5 @ 225 def test_basic_import(self): + # ensure that the data comes in import_fitnotes_db('lifting/testdata/example.fitnotes') assert Exercise.objects.count() == 2 - - bp = Exercise.objects.get(name="flat barbell bench press") - squat = Exercise.objects.get(name="barbell squat") - + bp = Exercise.objects.get(names__contains=["flat barbell bench press"]) + squat = Exercise.objects.get(names__contains=["barbell squat"]) + assert Set.objects.count() == 9 + + def test_double_import(self): + # two identical dbs, should be idempotent + import_fitnotes_db('lifting/testdata/example.fitnotes') + import_fitnotes_db('lifting/testdata/example.fitnotes') + assert Exercise.objects.count() == 2 assert Set.objects.count() == 9