Fix import of nomie numerical score values

This commit is contained in:
Marty Oehme 2021-12-07 09:32:26 +01:00
parent aa2aff4e17
commit b9c89155e3
Signed by: Marty
GPG key ID: B7538B8F50A1C800
3 changed files with 38 additions and 15 deletions

View file

@ -26,7 +26,7 @@ def trackers_to_habits(trackers):
habits.append( habits.append(
Habit( Habit(
archived=t.hidden, archived=t.hidden,
color=11 if t.score != "-1" else 0, color=0 if t.score == -1 else 11,
description=t.tag, description=t.tag,
name=f"{t.emoji} {t.label}", name=f"{t.emoji} {t.label}",
unit="" if t.uom == "num" else t.uom, unit="" if t.uom == "num" else t.uom,

View file

@ -1,30 +1,35 @@
from typing import Optional, Any from typing import Any, Union
from dataclasses import dataclass, field from dataclasses import dataclass, field
import re
# A nomie habit tracker. Tracks anything whose value can be encapsulated in a numerical value. # A nomie habit tracker. Tracks anything whose value can be encapsulated in a numerical value.
@dataclass(frozen=True) @dataclass(frozen=True)
class Tracker: class Tracker:
color: str
emoji: str
hidden: bool
id: str
ignore_zeros: bool
label: str
math: str
one_tap: bool
tag: str tag: str
type: str label: str
uom: 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 # TODO no idea what include does
include: str include: str = ""
min: int = 0 min: int = 0
max: int = 0 max: int = 0
goal: int = 0 goal: int = 0
default: int = 0 default: int = 0
# TODO score can be string (if custom) or int (if simple good/bad) score: Union[int, str] = 1 # score can be string ('custom') or int
score: str = ""
score_calc: list[dict[str, Any]] = field(default_factory=lambda: []) 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) @dataclass(frozen=True)
class Activity: class Activity:

18
tests/test_nomiedata.py Normal file
View file

@ -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