Skip to content

Commit a9626cd

Browse files
committed
fix review comments, add update feature, and add tests
1 parent 2b860ed commit a9626cd

File tree

2 files changed

+67
-29
lines changed

2 files changed

+67
-29
lines changed

src/apyanki/note.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ def update_or_add_to_collection(self, anki: Anki) -> Note:
632632
try:
633633
# Import NoteId here to avoid circular imports at module level
634634
from anki.notes import NoteId
635+
635636
note_id = NoteId(int(self.nid))
636637
existing_note = anki.col.get_note(note_id)
637638
return self._update_note(anki, existing_note)
@@ -649,6 +650,7 @@ def update_or_add_to_collection(self, anki: Anki) -> Note:
649650
try:
650651
# Import CardId here to avoid circular imports at module level
651652
from anki.cards import CardId
653+
652654
card_id = CardId(int(self.cid))
653655
card = anki.col.get_card(card_id)
654656
if card:
@@ -666,7 +668,7 @@ def update_or_add_to_collection(self, anki: Anki) -> Note:
666668
# If no existing note found or ID not provided, add as new
667669
return self.add_to_collection(anki)
668670

669-
def _update_note(self, anki: 'Anki', existing_note: Any) -> Note:
671+
def _update_note(self, anki: "Anki", existing_note: Any) -> Note:
670672
"""Update an existing note with new field values
671673
672674
Returns: The updated note

tests/test_batch_edit.py

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ def test_update_from_file(collection):
5858
"""Test updating a note from a Markdown file."""
5959
# First create a note
6060
with open("test.md", "w") as f:
61-
f.write(textwrap.dedent("""\
61+
f.write(
62+
textwrap.dedent(
63+
"""\
6264
model: Basic
6365
tags: marked
6466
@@ -68,7 +70,9 @@ def test_update_from_file(collection):
6870
6971
## Back
7072
Original answer.
71-
"""))
73+
"""
74+
)
75+
)
7276

7377
with Anki(collection_db_path=collection) as a:
7478
# Add initial note
@@ -77,7 +81,9 @@ def test_update_from_file(collection):
7781

7882
# Now create update file with the note ID
7983
with open("test_update.md", "w") as f:
80-
f.write(textwrap.dedent(f"""\
84+
f.write(
85+
textwrap.dedent(
86+
f"""\
8187
model: Basic
8288
tags: marked updated
8389
nid: {note_id}
@@ -88,7 +94,9 @@ def test_update_from_file(collection):
8894
8995
## Back
9096
Updated answer.
91-
"""))
97+
"""
98+
)
99+
)
92100

93101
# Update the note
94102
updated_note = a.update_notes_from_file("test_update.md")[0]
@@ -109,7 +117,9 @@ def test_update_from_file_by_cid(collection):
109117
"""Test updating a note from a Markdown file using card ID."""
110118
# First create a note
111119
with open("test.md", "w") as f:
112-
f.write(textwrap.dedent("""\
120+
f.write(
121+
textwrap.dedent(
122+
"""\
113123
model: Basic
114124
tags: marked
115125
@@ -119,7 +129,9 @@ def test_update_from_file_by_cid(collection):
119129
120130
## Back
121131
Original answer.
122-
"""))
132+
"""
133+
)
134+
)
123135

124136
with Anki(collection_db_path=collection) as a:
125137
# Add initial note
@@ -128,7 +140,9 @@ def test_update_from_file_by_cid(collection):
128140

129141
# Now create update file with the card ID
130142
with open("test_update_cid.md", "w") as f:
131-
f.write(textwrap.dedent(f"""\
143+
f.write(
144+
textwrap.dedent(
145+
f"""\
132146
model: Basic
133147
tags: marked card-updated
134148
cid: {card_id}
@@ -139,7 +153,9 @@ def test_update_from_file_by_cid(collection):
139153
140154
## Back
141155
Updated answer via card ID.
142-
"""))
156+
"""
157+
)
158+
)
143159

144160
# Update the note
145161
updated_note = a.update_notes_from_file("test_update_cid.md")[0]
@@ -159,7 +175,9 @@ def test_update_from_file_new_and_existing(collection):
159175
"""Test updating a file with both new and existing notes."""
160176
# First create a note
161177
with open("test.md", "w") as f:
162-
f.write(textwrap.dedent("""\
178+
f.write(
179+
textwrap.dedent(
180+
"""\
163181
model: Basic
164182
tags: marked
165183
@@ -169,7 +187,9 @@ def test_update_from_file_new_and_existing(collection):
169187
170188
## Back
171189
Original answer.
172-
"""))
190+
"""
191+
)
192+
)
173193

174194
with Anki(collection_db_path=collection) as a:
175195
# Add initial note
@@ -178,7 +198,9 @@ def test_update_from_file_new_and_existing(collection):
178198

179199
# Now create update file with both the existing note and a new note
180200
with open("test_mixed.md", "w") as f:
181-
f.write(textwrap.dedent(f"""\
201+
f.write(
202+
textwrap.dedent(
203+
f"""\
182204
model: Basic
183205
tags: common-tag
184206
@@ -200,7 +222,9 @@ def test_update_from_file_new_and_existing(collection):
200222
201223
## Back
202224
Brand new content.
203-
"""))
225+
"""
226+
)
227+
)
204228

205229
# Update the note
206230
updated_notes = a.update_notes_from_file("test_mixed.md")
@@ -233,7 +257,9 @@ def test_update_file_with_note_ids(collection):
233257
"""Test that --update-file option updates the original file with note IDs."""
234258
# First create a note file without IDs
235259
with open("test_no_ids.md", "w") as f:
236-
f.write(textwrap.dedent("""\
260+
f.write(
261+
textwrap.dedent(
262+
"""\
237263
model: Basic
238264
tags: test-update-file
239265
@@ -250,23 +276,25 @@ def test_update_file_with_note_ids(collection):
250276
251277
## Back
252278
Another test answer
253-
"""))
279+
"""
280+
)
281+
)
254282

255283
with Anki(collection_db_path=collection) as a:
256284
# Add notes with update_file=True
257285
notes = a.add_notes_from_file("test_no_ids.md", update_file=True)
258-
286+
259287
# Verify two notes were added
260288
assert len(notes) == 2
261-
289+
262290
# Read the file again to check if IDs were added
263291
with open("test_no_ids.md", "r") as f:
264292
updated_content = f.read()
265-
293+
266294
# The file should now contain nid: lines
267295
assert f"nid: {notes[0].n.id}" in updated_content
268296
assert f"nid: {notes[1].n.id}" in updated_content
269-
297+
270298
# Clean up
271299
os.remove("test_no_ids.md")
272300

@@ -275,7 +303,9 @@ def test_update_file_with_mixed_notes(collection):
275303
"""Test that --update-file option updates only new notes in update-from-file."""
276304
# First create a note to get its ID
277305
with open("test_initial.md", "w") as f:
278-
f.write(textwrap.dedent("""\
306+
f.write(
307+
textwrap.dedent(
308+
"""\
279309
model: Basic
280310
tags: initial-note
281311
@@ -285,16 +315,20 @@ def test_update_file_with_mixed_notes(collection):
285315
286316
## Back
287317
Initial answer
288-
"""))
318+
"""
319+
)
320+
)
289321

290322
with Anki(collection_db_path=collection) as a:
291323
# Add the initial note
292324
initial_note = a.add_notes_from_file("test_initial.md")[0]
293325
note_id = initial_note.n.id
294-
326+
295327
# Now create a file with the existing note ID and a new note
296328
with open("test_update_mix.md", "w") as f:
297-
f.write(textwrap.dedent(f"""\
329+
f.write(
330+
textwrap.dedent(
331+
f"""\
298332
model: Basic
299333
tags: common-tag
300334
@@ -316,23 +350,25 @@ def test_update_file_with_mixed_notes(collection):
316350
317351
## Back
318352
New answer without ID
319-
"""))
320-
353+
"""
354+
)
355+
)
356+
321357
# Update notes with update_file=True
322358
notes = a.update_notes_from_file("test_update_mix.md", update_file=True)
323-
359+
324360
# Verify two notes were affected
325361
assert len(notes) == 2
326-
362+
327363
# Read the updated file
328364
with open("test_update_mix.md", "r") as f:
329365
updated_content = f.read()
330-
366+
331367
# Verify the original ID is preserved and the new note got an ID
332368
new_note = next(n for n in notes if n.n.id != note_id)
333369
assert f"nid: {note_id}" in updated_content # Original ID
334370
assert f"nid: {new_note.n.id}" in updated_content # New ID
335-
371+
336372
# Clean up
337373
os.remove("test_initial.md")
338374
os.remove("test_update_mix.md")

0 commit comments

Comments
 (0)