repo-cloner/tests/lib/test_disk_stored_refs.py
Václav Valíček 5ca24960c3
Detector: almost complete
Signed-off-by: Václav Valíček <valicek1994@gmail.com>
2022-08-03 16:42:29 +02:00

86 lines
2.1 KiB
Python

import pytest
from repo_cloner.lib import DiskStoredRefs
def test_init(tmp_path):
file = tmp_path.joinpath("refs")
# nonexistent file
refs = DiskStoredRefs(file)
assert refs._DiskStoredRefs__file == file
assert refs._DiskStoredRefs__refs == {}
file.write_text('[["a", "b"], ["c", "d"]]')
refs = DiskStoredRefs(file)
assert refs._DiskStoredRefs__refs == {'a': 'b', 'c': 'd'}
def test__persist(tmp_path):
file = tmp_path.joinpath("refs")
refs = DiskStoredRefs(file)
refs._DiskStoredRefs__refs = {"key": "value"}
refs._DiskStoredRefs__persist()
assert file.read_text() == '[["key", "value"]]'
def test_keys(tmp_path):
file = tmp_path.joinpath("refs")
refs = DiskStoredRefs(file)
refs._DiskStoredRefs__refs = {"key": "value", "key2": "value"}
assert refs.keys() == ["key", "key2"]
def test_count(tmp_path):
file = tmp_path.joinpath("refs")
refs = DiskStoredRefs(file)
assert refs.count() == 0
refs._DiskStoredRefs__refs = {"key": "value", "key2": "value"}
assert refs.count() == 2
def test__len__(tmp_path):
file = tmp_path.joinpath("refs")
refs = DiskStoredRefs(file)
assert len(refs) == 0
refs._DiskStoredRefs__refs = {"key": "value", "key2": "value"}
assert len(refs) == 2
def test_get(tmp_path):
file = tmp_path.joinpath("refs")
refs = DiskStoredRefs(file)
refs._DiskStoredRefs__refs = {"key": "value", "key2": "value2"}
assert refs.get("key") == "value"
assert refs.get("key2") == "value2"
assert refs.get("wut") is None
def test_remove(tmp_path):
file = tmp_path.joinpath("refs")
refs = DiskStoredRefs(file)
refs._DiskStoredRefs__refs = {"key": "value", "key2": "value2"}
assert refs.remove("key")
assert refs._DiskStoredRefs__refs == {"key2": "value2"}
assert not refs.remove("key33")
assert file.read_text() == '[["key2", "value2"]]'
def test_update(tmp_path):
file = tmp_path.joinpath("refs")
refs = DiskStoredRefs(file)
refs._DiskStoredRefs__refs = {"key": "value", "key2": "value2"}
refs.update("key", "val")
assert refs._DiskStoredRefs__refs == {"key": "val", "key2": "value2"}
refs.update("key3", "valvalval")
assert file.read_text() == '[["key", "val"], ["key2", "value2"], ["key3", "valvalval"]]'