update FN import test to work w/ names

This commit is contained in:
James Turk 2015-04-01 17:19:28 -04:00
parent a3bf766642
commit 9d2bf76d3d
3 changed files with 17 additions and 7 deletions

View File

@ -8,7 +8,10 @@ def _clean_name(name):
def import_fitnotes_db(filename): def import_fitnotes_db(filename):
# exercise names to db ids # 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 # build mapping FitNotes exercise id => our exercise id
exercise_id_mapping = {} 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 # create Exercise if it wasn't found and there's a workout using it
if isinstance(exercise_id_mapping[fnid], str): 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] exercise_id = exercise_id_mapping[fnid]

View File

@ -11,7 +11,7 @@ class Exercise(models.Model):
names = ArrayField(models.CharField(max_length=200)) names = ArrayField(models.CharField(max_length=200))
def __str__(self): def __str__(self):
return self.name return self.names
class Set(models.Model): class Set(models.Model):

View File

@ -18,11 +18,17 @@ class TestFitnotesImport(TestCase):
# squat 5 @ 225 # squat 5 @ 225
def test_basic_import(self): def test_basic_import(self):
# ensure that the data comes in
import_fitnotes_db('lifting/testdata/example.fitnotes') import_fitnotes_db('lifting/testdata/example.fitnotes')
assert Exercise.objects.count() == 2 assert Exercise.objects.count() == 2
bp = Exercise.objects.get(names__contains=["flat barbell bench press"])
bp = Exercise.objects.get(name="flat barbell bench press") squat = Exercise.objects.get(names__contains=["barbell squat"])
squat = Exercise.objects.get(name="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 assert Set.objects.count() == 9