repo-cloner/tests/lib/test_disk_stored_list.py

84 lines
2.0 KiB
Python
Raw Normal View History

import pytest
from repo_cloner.lib.disk_stored_list import DiskStoredList
@pytest.fixture
def tmp_test_file(tmp_path):
test_file = tmp_path.joinpath("list")
test_file.write_text(
"Ahoj\n"
"Zkouška\n"
"Item1\n"
"Item2\n"
"\n")
return test_file
def test_init_no_file_no_dir(tmp_path):
with pytest.raises(NotADirectoryError) as e:
pass
DiskStoredList(tmp_path.joinpath("nonexistent_dir", "nonexistent_file").as_posix())
assert "nonexistent_dir/nonexistent_file does not exist!" in e.exconly()
def test_init_dir_exists_no_file(tmp_path):
x = DiskStoredList(tmp_path.joinpath("nonexistent_file").as_posix())
assert isinstance(x, DiskStoredList)
def test_load_from_file(tmp_path):
file = tmp_path.joinpath("list")
file.write_text("line1\nline2\nline3\n")
x = DiskStoredList(file.as_posix())
assert len(x._DiskStoredList__items) == 3
assert x._DiskStoredList__items == [
'line1',
'line2',
'line3',
]
def test_append_empty(tmp_path):
empty = tmp_path.joinpath("empty")
x = DiskStoredList(empty.as_posix())
x.append("test_line")
assert empty.read_text() == "test_line\n"
assert "test_line" in x._DiskStoredList__items
assert len(x._DiskStoredList__items) == 1
def test_append_utilized(tmp_test_file):
x = DiskStoredList(tmp_test_file.as_posix())
x.append("Item3")
assert x._DiskStoredList__items == ['Ahoj', 'Zkouška', 'Item1', 'Item2', 'Item3']
def test_count(tmp_test_file):
x = DiskStoredList(tmp_test_file.as_posix())
assert x.count() == 4
x.append("WTH")
assert x.count() == 5
def test__contains__(tmp_test_file):
x = DiskStoredList(tmp_test_file.parent.joinpath("a"))
x.append("WTF")
assert "WTF" in x
assert "Line2" not in x
y = DiskStoredList(tmp_test_file.as_posix())
assert "Item2" in y
assert "Ahoj" in y
def test__iter__(tmp_test_file):
result = []
for x in DiskStoredList(tmp_test_file.as_posix()):
result.append(x)
assert result == ['Ahoj', 'Zkouška', 'Item1', 'Item2']
def test__len__(tmp_test_file):
assert len(DiskStoredList(tmp_test_file.as_posix())) == 4