Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: DIA-1862: Improve SDK coverage and data flow #6993

Open
wants to merge 12 commits into
base: develop
Choose a base branch
from
3 changes: 3 additions & 0 deletions label_studio/core/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,9 @@ def collect_versions(force=False):
if 'commit' in result[package]:
sentry_sdk.set_tag('commit-' + package, result[package]['commit'])

# edition type
result['edition'] = settings.VERSION_EDITION

settings.VERSIONS = result
return result

Expand Down
2 changes: 2 additions & 0 deletions label_studio/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
print('\n\n !!! Please, pip install pytest-env \n\n')
exit(-100)

from label_studio.tests.sdk.fixtures import * # noqa: F403

from .utils import (
azure_client_mock,
create_business,
Expand Down
29 changes: 29 additions & 0 deletions label_studio/tests/sdk/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest
from label_studio_sdk.client import LabelStudio
from label_studio_sdk.data_manager import Column, Filters, Operator, Type

from .common import LABEL_CONFIG_AND_TASKS


@pytest.fixture
def test_project_with_view(django_live_url, business_client):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to have these as fixtures now with the current way we use pytest, but looking forward to progressively replacing them with something like Factory Boy when available (cc @mcanu )

ls = LabelStudio(base_url=django_live_url, api_key=business_client.api_key)
p = ls.projects.create(title='New Project', label_config=LABEL_CONFIG_AND_TASKS['label_config'])

project = ls.projects.get(id=p.id)

task_data = [{'data': {'my_text': 'Test task ' + str(i)}} for i in range(10)]
ls.projects.import_tasks(id=project.id, request=task_data)
orig_tasks = []
for task in ls.tasks.list(project=project.id):
orig_tasks.append(task)

filters = Filters.create(
Filters.OR,
[Filters.item(Column.id, Operator.EQUAL, Type.Number, Filters.value(t.id)) for t in orig_tasks[::2]],
)

view = ls.views.create(
project=project.id, data=dict(title='Test View', filters=filters, ordering=['-' + Column.id])
)
return ls, project, orig_tasks, view
58 changes: 58 additions & 0 deletions label_studio/tests/sdk/test_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pandas as pd
import pytest

from label_studio.tests.sdk.common import LABEL_CONFIG_AND_TASKS

pytestmark = pytest.mark.django_db
from label_studio_sdk.client import LabelStudio


@pytest.fixture
def test_project(django_live_url, business_client):
ls = LabelStudio(base_url=django_live_url, api_key=business_client.api_key)
project = ls.projects.create(title='Export Test Project', label_config=LABEL_CONFIG_AND_TASKS['label_config'])
ls.projects.import_tasks(id=project.id, request=LABEL_CONFIG_AND_TASKS['tasks_for_import'])
return ls, project


def test_export_formats(test_project):
ls, project = test_project

# Get available export formats
formats = ls.projects.exports.list_formats(project.id)
assert len(formats) > 0


def test_direct_export(test_project):
ls, project = test_project

# Test JSON export
json_data = ls.projects.exports.as_json(project.id)
assert isinstance(json_data, list)
assert len(json_data) == 1

# Test pandas export
df = ls.projects.exports.as_pandas(project.id)
assert isinstance(df, pd.DataFrame)
assert len(df) == 1

# Test low level export - import new task without annotations
ls.projects.import_tasks(
id=project.id,
request={
'data': {
'my_text': 'Opossums are great',
'ref_id': 456,
'meta_info': {'timestamp': '2020-03-09 18:15:28.212882', 'location': 'North Pole'},
}
},
)
data = ls.projects.exports.download_sync(project.id, download_all_tasks=False)
from label_studio_sdk.projects.exports.client_ext import _bytestream_to_json

assert len(_bytestream_to_json(data)) == 1

data = ls.projects.exports.download_sync(project.id, download_all_tasks=True)
from label_studio_sdk.projects.exports.client_ext import _bytestream_to_json

assert len(_bytestream_to_json(data)) == 2
24 changes: 5 additions & 19 deletions label_studio/tests/sdk/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,13 @@ def test_create_view(django_live_url, business_client):
}


def test_get_tasks_from_view(django_live_url, business_client):
ls = LabelStudio(base_url=django_live_url, api_key=business_client.api_key)
p = ls.projects.create(title='New Project', label_config=LABEL_CONFIG_AND_TASKS['label_config'])

project = ls.projects.get(id=p.id)

task_data = [{'data': {'my_text': 'Test task ' + str(i)}} for i in range(10)]
ls.projects.import_tasks(id=project.id, request=task_data)
orig_tasks = []
for task in ls.tasks.list(project=project.id):
orig_tasks.append(task)

filters = Filters.create(
Filters.OR,
[Filters.item(Column.id, Operator.EQUAL, Type.Number, Filters.value(t.id)) for t in orig_tasks[::2]],
)

ls.views.create(project=project.id, data=dict(title='Test View', filters=filters, ordering=['-' + Column.id]))
def test_get_tasks_from_view(test_project_with_view):
ls, project, orig_tasks, view = test_project_with_view
views = ls.views.list(project=project.id)
assert len(views) == 1
view = views[0]
found_view = views[0]

assert found_view.id == view.id
tasks = []
for task in ls.tasks.list(view=view.id):
tasks.append(task)
Expand Down
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ django-migration-linter = "^5.1.0"
setuptools = ">=75.4.0"

# Humansignal repo dependencies
label-studio-sdk = {url = "https://github.com/HumanSignal/label-studio-sdk/archive/29f7df5c6a705f865ade0c2484299d4e320562f8.zip"}
label-studio-sdk = {url = "https://github.com/HumanSignal/label-studio-sdk/archive/3b4e2d25e7494a7529ee8c0b3f2f73021ab6b55b.zip"}

[tool.poetry.group.test.dependencies]
pytest = "7.2.2"
Expand Down
Loading