|
| 1 | +import pytest |
| 2 | +import uuid |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +from labelbox.schema.ontology import Tool |
| 6 | +from labelbox.schema.tool_building.relationship_tool import RelationshipTool |
| 7 | +from labelbox.schema.tool_building.classification import Classification |
| 8 | + |
| 9 | + |
| 10 | +def test_basic_instantiation(): |
| 11 | + tool = RelationshipTool(name="Test Relationship Tool") |
| 12 | + |
| 13 | + assert tool.name == "Test Relationship Tool" |
| 14 | + assert tool.tool == Tool.Type.RELATIONSHIP |
| 15 | + assert tool.constraints is None |
| 16 | + assert tool.required is False |
| 17 | + assert tool.color is None |
| 18 | + assert tool.schema_id is None |
| 19 | + assert tool.feature_schema_id is None |
| 20 | + |
| 21 | + |
| 22 | +def test_instantiation_with_constraints(): |
| 23 | + constraints = [ |
| 24 | + ("source_id_1", "target_id_1"), |
| 25 | + ("source_id_2", "target_id_2") |
| 26 | + ] |
| 27 | + tool = RelationshipTool(name="Test Tool", constraints=constraints) |
| 28 | + |
| 29 | + assert tool.name == "Test Tool" |
| 30 | + assert tool.constraints == constraints |
| 31 | + assert len(tool.constraints) == 2 |
| 32 | + |
| 33 | +def test_post_init_sets_tool_type(): |
| 34 | + tool = RelationshipTool(name="Test Tool") |
| 35 | + assert tool.tool == Tool.Type.RELATIONSHIP |
| 36 | + |
| 37 | + |
| 38 | +def test_asdict_without_constraints(): |
| 39 | + tool = RelationshipTool( |
| 40 | + name="Test Tool", |
| 41 | + required=True, |
| 42 | + color="#FF0000" |
| 43 | + ) |
| 44 | + |
| 45 | + result = tool.asdict() |
| 46 | + expected = { |
| 47 | + "tool": "edge", |
| 48 | + "name": "Test Tool", |
| 49 | + "required": True, |
| 50 | + "color": "#FF0000", |
| 51 | + "classifications": [], |
| 52 | + "schemaNodeId": None, |
| 53 | + "featureSchemaId": None, |
| 54 | + "attributes": None |
| 55 | + } |
| 56 | + |
| 57 | + assert result == expected |
| 58 | + |
| 59 | +def test_asdict_with_constraints(): |
| 60 | + constraints = [("source_id", "target_id")] |
| 61 | + tool = RelationshipTool(name="Test Tool", constraints=constraints) |
| 62 | + |
| 63 | + result = tool.asdict() |
| 64 | + |
| 65 | + assert "definition" in result |
| 66 | + assert result["definition"] == {"constraints": constraints} |
| 67 | + assert result["tool"] == "edge" |
| 68 | + assert result["name"] == "Test Tool" |
| 69 | + |
| 70 | + |
| 71 | +def test_add_constraint_to_empty_constraints(): |
| 72 | + tool = RelationshipTool(name="Test Tool") |
| 73 | + start_tool = Tool(Tool.Type.BBOX, "Start Tool") |
| 74 | + end_tool = Tool(Tool.Type.POLYGON, "End Tool") |
| 75 | + |
| 76 | + with patch('uuid.uuid4') as mock_uuid: |
| 77 | + mock_uuid.return_value.hex = "test-uuid" |
| 78 | + tool.add_constraint(start_tool, end_tool) |
| 79 | + |
| 80 | + assert tool.constraints is not None |
| 81 | + assert len(tool.constraints) == 1 |
| 82 | + assert start_tool.feature_schema_id is not None |
| 83 | + assert start_tool.schema_id is not None |
| 84 | + assert end_tool.feature_schema_id is not None |
| 85 | + assert end_tool.schema_id is not None |
| 86 | + |
| 87 | + |
| 88 | +def test_add_constraint_to_existing_constraints(): |
| 89 | + existing_constraints = [("existing_source", "existing_target")] |
| 90 | + tool = RelationshipTool(name="Test Tool", constraints=existing_constraints) |
| 91 | + |
| 92 | + start_tool = Tool(Tool.Type.BBOX, "Start Tool") |
| 93 | + end_tool = Tool(Tool.Type.POLYGON, "End Tool") |
| 94 | + |
| 95 | + tool.add_constraint(start_tool, end_tool) |
| 96 | + |
| 97 | + assert len(tool.constraints) == 2 |
| 98 | + assert tool.constraints[0] == ("existing_source", "existing_target") |
| 99 | + assert tool.constraints[1] == (start_tool.feature_schema_id, end_tool.feature_schema_id) |
| 100 | + |
| 101 | + |
| 102 | +def test_add_constraint_preserves_existing_ids(): |
| 103 | + tool = RelationshipTool(name="Test Tool") |
| 104 | + start_tool_feature_schema_id = "start_tool_feature_schema_id" |
| 105 | + start_tool_schema_id = "start_tool_schema_id" |
| 106 | + start_tool = Tool(Tool.Type.BBOX, "Start Tool", feature_schema_id=start_tool_feature_schema_id, schema_id=start_tool_schema_id) |
| 107 | + end_tool_feature_schema_id = "end_tool_feature_schema_id" |
| 108 | + end_tool_schema_id = "end_tool_schema_id" |
| 109 | + end_tool = Tool(Tool.Type.POLYGON, "End Tool", feature_schema_id=end_tool_feature_schema_id, schema_id=end_tool_schema_id) |
| 110 | + |
| 111 | + tool.add_constraint(start_tool, end_tool) |
| 112 | + |
| 113 | + assert start_tool.feature_schema_id == start_tool_feature_schema_id |
| 114 | + assert start_tool.schema_id == start_tool_schema_id |
| 115 | + assert end_tool.feature_schema_id == end_tool_feature_schema_id |
| 116 | + assert end_tool.schema_id == end_tool_schema_id |
| 117 | + assert tool.constraints == [(start_tool_feature_schema_id, end_tool_feature_schema_id)] |
| 118 | + |
| 119 | + |
| 120 | +def test_set_constraints(): |
| 121 | + tool = RelationshipTool(name="Test Tool") |
| 122 | + |
| 123 | + start_tool1 = Tool(Tool.Type.BBOX, "Start Tool 1") |
| 124 | + end_tool1 = Tool(Tool.Type.POLYGON, "End Tool 1") |
| 125 | + start_tool2 = Tool(Tool.Type.POINT, "Start Tool 2") |
| 126 | + end_tool2 = Tool(Tool.Type.LINE, "End Tool 2") |
| 127 | + |
| 128 | + tool.set_constraints([ |
| 129 | + (start_tool1, end_tool1), |
| 130 | + (start_tool2, end_tool2) |
| 131 | + ]) |
| 132 | + |
| 133 | + assert len(tool.constraints) == 2 |
| 134 | + assert tool.constraints[0] == (start_tool1.feature_schema_id, end_tool1.feature_schema_id) |
| 135 | + assert tool.constraints[1] == (start_tool2.feature_schema_id, end_tool2.feature_schema_id) |
| 136 | + |
| 137 | + |
| 138 | +def test_set_constraints_replaces_existing(): |
| 139 | + existing_constraints = [("old_source", "old_target")] |
| 140 | + tool = RelationshipTool(name="Test Tool", constraints=existing_constraints) |
| 141 | + |
| 142 | + start_tool = Tool(Tool.Type.BBOX, "Start Tool") |
| 143 | + end_tool = Tool(Tool.Type.POLYGON, "End Tool") |
| 144 | + |
| 145 | + tool.set_constraints([(start_tool, end_tool)]) |
| 146 | + |
| 147 | + assert len(tool.constraints) == 1 |
| 148 | + assert tool.constraints[0] != ("old_source", "old_target") |
| 149 | + assert tool.constraints[0] == (start_tool.feature_schema_id, end_tool.feature_schema_id) |
| 150 | + |
| 151 | + |
| 152 | +def test_uuid_generation_in_add_constraint(): |
| 153 | + tool = RelationshipTool(name="Test Tool") |
| 154 | + |
| 155 | + start_tool = Tool(Tool.Type.BBOX, "Start Tool") |
| 156 | + end_tool = Tool(Tool.Type.POLYGON, "End Tool") |
| 157 | + |
| 158 | + # Ensure tools don't have IDs initially |
| 159 | + assert start_tool.feature_schema_id is None |
| 160 | + assert start_tool.schema_id is None |
| 161 | + assert end_tool.feature_schema_id is None |
| 162 | + assert end_tool.schema_id is None |
| 163 | + |
| 164 | + tool.add_constraint(start_tool, end_tool) |
| 165 | + |
| 166 | + # Check that UUIDs were generated |
| 167 | + assert start_tool.feature_schema_id is not None |
| 168 | + assert start_tool.schema_id is not None |
| 169 | + assert end_tool.feature_schema_id is not None |
| 170 | + assert end_tool.schema_id is not None |
| 171 | + |
| 172 | + # Check that they are valid UUID strings |
| 173 | + uuid.UUID(start_tool.feature_schema_id) # Will raise ValueError if invalid |
| 174 | + uuid.UUID(start_tool.schema_id) |
| 175 | + uuid.UUID(end_tool.feature_schema_id) |
| 176 | + uuid.UUID(end_tool.schema_id) |
| 177 | + |
| 178 | + |
| 179 | +def test_constraints_in_asdict(): |
| 180 | + tool = RelationshipTool(name="Test Tool") |
| 181 | + |
| 182 | + start_tool = Tool(Tool.Type.BBOX, "Start Tool") |
| 183 | + end_tool = Tool(Tool.Type.POLYGON, "End Tool") |
| 184 | + |
| 185 | + tool.add_constraint(start_tool, end_tool) |
| 186 | + |
| 187 | + result = tool.asdict() |
| 188 | + |
| 189 | + assert "definition" in result |
| 190 | + assert "constraints" in result["definition"] |
| 191 | + assert len(result["definition"]["constraints"]) == 1 |
| 192 | + assert result["definition"]["constraints"][0] == ( |
| 193 | + start_tool.feature_schema_id, |
| 194 | + end_tool.feature_schema_id |
| 195 | + ) |
0 commit comments