From d247efbd4d9db153f9ea2e8e143cf2042e936397 Mon Sep 17 00:00:00 2001 From: Jakub Miazek Date: Thu, 23 Nov 2023 19:55:49 +0100 Subject: [PATCH] add objectid tests --- tests/test_schemas.py | 56 ++++++++++++++++++++++++++++++++++++++++++ tests/test_vegs_api.py | 1 - 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 tests/test_schemas.py diff --git a/tests/test_schemas.py b/tests/test_schemas.py new file mode 100644 index 0000000..77ec27d --- /dev/null +++ b/tests/test_schemas.py @@ -0,0 +1,56 @@ +import pytest +from bson import ObjectId +from greens.schemas.vegs import DocumentResponse + + +# Since ObjectIdField is not a standard Pydantic field, we'll assume it's a custom field +# that should accept BSON ObjectId instances. We'll need to mock or construct valid and +# invalid ObjectId instances for testing. + +# Helper function to create a valid ObjectId instance +def create_valid_objectid(): + return ObjectId() + + +# Helper function to create an invalid ObjectId instance +def create_invalid_objectid(): + return "invalid_objectid" + + +# Happy path tests with various realistic test values +@pytest.mark.parametrize( + "test_id, object_id", + [ + ("HP-1", create_valid_objectid()), + # Add more test cases with different valid ObjectIds if necessary + ], +) +def test_document_response_with_valid_id(test_id, object_id): + # Act + document_response = DocumentResponse(id=object_id) + + # Assert + assert document_response.id == object_id, f"Test case {test_id} failed: The id field did not match the input ObjectId." + + +# Edge cases +# Assuming edge cases would be related to the boundaries of ObjectId creation and representation +# Since ObjectId is a specific BSON type, edge cases might not be as relevant here, +# but we can still test for unusual but valid ObjectIds + +# Error cases +@pytest.mark.parametrize( + "test_id, object_id, expected_exception", + [ + ("EC-1", create_invalid_objectid(), ValueError), # Invalid ObjectId format + ("EC-2", None, ValueError), # None as an ObjectId + ("EC-3", 12345, ValueError), # Integer as an ObjectId + # Add more test cases with different invalid ObjectIds if necessary + ], +) +def test_document_response_with_invalid_id(test_id, object_id, expected_exception): + # Act and Assert + with pytest.raises(expected_exception) as exc_info: + DocumentResponse(id=object_id) + + assert str(exc_info.value), f"Test case {test_id} failed: Expected exception {expected_exception} was not raised." diff --git a/tests/test_vegs_api.py b/tests/test_vegs_api.py index d773f13..a7eecfd 100644 --- a/tests/test_vegs_api.py +++ b/tests/test_vegs_api.py @@ -1,6 +1,5 @@ import pytest from bson import ObjectId -# from bson import ObjectId from fastapi import status from httpx import AsyncClient