-
Notifications
You must be signed in to change notification settings - Fork 0
🧪 Add tests for _memory_entry helper function in memory_mcp.py #56
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
Merged
sheepdestroyer
merged 1 commit into
master
from
test-memory-mcp-helper-9074601772171428400
Jun 24, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,9 @@ | ||
| 🎯 **What:** The `_memory_entry` helper function in `router/memory_mcp.py` was previously completely untested. This function is responsible for converting a raw dictionary representation of a memory entry from LiteLLM into a structured dictionary used by the MCP. | ||
|
|
||
| 📊 **Coverage:** The new `test_memory_mcp.py` file covers several scenarios for the `_memory_entry` dictionary translation helper: | ||
| - **Happy Path:** Tests that valid and complete memory dictionaries are properly parsed, mapped, and typed. | ||
| - **Invalid Key:** Verifies that if a memory has a key that does not start with `"memory:"`, the function properly returns `None`. | ||
| - **Malformed/String Value:** Tests the graceful fallback when a memory value is a raw string instead of the expected JSON payload, ensuring it still parses the string into the `data` field and initializes an empty `tags` array without throwing a JSON decode error. | ||
| - **Missing Fields:** Tests dictionary inputs that are missing either `"key"` or `"value"` or both to ensure graceful behavior, fallback values, and early exits without `KeyError`s. | ||
|
|
||
| ✨ **Result:** Increased code reliability and coverage for the core memory data transformation logic with pure, isolated dictionary tests ensuring zero runtime side effects. |
This file contains hidden or 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 @@ | ||
| #!/usr/bin/env python3 | ||
| import json | ||
| import pytest | ||
| from router.memory_mcp import _memory_entry | ||
|
|
||
| def test_memory_entry_happy_path(): | ||
| """Test correctly formatted and complete memory entry.""" | ||
| valid_key = "memory:global:project_standards::1689201948123:a1b2c3d4e5f6" | ||
| valid_value = json.dumps({"data": "Use pytest for all tests", "tags": ["testing", "python"]}) | ||
| lmem = { | ||
| "key": valid_key, | ||
| "value": valid_value, | ||
| "memory_id": "test_id_123" | ||
| } | ||
|
|
||
| result = _memory_entry(lmem) | ||
|
|
||
| assert result is not None | ||
| assert result["key"] == valid_key | ||
| assert result["category"] == "project_standards" | ||
| assert result["data"] == "Use pytest for all tests" | ||
| assert result["tags"] == ["testing", "python"] | ||
| assert result["scope"] == "global" | ||
| assert result["timestamp"] == "1689201948123" | ||
| assert result["memory_id"] == "test_id_123" | ||
|
|
||
| def test_memory_entry_invalid_key(): | ||
| """Test with a key that does not start with 'memory:'.""" | ||
| lmem = { | ||
| "key": "notamemory:global:cat::123:hash", | ||
| "value": json.dumps({"data": "test", "tags": []}) | ||
| } | ||
|
|
||
| result = _memory_entry(lmem) | ||
| assert result is None | ||
|
|
||
| def test_memory_entry_malformed_json_value(): | ||
| """Test with malformed/string value where JSON parsing fails.""" | ||
| valid_key = "memory:local:notes::1689201948123:a1b2c3d4e5f6" | ||
| # value is just a raw string, not JSON | ||
| lmem = { | ||
| "key": valid_key, | ||
| "value": "This is just a raw string without tags" | ||
| } | ||
|
|
||
| result = _memory_entry(lmem) | ||
|
|
||
| assert result is not None | ||
| assert result["data"] == "This is just a raw string without tags" | ||
| assert result["tags"] == [] # Falls back to empty tags list | ||
| assert result["category"] == "notes" | ||
| assert result["scope"] == "local" | ||
|
|
||
| def test_memory_entry_missing_fields(): | ||
| """Test gracefully handling dictionaries with missing keys.""" | ||
| # Missing 'value' and 'memory_id' | ||
| lmem1 = { | ||
| "key": "memory:global:ideas::123:hash" | ||
| } | ||
| result1 = _memory_entry(lmem1) | ||
| assert result1 is not None | ||
| assert result1["data"] == "" | ||
| assert result1["tags"] == [] | ||
| assert result1["memory_id"] == "" | ||
|
|
||
| # Missing 'key' | ||
| lmem2 = { | ||
| "value": json.dumps({"data": "test", "tags": []}) | ||
| } | ||
| result2 = _memory_entry(lmem2) | ||
| assert result2 is None | ||
|
|
||
| # Empty dict | ||
| result3 = _memory_entry({}) | ||
| assert result3 is None | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 function
test_memory_entry_missing_fieldsbundles multiple independent test scenarios (missing optional fields, missing required keys, and empty dictionaries) into a single test. This is a test smell known as Assertion Roulette, where a failure in an earlier assertion prevents subsequent independent scenarios from running, making debugging more difficult.Since
pytestis already imported in this file, we can leveragepytest.mark.parametrizeto cleanly separate these test cases. This also makes use of the currently unusedpytestimport.