"""Blood test spreadsheet: typed-in values are read, formula cells skipped, and edits to the sheet show at the next read.""" from datetime import datetime import openpyxl from mygarmin.library import Library from mygarmin.sources import import_path, labs def sheet(path, extra_row=True): wb = openpyxl.Workbook(); ws = wb.active ws.append(['Notes']) ws.append(['Date', 'Min', 'Max', 'Ferritin [ug/L] LabOne (30-400)', 'Lab LabTwo (20-250 ug/L)', 'Haemoglobin']) ws.append([datetime(2024, 1, 10), 50, 100, 300, None, 14.5]) ws.append([datetime(2024, 1, 10), 50, 100, '=D3*0.9', None, None]) # an estimate: must be skipped ws.append([datetime(2024, 2, 10), 50, 100, None, 180, None]) if extra_row: ws.append([datetime(2024, 3, 10), 50, 100, 120, None, None]) wb.save(path) def test_lab_sheet(tmp_path): lib = Library.create(str(tmp_path / 'lib'), name='Test') p = str(tmp_path / 'results.xlsx'); sheet(p) st = import_path(lib, p, log=lambda *a: None) assert st['blood test results'] == 4 rows = lib.series('lab') fer = [r for r in rows if r['m'] == 'ferritin'] assert [(r['d'], r['v'], r['lab']) for r in fer] == [('2024-01-10', 300.0, 'LabOne'), ('2024-02-10', 180.0, 'LabTwo'), ('2024-03-10', 120.0, 'LabOne')] assert fer[0]['ref'] == [30.0, 400.0] and fer[0]['target'] == [50.0, 100.0] and fer[1]['ref'] == [20.0, 250.0] assert [r['v'] for r in rows if r['m'] == 'hb'] == [14.5] assert Library(lib.root).settings['lab_files'] == [p] sheet(p, extra_row=False) # a row deleted in the sheet goes at the next read labs.refresh(Library(lib.root), log=lambda *a: None) assert len([r for r in lib.series('lab') if r['m'] == 'ferritin']) == 2