diff --git a/AGENTS.md b/AGENTS.md index 05b0c325..7ef83516 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,5 +1,12 @@ +## Code Converting +- When given code in another language for conversion, keep the original's overall naming for class, variable, and function names. + Only adjust as needed to conform to Python conventions, ex `myMethod` becomes `my_method`. + ## Code Linting -- Run `prek run -a` to lint and format the code.* +- Run `prek run -a` to lint and format the code. + +## Testing +- Test fixtures are located in the `./tests/data` directory. ## Releases - A release must be in a separate branch called `release/X.X.X` where X.X.X is the version number. diff --git a/mindee/v2/product/classification/classification_result.py b/mindee/v2/product/classification/classification_result.py index 2d314ffb..cb57e2ad 100644 --- a/mindee/v2/product/classification/classification_result.py +++ b/mindee/v2/product/classification/classification_result.py @@ -13,4 +13,4 @@ def __init__(self, raw_response: StringDict) -> None: self.classification = ClassificationClassifier(raw_response["classification"]) def __str__(self) -> str: - return f"Classification\n======{self.classification}" + return f"Classification\n==============\n{self.classification}" diff --git a/tests/data b/tests/data index cf609708..e6d5046f 160000 --- a/tests/data +++ b/tests/data @@ -1 +1 @@ -Subproject commit cf60970865e2f07a87aeebac53857b605eac185a +Subproject commit e6d5046f09c9c3fed4cf086c340664da50734615 diff --git a/tests/v2/product/classification/test_classification_parameters.py b/tests/v2/product/classification/test_classification_parameters.py new file mode 100644 index 00000000..299cf694 --- /dev/null +++ b/tests/v2/product/classification/test_classification_parameters.py @@ -0,0 +1,32 @@ +import pytest + +from mindee import ClassificationParameters + +MODEL_ID = "test-model-id" + + +@pytest.mark.v2 +def test_parameters_must_init_with_required(): + product_params = ClassificationParameters(MODEL_ID) + + request_params = product_params.get_request_parameters() + assert request_params["model_id"] == MODEL_ID + assert "alias" not in request_params + assert "webhook_ids" not in request_params + assert "extraction_text_context" not in request_params + + +@pytest.mark.v2 +def test_parameters_must_init_with_all(): + product_params = ClassificationParameters( + MODEL_ID, + alias="test-alias", + webhook_ids=["webhook-1", "webhook-2"], + extraction_text_context="context", + ) + + request_params = product_params.get_request_parameters() + assert request_params["model_id"] == MODEL_ID + assert request_params["alias"] == "test-alias" + assert request_params["webhook_ids"] == ["webhook-1", "webhook-2"] + assert request_params["extraction_text_context"] == "context" diff --git a/tests/v2/product/classification/test_classification_response.py b/tests/v2/product/classification/test_classification_response.py index 582dc521..a8e0afb3 100644 --- a/tests/v2/product/classification/test_classification_response.py +++ b/tests/v2/product/classification/test_classification_response.py @@ -14,7 +14,7 @@ @pytest.mark.v2 def test_classification_single(): - json_sample, _ = get_product_samples( + json_sample, rst_sample = get_product_samples( product="classification", file_name="default_sample" ) response = ClassificationResponse(json_sample) @@ -26,6 +26,8 @@ def test_classification_single(): ) assert response.inference.result.classification.document_type == "invoice" + assert rst_sample == str(response) + @pytest.mark.v2 def test_classification_with_extraction_result(): diff --git a/tests/v2/product/crop/test_crop_parameters.py b/tests/v2/product/crop/test_crop_parameters.py new file mode 100644 index 00000000..8b01e1ce --- /dev/null +++ b/tests/v2/product/crop/test_crop_parameters.py @@ -0,0 +1,32 @@ +import pytest + +from mindee import CropParameters + +MODEL_ID = "test-model-id" + + +@pytest.mark.v2 +def test_parameters_must_init_with_required(): + product_params = CropParameters(MODEL_ID) + + request_params = product_params.get_request_parameters() + assert request_params["model_id"] == MODEL_ID + assert "alias" not in request_params + assert "webhook_ids" not in request_params + assert "extraction_text_context" not in request_params + + +@pytest.mark.v2 +def test_parameters_must_init_with_all(): + product_params = CropParameters( + MODEL_ID, + alias="test-alias", + webhook_ids=["webhook-1", "webhook-2"], + extraction_text_context="context", + ) + + request_params = product_params.get_request_parameters() + assert request_params["model_id"] == MODEL_ID + assert request_params["alias"] == "test-alias" + assert request_params["webhook_ids"] == ["webhook-1", "webhook-2"] + assert request_params["extraction_text_context"] == "context" diff --git a/tests/v2/product/extraction/test_extraction_parameters.py b/tests/v2/product/extraction/test_extraction_parameters.py new file mode 100644 index 00000000..b28e0e3d --- /dev/null +++ b/tests/v2/product/extraction/test_extraction_parameters.py @@ -0,0 +1,72 @@ +import json + +import pytest + +from mindee import ExtractionParameters +from tests.utils import V2_PRODUCT_PATH + +MODEL_ID = "test-model-id" +DATA_SCHEMA_DICT = json.loads( + (V2_PRODUCT_PATH / "extraction" / "data_schema_replace_param.json").read_text() +) +DATA_SCHEMA = json.dumps( + DATA_SCHEMA_DICT, indent=None, sort_keys=True, separators=(",", ":") +) + + +@pytest.mark.v2 +def test_parameters_must_init_with_required(): + product_params = ExtractionParameters(MODEL_ID) + + request_params = product_params.get_request_parameters() + assert request_params["model_id"] == MODEL_ID + assert "alias" not in request_params + assert "webhook_ids" not in request_params + assert "rag" not in request_params + assert "raw_text" not in request_params + assert "polygon" not in request_params + assert "confidence" not in request_params + assert "text_context" not in request_params + assert "data_schema" not in request_params + + +@pytest.mark.v2 +def test_parameters_must_init_with_all(): + product_params = ExtractionParameters( + MODEL_ID, + alias="test-alias", + webhook_ids=["webhook-1", "webhook-2"], + rag=True, + raw_text=False, + polygon=True, + confidence=False, + text_context="test context", + data_schema=DATA_SCHEMA, + ) + + request_params = product_params.get_request_parameters() + assert request_params["model_id"] == MODEL_ID + assert request_params["alias"] == "test-alias" + assert request_params["webhook_ids"] == ["webhook-1", "webhook-2"] + assert request_params["rag"] == "true" + assert request_params["raw_text"] == "false" + assert request_params["polygon"] == "true" + assert request_params["confidence"] == "false" + assert request_params["text_context"] == "test context" + assert request_params["data_schema"] == DATA_SCHEMA + + +@pytest.mark.v2 +def test_data_schema_should_leave_unset_when_not_provided(): + product_params = ExtractionParameters(MODEL_ID) + + request_params = product_params.get_request_parameters() + assert "data_schema" not in request_params + + +@pytest.mark.v2 +def test_data_schema_string_should_initialize(): + product_params = ExtractionParameters(MODEL_ID, data_schema=DATA_SCHEMA) + + request_params = product_params.get_request_parameters() + assert request_params["data_schema"] == DATA_SCHEMA diff --git a/tests/v2/product/split/test_split_parameters.py b/tests/v2/product/split/test_split_parameters.py new file mode 100644 index 00000000..918eb603 --- /dev/null +++ b/tests/v2/product/split/test_split_parameters.py @@ -0,0 +1,32 @@ +import pytest + +from mindee import SplitParameters + +MODEL_ID = "test-model-id" + + +@pytest.mark.v2 +def test_parameters_must_init_with_required(): + product_params = SplitParameters(MODEL_ID) + + request_params = product_params.get_request_parameters() + assert request_params["model_id"] == MODEL_ID + assert "alias" not in request_params + assert "webhook_ids" not in request_params + assert "extraction_text_context" not in request_params + + +@pytest.mark.v2 +def test_parameters_must_init_with_all(): + product_params = SplitParameters( + MODEL_ID, + alias="test-alias", + webhook_ids=["webhook-1", "webhook-2"], + extraction_text_context="context", + ) + + request_params = product_params.get_request_parameters() + assert request_params["model_id"] == MODEL_ID + assert request_params["alias"] == "test-alias" + assert request_params["webhook_ids"] == ["webhook-1", "webhook-2"] + assert request_params["extraction_text_context"] == "context" diff --git a/tests/v2/product/split/test_split_response.py b/tests/v2/product/split/test_split_response.py index 734e9022..0693faab 100644 --- a/tests/v2/product/split/test_split_response.py +++ b/tests/v2/product/split/test_split_response.py @@ -10,7 +10,9 @@ @pytest.mark.v2 def test_split_single(): - json_sample, _ = get_product_samples(product="split", file_name="split_single") + json_sample, rst_sample = get_product_samples( + product="split", file_name="split_single" + ) response = SplitResponse(json_sample) assert isinstance(response.inference, SplitInference) @@ -20,10 +22,14 @@ def test_split_single(): assert response.inference.result.splits[0].page_range[1] == 0 assert response.inference.result.splits[0].document_type == "receipt" + assert rst_sample == str(response) + @pytest.mark.v2 def test_split_multiple(): - json_sample, _ = get_product_samples(product="split", file_name="split_multiple") + json_sample, rst_sample = get_product_samples( + product="split", file_name="split_multiple" + ) response = SplitResponse(json_sample) assert isinstance(response.inference, SplitInference) assert isinstance(response.inference.result, SplitResult) @@ -45,6 +51,8 @@ def test_split_multiple(): assert response.inference.result.splits[2].page_range[1] == 4 assert response.inference.result.splits[2].document_type == "receipt" + assert rst_sample == str(response) + @pytest.mark.v2 def test_split_with_extraction_result():