def test_ttl(): ledis = Ledis() ledis.set("str_type", "hello") assert ledis.storage["str_type"].expire_at is None ledis.expire("str_type", 10) assert ledis.ttl("str_type") == 10
def test_expire_invalid_seconds(): ledis = Ledis() ledis.set("str_type", "hello") with pytest.raises(InvalidValue): ledis.expire("str_type", -1) with pytest.raises(InvalidValue): ledis.expire("str_type", 0)
def test_expire_str(): ledis = Ledis() ledis.set("str_type", "hello") assert ledis.expire("str_type", 1) == 1 sleep(2) # The key will only be lazy-deleted # if the key is called in any operations assert "str_type" in ledis.storage assert ledis.get("str_type") is None assert "str_type" not in ledis.storage
def test_restore(test_snapshot_filename): ledis = Ledis() ledis.set("hello", "world") ledis.sadd("set_type", 1, 2, 3) ledis.save(test_snapshot_filename) # Clear current state ledis.storage = {} assert ledis.get("hello") is None assert ledis.get("set_type") is None # Restore from persistent data ledis.restore(test_snapshot_filename) assert ledis.get("hello") == "world" assert ledis.smembers("set_type") == [1, 2, 3]
def test_srem_to_str(): ledis = Ledis() ledis.set("hello", "world") with pytest.raises(InvalidType): ledis.srem("hello", {2, 3, 4})
import pytest from ledis import Ledis from ledis.exceptions import InvalidType ledis = Ledis() ledis.set("hello", "world") ledis.sadd("set_type", 1, 2, 3) def test_get(): assert ledis.get("hello") == "world" def test_get_key_not_exist(): assert ledis.get("key_not_found") is None def test_get_key_set_type(): with pytest.raises(InvalidType): ledis.get("set_type")
import pytest from ledis import Ledis from ledis.exceptions import InvalidType ledis = Ledis() ledis.sadd("set_type", 1, 2, 3) ledis.set("str_type", "hello") def test_smembers(): assert ledis.smembers("set_type") == [1, 2, 3] def test_smembers_not_exist(): assert ledis.smembers("key_not_found") is None def test_smembers_str_value(): with pytest.raises(InvalidType): ledis.smembers("str_type")
def test_save(test_snapshot_filename): ledis = Ledis() ledis.set("hello", "world") ledis.save(test_snapshot_filename) assert file_exists(path.join(SNAPSHOT_DIR, f"{test_snapshot_filename}.db"))
def test_keys(): ledis = Ledis() ledis.set("str_type", "hello") ledis.sadd("set_type", 1, 2, 3) assert ledis.keys() == ["str_type", "set_type"]
def test_delete_str(): ledis = Ledis() ledis.set("str_type", "hello") ledis.delete("str_type") assert "str_type" not in ledis.storage
def test_ttl_not_set(): ledis = Ledis() ledis.set("str_type", "hello") assert ledis.storage["str_type"].expire_at is None assert ledis.ttl("str_type") == -1