1
0
personal-data/test/test_deduplicate.py

96 lines
2.4 KiB
Python
Raw Normal View History

2024-06-06 21:49:52 +00:00
import datetime
from decimal import Decimal
2024-06-06 21:50:29 +00:00
from frozendict import frozendict
2024-05-18 19:52:22 +00:00
from personal_data.data import DeduplicateMode
from personal_data.main import deduplicate_dicts
LIST = [
2024-05-21 21:37:59 +00:00
frozendict({'a': 0, 'b': 12, 't': 300}),
frozendict({'a': 0, 'b': 12, 't': 301}),
2024-05-18 19:52:22 +00:00
frozendict({'a': 1, 'b': 2, 't': 300}),
frozendict({'a': 1, 'b': 2, 't': 301}),
frozendict({'a': 1, 'b': 2, 't': 302}),
frozendict({'a': 1, 'b': 2, 't': 303}),
]
2024-05-21 21:37:59 +00:00
def test_no_fields_to_ignore():
for mode in DeduplicateMode:
ls, fields = deduplicate_dicts(LIST, mode, [])
assert fields == ['a', 'b', 't']
assert ls == LIST
2024-05-18 19:52:22 +00:00
2024-05-21 21:37:59 +00:00
def test_only_latest():
ls, fields = deduplicate_dicts(LIST, DeduplicateMode.ONLY_LATEST, ['t'])
2024-05-18 19:52:22 +00:00
assert fields == ['a', 'b', 't']
2024-06-06 21:49:52 +00:00
assert ls == LIST[:3]
2024-05-18 19:52:22 +00:00
2024-05-21 21:37:59 +00:00
def test_all_fields():
ls, fields = deduplicate_dicts(LIST, DeduplicateMode.BY_ALL_COLUMNS, ['t'])
2024-05-18 19:52:22 +00:00
assert fields == ['a', 'b', 't']
2024-05-21 21:37:59 +00:00
print(ls)
assert ls == [
frozendict({'a': 0, 'b': 12, 't': 300}),
frozendict({'a': 1, 'b': 2, 't': 300}),
]
2024-06-06 21:49:52 +00:00
2024-06-06 21:50:29 +00:00
2024-06-06 21:49:52 +00:00
def test_all_fields():
ls, fields = deduplicate_dicts(LIST + LIST, DeduplicateMode.BY_ALL_COLUMNS, ['t'])
assert fields == ['a', 'b', 't']
print(ls)
assert ls == [
frozendict({'a': 0, 'b': 12, 't': 300}),
frozendict({'a': 1, 'b': 2, 't': 300}),
]
2024-06-06 21:50:29 +00:00
2024-06-06 21:49:52 +00:00
LIST_2 = [
2024-06-06 21:50:29 +00:00
frozendict(
{
'weight.sample_time': datetime.datetime(
2024,
5,
28,
6,
27,
31,
134506,
tzinfo=datetime.timezone.utc,
),
'weight.kg': Decimal('73.6'),
},
),
frozendict(
{
'weight.sample_time': datetime.datetime(
2024,
6,
1,
7,
36,
9,
590355,
tzinfo=datetime.timezone.utc,
),
'weight.kg': Decimal('74.7'),
},
),
2024-06-06 21:49:52 +00:00
]
2024-06-06 21:50:29 +00:00
2024-06-06 21:49:52 +00:00
def test_deduplicate_weight():
ls, fields = deduplicate_dicts(LIST_2, DeduplicateMode.BY_ALL_COLUMNS, [])
assert fields == ['weight.sample_time', 'weight.kg']
assert ls == LIST_2
2024-06-06 21:50:29 +00:00
2024-06-06 21:49:52 +00:00
def test_deduplicate_weight_2():
ls, fields = deduplicate_dicts(LIST_2 + LIST_2, DeduplicateMode.BY_ALL_COLUMNS, [])
assert fields == ['weight.sample_time', 'weight.kg']
assert ls == LIST_2