forked from MemTensor/MemOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_hello_world.py
More file actions
136 lines (102 loc) · 5.6 KB
/
test_hello_world.py
File metadata and controls
136 lines (102 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from unittest.mock import patch
from memos.hello_world import (
memos_chend_hello_world,
memos_chentang_hello_world,
memos_dany_hello_world,
memos_hello_world,
memos_huojh_hello_world,
memos_niusm_hello_world,
memos_wanghy_hello_world,
memos_wangyzh_hello_world,
memos_yuqingchen_hello_world,
memos_zhaojihao_hello_world,
)
def test_memos_hello_world_logger_called():
"""Test that the logger.info method is called and "Hello world from memos!" is returned."""
with patch("memos.hello_world.logger.info") as mock_logger:
result = memos_hello_world()
assert result == "Hello world from memos!"
mock_logger.assert_called_once_with("memos_hello_world function called.")
def test_memos_dany_hello_world_logger_called():
"""# What's patch for?
Using path, we can mock a function that is called in the function we are testing.
> For example, a new function A called function B, and function B will take a long time to run.
> So testing function A will take a long time.
> Using path, we can pmock a return value from B, so that we can test function A faster.
"""
# Multiple test cases example:
test_cases = [
(1, "data1", "logger.info: para_1 is 1", "logger.debug: para_2 is data1", "return_value_1"),
(2, "data2", "logger.info: para_1 is 2", "logger.debug: para_2 is data2", "return_value_2"),
(3, "data3", "logger.info: para_1 is 3", "logger.debug: para_2 is data3", "return_value_3"),
]
with (
patch("memos.hello_world.logger.info") as mock_logger_info,
patch("memos.hello_world.logger.debug") as mock_logger_debug,
):
for para1, para2, expected_output_1, expected_output_2, expected_return_value in test_cases:
result = memos_dany_hello_world(para1, para2)
assert result == expected_return_value
mock_logger_info.assert_any_call(expected_output_1)
mock_logger_debug.assert_called_once_with(expected_output_2)
mock_logger_info.reset_mock()
mock_logger_debug.reset_mock()
def test_memos_chend_hello_world_logger_called():
"""Test that the logger.info method is called and "Hello world from memos-chend!" is returned."""
with patch("memos.hello_world.logger.info") as mock_logger:
result = memos_chend_hello_world()
assert result == "Hello world from memos-chend!"
mock_logger.assert_called_once_with("memos_chend_hello_world function called.")
def test_memos_wanghy_hello_world_logger_called():
"""Test that the logger.info method is called and "Hello world from memos-wanghy!" is returned."""
with patch("memos.hello_world.logger.info") as mock_logger:
result = memos_wanghy_hello_world()
assert result == "Hello world from memos-wanghy!"
mock_logger.assert_called_once_with("memos_wanghy_hello_world function called.")
def test_memos_huojh_hello_world_logger_called():
"""Test that the logger.info method is called and quicksort is okay."""
with patch("memos.hello_world.logger.info") as mock_logger:
arr = [1, 7, 4, 1, 10, 9, -2]
sorted_arr = [-2, 1, 1, 4, 7, 9, 10]
res = memos_huojh_hello_world(arr)
assert all(x == y for x, y in zip(sorted_arr, res, strict=False))
mock_logger.assert_called_with("memos_huojh_hello_world function called.")
def test_memos_niusm_hello_world_logger_called():
"""Test that the logger.info method is called and "Hello world from memos-niusm!" is returned."""
with patch("memos.hello_world.logger.info") as mock_logger:
result = memos_niusm_hello_world()
assert result == "Hello world from memos-niusm!"
mock_logger.assert_called_once_with("memos_niusm_hello_world function called.")
def test_memos_wangyzh_hello_world_logger_called():
"""Test that the logger.info method is called and "Hello world from memos-wangyzh!" is returned."""
with patch("memos.hello_world.logger.info") as mock_logger:
result = memos_wangyzh_hello_world()
assert result == "Hello world from memos-wangyzh!"
mock_logger.assert_called_once_with("memos_wangyzh_hello_world function called.")
def test_memos_zhaojihao_hello_world_logger_called():
"""Test that the logger.info method is called and "Hello world from memos-zhaojihao!" is returned."""
with patch("memos.hello_world.logger.info") as mock_logger:
result = memos_zhaojihao_hello_world()
assert result == "Hello world from memos-zhaojihao!"
mock_logger.assert_called_once_with("memos_zhaojihao_hello_world function called.")
def test_memos_yuqingchen_hello_world_logger_called():
"""Test that the logger.info method is called and "Hello world from memos-yuqingchen!" is returned."""
with patch("memos.hello_world.logger.info") as mock_logger:
result = memos_yuqingchen_hello_world()
assert result == "Hello world from memos-yuqingchen!"
mock_logger.assert_called_once_with("memos_yuqingchen_hello_world function called.")
def test_memos_chen_tang_hello_world():
from memos.memories.textual.general import GeneralTextMemory
# Define return values for os.getenv
def mock_getenv(key, default=None):
mock_values = {
"MODEL": "mock-model-name",
"OPENAI_API_KEY": "mock-api-key",
"OPENAI_BASE_URL": "mock-api-url",
"EMBEDDING_MODEL": "mock-embedding-model",
}
return mock_values.get(key, default)
# Use patch to mock os.getenv
with patch("os.getenv", side_effect=mock_getenv):
memory = memos_chentang_hello_world()
assert isinstance(memory, GeneralTextMemory)