Spaces:
Sleeping
Sleeping
Download tests/test_formulary.py from andychoo98/WQF7002-Assignment1: direct link, hf CLI and curl.
- Browser
- Download file 6.06 kB
-
https://huggingface.co/spaces/andychoo98/WQF7002-Assignment1/resolve/main/tests/test_formulary.py
- Command line
-
hf download hf://spaces/andychoo98/WQF7002-Assignment1/tests/test_formulary.py
-
curl -L -o test_formulary.py https://huggingface.co/spaces/andychoo98/WQF7002-Assignment1/resolve/main/tests/test_formulary.py
6.06 kB
| import pytest | |
| import tools | |
| from data_loader import DATA, Formulary, normalize, parse_dose, parse_fixed | |
| from validation import validate_call | |
| def test_workbook(): | |
| assert DATA.error is None | |
| assert len(DATA.ingredients) > 150 | |
| assert len(DATA.diagnoses) > 80 | |
| assert len([r for r in DATA.ingredients if r['source_row'] in (28, 29)]) == 1 | |
| assert all(r['category'] for r in DATA.diagnoses) | |
| assert DATA.diagnoses[1]['category'] == DATA.diagnoses[0]['category'] | |
| assert next(r for r in DATA.ingredients if r['name'] == 'Astaxanthin')['purity'] == '10%' | |
| def test_dose_parser(text, expected): | |
| assert parse_dose(text) == expected | |
| def test_fixed_parser(text, expected): | |
| assert parse_fixed(text) == expected | |
| def test_check_dose(ingredient, amount, unit, verdict, mg): | |
| result = tools.check_dose(ingredient, amount, unit) | |
| assert result['verdict'] == verdict | |
| assert result['amount_mg'] == mg | |
| def test_matching(): | |
| assert tools.lookup_ingredient('qzx nonexistent')['status'] == 'not_found' | |
| # Exact row names resolve directly and list the other brands of the same compound. | |
| result = tools.lookup_ingredient('resveratrol') | |
| assert result['status'] == 'ok' and result['name'] == 'Resveratrol' | |
| assert 'Resveratrol (Veri-te™)' in result['other_products'] | |
| # No exact row: both milk thistle products are named "Milk thistle extract ...". | |
| assert tools.lookup_ingredient('milk thistle')['status'] == 'ambiguous' | |
| assert tools.lookup_ingredient('Resveratrol (Veri-te™)')['status'] == 'ok' | |
| assert tools.lookup_ingredient('turmeric extract')['name'] == 'Tumeric extract' | |
| assert normalize(' Resveratrol (Veri-te™) ®≡ ') == 'resveratrol veri te' | |
| def test_sibo(): | |
| result = tools.find_ingredients_for_diagnosis('SIBO') | |
| assert result['status'] == 'ok' | |
| names = [r.get('name') for r in result['ingredients']] | |
| assert 'L-Glutamine' in names | |
| assert '11-strains probiotics blend' in names | |
| def test_order(): | |
| result = tools.check_order([{'ingredient': 'DIM', 'amount': 600, 'unit': 'mg'}, | |
| {'ingredient': 'Melatonin', 'amount': 2, 'unit': 'mg'}]) | |
| assert result['summary'] == {'above_max': 1, 'within_range': 1} | |
| def test_rejected(name, args): | |
| assert validate_call(name, args)[1] | |
| def test_numeric_strings(): | |
| args, error = validate_call('check_dose', {'ingredient': ' DIM ', 'amount': '500', 'unit': 'MG'}) | |
| assert error is None | |
| assert args == {'ingredient': 'DIM', 'amount': 500, 'unit': 'mg'} | |
| def test_missing_data(monkeypatch, tmp_path): | |
| missing = Formulary(tmp_path / 'missing.xlsx') | |
| monkeypatch.setattr(tools, 'DATA', missing) | |
| for name, args in [('lookup_ingredient', {'name': 'DIM'}), | |
| ('check_dose', {'ingredient': 'DIM', 'amount': 200, 'unit': 'mg'}), | |
| ('check_order', {'items': []}), | |
| ('find_ingredients_for_diagnosis', {'diagnosis': 'SIBO'}), | |
| ('suggest_alternatives', {'ingredient': 'DIM'})]: | |
| result = tools.TOOL_FUNCTIONS[name](**args) | |
| assert result['status'] == 'error' | |
| assert result['message'].startswith('Formulary data unavailable:') | |
| def test_alternatives(): | |
| result = tools.suggest_alternatives('L-Glutamine', 'IBS') | |
| assert result['status'] == 'ok' | |
| assert result['same_diagnosis'] | |
| assert all(r.get('name') != 'L-Glutamine' for g in result['same_diagnosis'] for r in g['ingredients']) | |
| result = tools.suggest_alternatives('Resveratrol (Veri-te™)') | |
| assert any(r['name'] == 'Resveratrol' for r in result['other_brands']) | |
| def test_range_and_fixed_rules(monkeypatch): | |
| sample = {'name': 'Example', 'normalized_name': 'example', 'purity': '', 'min_mg': 1, | |
| 'max_mg': 10, 'min_note': '', 'max_note': '', 'fixed_rule': {'type': 'exact', 'dose_mg': 4}, 'source_row': 1} | |
| monkeypatch.setattr(DATA, 'ingredients', [sample]) | |
| assert tools.check_dose('Example', 3, 'mg')['verdict'] == 'not_fixed_dose' | |
| assert tools.check_dose('Example', 4, 'mg')['verdict'] == 'within_range' | |
| sample['min_mg'] = None | |
| assert tools.check_dose('Example', 4, 'mg')['verdict'] == 'range_unavailable' | |
| def test_vitamin_k2_exact_row_wins(): | |
| result = tools.check_dose('vitamin K2', 5000, 'mg') | |
| assert result['status'] == 'ok' | |
| assert result['name'] == 'Vitamin K2' | |
| assert result['verdict'] == 'above_max' | |
| assert 'Vitamin K2 (MenaQ7® Matrix)' in result['other_products'] | |