more FitNotes tests

This commit is contained in:
James Turk 2015-04-01 17:31:25 -04:00
parent 397d1223c9
commit 03650d27ef
3 changed files with 23 additions and 10 deletions

View File

@ -1,4 +1,5 @@
import sqlite3 import sqlite3
from django.db import transaction
from lifting.models import Exercise, Set from lifting.models import Exercise, Set
@ -23,16 +24,17 @@ def import_fitnotes_db(filename):
# map to an Exercise id or str # map to an Exercise id or str
exercise_id_mapping[fnid] = exercises[cleaned] if cleaned in exercises else cleaned exercise_id_mapping[fnid] = exercises[cleaned] if cleaned in exercises else cleaned
Set.objects.filter(source='fitnotes').delete() with transaction.atomic():
for fnid, date, weight_kg, reps in cur.execute( Set.objects.filter(source='fitnotes').delete()
'SELECT exercise_id, date, metric_weight, reps FROM training_log'): 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 # 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( exercise_id_mapping[fnid] = Exercise.objects.create(
names=[exercise_id_mapping[fnid]]).id names=[exercise_id_mapping[fnid]]).id
exercise_id = exercise_id_mapping[fnid] exercise_id = exercise_id_mapping[fnid]
Set.objects.create(exercise_id=exercise_id, date=date, weight_kg=weight_kg, reps=reps, Set.objects.create(exercise_id=exercise_id, date=date, weight_kg=weight_kg, reps=reps,
source='fitnotes') source='fitnotes')

BIN
lifting/testdata/baddata.fitnotes vendored Normal file

Binary file not shown.

View File

@ -32,3 +32,14 @@ class TestFitnotesImport(TestCase):
import_fitnotes_db('lifting/testdata/example.fitnotes') import_fitnotes_db('lifting/testdata/example.fitnotes')
assert Exercise.objects.count() == 2 assert Exercise.objects.count() == 2
assert Set.objects.count() == 9 assert Set.objects.count() == 9
def test_bad_import(self):
# good db then bad db, should fail without screwing up existing data
import_fitnotes_db('lifting/testdata/example.fitnotes')
try:
# baddata.fitnotes has all exercise ids set to 9999
import_fitnotes_db('lifting/testdata/baddata.fitnotes')
except Exception:
pass
assert Exercise.objects.count() == 2
assert Set.objects.count() == 9