From aa2aff4e179121decb1afff91f753f43172ea786 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Tue, 7 Dec 2021 09:25:56 +0100 Subject: [PATCH] Fix import of nomie goal value --- src/habitmove/habits.py | 9 ++++-- tests/test_habits.py | 66 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 tests/test_habits.py diff --git a/src/habitmove/habits.py b/src/habitmove/habits.py index b7099c3..4a03e68 100644 --- a/src/habitmove/habits.py +++ b/src/habitmove/habits.py @@ -1,7 +1,10 @@ +import sqlite3 + +from habitmove.nomiedata import Tracker from habitmove.loopdata import Habit -def migrate(db, trackers): +def migrate(db: sqlite3.Connection, trackers: list[Tracker]): c = db.cursor() habits = trackers_to_habits(trackers) for habit in habits: @@ -34,7 +37,9 @@ def trackers_to_habits(trackers): habits[-1].type = 1 # nomie only has concept of max value, # use a percentage of it for Loop range target - habits[-1].target_value = int(t.max) // NOMIE_MAX_TO_TARGET_VALUE_RATIO + habits[-1].target_value = ( + t.goal or int(t.max) // NOMIE_MAX_TO_TARGET_VALUE_RATIO + ) return habits diff --git a/tests/test_habits.py b/tests/test_habits.py new file mode 100644 index 0000000..a015a14 --- /dev/null +++ b/tests/test_habits.py @@ -0,0 +1,66 @@ +import pytest +from habitmove import loopdata, nomiedata +from habitmove import habits + + +@pytest.fixture +def trackerlist(): + return [ + nomiedata.Tracker( + hidden=False, + score=-1, + tag="testtrack", + emoji="🧪", + label="Testing", + uom="kilotest", + id="12345", + ), + nomiedata.Tracker( + hidden=False, + tag="testtrack", + emoji="🧪", + label="Testing", + id="54321", + one_tap=False, + color="#FF0000", + ignore_zeros=False, + math="mean", + type="range", + uom="megatest", + min=0, + max=10, + goal=6, + default=2, + score=1, + ), + ] + + +def test_simple_habit_transform_from_tracker(trackerlist): + sut = trackerlist[0] + assert habits.trackers_to_habits([sut]) == [ + loopdata.Habit( + name="🧪 Testing", + description="testtrack", + unit="kilotest", + uuid="12345", + archived=False, + color=0, + ) + ] + + +def test_range_habit_transform_from_tracker(trackerlist): + sut = trackerlist[1] + assert habits.trackers_to_habits([sut]) == [ + loopdata.Habit( + name="🧪 Testing", + description="testtrack", + unit="megatest", + uuid="54321", + archived=False, + color=11, + type=1, + target_value=6, + ) + ]