def test_complex(self): td = TagDict() td.add('tag') rl = RecordList() dt = datetime(year=2021, month=1, day=15, hour=21, minute=13) rl.append(Record(dt, note='test')) ws = WordSet() with self.assertRaises(Exception): ws.check_note('test') ws.check_note('test', update=True) self.assertEqual('0) tag', str(td)) self.assertEqual('[15.01.2021 21:13:00] <tag> test', str(rl)) ws.check_note('test') db.save() db.clear() self.assertEqual('', str(td)) self.assertEqual('', str(rl)) with self.assertRaises(Exception): ws.check_note('test') db.load() self.assertEqual('0) tag', str(td)) self.assertEqual('[15.01.2021 21:13:00] <tag> test', str(rl)) ws.check_note('test')
class DiaryManager: def __init__(self): self.tag_dict = TagDict() self.word_set = WordSet() self.record_list = RecordList() def add_record( self, dt: Optional[datetime] = None, tag: str = '', is_active: Optional[bool] = None, note: str = '', update: bool = False, ): tag_id = self._tag_to_tag_id(tag) self.word_set.check_note(note, update) self.record_list.append(Record(dt, tag_id, is_active, note)) def update_record( self, index: int, dt: Optional[datetime], tag: str, is_active: Optional[bool], note: str, update: bool = False, ): tag_id = self._tag_to_tag_id(tag) self.word_set.check_note(note, update) self.record_list[index] = Record(dt, tag_id, is_active, note) def _tag_to_tag_id(self, tag) -> int: return self.tag_dict.get_id(tag) def tag_stat(self) -> str: tag_id_stat = self.record_list.get_tag_id_stat() for tag_id in self.tag_dict: if tag_id not in tag_id_stat: tag_id_stat[tag_id] = 0 result = [(v, self.tag_dict[k]) for k, v in tag_id_stat.items()] result.sort(reverse=True) return '\n'.join(f'{item[0]} {item[1]}' for item in result) def set_tag_id(self, new_id: int, tag: str): old_id = self.tag_dict.set_id(new_id, tag) self.record_list.swap_tag_id(old_id, new_id) def tag_filter(self, tag: Optional[str] = None) -> Tuple[List[int], List[str]]: if tag is None: return ( list(range(len(self.record_list))), [str(record) for record in self.record_list], ) else: return self.record_list.filter_record( tag_id=self.tag_dict.get_id(tag))
class TestRecordList(unittest.TestCase): def setUp(self) -> None: db.load() td = TagDict() td.add('no tag') td.add('test tag') self._record_list = RecordList() def test_append(self): self._record_list.append(Record()) self.assertEqual(1, len(self._record_list)) def test_str(self): dt = datetime(year=2021, month=1, day=16, hour=19, minute=4) self._record_list.append(Record(dt, note='first note')) dt = datetime(year=2021, month=1, day=16, hour=19, minute=20) self._record_list.append(Record(dt, note='second note')) self.assertEqual( '[16.01.2021 19:04:00] <no tag> first note\n' '[16.01.2021 19:20:00] <no tag> second note', str(self._record_list), ) def test_str_len(self): dt = datetime(year=2021, month=1, day=19, hour=19, minute=38) for _ in range(10): self._record_list.append(Record(dt, note='test')) self._record_list.set_str_len(5) self.assertEqual( '[19.01.2021 19:38:00] <no tag> test\n' * 5 + 'last 5 from 10', str(self._record_list), ) self._record_list.set_str_len(50) self.assertEqual( '[19.01.2021 19:38:00] <no tag> test\n' * 9 + '[19.01.2021 19:38:00] <no tag> test', str(self._record_list), ) def test_delete_record(self): dt = datetime(year=2021, month=1, day=25, hour=16, minute=21) self._record_list.append(Record(dt, note='first')) dt = datetime(year=2021, month=1, day=25, hour=16, minute=23) self._record_list.append(Record(dt, note='second')) self.assertEqual( '[25.01.2021 16:21:00] <no tag> first\n' '[25.01.2021 16:23:00] <no tag> second', str(self._record_list), ) self._record_list.delete_record(index=0) self.assertEqual( '[25.01.2021 16:23:00] <no tag> second', str(self._record_list), ) def test_swap_tag_id(self): dt = datetime(year=2021, month=1, day=26, hour=21, minute=42) self._record_list.append(Record(dt, note='first')) dt = datetime(year=2021, month=1, day=26, hour=21, minute=43) self._record_list.append(Record(dt, tag_id=1, note='second')) self.assertEqual( '[26.01.2021 21:42:00] <no tag> first\n' '[26.01.2021 21:43:00] <test tag> second', str(self._record_list), ) self._record_list.swap_tag_id(0, 1) self.assertEqual( '[26.01.2021 21:42:00] <test tag> first\n' '[26.01.2021 21:43:00] <no tag> second', str(self._record_list), ) def test_get_tag_id_stat(self): self.assertEqual({}, self._record_list.get_tag_id_stat()) self._record_list.append(Record()) self.assertEqual({0: 1}, self._record_list.get_tag_id_stat()) self._record_list.append(Record(tag_id=1)) self.assertEqual({0: 1, 1: 1}, self._record_list.get_tag_id_stat()) self._record_list.append(Record(tag_id=1)) self.assertEqual({0: 1, 1: 2}, self._record_list.get_tag_id_stat()) def test_sort_dt(self): dt = datetime(year=2021, month=1, day=23, hour=17, minute=37) self._record_list.append(Record(dt)) dt = datetime(year=2021, month=1, day=23, hour=17, minute=36) self._record_list.append(Record(dt)) self._record_list.sort() self.assertEqual( '[23.01.2021 17:36:00] <no tag>\n' '[23.01.2021 17:37:00] <no tag>', str(self._record_list), ) def test_sort_is_active(self): dt = datetime(year=2021, month=1, day=23, hour=17, minute=36) self._record_list.append(Record(dt, is_active=True)) # start dt = datetime( year=2021, month=1, day=23, hour=17, minute=36, microsecond=1) self._record_list.append(Record(dt, tag_id=1, is_active=False)) # end self._record_list.sort() self.assertEqual( '[23.01.2021 17:36:00] <test tag: end>\n' '[23.01.2021 17:36:00] <no tag: start>', str(self._record_list), ) def test_sort_is_active_none(self): dt = datetime(year=2021, month=1, day=23, hour=18, minute=6) self._record_list.append(Record(dt, is_active=True)) # start dt = datetime( year=2021, month=1, day=23, hour=18, minute=6, microsecond=1) self._record_list.append(Record(dt, tag_id=1)) self._record_list.sort() self.assertEqual( '[23.01.2021 18:06:00] <test tag>\n' '[23.01.2021 18:06:00] <no tag: start>', str(self._record_list), ) def test_tag_id_filter(self): dt = datetime(year=2021, month=1, day=27, hour=12, minute=40) self._record_list.append(Record(dt, note='first')) self._record_list.append(Record(dt, tag_id=1, note='second')) self._record_list.append(Record(dt, note='third')) self._record_list.append(Record(dt, tag_id=1, note='fourth')) indexes, str_records = self._record_list.filter_record(tag_id=0) self.assertEqual([0, 2], indexes) self.assertEqual([ '[27.01.2021 12:40:00] <no tag> first', '[27.01.2021 12:40:00] <no tag> third', ], str_records) indexes, str_records = self._record_list.filter_record(tag_id=1) self.assertEqual([1, 3], indexes) self.assertEqual([ '[27.01.2021 12:40:00] <test tag> second', '[27.01.2021 12:40:00] <test tag> fourth', ], str_records) def test_note_filter(self): dt = datetime(year=2021, month=1, day=25, hour=19, minute=32) self._record_list.append(Record(dt, note='ABC')) self._record_list.append(Record(dt, note='BCD')) self._record_list.append(Record(dt, note='CDA')) indexes, str_records = self._record_list.filter_record(fragment='A') self.assertEqual([0, 2], indexes) self.assertEqual([ '[25.01.2021 19:32:00] <no tag> ABC', '[25.01.2021 19:32:00] <no tag> CDA', ], str_records) indexes, str_records = self._record_list.filter_record(fragment='B') self.assertEqual([0, 1], indexes) self.assertEqual([ '[25.01.2021 19:32:00] <no tag> ABC', '[25.01.2021 19:32:00] <no tag> BCD', ], str_records) indexes, str_records = self._record_list.filter_record(fragment='C') self.assertEqual([0, 1, 2], indexes) self.assertEqual([ '[25.01.2021 19:32:00] <no tag> ABC', '[25.01.2021 19:32:00] <no tag> BCD', '[25.01.2021 19:32:00] <no tag> CDA', ], str_records) indexes, str_records = self._record_list.filter_record(fragment='D') self.assertEqual([1, 2], indexes) self.assertEqual([ '[25.01.2021 19:32:00] <no tag> BCD', '[25.01.2021 19:32:00] <no tag> CDA', ], str_records) def test_tag_id_and_note_filter(self): dt = datetime(year=2021, month=1, day=27, hour=20, minute=53) self._record_list.append(Record(dt, note='note A')) self._record_list.append(Record(dt, note='note B')) self._record_list.append(Record(dt, tag_id=1, note='note A')) self._record_list.append(Record(dt, tag_id=1, note='note B')) self.assertEqual( ([0], ['[27.01.2021 20:53:00] <no tag> note A']), self._record_list.filter_record(tag_id=0, fragment='A'), ) self.assertEqual( ([1], ['[27.01.2021 20:53:00] <no tag> note B']), self._record_list.filter_record(tag_id=0, fragment='B'), ) self.assertEqual( ([2], ['[27.01.2021 20:53:00] <test tag> note A']), self._record_list.filter_record(tag_id=1, fragment='A'), ) self.assertEqual( ([3], ['[27.01.2021 20:53:00] <test tag> note B']), self._record_list.filter_record(tag_id=1, fragment='B'), ) def tearDown(self) -> None: db.clear() db.save()