Skip to content

Commit 74a6973

Browse files
committed
test: add unit tests for MemOS CLI functions
1 parent 59bff62 commit 74a6973

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

tests/test_cli.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"""
2+
Tests for the MemOS CLI tool.
3+
"""
4+
5+
import zipfile
6+
7+
from io import BytesIO
8+
from unittest.mock import MagicMock, mock_open, patch
9+
10+
import pytest
11+
import requests
12+
13+
from memos.cli import download_examples, export_openapi, main
14+
15+
16+
class TestExportOpenAPI:
17+
"""Test the export_openapi function."""
18+
19+
@patch("memos.api.start_api.app")
20+
@patch("builtins.open", new_callable=mock_open)
21+
@patch("os.makedirs")
22+
def test_export_openapi_success(self, mock_makedirs, mock_file, mock_app):
23+
"""Test successful OpenAPI export."""
24+
mock_openapi_data = {"openapi": "3.0.0", "info": {"title": "Test API"}}
25+
mock_app.openapi.return_value = mock_openapi_data
26+
27+
result = export_openapi("/test/path/openapi.json")
28+
29+
assert result is True
30+
mock_makedirs.assert_called_once_with("/test/path", exist_ok=True)
31+
mock_file.assert_called_once_with("/test/path/openapi.json", "w")
32+
33+
@patch("memos.api.start_api.app")
34+
@patch("builtins.open", side_effect=OSError("Permission denied"))
35+
def test_export_openapi_error(self, mock_file, mock_app):
36+
"""Test OpenAPI export when file writing fails."""
37+
mock_app.openapi.return_value = {"test": "data"}
38+
39+
with pytest.raises(IOError):
40+
export_openapi("/invalid/path/openapi.json")
41+
42+
43+
class TestDownloadExamples:
44+
"""Test the download_examples function."""
45+
46+
def create_mock_zip_content(self):
47+
"""Create mock zip file content for testing."""
48+
zip_buffer = BytesIO()
49+
with zipfile.ZipFile(zip_buffer, "w") as zip_file:
50+
zip_file.writestr("MemOS-main/examples/test_example.py", "# Test example content")
51+
zip_file.writestr(
52+
"MemOS-main/examples/subfolder/another_example.py", "# Another example"
53+
)
54+
return zip_buffer.getvalue()
55+
56+
@patch("requests.get")
57+
@patch("os.makedirs")
58+
@patch("builtins.open", new_callable=mock_open)
59+
def test_download_examples_success(self, mock_file, mock_makedirs, mock_requests):
60+
"""Test successful examples download."""
61+
mock_response = MagicMock()
62+
mock_response.content = self.create_mock_zip_content()
63+
mock_requests.return_value = mock_response
64+
65+
result = download_examples("/test/dest")
66+
67+
assert result is True
68+
mock_requests.assert_called_once_with(
69+
"https://github.com/MemTensor/MemOS/archive/refs/heads/main.zip"
70+
)
71+
mock_response.raise_for_status.assert_called_once()
72+
73+
@patch("requests.get")
74+
def test_download_examples_error(self, mock_requests):
75+
"""Test download examples when request fails."""
76+
mock_requests.side_effect = requests.RequestException("Network error")
77+
78+
result = download_examples("/test/dest")
79+
80+
assert result is False
81+
82+
83+
class TestMainCLI:
84+
"""Test the main CLI function."""
85+
86+
@patch("memos.cli.download_examples")
87+
def test_main_download_examples(self, mock_download):
88+
"""Test main function with download_examples command."""
89+
mock_download.return_value = True
90+
91+
with patch("sys.argv", ["memos", "download_examples", "--dest", "/test/dest"]):
92+
with pytest.raises(SystemExit) as exc_info:
93+
main()
94+
assert exc_info.value.code == 0
95+
mock_download.assert_called_once_with("/test/dest")
96+
97+
@patch("memos.cli.export_openapi")
98+
def test_main_export_openapi(self, mock_export):
99+
"""Test main function with export_openapi command."""
100+
mock_export.return_value = True
101+
102+
with patch("sys.argv", ["memos", "export_openapi", "--output", "/test/openapi.json"]):
103+
with pytest.raises(SystemExit) as exc_info:
104+
main()
105+
assert exc_info.value.code == 0
106+
mock_export.assert_called_once_with("/test/openapi.json")

0 commit comments

Comments
 (0)