DiskStoredList
Signed-off-by: Václav Valíček <valicek1994@gmail.com>
This commit is contained in:
parent
d182371520
commit
4cf4c3ff80
43
repo_cloner/lib/disk_stored_list.py
Normal file
43
repo_cloner/lib/disk_stored_list.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
from typing import Optional
|
||||
import os
|
||||
|
||||
|
||||
class DiskStoredList:
|
||||
__file: Optional[str] = None
|
||||
__items: Optional[list] = None
|
||||
|
||||
def __init__(self, file: str):
|
||||
# file does not have to exist, but parent directory does
|
||||
if not os.path.exists(file):
|
||||
if not os.path.isdir(os.path.dirname(file)):
|
||||
raise NotADirectoryError(f"Parent directory for file {file} does not exist!")
|
||||
|
||||
self.__file = file
|
||||
self.__items = []
|
||||
|
||||
if os.path.exists(file):
|
||||
self._load_from_file()
|
||||
|
||||
def _load_from_file(self):
|
||||
with open(self.__file, 'r') as f:
|
||||
for line in f.readlines():
|
||||
line = line.strip()
|
||||
if len(line):
|
||||
self.__items.append(line)
|
||||
|
||||
def append(self, line: str):
|
||||
with open(self.__file, 'a') as f:
|
||||
self.__items.append(line)
|
||||
f.write(f"{line}\n")
|
||||
|
||||
def count(self):
|
||||
return len(self.__items)
|
||||
|
||||
def __contains__(self, line: str) -> bool:
|
||||
return line in self.__items
|
||||
|
||||
def __iter__(self):
|
||||
return self.__items.__iter__()
|
||||
|
||||
def __len__(self):
|
||||
return len(self.__items)
|
83
tests/lib/test_disk_stored_list.py
Normal file
83
tests/lib/test_disk_stored_list.py
Normal file
|
@ -0,0 +1,83 @@
|
|||
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
|
Loading…
Reference in New Issue
Block a user