|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Test suite for NeuronClassesFasciculatingHere query. |
| 4 | +
|
| 5 | +Tests the query that finds neuron classes that fasciculate with (run along) tracts or nerves. |
| 6 | +This implements the NeuronClassesFasciculatingHere query from the VFB XMI specification. |
| 7 | +
|
| 8 | +Test cases: |
| 9 | +1. Query execution with known tract |
| 10 | +2. Schema generation and validation |
| 11 | +3. Term info integration |
| 12 | +4. Preview results validation |
| 13 | +5. Cache functionality |
| 14 | +""" |
| 15 | + |
| 16 | +import unittest |
| 17 | +import sys |
| 18 | +import os |
| 19 | + |
| 20 | +# Add the src directory to the path |
| 21 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) |
| 22 | + |
| 23 | +from vfbquery.vfb_queries import ( |
| 24 | + get_neuron_classes_fasciculating_here, |
| 25 | + NeuronClassesFasciculatingHere_to_schema, |
| 26 | + get_term_info |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +class NeuronClassesFasciculatingTest(unittest.TestCase): |
| 31 | + """Test suite for NeuronClassesFasciculatingHere query""" |
| 32 | + |
| 33 | + def setUp(self): |
| 34 | + """Set up test fixtures""" |
| 35 | + # Example tract/nerve: broad root (FBbt_00003987) - a neuron projection bundle |
| 36 | + self.test_tract = "FBbt_00003987" # broad root |
| 37 | + |
| 38 | + def test_query_execution(self): |
| 39 | + """Test that the query executes successfully""" |
| 40 | + print(f"\n=== Testing NeuronClassesFasciculatingHere query execution ===") |
| 41 | + |
| 42 | + # Execute the query |
| 43 | + result = get_neuron_classes_fasciculating_here(self.test_tract, return_dataframe=False, limit=5) |
| 44 | + |
| 45 | + # Validate result structure |
| 46 | + self.assertIsNotNone(result, "Query should return a result") |
| 47 | + self.assertIsInstance(result, dict, "Result should be a dictionary") |
| 48 | + |
| 49 | + # Check for expected keys |
| 50 | + if result: |
| 51 | + print(f"Query returned {len(result.get('data', []))} results") |
| 52 | + |
| 53 | + # Validate data structure |
| 54 | + if 'data' in result and len(result['data']) > 0: |
| 55 | + first_result = result['data'][0] |
| 56 | + self.assertIn('id', first_result, "Result should contain 'id' field") |
| 57 | + self.assertIn('label', first_result, "Result should contain 'label' field") |
| 58 | + print(f"First result: {first_result.get('label', 'N/A')} ({first_result.get('id', 'N/A')})") |
| 59 | + |
| 60 | + def test_schema_generation(self): |
| 61 | + """Test schema function generates correct structure""" |
| 62 | + print(f"\n=== Testing NeuronClassesFasciculatingHere schema generation ===") |
| 63 | + |
| 64 | + test_name = "Test Tract" |
| 65 | + test_takes = {"short_form": self.test_tract} |
| 66 | + |
| 67 | + schema = NeuronClassesFasciculatingHere_to_schema(test_name, test_takes) |
| 68 | + |
| 69 | + # Validate schema structure |
| 70 | + self.assertIsNotNone(schema, "Schema should not be None") |
| 71 | + self.assertEqual(schema.query, "NeuronClassesFasciculatingHere", "Query name should match") |
| 72 | + self.assertEqual(schema.label, f"Neurons fasciculating in {test_name}", "Label should be formatted correctly") |
| 73 | + self.assertEqual(schema.function, "get_neuron_classes_fasciculating_here", "Function name should match") |
| 74 | + self.assertEqual(schema.preview, 10, "Preview should be 10") |
| 75 | + |
| 76 | + # Check preview columns |
| 77 | + expected_columns = ["id", "label", "tags", "thumbnail"] |
| 78 | + self.assertEqual(schema.preview_columns, expected_columns, f"Preview columns should be {expected_columns}") |
| 79 | + |
| 80 | + print(f"Schema generated successfully: {schema.label}") |
| 81 | + |
| 82 | + def test_term_info_integration(self): |
| 83 | + """Test that query appears in term info for appropriate terms""" |
| 84 | + print(f"\n=== Testing term info integration ===") |
| 85 | + |
| 86 | + # Get term info for a tract/nerve |
| 87 | + term_info = get_term_info(self.test_tract, preview=False) |
| 88 | + |
| 89 | + self.assertIsNotNone(term_info, "Term info should not be None") |
| 90 | + self.assertIn("Queries", term_info, "Term info should contain Queries") |
| 91 | + |
| 92 | + # Check if our query is present |
| 93 | + queries = term_info.get("Queries", []) |
| 94 | + query_names = [q.get('query') for q in queries] |
| 95 | + |
| 96 | + print(f"Available queries for {self.test_tract}: {query_names}") |
| 97 | + |
| 98 | + # For tracts/nerves (Neuron_projection_bundle), this query should be available |
| 99 | + if "Neuron_projection_bundle" in term_info.get("SuperTypes", []): |
| 100 | + self.assertIn("NeuronClassesFasciculatingHere", query_names, |
| 101 | + "NeuronClassesFasciculatingHere should be available for Neuron_projection_bundle") |
| 102 | + print("✓ Query correctly appears for Neuron_projection_bundle type") |
| 103 | + else: |
| 104 | + print(f"Warning: {self.test_tract} does not have Neuron_projection_bundle type") |
| 105 | + print(f"SuperTypes: {term_info.get('SuperTypes', [])}") |
| 106 | + |
| 107 | + def test_preview_results(self): |
| 108 | + """Test that preview results are properly formatted""" |
| 109 | + print(f"\n=== Testing preview results ===") |
| 110 | + |
| 111 | + # Get term info with preview enabled |
| 112 | + term_info = get_term_info(self.test_tract, preview=True) |
| 113 | + |
| 114 | + self.assertIsNotNone(term_info, "Term info should not be None") |
| 115 | + |
| 116 | + # Find our query in the results |
| 117 | + queries = term_info.get("Queries", []) |
| 118 | + fasciculating_query = None |
| 119 | + for q in queries: |
| 120 | + if q.get('query') == "NeuronClassesFasciculatingHere": |
| 121 | + fasciculating_query = q |
| 122 | + break |
| 123 | + |
| 124 | + if fasciculating_query: |
| 125 | + print(f"Found NeuronClassesFasciculatingHere query") |
| 126 | + |
| 127 | + # Check if preview_results exist |
| 128 | + if fasciculating_query.get('preview_results'): |
| 129 | + preview = fasciculating_query['preview_results'] |
| 130 | + data_key = 'data' if 'data' in preview else 'rows' |
| 131 | + print(f"Preview contains {len(preview.get(data_key, []))} results") |
| 132 | + |
| 133 | + # Validate preview structure |
| 134 | + self.assertIn(data_key, preview, f"Preview should contain '{data_key}' key") |
| 135 | + self.assertIn('headers', preview, "Preview should contain 'headers' key") |
| 136 | + |
| 137 | + # Check first result if available |
| 138 | + if preview.get(data_key) and len(preview[data_key]) > 0: |
| 139 | + first_result = preview[data_key][0] |
| 140 | + print(f"First preview result: {first_result.get('label', 'N/A')}") |
| 141 | + |
| 142 | + # Validate required fields |
| 143 | + self.assertIn('id', first_result, "Preview result should have 'id'") |
| 144 | + self.assertIn('label', first_result, "Preview result should have 'label'") |
| 145 | + else: |
| 146 | + print("No preview results available (this is OK if no matching neurons exist)") |
| 147 | + else: |
| 148 | + print("NeuronClassesFasciculatingHere query not found in term info") |
| 149 | + |
| 150 | + def test_with_different_tracts(self): |
| 151 | + """Test with multiple tract/nerve types""" |
| 152 | + print(f"\n=== Testing with different tracts/nerves ===") |
| 153 | + |
| 154 | + test_tracts = [ |
| 155 | + ("FBbt_00003987", "broad root"), |
| 156 | + ("FBbt_00007354", "adult antenno-subesophageal tract"), |
| 157 | + ("FBbt_00003985", "adult medial antennal lobe tract"), |
| 158 | + ] |
| 159 | + |
| 160 | + for tract_id, tract_name in test_tracts: |
| 161 | + print(f"\nTesting {tract_name} ({tract_id})...") |
| 162 | + |
| 163 | + try: |
| 164 | + result = get_neuron_classes_fasciculating_here(tract_id, return_dataframe=False, limit=3) |
| 165 | + |
| 166 | + if result and 'data' in result: |
| 167 | + print(f" ✓ Query successful, found {len(result['data'])} results") |
| 168 | + else: |
| 169 | + print(f" ✓ Query successful, no results found") |
| 170 | + |
| 171 | + except Exception as e: |
| 172 | + print(f" ✗ Query failed: {str(e)}") |
| 173 | + # Don't fail the test, just log the error |
| 174 | + # raise |
| 175 | + |
| 176 | + |
| 177 | +def run_tests(): |
| 178 | + """Run the test suite""" |
| 179 | + suite = unittest.TestLoader().loadTestsFromTestCase(NeuronClassesFasciculatingTest) |
| 180 | + runner = unittest.TextTestRunner(verbosity=2) |
| 181 | + result = runner.run(suite) |
| 182 | + return result.wasSuccessful() |
| 183 | + |
| 184 | + |
| 185 | +if __name__ == '__main__': |
| 186 | + success = run_tests() |
| 187 | + sys.exit(0 if success else 1) |
0 commit comments