Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
 def __init__(self):
     self.ledis = Ledis()
     self.commands = {
         "set": self.ledis.set,
         "get": self.ledis.get,
         "sadd": self.ledis.sadd,
         "srem": self.ledis.srem,
         "smembers": self.ledis.smembers,
         "sinter": self.ledis.sinter,
         "keys": self.ledis.keys,
         "del": self.ledis.delete,
         "expire": self.ledis.expire,
         "ttl": self.ledis.ttl,
         "save": self.ledis.save,
         "restore": self.ledis.restore,
     }
Ejemplo n.º 4
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
Ejemplo n.º 5
0
def test_expire_key_not_exist():
    ledis = Ledis()
    assert ledis.expire("key_not_found", 1) == -1
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
def test_srem_to_str():
    ledis = Ledis()
    ledis.set("hello", "world")

    with pytest.raises(InvalidType):
        ledis.srem("hello", {2, 3, 4})
Ejemplo n.º 8
0
def test_srem_not_exist():
    ledis = Ledis()
    ledis.srem("set_type", *{1, 2, 3})
Ejemplo n.º 9
0
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")
Ejemplo n.º 10
0
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")
Ejemplo n.º 11
0
def test_keys():
    ledis = Ledis()
    ledis.set("str_type", "hello")
    ledis.sadd("set_type", 1, 2, 3)

    assert ledis.keys() == ["str_type", "set_type"]
Ejemplo n.º 12
0
def test_keys_empty():
    ledis = Ledis()
    assert ledis.keys() == []
Ejemplo n.º 13
0
def test_delete_str():
    ledis = Ledis()
    ledis.set("str_type", "hello")
    ledis.delete("str_type")
    assert "str_type" not in ledis.storage
Ejemplo n.º 14
0
def test_delete_non_exist():
    ledis = Ledis()
    ledis.delete("key_not_found")
Ejemplo n.º 15
0
def test_delete_set():
    ledis = Ledis()
    ledis.sadd("set_type", 1, 2, 3)
    ledis.delete("set_type")
    assert "set_type" not in ledis.storage
Ejemplo n.º 16
0
def test_ledis_init():
    Ledis()
Ejemplo n.º 17
0
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"))
Ejemplo n.º 18
0
def test_sadd_exist():
    ledis = Ledis()
    ledis.sadd("set_type", 1, 2, 3)
    ledis.sadd("set_type", 2, 3, 4)
    assert ledis.storage["set_type"] == Set({1, 2, 3, 4})
Ejemplo n.º 19
0
import pytest

from ledis import Ledis
from ledis.exceptions import InvalidType
from ledis.datastructures import String

ledis = Ledis()


def test_set():
    assert ledis.set("hello", "world") is None
    assert "hello" in ledis.storage
    assert ledis.storage["hello"] == String("world")


def test_set_using_set_type():
    with pytest.raises(InvalidType):
        ledis.set("set_type", {1, 2, 3})
Ejemplo n.º 20
0
def test_sadd_str_value():
    ledis = Ledis()
    ledis.sadd("hello", "world")

    assert ledis.storage["hello"] == Set({"world"})
Ejemplo n.º 21
0
def test_sadd_empty():
    ledis = Ledis()
    ledis.sadd("set_type", 1, 2, 3)
    assert ledis.storage["set_type"] == Set({1, 2, 3})
Ejemplo n.º 22
0
def test_restore_without_snapshot():
    ledis = Ledis()
    with pytest.raises(NoSnapshotFound):
        ledis.restore("snapshot_not_found")
Ejemplo n.º 23
0
def test_srem_str_value():
    ledis = Ledis()
    ledis.sadd("hello", 1, 2, 3)
    ledis.srem("hello", "world")

    assert ledis.storage["hello"] == Set({1, 2, 3})
Ejemplo n.º 24
0
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]
Ejemplo n.º 25
0
def test_srem():
    ledis = Ledis()
    ledis.sadd("set_type", *{1, 2, 3})

    ledis.srem("set_type", *{2, 3})
    assert ledis.storage["set_type"].data == {1}
Ejemplo n.º 26
0
def test_ttl_key_not_exist():
    ledis = Ledis()
    assert ledis.ttl("str_type") == -2