From b9c89155e346932cf338f9d146bd46bb1408d1b1 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Tue, 7 Dec 2021 09:32:26 +0100 Subject: [PATCH] Fix import of nomie numerical score values --- src/habitmove/habits.py | 2 +- src/habitmove/nomiedata.py | 33 +++++++++++++++++++-------------- tests/test_nomiedata.py | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 tests/test_nomiedata.py diff --git a/src/habitmove/habits.py b/src/habitmove/habits.py index 4a03e68..0866380 100644 --- a/src/habitmove/habits.py +++ b/src/habitmove/habits.py @@ -26,7 +26,7 @@ def trackers_to_habits(trackers): habits.append( Habit( archived=t.hidden, - color=11 if t.score != "-1" else 0, + color=0 if t.score == -1 else 11, description=t.tag, name=f"{t.emoji} {t.label}", unit="" if t.uom == "num" else t.uom, diff --git a/src/habitmove/nomiedata.py b/src/habitmove/nomiedata.py index cbcb9c7..9deab64 100644 --- a/src/habitmove/nomiedata.py +++ b/src/habitmove/nomiedata.py @@ -1,30 +1,35 @@ -from typing import Optional, Any +from typing import Any, Union from dataclasses import dataclass, field +import re # A nomie habit tracker. Tracks anything whose value can be encapsulated in a numerical value. @dataclass(frozen=True) class Tracker: - color: str - emoji: str - hidden: bool - id: str - ignore_zeros: bool - label: str - math: str - one_tap: bool tag: str - type: str - uom: str + label: str + id: str + one_tap: bool = True + color: str = "#000080" + emoji: str = "" + hidden: bool = False + ignore_zeros: bool = False + math: str = "mean" + type: str = "tick" # tick or range mostly + uom: str = "" # TODO no idea what include does - include: str + include: str = "" min: int = 0 max: int = 0 goal: int = 0 default: int = 0 - # TODO score can be string (if custom) or int (if simple good/bad) - score: str = "" + score: Union[int, str] = 1 # score can be string ('custom') or int score_calc: list[dict[str, Any]] = field(default_factory=lambda: []) + def __post_init__(self): + # ensure save as int if not 'custom' scoring + if re.match(r"^-?[0-9]+$", str(self.score)): + object.__setattr__(self, "score", int(self.score)) + @dataclass(frozen=True) class Activity: diff --git a/tests/test_nomiedata.py b/tests/test_nomiedata.py new file mode 100644 index 0000000..c2f9644 --- /dev/null +++ b/tests/test_nomiedata.py @@ -0,0 +1,18 @@ +from habitmove import nomiedata + + +def test_score_numerical_becomes_int(): + sut = nomiedata.Tracker(label="Int checking", tag="isint", id="1337", score="-1") + assert type(sut.score) == int + + +def test_score_invalid_int_stays_string(): + sut = nomiedata.Tracker(label="Int checking", tag="isint", id="1337", score="-1.3") + assert type(sut.score) == str + + +def test_score_string_stays_string(): + sut = nomiedata.Tracker( + label="Int checking", tag="isint", id="1337", score="custom" + ) + assert type(sut.score) == str