Ejemplo n.º 1
0
    def test_invalidate(self):
        b = MemoryBackend([
            "int_str=1",
            "str_str=2",
            "_SHA_CKSUM=fd58b7962408a4956bd694d617a1201306b363c2",
        ])
        pd = persistent.PersistentDict(b)
        dv = persistent.DictValidator(pd, self.VALID_FIELDS)

        # Trigger reading from storage.
        assert dv["int_str"] == 1

        # Storage contents changed from another host...
        b.lines = [
            "int_str=1",
            "str_str=2",
            "func_func=3",
            "_SHA_CKSUM=5e5ad85614c502d9a2f44d0473b9384ac49eedff",
        ]

        # Return value read before.
        assert dv["str_str"] == "2"
        assert "func_func" not in dv

        # Invalidating the dict will cause the next get to read again from
        # storage.
        dv.invalidate()

        assert dv["int_str"] == 1
        assert dv["str_str"] == "2"
        assert dv["func_func"] == "3"
Ejemplo n.º 2
0
    def test_set(self):
        pd = persistent.PersistentDict(MemoryBackend())
        dv = persistent.DictValidator(pd, self.VALID_FIELDS)
        dv["str_str"] = "value 1"

        # test item was created and underlying dict has same value
        assert pd["str_str"] == "value 1"
        assert dv["str_str"] == "value 1"
Ejemplo n.º 3
0
    def test_contains(self):
        pd = persistent.PersistentDict(MemoryBackend())
        dv = persistent.DictValidator(pd, self.VALID_FIELDS)
        dv["str_str"] = "value 1"

        # test contains operation
        assert "str_str" in dv
        assert "int_str" not in dv
        assert "not-exists" not in dv
Ejemplo n.º 4
0
    def test_clear(self):
        pd = persistent.PersistentDict(MemoryBackend())
        dv = persistent.DictValidator(pd, self.VALID_FIELDS)
        dv["int_str"] = 1
        dv["str_str"] = "2"

        dv.clear()

        assert not pd
        assert not dv
Ejemplo n.º 5
0
    def test_key_iteration(self):
        pd = persistent.PersistentDict(MemoryBackend())
        dv = persistent.DictValidator(pd, self.VALID_FIELDS)
        dv["int_str"] = 1
        dv["str_str"] = "2"
        dv["func_func"] = "3"
        expected = {"int_str", "str_str", "func_func"}

        assert set(dv.iterkeys()) == expected
        assert set(dv) == expected
        assert set(dv.keys()) == expected
Ejemplo n.º 6
0
    def test_length(self):
        pd = persistent.PersistentDict(MemoryBackend())
        dv = persistent.DictValidator(pd, self.VALID_FIELDS)

        assert len(dv) == 0

        dv["str_str"] = "value 1"
        assert len(pd) == 1
        assert len(dv) == 1

        dv["func_func"] = "value 2"
        assert len(pd) == 2
        assert len(dv) == 2
Ejemplo n.º 7
0
    def test_persistent_transaction(self):
        b = MemoryBackend()
        pd = persistent.PersistentDict(b)
        dv = persistent.DictValidator(pd, self.VALID_FIELDS)

        # Transaction flushes lines to storage once.
        with dv.transaction():
            dv["str_str"] = "value 1"
            dv["func_func"] = "value 2"

        assert dv["str_str"] == "value 1"
        assert dv["func_func"] == "value 2"
        assert b.version == 1
Ejemplo n.º 8
0
    def test_copy_with_invalid_items(self):
        pd = persistent.PersistentDict(MemoryBackend())
        dv = persistent.DictValidator(pd, self.VALID_FIELDS)
        dv_expected = {"int_str": 1, "str_str": "2", "func_func": "3"}
        dv.update(dv_expected)

        dv._dict["invalid_key"] = "invalid value"
        dv_expected.update({"invalid_key": "invalid value"})

        dv_copy = dv.copy()

        # Expected behaviour is that even invalid items are still kept in the
        # dict. This is used e.g. in spbackends.py
        assert dv_copy == dv_expected
Ejemplo n.º 9
0
    def test_read(self):
        pd = persistent.PersistentDict(MemoryBackend())
        dv = persistent.DictValidator(pd, self.VALID_FIELDS)
        dv["str_str"] = "value 1"

        # test item read
        assert dv.get("str_str") == "value 1"
        assert dv["str_str"] == "value 1"

        # test read item which doesn't exists
        assert dv.get("int_str") is None

        # test read item which is not allowed
        with pytest.raises(KeyError):
            dv.get("not-exists")
Ejemplo n.º 10
0
    def test_delete(self):
        pd = persistent.PersistentDict(MemoryBackend())
        dv = persistent.DictValidator(pd, self.VALID_FIELDS)
        dv["str_str"] = "value 1"
        dv["func_func"] = "value 2"

        del dv["func_func"]

        # test second item was removed
        assert "func_func" not in pd
        assert "func_func" not in dv

        # and first one is still there
        assert "str_str" in pd
        assert "str_str" in dv
Ejemplo n.º 11
0
    def test_update(self):
        pd = persistent.PersistentDict(MemoryBackend())
        dv = persistent.DictValidator(pd, self.VALID_FIELDS)
        dv["int_str"] = 1
        dv["str_str"] = "2"
        dv["func_func"] = "old value"

        updates = {"int_str": 4, "str_str": "5", "func_func": "new value"}
        dv.update(updates)

        assert pd["int_str"] == "4"
        assert dv["int_str"] == 4
        assert pd["str_str"] == "5"
        assert dv["str_str"] == "5"
        assert pd["func_func"] == "NEW VALUE"
        assert dv["func_func"] == "new value"
Ejemplo n.º 12
0
    def test_copy(self):
        pd = persistent.PersistentDict(MemoryBackend())
        dv = persistent.DictValidator(pd, self.VALID_FIELDS)
        dv_expected = {"int_str": 1, "str_str": "2", "func_func": "3"}
        dv.update(dv_expected)

        dv_copy = dv.copy()

        assert dv_copy == dv_expected

        # update the copy
        dv_copy["int_str"] = 4
        dv_copy["str_str"] = "5"
        dv_copy["func_func"] = "6"

        # and check the original dict is intact
        assert dv.copy() == dv_expected
Ejemplo n.º 13
0
 def test_decode(self):
     pd = persistent.PersistentDict(MemoryBackend())
     dv = persistent.DictValidator(pd, self.VALID_FIELDS)
     pd["func_func"] = "VALUE"
     assert dv["func_func"] == "value"