-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_scripts.py
More file actions
82 lines (72 loc) · 3.25 KB
/
test_scripts.py
File metadata and controls
82 lines (72 loc) · 3.25 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
import unittest
from unittest.mock import patch, MagicMock
import os
import json
# Import functions from scripts if possible, or we can test the CLI behavior
# Since the scripts are mostly monolithic main blocks, we will test the key logic functions
import check_word
import get_pexels_image
import anki_tools
class TestAnkiAutomation(unittest.TestCase):
def test_check_word_load_vocabulary(self):
"""Test that the duplicate checker correctly identifies words from a mock file."""
test_file = "test_deck.txt"
with open(test_file, "w") as f:
f.write("#separator:tab\n")
f.write("guid1\tnotetype\tApple\tphonetic\tsound\tipa\n")
f.write("guid2\tnotetype\t\"Banana\"\tphonetic\tsound\tipa\n")
vocab = check_word.load_vocabulary(test_file)
self.assertIn("apple", vocab)
self.assertIn("banana", vocab)
self.assertNotIn("cherry", vocab)
os.remove(test_file)
@patch('urllib.request.urlopen')
def test_get_word_data(self, mock_urlopen):
"""Test fetching dictionary data with mocked API response."""
mock_response = MagicMock()
mock_json = [
{
"word": "test",
"phonetic": "/test/",
"meanings": [
{
"definitions": [
{
"definition": "a trial or experiment",
"example": "This is a test case."
}
]
}
]
}
]
mock_response.read.return_value = json.dumps(mock_json).encode()
mock_urlopen.return_value.__enter__.return_value = mock_response
data = anki_tools.get_word_data("test")
self.assertEqual(data["ipa"], "/test/")
self.assertEqual(data["meaning"], "a trial or experiment")
self.assertIn("<b>test</b>", data["example"])
@patch('subprocess.run')
def test_generate_audio_logic(self, mock_run):
"""Test the audio generation command sequence."""
# Mocking open to simulate file presence for the base64 conversion
with patch("builtins.open", unittest.mock.mock_open(read_data=b"audio_data")):
with patch("os.path.exists", return_value=True):
with patch("os.remove"):
data = anki_tools.generate_audio_base64("hello", "tmp")
self.assertIsNotNone(data)
# Verify say and ffmpeg were called
self.assertEqual(mock_run.call_count, 2)
args1 = mock_run.call_args_list[0][0][0]
args2 = mock_run.call_args_list[1][0][0]
self.assertEqual(args1[0], "say")
self.assertEqual(args2[0], "ffmpeg")
def test_find_note_id_formatting(self):
"""Test that the Anki search query is properly formatted."""
with patch('anki_tools.invoke') as mock_invoke:
mock_invoke.return_value = [12345]
note_id = anki_tools.find_note_id("apple")
self.assertEqual(note_id, 12345)
mock_invoke.assert_called_with("findNotes", query='"Word:apple"')
if __name__ == "__main__":
unittest.main()