-
Notifications
You must be signed in to change notification settings - Fork 148
/
test_note.py
315 lines (255 loc) · 8.87 KB
/
test_note.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
import itertools
import pytest
import time
import genanki
import os
from unittest import mock
import tempfile
import textwrap
import warnings
def test_ok():
my_model = genanki.Model(
1376484377,
'Simple Model',
fields=[
{'name': 'Question'},
{'name': 'Answer'},
],
templates=[
{
'name': 'Card 1',
'qfmt': '{{Question}}',
'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
},
])
my_note = genanki.Note(
model=my_model,
fields=['Capital of Argentina', 'Buenos Aires'])
# https://stackoverflow.com/a/45671804
with warnings.catch_warnings():
warnings.simplefilter('error')
my_note.write_to_db(mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), itertools.count(int(time.time() * 1000)))
class TestTags:
def test_init(self):
n = genanki.Note(tags=['foo', 'bar', 'baz'])
with pytest.raises(ValueError):
n = genanki.Note(tags=['foo', 'b ar', 'baz'])
def test_assign(self):
n = genanki.Note()
n.tags = ['foo', 'bar', 'baz']
with pytest.raises(ValueError):
n.tags = ['foo', 'bar', ' baz']
def test_assign_element(self):
n = genanki.Note(tags=['foo', 'bar', 'baz'])
n.tags[0] = 'dankey_kang'
with pytest.raises(ValueError):
n.tags[0] = 'dankey kang'
def test_assign_slice(self):
n = genanki.Note(tags=['foo', 'bar', 'baz'])
n.tags[1:3] = ['bowser', 'daisy']
with pytest.raises(ValueError):
n.tags[1:3] = ['bowser', 'princess peach']
def test_append(self):
n = genanki.Note(tags=['foo', 'bar', 'baz'])
n.tags.append('sheik_hashtag_melee')
with pytest.raises(ValueError):
n.tags.append('king dedede')
def test_extend(self):
n = genanki.Note(tags=['foo', 'bar', 'baz'])
n.tags.extend(['palu', 'wolf'])
with pytest.raises(ValueError):
n.tags.extend(['dat fox doe'])
def test_insert(self):
n = genanki.Note(tags=['foo', 'bar', 'baz'])
n.tags.insert(0, 'lucina')
with pytest.raises(ValueError):
n.tags.insert(0, 'nerf joker pls')
def test_num_fields_equals_model_ok():
m = genanki.Model(
1894808898,
'Test Model',
fields=[
{'name': 'Question'},
{'name': 'Answer'},
{'name': 'Extra'},
],
templates=[
{
'name': 'Card 1',
'qfmt': '{{Question}}',
'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
},
])
n = genanki.Note(
model=m,
fields=['What is the capital of Taiwan?', 'Taipei',
'Taipei was originally inhabitied by the Ketagalan people prior to the arrival of Han settlers in 1709.'])
n.write_to_db(mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), itertools.count(int(time.time() * 1000)))
# test passes if code gets to here without raising
def test_num_fields_less_than_model_raises():
m = genanki.Model(
1894808898,
'Test Model',
fields=[
{'name': 'Question'},
{'name': 'Answer'},
{'name': 'Extra'},
],
templates=[
{
'name': 'Card 1',
'qfmt': '{{Question}}',
'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
},
])
n = genanki.Note(model=m, fields=['What is the capital of Taiwan?', 'Taipei'])
with pytest.raises(ValueError):
n.write_to_db(mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), itertools.count(int(time.time() * 1000)))
def test_num_fields_more_than_model_raises():
m = genanki.Model(
1894808898,
'Test Model',
fields=[
{'name': 'Question'},
{'name': 'Answer'},
],
templates=[
{
'name': 'Card 1',
'qfmt': '{{Question}}',
'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
},
])
n = genanki.Note(
model=m,
fields=['What is the capital of Taiwan?', 'Taipei',
'Taipei was originally inhabitied by the Ketagalan people prior to the arrival of Han settlers in 1709.'])
with pytest.raises(ValueError):
n.write_to_db(mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), itertools.count(int(time.time() * 1000)))
class TestFindInvalidHtmlTagsInField:
def test_ok(self):
assert genanki.Note._find_invalid_html_tags_in_field('<h1>') == []
def test_ok_with_space(self):
assert genanki.Note._find_invalid_html_tags_in_field(' <h1> ') == []
def test_ok_multiple(self):
assert genanki.Note._find_invalid_html_tags_in_field('<h1>test</h1>') == []
def test_ok_br(self):
assert genanki.Note._find_invalid_html_tags_in_field('<br>') == []
def test_ok_br2(self):
assert genanki.Note._find_invalid_html_tags_in_field('<br/>') == []
def test_ok_br3(self):
assert genanki.Note._find_invalid_html_tags_in_field('<br />') == []
def test_ok_attrs(self):
assert genanki.Note._find_invalid_html_tags_in_field('<h1 style="color: red">STOP</h1>') == []
def test_ok_uppercase(self):
assert genanki.Note._find_invalid_html_tags_in_field('<TD></Td>') == []
def test_ng_empty(self):
assert genanki.Note._find_invalid_html_tags_in_field(' hello <> goodbye') == ['<>']
def test_ng_empty_space(self):
assert genanki.Note._find_invalid_html_tags_in_field(' hello < > goodbye') == ['< >']
def test_ng_invalid_characters(self):
assert genanki.Note._find_invalid_html_tags_in_field('<@h1>') == ['<@h1>']
def test_ng_invalid_characters_end(self):
assert genanki.Note._find_invalid_html_tags_in_field('<h1@>') == ['<h1@>']
def test_ng_issue_28(self):
latex_code = r'''
[latex]
\schemestart
\chemfig{*6(--(<OH)-(<:Br)---)}
\arrow{->[?]}
\chemfig{*6(--(<[:30]{O}?)(<:H)-?[,{>},](<:H)---)}
\schemestop
[/latex]
'''
latex_code = textwrap.dedent(latex_code[1:])
expected_invalid_tags = [
'<OH)-(<:Br)---)}\n\\arrow{->',
'<[:30]{O}?)(<:H)-?[,{>',
]
assert genanki.Note._find_invalid_html_tags_in_field(latex_code) == expected_invalid_tags
def test_ok_html_comment(self):
# see https://github.com/kerrickstaley/genanki/issues/108
assert genanki.Note._find_invalid_html_tags_in_field('<!-- here is a comment -->') == []
def test_ok_cdata(self):
# see https://github.com/kerrickstaley/genanki/issues/108
assert genanki.Note._find_invalid_html_tags_in_field('<![CDATA[ here is some cdata ]]>') == []
def test_warns_on_invalid_html_tags():
my_model = genanki.Model(
1376484377,
'Simple Model',
fields=[
{'name': 'Question'},
{'name': 'Answer'},
],
templates=[
{
'name': 'Card 1',
'qfmt': '{{Question}}',
'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
},
])
my_note = genanki.Note(
model=my_model,
fields=['Capital of <$> Argentina', 'Buenos Aires'])
with pytest.warns(UserWarning, match='^Field contained the following invalid HTML tags.*$'):
my_note.write_to_db(mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), itertools.count(int(time.time() * 1000)))
def test_suppress_warnings(recwarn):
my_model = genanki.Model(
1376484377,
'Simple Model',
fields=[
{'name': 'Question'},
{'name': 'Answer'},
],
templates=[
{
'name': 'Card 1',
'qfmt': '{{Question}}',
'afmt': '{{FrontSide}}<hr id="answer">{{Answer}}',
},
])
my_note = genanki.Note(
model=my_model,
fields=['Capital of <$> Argentina', 'Buenos Aires'])
with warnings.catch_warnings():
warnings.simplefilter('error')
warnings.filterwarnings('ignore', message='^Field contained the following invalid HTML tags', module='genanki')
my_note.write_to_db(mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), itertools.count(int(time.time() * 1000)))
# https://github.com/kerrickstaley/genanki/issues/121
def test_does_not_warn_on_html_tags_in_guid():
my_note = genanki.Note(
model=genanki.BASIC_MODEL,
fields=['Capital of Iowa', 'Des Moines'],
guid='Gt<p8{N2>Z')
with warnings.catch_warnings():
warnings.simplefilter('error')
my_note.write_to_db(mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), itertools.count(int(time.time() * 1000)))
def test_furigana_field():
# Fields like {{furigana:Reading}} are supported by the Japanese Support plugin:
# https://ankiweb.net/shared/info/3918629684
# Japanese Support is quasi-official (it was created by Damien Elmes, the creator of Anki) and so
# we support it in genanki.
my_model = genanki.Model(
1523004567,
'Japanese',
fields=[
{'name': 'Question'},
{'name': 'Answer'},
],
templates=[
{
'name': 'Card 1',
'qfmt': '{{Question}}',
'afmt': '{{FrontSide}}<hr id="answer">{{furigana:Answer}}',
},
])
my_note = genanki.Note(
model=my_model,
fields=['kanji character', '漢字[かんじ]'])
my_deck = genanki.Deck(1702181380, 'Japanese')
my_deck.add_note(my_note)
with tempfile.NamedTemporaryFile(delete=False) as tempf:
pass
my_deck.write_to_file(tempf.name)
os.remove(tempf.name)
# test passes if there is no exception