refactor: rename list to search, load to import_file, split copy from move and use keyword flags

This commit is contained in:
randogoth 2026-10-24 21:57:01 +03:00
parent 3a9f3a2fcd
commit 78528abd3a
3 changed files with 55 additions and 44 deletions

View file

@ -17,7 +17,7 @@ def tree(tmp_path, monkeypatch):
def found(o, *query, either=False, path=''):
return o.list(path, True, either, True, False, False, *query)['found']
return o.search(*query, path=path, exact=True, either=either)['found']
def lines(path):
@ -98,13 +98,13 @@ def test_index_is_rebuilt_per_list_call(tree):
def test_move_untagged_file(tree):
with pytest.raises(NoEntry):
OrvaniTag().move('a.mp3', 'sub/a.mp3', False, False)
OrvaniTag().move('a.mp3', 'sub/a.mp3')
def test_move_tagged_file(tree):
o = OrvaniTag()
o.add('a.mp3', '#lofi')
o.move('a.mp3', 'sub/', False, True)
o.move('a.mp3', 'sub/', with_file=True)
assert (tree / 'sub' / 'a.mp3').exists()
assert lines(tree / 'sub') == ['a.mp3 --- #lofi']
assert not (tree / orvani.CONTEXT_FILE).exists()
@ -113,14 +113,14 @@ def test_move_tagged_file(tree):
def test_copy_tags_within_current_directory(tree):
o = OrvaniTag()
o.add('a.mp3', '#lofi')
o.move('a.mp3', 'b.mp3', True, False)
o.copy('a.mp3', 'b.mp3')
assert lines(tree) == ['a.mp3 --- #lofi', 'b.mp3 --- #lofi']
def test_rename_within_current_directory(tree):
o = OrvaniTag()
o.add('a.mp3', '#lofi')
o.move('a.mp3', 'renamed.mp3', False, True)
o.move('a.mp3', 'renamed.mp3', with_file=True)
assert not (tree / 'a.mp3').exists()
assert lines(tree) == ['renamed.mp3 --- #lofi']
@ -128,7 +128,7 @@ def test_rename_within_current_directory(tree):
def test_move_into_directory_without_slash(tree):
o = OrvaniTag()
o.add('a.mp3', '#lofi')
o.move('a.mp3', 'sub', False, True)
o.move('a.mp3', 'sub', with_file=True)
assert (tree / 'sub' / 'a.mp3').exists()
assert lines(tree / 'sub') == ['a.mp3 --- #lofi']
assert not (tree / orvani.CONTEXT_FILE).exists()
@ -138,7 +138,7 @@ def test_copy_tags_from_subfolder_to_folder(tree):
(tree / 'other').mkdir()
o = OrvaniTag()
o.add('sub/d.txt', '#doc')
o.move('sub/d.txt', 'other/', True, False)
o.copy('sub/d.txt', 'other/')
assert lines(tree) == ['other/ --- #doc']
assert lines(tree / 'sub') == ['d.txt --- #doc']
@ -216,13 +216,13 @@ def test_remove_saves_once(tree, monkeypatch):
def test_cache_is_rebuilt_when_context_changes(tree):
o = OrvaniTag()
o.add('a.mp3', '#lofi')
assert o.list('', True, False, True, True, False, '#lofi')['found'] == ['a.mp3']
assert o.search('#lofi', exact=True, cache=True)['found'] == ['a.mp3']
assert (tree / orvani.CACHE_FILE).exists()
o.add('b.mp3', '#lofi')
stat = (tree / orvani.CONTEXT_FILE).stat()
os.utime(tree / orvani.CONTEXT_FILE, ns=(stat.st_atime_ns, stat.st_mtime_ns + 10**9))
assert OrvaniTag().list('', True, False, True, True, False, '#lofi')['found'] == ['a.mp3', 'b.mp3']
assert OrvaniTag().search('#lofi', exact=True, cache=True)['found'] == ['a.mp3', 'b.mp3']
def test_import_untagged_files(tree):
@ -234,7 +234,7 @@ def test_import_untagged_files(tree):
(tree / orvani.CONTEXT_FILE).unlink()
(tree / 'sub' / orvani.CONTEXT_FILE).unlink()
assert OrvaniTag().load('context.txt') == 2
assert OrvaniTag().import_file('context.txt') == 2
assert lines(tree) == ['a.mp3 --- #lofi a note']
assert lines(tree / 'sub') == ['d.txt --- @docs']
@ -243,13 +243,13 @@ def test_import_skips_known_tags_and_empty_lines(tree):
o = OrvaniTag()
o.add('a.mp3', '#lofi')
(tree / 'context.txt').write_text('./a.mp3 --- #lofi #snare\n./b.mp3 --- \n')
assert OrvaniTag().load('context.txt') == 1
assert OrvaniTag().import_file('context.txt') == 1
assert lines(tree) == ['a.mp3 --- #lofi #snare']
def test_import_keeps_hyphenated_tags(tree):
(tree / 'context.txt').write_text('./a.mp3 --- @home-office #lo-fi rock+roll take-2\n')
OrvaniTag().load('context.txt')
OrvaniTag().import_file('context.txt')
assert lines(tree) == ['a.mp3 --- @home-office #lo-fi rock+roll take-2']
@ -261,11 +261,11 @@ def test_duplicate_and_bare_prefixes(tree):
def test_move_reports_what_is_missing(tree):
o = OrvaniTag()
with pytest.raises(NotFound, match=r"^Source file x\.mp3 doesn't exist\.$"):
o.move('x.mp3', 'sub/', False, True)
o.move('x.mp3', 'sub/', with_file=True)
with pytest.raises(NotFound, match=r"^Destination nope/a\.mp3 doesn't exist\.$"):
o.move('a.mp3', 'nope/a.mp3', False, True)
o.move('a.mp3', 'nope/a.mp3', with_file=True)
with pytest.raises(NotFound, match=r"^Source file x\.mp3 and destination nope/x don't exist\.$"):
o.move('x.mp3', 'nope/x', False, True)
o.move('x.mp3', 'nope/x', with_file=True)
def test_errors_of_remove_delete_export_and_import(tree):
@ -278,10 +278,10 @@ def test_errors_of_remove_delete_export_and_import(tree):
with pytest.raises(NotFound):
o.add('missing.mp3', '#x')
with pytest.raises(NotFound):
o.load('missing.txt')
o.import_file('missing.txt')
(tree / 'bad.txt').write_text('no separator on this line\n')
with pytest.raises(FormatError):
o.load('bad.txt')
o.import_file('bad.txt')
o.delete('a.mp3')
with pytest.raises(NoEntry):
o.export('context.txt')