From 03650d27ef3c817fbb0753fb7fdc934829dd89e6 Mon Sep 17 00:00:00 2001 From: James Turk Date: Wed, 1 Apr 2015 17:31:25 -0400 Subject: [PATCH] more FitNotes tests --- lifting/importers.py | 22 ++++++++++++---------- lifting/testdata/baddata.fitnotes | Bin 0 -> 73728 bytes lifting/tests.py | 11 +++++++++++ 3 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 lifting/testdata/baddata.fitnotes diff --git a/lifting/importers.py b/lifting/importers.py index 1809016..f373803 100644 --- a/lifting/importers.py +++ b/lifting/importers.py @@ -1,4 +1,5 @@ import sqlite3 +from django.db import transaction from lifting.models import Exercise, Set @@ -23,16 +24,17 @@ def import_fitnotes_db(filename): # map to an Exercise id or str exercise_id_mapping[fnid] = exercises[cleaned] if cleaned in exercises else cleaned - Set.objects.filter(source='fitnotes').delete() - for fnid, date, weight_kg, reps in cur.execute( - 'SELECT exercise_id, date, metric_weight, reps FROM training_log'): + with transaction.atomic(): + Set.objects.filter(source='fitnotes').delete() + 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 isinstance(exercise_id_mapping[fnid], str): - exercise_id_mapping[fnid] = Exercise.objects.create( - names=[exercise_id_mapping[fnid]]).id + # 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( + 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, - source='fitnotes') + Set.objects.create(exercise_id=exercise_id, date=date, weight_kg=weight_kg, reps=reps, + source='fitnotes') diff --git a/lifting/testdata/baddata.fitnotes b/lifting/testdata/baddata.fitnotes new file mode 100644 index 0000000000000000000000000000000000000000..a0de8a85a6343ec84153f3b230c65c2c11aa4306 GIT binary patch literal 73728 zcmeI5e{38_7035{z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ao~2!H?xfB*=900@8p2!H?x@biB_eM_Q0ct8LIKmY_l00ck)1V8`; gKmY_l00eGv0$T!6Nl^k!;ZIlz9sBMV`18eo0cPVX4*&oF literal 0 HcmV?d00001 diff --git a/lifting/tests.py b/lifting/tests.py index 1892aa8..a82af2d 100644 --- a/lifting/tests.py +++ b/lifting/tests.py @@ -32,3 +32,14 @@ class TestFitnotesImport(TestCase): import_fitnotes_db('lifting/testdata/example.fitnotes') assert Exercise.objects.count() == 2 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