-
Notifications
You must be signed in to change notification settings - Fork 18.5k
feat: complete app modesls test script #28549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @codomposer, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new, extensive suite of unit tests for the core application models. The primary goal is to ensure the robustness and correctness of various model functionalities, including data validation, property behaviors, serialization, and inter-model relationships. This enhancement significantly improves the test coverage and stability of the application's foundational data structures. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a comprehensive suite of unit tests for the application models, significantly improving test coverage and code reliability. The tests are well-structured and cover a wide range of functionalities and model relationships. My review focuses on enhancing the robustness and maintainability of these new tests. I've identified a critical issue in the test_message_from_dict_deserialization test, which currently overlooks a bug in the Message.from_dict method. Additionally, I've provided suggestions to refactor a couple of enum validation tests to make them less brittle and more maintainable. Overall, this is a valuable contribution, and with these adjustments, the test suite will be even stronger.
| def test_message_from_dict_deserialization(self): | ||
| """Test message from_dict method.""" | ||
| # Arrange | ||
| message_id = str(uuid4()) | ||
| app_id = str(uuid4()) | ||
| conversation_id = str(uuid4()) | ||
| data = { | ||
| "id": message_id, | ||
| "app_id": app_id, | ||
| "conversation_id": conversation_id, | ||
| "model_id": "gpt-4", | ||
| "inputs": {"query": "test"}, | ||
| "query": "Test query", | ||
| "message": {"role": "user", "content": "Test"}, | ||
| "answer": "Test answer", | ||
| "total_price": Decimal("0.0003"), | ||
| "status": "normal", | ||
| "error": None, | ||
| "message_metadata": {"usage": {"tokens": 100}}, | ||
| "from_source": "api", | ||
| "from_end_user_id": None, | ||
| "from_account_id": None, | ||
| "created_at": "2024-01-01T00:00:00", | ||
| "updated_at": "2024-01-01T00:00:00", | ||
| "agent_based": False, | ||
| "workflow_run_id": None, | ||
| } | ||
|
|
||
| # Act | ||
| message = Message.from_dict(data) | ||
|
|
||
| # Assert | ||
| assert message.id == message_id | ||
| assert message.app_id == app_id | ||
| assert message.conversation_id == conversation_id | ||
| assert message.query == "Test query" | ||
| assert message.answer == "Test answer" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test test_message_from_dict_deserialization is incomplete and fails to uncover a bug in the Message.from_dict method.
The Message model has several required (non-nullable) fields such as message_unit_price, answer_unit_price, and currency that are not being handled in the from_dict method. The current test passes only because it doesn't assert the presence of these fields and doesn't attempt to persist the object, which would lead to a database integrity error.
To make this test more robust and correctly validate the deserialization logic, you should:
- Include the missing required fields in the
datadictionary. - Add assertions to verify that these fields are correctly set on the deserialized
messageobject.
This will cause the test to fail, correctly identifying the bug in Message.from_dict, which will then need to be updated to handle all required fields.
def test_message_from_dict_deserialization(self):
"""Test message from_dict method."""
# Arrange
message_id = str(uuid4())
app_id = str(uuid4())
conversation_id = str(uuid4())
data = {
"id": message_id,
"app_id": app_id,
"conversation_id": conversation_id,
"model_id": "gpt-4",
"inputs": {"query": "test"},
"query": "Test query",
"message": {"role": "user", "content": "Test"},
"answer": "Test answer",
"message_unit_price": Decimal("0.0001"),
"answer_unit_price": Decimal("0.0002"),
"total_price": Decimal("0.0003"),
"currency": "USD",
"status": "normal",
"error": None,
"message_metadata": {"usage": {"tokens": 100}},
"from_source": "api",
"from_end_user_id": None,
"from_account_id": None,
"created_at": "2024-01-01T00:00:00",
"updated_at": "2024-01-01T00:00:00",
"agent_based": False,
"workflow_run_id": None,
}
# Act
# Note: This will fail until Message.from_dict is updated to handle all required fields.
message = Message.from_dict(data)
# Assert
assert message.id == message_id
assert message.app_id == app_id
assert message.conversation_id == conversation_id
assert message.query == "Test query"
assert message.answer == "Test answer"
assert message.message_unit_price == Decimal("0.0001")
assert message.answer_unit_price == Decimal("0.0002")
assert message.currency == "USD"| def test_app_mode_validation(self): | ||
| """Test app mode enum values.""" | ||
| # Assert | ||
| assert AppMode.CHAT == "chat" | ||
| assert AppMode.COMPLETION == "completion" | ||
| assert AppMode.WORKFLOW == "workflow" | ||
| assert AppMode.ADVANCED_CHAT == "advanced-chat" | ||
| assert AppMode.AGENT_CHAT == "agent-chat" | ||
| assert AppMode.CHANNEL == "channel" | ||
| assert AppMode.RAG_PIPELINE == "rag-pipeline" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test hardcodes assertions for each enum member. This approach is brittle and not easily maintainable. If a new AppMode is added or an existing one is removed, this test will need to be manually updated. A more robust approach would be to compare the set of all enum values against an expected set. This makes the test more resilient to changes and ensures all enum members are checked.
| def test_app_mode_validation(self): | |
| """Test app mode enum values.""" | |
| # Assert | |
| assert AppMode.CHAT == "chat" | |
| assert AppMode.COMPLETION == "completion" | |
| assert AppMode.WORKFLOW == "workflow" | |
| assert AppMode.ADVANCED_CHAT == "advanced-chat" | |
| assert AppMode.AGENT_CHAT == "agent-chat" | |
| assert AppMode.CHANNEL == "channel" | |
| assert AppMode.RAG_PIPELINE == "rag-pipeline" | |
| def test_app_mode_validation(self): | |
| """Test app mode enum values.""" | |
| # Assert | |
| expected_modes = { | |
| "chat", | |
| "completion", | |
| "workflow", | |
| "advanced-chat", | |
| "agent-chat", | |
| "channel", | |
| "rag-pipeline", | |
| } | |
| assert {mode.value for mode in AppMode} == expected_modes |
| def test_icon_type_validation(self): | ||
| """Test icon type enum values.""" | ||
| # Assert | ||
| assert IconType.IMAGE.value == "image" | ||
| assert IconType.EMOJI.value == "emoji" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to test_app_mode_validation, this test hardcodes assertions for each enum member, making it brittle. A better practice is to compare the set of all enum values against an expected set to make the test more maintainable and robust against changes in the IconType enum.
| def test_icon_type_validation(self): | |
| """Test icon type enum values.""" | |
| # Assert | |
| assert IconType.IMAGE.value == "image" | |
| assert IconType.EMOJI.value == "emoji" | |
| def test_icon_type_validation(self): | |
| """Test icon type enum values.""" | |
| # Assert | |
| assert {t.value for t in IconType} == {"image", "emoji"} |
Add comprehensive unit tests for App models
Fixes
Closes #28548
Summary
This PR implements comprehensive unit tests for the App models in
api/tests/unit_tests/models/test_app_models.py, covering:The test suite includes 47 test cases organized into 8 test classes:
TestAppModelValidation- Core app model validationTestAppModelConfig- App configuration testingTestConversationModel- Conversation model integrityTestMessageModel- Message model functionalityTestMessageAnnotation- Annotation creation and relationshipsTestAppAnnotationSetting- Annotation settingsTestAppAnnotationHitHistory- Annotation hit trackingTestSiteModel- Site model validationTestModelIntegration- Integration scenarios across modelsAll tests follow the Arrange-Act-Assert pattern with concise docstrings and inline comments.
Test Results
All tests pass successfully with proper coverage of:
AppMode,IconType)Checklist
dev/reformat(backend) andcd web && npx lint-staged(frontend) to appease the lint godsContribution by Gittensor, learn more at https://gittensor.io/