Skip to content

Commit

Permalink
add objectid tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grillazz committed Nov 23, 2023
1 parent 926444c commit d247efb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
56 changes: 56 additions & 0 deletions tests/test_schemas.py
Original file line number Diff line number Diff line change
@@ -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."
1 change: 0 additions & 1 deletion tests/test_vegs_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pytest
from bson import ObjectId
# from bson import ObjectId
from fastapi import status
from httpx import AsyncClient

Expand Down

0 comments on commit d247efb

Please sign in to comment.