This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support to new
options_order
settings attribute for multi…
… label selection questions (#133) # Description This PR add changes so a new attribute with name `options_order` can be set for multi selection question settings. This attribute is an enumerable that can have the following values: * `natural`: the default value, showing that the options/labels should be ordered in their natural order where they have been specified for the questions. * `suggestion`: showing that options/labels should be ordered giving priority to the labels that have suggestions (having or not an assigned `score`) and sorting by `score` when available. Some more details about this implementation: - When retrieving a multi label selection question without `options_order` in his settings, `options_order: natural` will be returned inside the settings. - When creating a multi label selection question if `options_order` has not been defined as part of the creation body it will be inserted as `options_order: natural` inside the database. Notice how a `None` value is not accepted. - When updating a multi label selection question `options_order` is optional but `None` is not a valid value. Closes #132 **Type of change** (Please delete options that are not relevant. Remember to title the PR according to the type of change) - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Refactor (change restructuring the codebase without changing functionality) - [ ] Improvement (change adding some improvement to an existing functionality) - [ ] Documentation update **How Has This Been Tested** (Please describe the tests that you ran to verify your changes. And ideally, reference `tests`) - [x] Adding new tests and improving old ones. **Checklist** - [ ] I added relevant documentation - [ ] follows the style guidelines of this project - [ ] I did a self-review of my code - [ ] I made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] I filled out [the contributor form](https://tally.so/r/n9XrxK) (see text above) - [ ] I have added relevant notes to the CHANGELOG.md file (See https://keepachangelog.com/)
- Loading branch information
Showing
12 changed files
with
320 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright 2021-present, the Recognai S.L. team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
114 changes: 114 additions & 0 deletions
114
tests/unit/api/v1/datasets/questions/test_create_dataset_question.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# Copyright 2021-present, the Recognai S.L. team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from uuid import UUID | ||
|
||
import pytest | ||
from argilla_server.enums import OptionsOrder, QuestionType | ||
from argilla_server.models import Question | ||
from httpx import AsyncClient | ||
from sqlalchemy import func, select | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
|
||
from tests.factories import DatasetFactory | ||
|
||
|
||
@pytest.mark.asyncio | ||
class TestCreateDatasetQuestion: | ||
def url(self, dataset_id: UUID) -> str: | ||
return f"/api/v1/datasets/{dataset_id}/questions" | ||
|
||
async def test_create_dataset_multi_label_selection_question_with_options_order( | ||
self, db: AsyncSession, async_client: AsyncClient, owner_auth_header: dict | ||
): | ||
dataset = await DatasetFactory.create() | ||
|
||
response = await async_client.post( | ||
self.url(dataset.id), | ||
headers=owner_auth_header, | ||
json={ | ||
"name": "name", | ||
"title": "title", | ||
"settings": { | ||
"type": QuestionType.multi_label_selection, | ||
"options": [ | ||
{"value": "label-a", "text": "Label A"}, | ||
{"value": "label-b", "text": "Label B"}, | ||
], | ||
"options_order": OptionsOrder.suggestion, | ||
}, | ||
}, | ||
) | ||
|
||
assert response.status_code == 201 | ||
|
||
response_json = response.json() | ||
assert response_json["settings"]["options_order"] == OptionsOrder.suggestion | ||
|
||
question = (await db.execute(select(Question).filter_by(id=UUID(response_json["id"])))).scalar_one() | ||
assert question.settings["options_order"] == OptionsOrder.suggestion | ||
|
||
async def test_create_dataset_multi_label_selection_question_without_options_order( | ||
self, db: AsyncSession, async_client: AsyncClient, owner_auth_header: dict | ||
): | ||
dataset = await DatasetFactory.create() | ||
|
||
response = await async_client.post( | ||
self.url(dataset.id), | ||
headers=owner_auth_header, | ||
json={ | ||
"name": "name", | ||
"title": "title", | ||
"settings": { | ||
"type": QuestionType.multi_label_selection, | ||
"options": [ | ||
{"value": "label-a", "text": "Label A"}, | ||
{"value": "label-b", "text": "Label B"}, | ||
], | ||
}, | ||
}, | ||
) | ||
|
||
assert response.status_code == 201 | ||
|
||
response_json = response.json() | ||
assert response_json["settings"]["options_order"] == OptionsOrder.natural | ||
|
||
question = (await db.execute(select(Question).filter_by(id=UUID(response_json["id"])))).scalar_one() | ||
assert question.settings["options_order"] == OptionsOrder.natural | ||
|
||
async def test_create_dataset_multi_label_selection_question_with_options_order_as_none( | ||
self, db: AsyncSession, async_client: AsyncClient, owner_auth_header: dict | ||
): | ||
dataset = await DatasetFactory.create() | ||
|
||
response = await async_client.post( | ||
self.url(dataset.id), | ||
headers=owner_auth_header, | ||
json={ | ||
"name": "name", | ||
"title": "title", | ||
"settings": { | ||
"type": QuestionType.multi_label_selection, | ||
"options": [ | ||
{"value": "label-a", "text": "Label A"}, | ||
{"value": "label-b", "text": "Label B"}, | ||
], | ||
"options_order": None, | ||
}, | ||
}, | ||
) | ||
|
||
assert response.status_code == 422 | ||
assert (await db.execute(select(func.count(Question.id)))).scalar_one() == 0 |
66 changes: 66 additions & 0 deletions
66
tests/unit/api/v1/datasets/questions/test_list_dataset_questions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Copyright 2021-present, the Recognai S.L. team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from uuid import UUID | ||
|
||
import pytest | ||
from argilla_server.enums import OptionsOrder, QuestionType | ||
from httpx import AsyncClient | ||
|
||
from tests.factories import QuestionFactory | ||
|
||
|
||
@pytest.mark.asyncio | ||
class TestListDatasetQuestions: | ||
def url(self, dataset_id: UUID) -> str: | ||
return f"/api/v1/datasets/{dataset_id}/questions" | ||
|
||
async def test_list_dataset_multi_label_selection_question_with_options_order( | ||
self, async_client: AsyncClient, owner_auth_header: dict | ||
): | ||
question = await QuestionFactory.create( | ||
settings={ | ||
"type": QuestionType.multi_label_selection, | ||
"options": [ | ||
{"value": "label-a", "text": "Label A"}, | ||
{"value": "label-b", "text": "Label B"}, | ||
], | ||
"options_order": OptionsOrder.suggestion, | ||
}, | ||
) | ||
|
||
response = await async_client.get(self.url(question.dataset_id), headers=owner_auth_header) | ||
|
||
assert response.status_code == 200 | ||
assert response.json()["items"][0]["settings"]["options_order"] == OptionsOrder.suggestion | ||
assert question.settings["options_order"] == OptionsOrder.suggestion | ||
|
||
async def test_list_dataset_multi_label_selection_question_without_options_order( | ||
self, async_client: AsyncClient, owner_auth_header: dict | ||
): | ||
question = await QuestionFactory.create( | ||
settings={ | ||
"type": QuestionType.multi_label_selection, | ||
"options": [ | ||
{"value": "label-a", "text": "Label A"}, | ||
{"value": "label-b", "text": "Label B"}, | ||
], | ||
}, | ||
) | ||
|
||
response = await async_client.get(self.url(question.dataset_id), headers=owner_auth_header) | ||
|
||
assert response.status_code == 200 | ||
assert response.json()["items"][0]["settings"]["options_order"] == OptionsOrder.natural | ||
assert "options_order" not in question.settings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright 2021-present, the Recognai S.L. team. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.