From b1e7b4f2a5ddc5caa8f48f285d9839e1d7cc49ab Mon Sep 17 00:00:00 2001 From: DeutscheGabanna Date: Wed, 17 Jan 2024 19:43:39 +0100 Subject: [PATCH] fixed all test scenarios - returns OK --- _tests/test_dated_entry.py | 7 ++----- _tests/test_librarian.py | 5 +++-- _tests/test_utils.py | 9 +++++++-- dated_entries_group.py | 16 ++++++++++------ dated_entry.py | 2 ++ librarian.py | 2 +- utils.py | 4 +++- 7 files changed, 28 insertions(+), 17 deletions(-) diff --git a/_tests/test_dated_entry.py b/_tests/test_dated_entry.py index 710053a..90a6f22 100644 --- a/_tests/test_dated_entry.py +++ b/_tests/test_dated_entry.py @@ -40,10 +40,7 @@ def test_bare_minimum_dated_entries(self): # When bare_minimum_dated_entry = DatedEntry( time="1:49 AM", - mood="vaguely ok", - override_mood_set={ - "neutral": ["vaguely ok"] - } + mood="vaguely ok" ) # Then @@ -54,4 +51,4 @@ def test_bare_minimum_dated_entries(self): self.assertTrue(bare_minimum_dated_entry.activities, []) def test_insufficient_dated_entries(self): - self.assertRaises(ValueError, DatedEntry, "2:00", mood="", known_moods={"neutral": ["vaguely ok"]}) + self.assertRaises(ValueError, DatedEntry, time="2:00", mood="") diff --git a/_tests/test_librarian.py b/_tests/test_librarian.py index 6f9d1c9..9e74fc8 100644 --- a/_tests/test_librarian.py +++ b/_tests/test_librarian.py @@ -115,8 +115,9 @@ def test_custom_moods_when_json_invalid(self): def test_custom_moods_that_are_incomplete(self): """ - Moodverse can deal with incomplete moods because the file merely expands its default knowledge - Therefore it will still be truthy. + Moodverse can deal with incomplete moods because the file merely expands its default knowledge. + However, it can only expand it (and be truthy) if the dict with moods has all required groups. + Therefore, since ``incomplete-moods`` lacks the ``good`` group, the assertion will evaluate to False. """ lib_to_test = Librarian("sheet-1-valid-data.csv", "_tests/output-results/", "incomplete-moods.json") self.assertFalse(lib_to_test.current_mood_set.has_custom_moods) diff --git a/_tests/test_utils.py b/_tests/test_utils.py index aa4446f..0ed0789 100644 --- a/_tests/test_utils.py +++ b/_tests/test_utils.py @@ -7,9 +7,12 @@ class TestUtils(TestCase): def test_slugify(self): # no need to check if slug is a valid tag + # noinspection SpellCheckingInspection self.assertEqual(utils.slugify("ConvertThis to-------a SLUG", False), "convertthis-to-a-slug") + # noinspection SpellCheckingInspection self.assertEqual(utils.slugify("Zażółć gęślą jaźń ", False), "zażółć-gęślą-jaźń") - self.assertEqual(utils.slugify(" Multiple spaces between words", False), "multiple-spaces-between-words") + self.assertEqual(utils.slugify(" Multiple spaces between words", False), "multiple-spaces-between-words") + # noinspection SpellCheckingInspection self.assertEqual(utils.slugify("Хлеба нашего повшеднего", False), "хлеба-нашего-повшеднего") # check if the slug is a valid tag @@ -23,5 +26,7 @@ def test_slugify(self): utils.slugify("Digits at the end of the string are also ok 456", True) def test_expand_path(self): + # noinspection SpellCheckingInspection self.assertEqual(utils.expand_path("$HOME/whatever"), "/home/deutschegabanna/whatever") - self.assertEqual(utils.expand_path('~'), "/home/deutschegabanna") + # noinspection SpellCheckingInspection + self.assertEqual(utils.expand_path('~/yes'), "/home/deutschegabanna/yes") diff --git a/dated_entries_group.py b/dated_entries_group.py index 57606a5..c1be148 100644 --- a/dated_entries_group.py +++ b/dated_entries_group.py @@ -7,6 +7,7 @@ """ from __future__ import annotations +from typing import Optional import re import logging @@ -119,7 +120,7 @@ class DatedEntriesGroup(utils.Core): """ _instances = {} - def __new__(cls, date: str, current_mood_set: Moodverse): + def __new__(cls, date: str, current_mood_set: Moodverse = Moodverse()): # Check if an instance for the given date already exists if date in cls._instances: return cls._instances[date] @@ -129,10 +130,11 @@ def __new__(cls, date: str, current_mood_set: Moodverse): cls._instances[date] = instance return instance - def __init__(self, date, current_mood_set: Moodverse): + def __init__(self, date, current_mood_set: Moodverse = Moodverse()): """ :raises InvalidDateError: if the date string is deemed invalid by :class:`Date` :param date: The date for all child entries within. + :param current_mood_set: Use custom :class:`Moodverse` or default if not provided. """ self.__logger = logging.getLogger(self.__class__.__name__) @@ -151,15 +153,13 @@ def __init__(self, date, current_mood_set: Moodverse): self.__known_moods: Moodverse = current_mood_set def create_dated_entry_from_row(self, - line: dict[str], - override_mood_set: Moodverse = Moodverse()) -> dated_entry.DatedEntry: + line: dict[str, str]) -> dated_entry.DatedEntry: """ :func:`access_dated_entry` of :class:`DatedEntry` object with the specified parameters. :raises TriedCreatingDuplicateDatedEntryError: if it would result in making a duplicate :class:`DatedEntry` - :raises IncompleteDataRow: if ``line`` does not have ``time`` and ``mood`` keys at the very least + :raises IncompleteDataRow: if ``line`` does not have ``time mood`` keys at the very least, or either is empty :raises ValueError: re-raises ValueError from :class:`DatedEntry` :param line: a dictionary of strings. Required keys: mood, activities, note_title & note. - :param override_mood_set: each key of the dict should have a set of strings containing moods. """ # TODO: test case this # Try accessing the minimum required keys @@ -168,6 +168,10 @@ def create_dated_entry_from_row(self, line[key] except KeyError: raise IncompleteDataRow(key) + # is it empty then, maybe? + else: + if not line[key]: + raise IncompleteDataRow(key) # Check if there's already an object with this time if line["time"] in self.__known_entries_for_this_date: diff --git a/dated_entry.py b/dated_entry.py index f6890b7..b77695a 100644 --- a/dated_entry.py +++ b/dated_entry.py @@ -200,6 +200,8 @@ def __init__(self, # --- # MOOD # --- + if len(mood) == 0: + raise ValueError # Check if the mood is valid - i.e. it does exist in the currently used Moodverse if not override_mood_set.get_mood(mood): errors.ErrorMsgBase.print(ErrorMsg.INVALID_MOOD, mood) diff --git a/librarian.py b/librarian.py index fd79799..a2e2a80 100644 --- a/librarian.py +++ b/librarian.py @@ -307,7 +307,7 @@ def __process_line(self, line: dict[str]) -> bool: else: # Let DatedEntriesGroup handle the rest and increment the counter (True == 1) try: - self.access_date(line["full_date"]).create_dated_entry_from_row(line, self.__mood_set) + self.access_date(line["full_date"]).create_dated_entry_from_row(line) except (dated_entries_group.TriedCreatingDuplicateDatedEntryError, dated_entries_group.IncompleteDataRow, dated_entries_group.InvalidDateError, diff --git a/utils.py b/utils.py index 594f1b0..e2f3e9b 100755 --- a/utils.py +++ b/utils.py @@ -61,6 +61,8 @@ def expand_path(path): # Gets full path, resolving things like ../ return os.path.realpath( # Expands the tilde (~) character to the user's home directory - os.path.expanduser(path) + os.path.expanduser( + os.path.expandvars(path) + ) )