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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
PhD Research OS — Taxonomy Tests
|
| 3 |
+
==================================
|
| 4 |
+
Tests for the Quantum-Bio V2 taxonomy, domain management, and confidence scoring.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
import sys
|
| 9 |
+
import json
|
| 10 |
+
import pytest
|
| 11 |
+
|
| 12 |
+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 13 |
+
|
| 14 |
+
from phd_research_os.taxonomy import (
|
| 15 |
+
TaxonomyManager, STUDY_TYPE_WEIGHTS, ALLOWED_STUDY_TYPES,
|
| 16 |
+
TAXONOMY_VERSION, PIPELINE_VERSION, LEGACY_TO_V2_MAP
|
| 17 |
+
)
|
| 18 |
+
from phd_research_os.db import init_db, get_db, create_claim, create_source, to_fixed, from_fixed
|
| 19 |
+
|
| 20 |
+
TEST_DB = "test_taxonomy.db"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
@pytest.fixture(autouse=True)
|
| 24 |
+
def setup_teardown():
|
| 25 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 26 |
+
# Seed test data
|
| 27 |
+
conn = get_db(TEST_DB)
|
| 28 |
+
create_claim(conn, "Test claim", "Fact", 0.85,
|
| 29 |
+
study_type="primary_experimental", evidence_strength=0.9)
|
| 30 |
+
create_source(conn, "10.1234/test", "Test Paper", study_type="Simulation")
|
| 31 |
+
conn.close()
|
| 32 |
+
yield
|
| 33 |
+
for suffix in ["", "-wal", "-shm"]:
|
| 34 |
+
p = TEST_DB + suffix
|
| 35 |
+
if os.path.exists(p):
|
| 36 |
+
os.remove(p)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# ============================================================
|
| 40 |
+
# Base Taxonomy Tests
|
| 41 |
+
# ============================================================
|
| 42 |
+
|
| 43 |
+
def test_8_study_types_defined():
|
| 44 |
+
assert len(ALLOWED_STUDY_TYPES) == 8
|
| 45 |
+
|
| 46 |
+
def test_weights_are_fixed_point():
|
| 47 |
+
for st, weight in STUDY_TYPE_WEIGHTS.items():
|
| 48 |
+
assert isinstance(weight, int), f"{st} weight should be int (fixed-point)"
|
| 49 |
+
assert 0 <= weight <= 1000, f"{st} weight {weight} out of range"
|
| 50 |
+
|
| 51 |
+
def test_in_vivo_highest():
|
| 52 |
+
assert STUDY_TYPE_WEIGHTS["in_vivo"] == 1000
|
| 53 |
+
|
| 54 |
+
def test_perspective_lowest():
|
| 55 |
+
assert STUDY_TYPE_WEIGHTS["perspective"] == 200
|
| 56 |
+
|
| 57 |
+
def test_simulation_split():
|
| 58 |
+
"""First-principles > phenomenological."""
|
| 59 |
+
assert STUDY_TYPE_WEIGHTS["first_principles_simulation"] > STUDY_TYPE_WEIGHTS["phenomenological_simulation"]
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# ============================================================
|
| 63 |
+
# Normalization Tests
|
| 64 |
+
# ============================================================
|
| 65 |
+
|
| 66 |
+
def test_normalize_legacy_types():
|
| 67 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 68 |
+
assert tm.normalize_study_type("PrimaryExperimental") == "direct_physical_measurement"
|
| 69 |
+
assert tm.normalize_study_type("InVitro") == "in_vitro"
|
| 70 |
+
assert tm.normalize_study_type("Simulation") == "phenomenological_simulation"
|
| 71 |
+
assert tm.normalize_study_type("Review") == "review"
|
| 72 |
+
|
| 73 |
+
def test_normalize_aliases():
|
| 74 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 75 |
+
assert tm.normalize_study_type("clinical_trial") == "in_vivo"
|
| 76 |
+
assert tm.normalize_study_type("meta-analysis") == "review"
|
| 77 |
+
assert tm.normalize_study_type("case_study") == "perspective"
|
| 78 |
+
|
| 79 |
+
def test_normalize_v2_identity():
|
| 80 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 81 |
+
for st in ALLOWED_STUDY_TYPES:
|
| 82 |
+
assert tm.normalize_study_type(st) == st
|
| 83 |
+
|
| 84 |
+
def test_normalize_case_insensitive():
|
| 85 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 86 |
+
assert tm.normalize_study_type("IN_VIVO") == "in_vivo"
|
| 87 |
+
assert tm.normalize_study_type("Mathematical_Proof") == "mathematical_proof"
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
# ============================================================
|
| 91 |
+
# Confidence Scoring Tests
|
| 92 |
+
# ============================================================
|
| 93 |
+
|
| 94 |
+
def test_confidence_max():
|
| 95 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 96 |
+
result = tm.score_confidence(1.0, "in_vivo", 1, True)
|
| 97 |
+
assert result["confidence"] == 1.0
|
| 98 |
+
|
| 99 |
+
def test_confidence_formula():
|
| 100 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 101 |
+
result = tm.score_confidence(0.8, "in_vitro", 2, True)
|
| 102 |
+
# 0.8 × 0.85 × 0.85 × 1.0 = 0.578
|
| 103 |
+
assert 0.57 <= result["confidence"] <= 0.58
|
| 104 |
+
|
| 105 |
+
def test_confidence_incomplete_penalty():
|
| 106 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 107 |
+
complete = tm.score_confidence(0.9, "in_vivo", 1, True)
|
| 108 |
+
incomplete = tm.score_confidence(0.9, "in_vivo", 1, False)
|
| 109 |
+
assert incomplete["confidence"] < complete["confidence"]
|
| 110 |
+
assert incomplete["completeness_penalty"] == 0.7
|
| 111 |
+
|
| 112 |
+
def test_confidence_taxonomy_version_tag():
|
| 113 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 114 |
+
result = tm.score_confidence(0.5, "review", 3, True)
|
| 115 |
+
assert result["taxonomy_version"] == TAXONOMY_VERSION
|
| 116 |
+
|
| 117 |
+
def test_confidence_perspective_low():
|
| 118 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 119 |
+
result = tm.score_confidence(1.0, "perspective", 1, True)
|
| 120 |
+
assert result["confidence"] == 0.2 # 1.0 × 0.2 × 1.0 × 1.0
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
# ============================================================
|
| 124 |
+
# Domain Management Tests
|
| 125 |
+
# ============================================================
|
| 126 |
+
|
| 127 |
+
def test_default_domains_seeded():
|
| 128 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 129 |
+
domains = tm.list_domains()
|
| 130 |
+
domain_ids = [d["domain_id"] for d in domains]
|
| 131 |
+
assert "quantum_bio" in domain_ids
|
| 132 |
+
assert "biosensors" in domain_ids
|
| 133 |
+
assert "materials_science" in domain_ids
|
| 134 |
+
|
| 135 |
+
def test_create_custom_domain():
|
| 136 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 137 |
+
tm.create_domain("my_field", "My Research Field", "Custom taxonomy")
|
| 138 |
+
domain = tm.get_domain("my_field")
|
| 139 |
+
assert domain is not None
|
| 140 |
+
assert domain["name"] == "My Research Field"
|
| 141 |
+
|
| 142 |
+
def test_add_custom_study_type():
|
| 143 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 144 |
+
tm.create_domain("test_domain", "Test", "Test domain")
|
| 145 |
+
tm.add_study_type("test_domain", "custom_assay", 0.75, "A custom assay type")
|
| 146 |
+
|
| 147 |
+
domain = tm.get_domain("test_domain")
|
| 148 |
+
assert "custom_assay" in domain["custom_study_types"]
|
| 149 |
+
assert domain["custom_study_types"]["custom_assay"]["weight"] == 750
|
| 150 |
+
|
| 151 |
+
def test_custom_type_affects_scoring():
|
| 152 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 153 |
+
tm.create_domain("test_scoring", "Test", "Test")
|
| 154 |
+
tm.add_study_type("test_scoring", "ultra_precise", 0.99, "Ultra-precise measurement")
|
| 155 |
+
|
| 156 |
+
result = tm.score_confidence(1.0, "ultra_precise", 1, True, domain_id="test_scoring")
|
| 157 |
+
assert 0.98 <= result["confidence"] <= 1.0
|
| 158 |
+
|
| 159 |
+
def test_remove_study_type():
|
| 160 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 161 |
+
tm.create_domain("rm_test", "Test", "Test")
|
| 162 |
+
tm.add_study_type("rm_test", "temp_type", 0.5, "Temporary")
|
| 163 |
+
assert tm.remove_study_type("rm_test", "temp_type")
|
| 164 |
+
|
| 165 |
+
domain = tm.get_domain("rm_test")
|
| 166 |
+
assert "temp_type" not in domain["custom_study_types"]
|
| 167 |
+
|
| 168 |
+
def test_cannot_delete_base_taxonomy():
|
| 169 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 170 |
+
assert not tm.delete_domain("quantum_bio")
|
| 171 |
+
|
| 172 |
+
def test_soft_delete_domain():
|
| 173 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 174 |
+
tm.create_domain("deletable", "Deletable", "Will be deleted")
|
| 175 |
+
assert tm.delete_domain("deletable")
|
| 176 |
+
|
| 177 |
+
active = tm.list_domains(active_only=True)
|
| 178 |
+
assert not any(d["domain_id"] == "deletable" for d in active)
|
| 179 |
+
|
| 180 |
+
all_domains = tm.list_domains(active_only=False)
|
| 181 |
+
assert any(d["domain_id"] == "deletable" for d in all_domains)
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
# ============================================================
|
| 185 |
+
# Migration Tests
|
| 186 |
+
# ============================================================
|
| 187 |
+
|
| 188 |
+
def test_migration_idempotent():
|
| 189 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 190 |
+
result1 = tm.migrate_to_v2()
|
| 191 |
+
result2 = tm.migrate_to_v2()
|
| 192 |
+
assert result2["already_migrated"]
|
| 193 |
+
|
| 194 |
+
def test_migration_normalizes_types():
|
| 195 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 196 |
+
result = tm.migrate_to_v2()
|
| 197 |
+
|
| 198 |
+
conn = get_db(TEST_DB)
|
| 199 |
+
claims = conn.execute("SELECT study_type FROM claims WHERE study_type IS NOT NULL").fetchall()
|
| 200 |
+
conn.close()
|
| 201 |
+
|
| 202 |
+
for claim in claims:
|
| 203 |
+
st = dict(claim).get("study_type", "")
|
| 204 |
+
if st:
|
| 205 |
+
assert st in ALLOWED_STUDY_TYPES or st in ["primary_experimental"], f"Unexpected type: {st}"
|
| 206 |
+
|
| 207 |
+
def test_rollback():
|
| 208 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 209 |
+
tm.migrate_to_v2()
|
| 210 |
+
result = tm.rollback_to_v1()
|
| 211 |
+
assert result["rows_reverted"] >= 0
|
| 212 |
+
assert not result["errors"]
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
# ============================================================
|
| 216 |
+
# Audit Log Tests
|
| 217 |
+
# ============================================================
|
| 218 |
+
|
| 219 |
+
def test_audit_log_records_actions():
|
| 220 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 221 |
+
tm.create_domain("audit_test", "Audit Test", "Testing audit")
|
| 222 |
+
|
| 223 |
+
log = tm.get_audit_log()
|
| 224 |
+
assert len(log) >= 1
|
| 225 |
+
actions = [e["action"] for e in log]
|
| 226 |
+
assert "create_domain" in actions
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
# ============================================================
|
| 230 |
+
# Cache Tests
|
| 231 |
+
# ============================================================
|
| 232 |
+
|
| 233 |
+
def test_cache_key_versioned():
|
| 234 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 235 |
+
key1 = tm.generate_cache_key("abc123")
|
| 236 |
+
key2 = tm.generate_cache_key("abc123")
|
| 237 |
+
assert key1 == key2 # Deterministic
|
| 238 |
+
|
| 239 |
+
key3 = tm.generate_cache_key("different_hash")
|
| 240 |
+
assert key1 != key3
|
| 241 |
+
|
| 242 |
+
def test_cache_validation():
|
| 243 |
+
tm = TaxonomyManager(db_path=TEST_DB)
|
| 244 |
+
valid_entry = {"taxonomy_version": TAXONOMY_VERSION, "pipeline_version": PIPELINE_VERSION}
|
| 245 |
+
assert tm.validate_cache_entry(valid_entry)
|
| 246 |
+
|
| 247 |
+
stale_entry = {"taxonomy_version": "old_v1", "pipeline_version": "1.0.0"}
|
| 248 |
+
assert not tm.validate_cache_entry(stale_entry)
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
if __name__ == "__main__":
|
| 252 |
+
pytest.main([__file__, "-v"])
|