Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nik committed Oct 29, 2024
1 parent c0a3755 commit dc0f88f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/label_studio_sdk/label_interface/control_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def get_labels(self, regions: List[Dict]):
labels.append(v[0] if len(v) == 1 else v)
else:
labels.append(value)
return labels
return labels[0] if len(labels) == 1 else labels

def as_tuple(self):
""" """
Expand Down
53 changes: 52 additions & 1 deletion tests/custom/test_interface/test_control_tags.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import json
import pytest
from lxml.etree import Element

from label_studio_sdk.label_interface import LabelInterface
from label_studio_sdk.label_interface.control_tags import ControlTag
from label_studio_sdk.label_interface.control_tags import ControlTag, ChoicesTag, LabelsTag, RectangleLabelsTag

from . import configs as c

Expand Down Expand Up @@ -53,3 +54,53 @@ def test_label_with_choices():

assert "choices" in rpy.get("value")
assert c.LABEL1 in rpy["value"]["choices"]


@pytest.mark.parametrize("tag, regions, expected", [
# Test case 1: ChoicesTag with single choice
(
ChoicesTag(name="choices", to_name=["text"], tag="Choices"),
[{"from_name": "choices", "value": {"choices": ["positive"]}}],
"positive"
),
# Test case 2: ChoicesTag with multiple choices
(
ChoicesTag(name="choices", to_name=["text"], tag="Choices"),
[{"from_name": "choices", "value": {"choices": ["positive", "negative"]}}],
["positive", "negative"]
),
# Test case 3: Multiple regions with labels
(
LabelsTag(name="label", to_name=["text"], tag="Labels"),
[
{"from_name": "label", "to_name": "text", "value": {"labels": ["positive"], "start": 0, "end": 1}},
{"from_name": "label", "to_name": "text", "value": {"labels": ["negative"], "start": 2, "end": 3}}
],
[{"start": 0, "end": 1, "labels": ["positive"]}, {"start": 2, "end": 3, "labels": ["negative"]}]
),
# Test case 4: Empty regions
(
ChoicesTag(name="choices", to_name=["text"], tag="Choices"),
[],
[]
),
# Test case 5: Regions with different from_name
(
ChoicesTag(name="choices", to_name=["text"], tag="Choices"),
[{"from_name": "other_tag", "value": {"choices": ["positive"]}}],
[]
),
# Test case 6: Tag without label_attr_name
(
ControlTag(name="base", to_name=["text"], tag="BaseTag"),
[{"from_name": "base", "value": {"some_value": 42}}],
[{"some_value": 42}]
),
])
def test_control_tag_get_labels(tag, regions, expected):
assert tag.get_labels(regions) == expected

0 comments on commit dc0f88f

Please sign in to comment.