-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathtest_get.py
319 lines (271 loc) · 11 KB
/
test_get.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"""
Tests for the CLI get functionality.
.. raw:: html
<!--
* Copyright (C) 2024 Scribe
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
"""
import unittest
from pathlib import Path
from unittest.mock import patch
from scribe_data.cli.get import get_data
class TestGetData(unittest.TestCase):
"""
Unit tests for the get_data function.
These tests ensure the correct behavior of the get_data function.
"""
# MARK: Subprocess Patching
@patch("scribe_data.cli.get.generate_emoji")
def test_get_emoji_keywords(self, generate_emoji):
"""
Test the generation of emoji keywords.
This test ensures that when thee `data_type` is `emoji_keywords`, the `generate_emoji` function is called with the correct arguments.
"""
get_data(
language="English", data_type="emoji_keywords", output_dir="./test_output"
)
generate_emoji.assert_called_once_with(
language="English",
output_dir="./test_output",
)
# MARK: Invalid Arguments
def test_invalid_arguments(self):
"""
Test the behavior of the get_data function when invalid arguments are provided.
"""
with self.assertRaises(ValueError):
get_data()
# MARK: All Data
@patch("scribe_data.cli.get.query_data")
@patch("scribe_data.cli.get.parse_wd_lexeme_dump")
@patch("scribe_data.cli.get.questionary.confirm")
def test_get_all_data_types_for_language_user_says_yes(
self, mock_questionary_confirm, mock_parse, mock_query_data
):
"""
Test the behavior when the user agrees to query Wikidata directly.
This test checks that `parse_wd_lexeme_dump` is called with the correct parameters
when the user confirms they want to query Wikidata.
"""
mock_questionary_confirm.return_value.ask.return_value = True
get_data(all_bool=True, language="English")
mock_parse.assert_called_once_with(
language="English",
wikidata_dump_type=["form"],
data_types="all", # because if only language given, data_types is None
type_output_dir="scribe_data_json_export", # default for JSON
)
mock_query_data.assert_not_called()
@patch("scribe_data.cli.get.parse_wd_lexeme_dump")
def test_get_all_languages_and_data_types(self, mock_parse):
"""
Test retrieving all languages for a specific data type.
Ensures that `query_data` is called properly when `--all` flag is used with a data type.
"""
get_data(all_bool=True)
mock_parse.assert_called_once_with(
language="all",
wikidata_dump_type=["form", "translations"],
data_types="all",
type_output_dir="scribe_data_json_export",
wikidata_dump_path=None,
)
# MARK: Language and Data Type
@patch("scribe_data.cli.get.query_data")
def test_get_specific_language_and_data_type(self, mock_query_data):
"""
Test retrieving a specific language and data type.
Ensures that `query_data` is called properly when a specific language and data type are provided.
"""
get_data(language="german", data_type="nouns", output_dir="./test_output")
mock_query_data.assert_called_once_with(
languages=["german"],
data_type=["nouns"],
output_dir="./test_output",
overwrite=False,
interactive=False,
)
# MARK: Capitalized Language
@patch("scribe_data.cli.get.query_data")
@patch("scribe_data.cli.get.Path.glob", return_value=[])
def test_get_data_with_capitalized_language(self, mock_glob, mock_query_data):
"""
Test retrieving data with a capitalized language.
Ensures that `query_data` is called properly when a capitalized language is provided.
"""
get_data(language="German", data_type="nouns")
mock_query_data.assert_called_once_with(
languages=["German"],
data_type=["nouns"],
output_dir="scribe_data_json_export",
overwrite=False,
interactive=False,
)
# MARK: Lowercase Language
@patch("scribe_data.cli.get.query_data")
@patch("scribe_data.cli.get.Path.glob", return_value=[])
def test_get_data_with_lowercase_language(self, mock_glob, mock_query_data):
"""
Test retrieving data with a lowercase language.
Ensures that `query_data` is called properly when a lowercase language is provided.
"""
get_data(language="german", data_type="nouns")
mock_query_data.assert_called_once_with(
languages=["german"],
data_type=["nouns"],
output_dir="scribe_data_json_export",
overwrite=False,
interactive=False,
)
# MARK: Output Directory
@patch("scribe_data.cli.get.query_data")
def test_get_data_with_different_output_directory(self, mock_query_data):
"""
Test retrieving data with a different output directory.
Ensures that `query_data` is called properly when a different output directory is provided.
"""
get_data(
language="german", data_type="nouns", output_dir="./custom_output_test"
)
mock_query_data.assert_called_once_with(
languages=["german"],
data_type=["nouns"],
output_dir="./custom_output_test",
overwrite=False,
interactive=False,
)
# MARK: Overwrite is True
@patch("scribe_data.cli.get.query_data")
@patch("scribe_data.cli.get.Path.glob", return_value=[])
def test_get_data_with_overwrite_true(self, mock_glob, mock_query_data):
"""
Test retrieving data with the overwrite flag set to True.
Ensures that `query_data` is called properly when the overwrite flag is set to True.
"""
get_data(language="English", data_type="verbs", overwrite=True)
mock_query_data.assert_called_once_with(
languages=["English"],
data_type=["verbs"],
output_dir="scribe_data_json_export",
overwrite=True,
interactive=False,
)
# MARK: Overwrite is False
@patch("scribe_data.cli.get.query_data")
def test_get_data_with_overwrite_false(self, mock_query_data):
get_data(
language="English",
data_type="verbs",
overwrite=False,
output_dir="./custom_output_test",
interactive=False,
)
mock_query_data.assert_called_once_with(
languages=["English"],
data_type=["verbs"],
output_dir="./custom_output_test",
overwrite=False,
interactive=False,
)
# MARK: User Chooses Skip
@patch("scribe_data.cli.get.query_data")
@patch(
"scribe_data.cli.get.Path.glob",
return_value=[Path("./test_output/English/nouns.json")],
)
@patch("scribe_data.cli.get.questionary.confirm")
def test_user_skips_existing_file(
self, mock_questionary_confirm, mock_glob, mock_query_data
):
"""
Test the behavior when the user chooses to skip an existing file.
Ensures that the file is not overwritten and the function returns the correct result.
"""
mock_questionary_confirm.return_value.ask.return_value = False
result = get_data(
language="English", data_type="nouns", output_dir="./test_output"
)
# Validate the skip result.
self.assertEqual(result, {"success": False, "skipped": True})
mock_query_data.assert_not_called()
# MARK: User Chooses Overwrite
@patch("scribe_data.cli.get.query_data")
@patch(
"scribe_data.cli.get.Path.glob",
return_value=[Path("./test_output/English/nouns.json")],
)
@patch("scribe_data.cli.get.questionary.confirm")
def test_user_overwrites_existing_file(
self, mock_questionary_confirm, mock_glob, mock_query_data
):
"""
Test the behavior when the user chooses to overwrite an existing file.
Ensures that the file is overwritten and the function returns the correct result.
"""
mock_questionary_confirm.return_value.ask.return_value = True
get_data(language="English", data_type="nouns", output_dir="./test_output")
mock_query_data.assert_called_once_with(
languages=["English"],
data_type=["nouns"],
output_dir="./test_output",
overwrite=False,
interactive=False,
)
# MARK: Translations
@patch("scribe_data.cli.get.parse_wd_lexeme_dump")
def test_get_translations_no_language_specified(self, mock_parse):
"""
Test behavior when no language is specified for 'translations'.
Expect language="all".
"""
get_data(data_type="translations")
mock_parse.assert_called_once_with(
language="all",
wikidata_dump_type=["translations"],
type_output_dir="scribe_data_json_export", # default output dir for JSON
wikidata_dump_path=None,
)
@patch("scribe_data.cli.get.parse_wd_lexeme_dump")
def test_get_translations_with_specific_language(self, mock_parse):
"""
Test behavior when a specific language is provided for 'translations'.
Expect parse_wd_lexeme_dump to be called with that language.
"""
get_data(
language="Spanish", data_type="translations", output_dir="./test_output"
)
mock_parse.assert_called_once_with(
language="Spanish",
wikidata_dump_type=["translations"],
type_output_dir="./test_output",
wikidata_dump_path=None,
)
@patch("scribe_data.cli.get.parse_wd_lexeme_dump")
def test_get_translations_with_dump(self, mock_parse):
"""
Test behavior when a Wikidata dump path is specified for 'translations'.
Even with a language, it should call parse_wd_lexeme_dump
passing that dump path.
"""
get_data(
language="German", data_type="translations", wikidata_dump="./wikidump.json"
)
mock_parse.assert_called_once_with(
language="German",
wikidata_dump_type=["translations"],
type_output_dir="scribe_data_json_export", # default for JSON
wikidata_dump_path="./wikidump.json",
)