Example #1
0
def test_delete_deletes_file_when_last_key() -> None:
    path = Path.cwd() / "config.json"

    storage = Storage(str(path))
    storage.set("key", "value")
    storage.delete("key")

    assert not path.exists()
Example #2
0
def test_set_creates_new_file_when_file_does_not_exist() -> None:
    path = Path.cwd() / "config.json"

    storage = Storage(str(path))
    storage.set("key", "value")

    data = json.loads(path.read_text(encoding="utf-8"))
    assert data == {"key": "value"}
Example #3
0
def test_set_overrides_values_in_existing_file() -> None:
    path = Path.cwd() / "config.json"
    with path.open("w+", encoding="utf-8") as file:
        file.write('{ "key": "value" }')

    storage = Storage(str(path))
    storage.set("key", "new-value")

    data = json.loads(path.read_text(encoding="utf-8"))
    assert data == {"key": "new-value"}
Example #4
0
def test_delete_unsets_value() -> None:
    path = Path.cwd() / "config.json"

    storage = Storage(str(path))
    storage.set("key1", "value1")
    storage.set("key2", "value2")
    storage.set("key3", "value3")
    storage.delete("key2")

    data = json.loads(path.read_text(encoding="utf-8"))
    assert data == {"key1": "value1", "key3": "value3"}