|
1 | | -#!/usr/bin/env python3 |
2 | | -""" |
3 | | -Test script to verify that both old and new config formats work with the new system. |
| 1 | +"""Tests for configuration backwards compatibility between old nested and new flat formats.""" |
4 | 2 |
|
5 | | -This demonstrates that the new system is fully backwards compatible. |
6 | | -""" |
| 3 | +import os |
| 4 | + |
| 5 | +import pytest |
7 | 6 |
|
8 | 7 | from config import load_config_from_json |
9 | 8 |
|
10 | | -def test_config_format(config_path, format_name): |
11 | | - """Test loading and using a specific config format.""" |
12 | | - print(f"\n{'='*60}") |
13 | | - print(f"🧪 Testing {format_name} Config Format") |
14 | | - print(f"📁 File: {config_path}") |
15 | | - print(f"{'='*60}") |
16 | | - |
17 | | - try: |
18 | | - # Load config |
19 | | - config = load_config_from_json(config_path) |
20 | | - print(f"✅ Config loaded successfully!") |
21 | | - |
22 | | - # Show basic parameters |
23 | | - print(f"\n📋 Basic Parameters:") |
24 | | - print(f" mzML file: {config.mzml_file}") |
25 | | - print(f" FASTA file: {config.fasta_file}") |
26 | | - print(f" Result dir: {config.result_dir}") |
27 | | - |
28 | | - # Show search parameter differences |
29 | | - print(f"\n🔬 Search Parameters:") |
30 | | - initial_config = config.get_initial_search_config() |
31 | | - full_config = config.get_full_search_config() |
32 | | - |
33 | | - print(f" Parameter Initial Search Full Search") |
34 | | - print(f" cleave_at {initial_config['database']['enzyme']['cleave_at']:15} {full_config['database']['enzyme']['cleave_at']}") |
35 | | - print(f" deisotope {str(initial_config['deisotope']):15} {str(full_config['deisotope'])}") |
36 | | - print(f" report_psms {str(initial_config['report_psms']):15} {str(full_config['report_psms'])}") |
37 | | - print(f" max_variable_mods {str(initial_config['database']['max_variable_mods']):15} {str(full_config['database']['max_variable_mods'])}") |
38 | | - |
39 | | - # Show MuMDIA settings |
40 | | - mumdia_config = config.get_mumdia_config() |
41 | | - print(f"\n📊 MuMDIA Settings:") |
42 | | - print(f" FDR initial search: {config.fdr_init_search}") |
43 | | - print(f" Read initial pickle: {mumdia_config['read_initial_search_pickle']}") |
44 | | - print(f" Write initial pickle: {mumdia_config['write_initial_search_pickle']}") |
45 | | - |
46 | | - print(f"\n✅ {format_name} format works perfectly!") |
47 | | - return True |
48 | | - |
49 | | - except Exception as e: |
50 | | - print(f"❌ Error testing {format_name} format: {e}") |
51 | | - return False |
52 | 9 |
|
53 | | -def main(): |
54 | | - print("🔧 MuMDIA Config Backwards Compatibility Test") |
55 | | - print("Testing both old (nested) and new (flat) config formats...") |
56 | | - |
57 | | - # Test old nested format |
58 | | - old_works = test_config_format("configs/config.json", "Legacy/Old Nested") |
59 | | - |
60 | | - # Test new flat format |
61 | | - new_works = test_config_format("configs/config_simple.json", "New Simplified Flat") |
62 | | - |
63 | | - print(f"\n{'='*60}") |
64 | | - print("🎯 Test Summary") |
65 | | - print(f"{'='*60}") |
66 | | - print(f"Legacy config (nested): {'✅ PASS' if old_works else '❌ FAIL'}") |
67 | | - print(f"New config (flat): {'✅ PASS' if new_works else '❌ FAIL'}") |
68 | | - print(f"Backwards compatibility: {'✅ MAINTAINED' if old_works and new_works else '❌ BROKEN'}") |
69 | | - |
70 | | - if old_works and new_works: |
71 | | - print(f"\n🎉 SUCCESS: Both config formats work!") |
72 | | - print(f" • Users can keep using their existing config.json files") |
73 | | - print(f" • Users can also switch to the new simplified format") |
74 | | - print(f" • The new system automatically detects and converts formats") |
75 | | - print(f"\nTo run MuMDIA:") |
76 | | - print(f" python run.py configs/config.json # Old format") |
77 | | - print(f" python run.py configs/config_simple.json # New format") |
78 | | - else: |
79 | | - print(f"\n❌ FAILURE: Config compatibility is broken!") |
| 10 | +@pytest.mark.unit |
| 11 | +class TestConfigCompatibility: |
| 12 | + """Verify that both old and new config formats load correctly.""" |
| 13 | + |
| 14 | + @pytest.fixture |
| 15 | + def legacy_config_path(self): |
| 16 | + path = "configs/config.json" |
| 17 | + if not os.path.exists(path): |
| 18 | + pytest.skip("configs/config.json not found") |
| 19 | + return path |
| 20 | + |
| 21 | + @pytest.fixture |
| 22 | + def flat_config_path(self): |
| 23 | + path = "configs/config_simple.json" |
| 24 | + if not os.path.exists(path): |
| 25 | + pytest.skip("configs/config_simple.json not found") |
| 26 | + return path |
| 27 | + |
| 28 | + def _check_config(self, config): |
| 29 | + """Common assertions for any loaded config.""" |
| 30 | + assert hasattr(config, "mzml_file") |
| 31 | + assert hasattr(config, "fasta_file") |
| 32 | + assert hasattr(config, "result_dir") |
| 33 | + |
| 34 | + initial = config.get_initial_search_config() |
| 35 | + full = config.get_full_search_config() |
| 36 | + |
| 37 | + assert "database" in initial |
| 38 | + assert "database" in full |
| 39 | + assert "enzyme" in initial["database"] |
| 40 | + assert "report_psms" in initial |
| 41 | + |
| 42 | + mumdia = config.get_mumdia_config() |
| 43 | + assert "read_initial_search_pickle" in mumdia |
| 44 | + assert "write_deeplc_pickle" in mumdia |
| 45 | + |
| 46 | + def test_legacy_nested_format(self, legacy_config_path): |
| 47 | + """Load the old nested (sage_basic / sage / mumdia) format.""" |
| 48 | + config = load_config_from_json(legacy_config_path) |
| 49 | + self._check_config(config) |
| 50 | + |
| 51 | + def test_flat_format(self, flat_config_path): |
| 52 | + """Load the new flat format with _initial_search / _full_search overrides.""" |
| 53 | + config = load_config_from_json(flat_config_path) |
| 54 | + self._check_config(config) |
| 55 | + |
| 56 | + def test_override_mechanism(self, flat_config_path): |
| 57 | + """Verify that search-stage overrides produce different configs.""" |
| 58 | + config = load_config_from_json(flat_config_path) |
| 59 | + initial = config.get_initial_search_config() |
| 60 | + full = config.get_full_search_config() |
80 | 61 |
|
81 | | -if __name__ == "__main__": |
82 | | - main() |
| 62 | + # At least one parameter should differ between stages |
| 63 | + differs = ( |
| 64 | + initial["report_psms"] != full["report_psms"] |
| 65 | + or initial["deisotope"] != full["deisotope"] |
| 66 | + or initial["database"]["enzyme"]["cleave_at"] |
| 67 | + != full["database"]["enzyme"]["cleave_at"] |
| 68 | + ) |
| 69 | + assert differs, "Initial and full search configs should have at least one difference" |
0 commit comments