-
Notifications
You must be signed in to change notification settings - Fork 12
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 for retrieving EngineFields
- Loading branch information
Milan Ondrašovič
committed
Jan 14, 2025
1 parent
3ba3e90
commit 00b86c0
Showing
6 changed files
with
140 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,34 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from dataclasses import dataclass, field | ||
from typing import Literal, Optional | ||
|
||
EngineFieldType = Literal["string", "number", "date", "enum", "button"] | ||
MultilineType = Literal["true", "false", ""] # Preparation for "auto" option | ||
|
||
|
||
@dataclass | ||
class Engine: | ||
id: int | ||
url: str | ||
name: str | ||
type: str | ||
# Workout around the issue that "type" is a reserved keyword | ||
type: str = field(metadata={"dataclasses_json": {"name": "type"}}) | ||
learning_enabled: bool | ||
description: str | ||
agenda_id: str | ||
|
||
|
||
@dataclass | ||
class EngineField: | ||
id: int | ||
url: str | ||
engine: str | ||
name: str | ||
tabular: bool | ||
label: str | ||
# Workout around the issue that "type" is a reserved keyword | ||
type: EngineFieldType = field(metadata={"dataclasses_json": {"name": "type"}}) | ||
subtype: Optional[str] | ||
pre_trained_field_id: Optional[str] | ||
multiline: MultilineType |
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,75 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
from rossum_api.domain_logic.resources import Resource | ||
from rossum_api.models.engine import Engine, EngineField | ||
|
||
TEST_ENGINE_ID = 123 | ||
|
||
|
||
@pytest.mark.asyncio | ||
class TestEngine: | ||
@pytest.fixture | ||
def dummy_engine(self): | ||
# https://elis.rossum.ai/api/docs/internal/#engine | ||
return { | ||
"id": TEST_ENGINE_ID, | ||
"url": f"https://elis.rossum.ai/api/v1/engines/{TEST_ENGINE_ID}", | ||
"name": "test_engine", | ||
"type": "extractor", | ||
"learning_enabled": False, | ||
"description": "Test engine", | ||
"agenda_id": "test_agenda_id", | ||
} | ||
|
||
async def test_retrieve_engine(self, elis_client, dummy_engine): | ||
client, http_client = elis_client | ||
http_client.fetch_one.return_value = dummy_engine | ||
|
||
engine: Engine = await client.retrieve_engine(TEST_ENGINE_ID) | ||
|
||
assert engine == Engine(**dummy_engine) | ||
http_client.fetch_one.assert_called_with(Resource.Engine, TEST_ENGINE_ID) | ||
|
||
async def test_list_all_engines(self, elis_client, dummy_engine, mock_generator): | ||
client, http_client = elis_client | ||
http_client.fetch_all.return_value = mock_generator(dummy_engine) | ||
|
||
engines = client.list_all_engines() | ||
|
||
async for engine in engines: | ||
assert engine == Engine(**dummy_engine) | ||
|
||
http_client.fetch_all.assert_called_with(Resource.Engine, (), ()) | ||
|
||
|
||
@pytest.mark.asyncio | ||
class TestEngineFields: | ||
@pytest.fixture | ||
def dummy_engine_field(self): | ||
# https://elis.rossum.ai/api/docs/internal/#engine-field | ||
return { | ||
"id": 456, # EngineField ID, not the Engine ID | ||
"url": f"https://elis.rossum.ai/api/v1/engine_fields/{TEST_ENGINE_ID}", | ||
"engine": f"https://elis.rossum.ai/api/v1/engines/{TEST_ENGINE_ID}", | ||
"name": f"test_engine_field_{TEST_ENGINE_ID}", | ||
"label": "Test engine field", | ||
"type": "string", | ||
"subtype": "alphanumeric", | ||
"pre_trained_field_id": "document_id", # https://elis.rossum.ai/api/docs/internal/#get-list-of-possible-pre_trained_field_id-fields | ||
"tabular": False, | ||
"multiline": "false", | ||
} | ||
|
||
async def test_retrieve_engine_fields(self, elis_client, dummy_engine_field, mock_generator): | ||
client, http_client = elis_client | ||
|
||
http_client.fetch_all.return_value = mock_generator(dummy_engine_field) | ||
|
||
engine_fields = client.retrieve_engine_fields(TEST_ENGINE_ID) | ||
|
||
async for engine_field in engine_fields: | ||
assert engine_field == EngineField(**dummy_engine_field) | ||
|
||
http_client.fetch_all.assert_called_with(Resource.EngineField, engine=TEST_ENGINE_ID) |