|
| 1 | +"""Tests for get_hierarchy function. |
| 2 | +
|
| 3 | +Tests the hierarchy tree builder for both part_of (brain region structure) |
| 4 | +and subclass_of (cell type hierarchies), in both ancestor and descendant |
| 5 | +directions. |
| 6 | +""" |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from vfbquery.vfb_queries import get_hierarchy |
| 11 | + |
| 12 | + |
| 13 | +# Known test terms |
| 14 | +MUSHROOM_BODY = "FBbt_00005801" |
| 15 | +KENYON_CELL = "FBbt_00003686" |
| 16 | + |
| 17 | + |
| 18 | +class TestHierarchyValidation: |
| 19 | + def test_invalid_relationship_raises(self): |
| 20 | + with pytest.raises(ValueError, match="relationship"): |
| 21 | + get_hierarchy(KENYON_CELL, relationship="invalid") |
| 22 | + |
| 23 | + def test_invalid_direction_raises(self): |
| 24 | + with pytest.raises(ValueError, match="direction"): |
| 25 | + get_hierarchy(KENYON_CELL, direction="invalid") |
| 26 | + |
| 27 | + |
| 28 | +class TestSubclassOfDescendants: |
| 29 | + @pytest.mark.integration |
| 30 | + def test_returns_descendants(self): |
| 31 | + result = get_hierarchy(KENYON_CELL, 'subclass_of', 'descendants', max_depth=1) |
| 32 | + assert result['id'] == KENYON_CELL |
| 33 | + assert result['label'] == 'Kenyon cell' |
| 34 | + assert result['relationship'] == 'subclass_of' |
| 35 | + assert 'descendants' in result |
| 36 | + assert len(result['descendants']) > 0 |
| 37 | + |
| 38 | + @pytest.mark.integration |
| 39 | + def test_descendants_have_id_and_label(self): |
| 40 | + result = get_hierarchy(KENYON_CELL, 'subclass_of', 'descendants', max_depth=1) |
| 41 | + for child in result['descendants']: |
| 42 | + assert 'id' in child |
| 43 | + assert 'label' in child |
| 44 | + assert child['id'].startswith('FBbt_') |
| 45 | + assert child['label'] != child['id'] # label should be resolved |
| 46 | + |
| 47 | + @pytest.mark.integration |
| 48 | + def test_depth_1_has_no_grandchildren(self): |
| 49 | + result = get_hierarchy(KENYON_CELL, 'subclass_of', 'descendants', max_depth=1) |
| 50 | + for child in result['descendants']: |
| 51 | + assert 'descendants' not in child |
| 52 | + |
| 53 | + @pytest.mark.integration |
| 54 | + def test_depth_2_has_nested_children(self): |
| 55 | + result = get_hierarchy(KENYON_CELL, 'subclass_of', 'descendants', max_depth=2) |
| 56 | + has_grandchildren = any('descendants' in child for child in result['descendants']) |
| 57 | + assert has_grandchildren, "At least one direct subclass should have its own subclasses" |
| 58 | + |
| 59 | + |
| 60 | +class TestSubclassOfAncestors: |
| 61 | + @pytest.mark.integration |
| 62 | + def test_returns_ancestors(self): |
| 63 | + result = get_hierarchy(KENYON_CELL, 'subclass_of', 'ancestors', max_depth=1) |
| 64 | + assert 'ancestors' in result |
| 65 | + assert len(result['ancestors']) > 0 |
| 66 | + |
| 67 | + @pytest.mark.integration |
| 68 | + def test_ancestors_have_id_and_label(self): |
| 69 | + result = get_hierarchy(KENYON_CELL, 'subclass_of', 'ancestors', max_depth=1) |
| 70 | + for anc in result['ancestors']: |
| 71 | + assert 'id' in anc |
| 72 | + assert 'label' in anc |
| 73 | + |
| 74 | + @pytest.mark.integration |
| 75 | + def test_kenyon_cell_ancestor_is_mb_intrinsic_neuron(self): |
| 76 | + result = get_hierarchy(KENYON_CELL, 'subclass_of', 'ancestors', max_depth=1) |
| 77 | + ancestor_ids = [a['id'] for a in result['ancestors']] |
| 78 | + assert 'FBbt_00007484' in ancestor_ids # mushroom body intrinsic neuron |
| 79 | + |
| 80 | + @pytest.mark.integration |
| 81 | + def test_depth_2_has_nested_ancestors(self): |
| 82 | + result = get_hierarchy(KENYON_CELL, 'subclass_of', 'ancestors', max_depth=2) |
| 83 | + has_grandparent = any('ancestors' in anc for anc in result['ancestors']) |
| 84 | + assert has_grandparent |
| 85 | + |
| 86 | + |
| 87 | +class TestPartOfDescendants: |
| 88 | + @pytest.mark.integration |
| 89 | + def test_returns_parts(self): |
| 90 | + result = get_hierarchy(MUSHROOM_BODY, 'part_of', 'descendants', max_depth=1) |
| 91 | + assert result['id'] == MUSHROOM_BODY |
| 92 | + assert result['label'] == 'mushroom body' |
| 93 | + assert 'descendants' in result |
| 94 | + assert len(result['descendants']) > 0 |
| 95 | + |
| 96 | + @pytest.mark.integration |
| 97 | + def test_parts_have_id_and_label(self): |
| 98 | + result = get_hierarchy(MUSHROOM_BODY, 'part_of', 'descendants', max_depth=1) |
| 99 | + for part in result['descendants']: |
| 100 | + assert 'id' in part |
| 101 | + assert 'label' in part |
| 102 | + assert part['id'].startswith('FBbt_') |
| 103 | + |
| 104 | + |
| 105 | +class TestPartOfAncestors: |
| 106 | + @pytest.mark.integration |
| 107 | + def test_mushroom_body_part_of_protocerebrum(self): |
| 108 | + result = get_hierarchy(MUSHROOM_BODY, 'part_of', 'ancestors', max_depth=1) |
| 109 | + assert 'ancestors' in result |
| 110 | + ancestor_ids = [a['id'] for a in result['ancestors']] |
| 111 | + assert 'FBbt_00003627' in ancestor_ids # protocerebrum |
| 112 | + |
| 113 | + |
| 114 | +class TestDisplayOutput: |
| 115 | + @pytest.mark.integration |
| 116 | + def test_display_field_present(self): |
| 117 | + result = get_hierarchy(KENYON_CELL, 'subclass_of', 'both', max_depth=1) |
| 118 | + assert 'display' in result |
| 119 | + assert isinstance(result['display'], str) |
| 120 | + assert 'Kenyon cell' in result['display'] |
| 121 | + |
| 122 | + @pytest.mark.integration |
| 123 | + def test_display_shows_ancestors(self): |
| 124 | + result = get_hierarchy(KENYON_CELL, 'subclass_of', 'both', max_depth=1) |
| 125 | + assert 'ancestors' in result['display'].lower() |
| 126 | + assert 'mushroom body intrinsic neuron' in result['display'] |
| 127 | + |
| 128 | + @pytest.mark.integration |
| 129 | + def test_display_shows_tree_connectors(self): |
| 130 | + result = get_hierarchy(KENYON_CELL, 'subclass_of', 'descendants', max_depth=1) |
| 131 | + assert '├──' in result['display'] or '└──' in result['display'] |
| 132 | + |
| 133 | + @pytest.mark.integration |
| 134 | + def test_html_field_present(self): |
| 135 | + result = get_hierarchy(KENYON_CELL, 'subclass_of', 'both', max_depth=1) |
| 136 | + assert 'html' in result |
| 137 | + assert '<!DOCTYPE html>' in result['html'] |
| 138 | + assert 'Kenyon cell' in result['html'] |
| 139 | + |
| 140 | + @pytest.mark.integration |
| 141 | + def test_html_contains_vfb_links(self): |
| 142 | + result = get_hierarchy(KENYON_CELL, 'subclass_of', 'descendants', max_depth=1) |
| 143 | + assert 'virtualflybrain.org' in result['html'] |
| 144 | + assert KENYON_CELL in result['html'] |
| 145 | + |
| 146 | + |
| 147 | +class TestBothDirections: |
| 148 | + @pytest.mark.integration |
| 149 | + def test_both_returns_ancestors_and_descendants(self): |
| 150 | + result = get_hierarchy(KENYON_CELL, 'subclass_of', 'both', max_depth=1) |
| 151 | + assert 'ancestors' in result |
| 152 | + assert 'descendants' in result |
| 153 | + assert len(result['ancestors']) > 0 |
| 154 | + assert len(result['descendants']) > 0 |
| 155 | + |
| 156 | + @pytest.mark.integration |
| 157 | + def test_descendants_only_has_no_ancestors(self): |
| 158 | + result = get_hierarchy(KENYON_CELL, 'subclass_of', 'descendants', max_depth=1) |
| 159 | + assert 'descendants' in result |
| 160 | + assert 'ancestors' not in result |
| 161 | + |
| 162 | + @pytest.mark.integration |
| 163 | + def test_ancestors_only_has_no_descendants(self): |
| 164 | + result = get_hierarchy(KENYON_CELL, 'subclass_of', 'ancestors', max_depth=1) |
| 165 | + assert 'ancestors' in result |
| 166 | + assert 'descendants' not in result |
0 commit comments