Beispiel #1
0
def test_storage():
    """
    Test PDStorage class
    """
    temp = tempfile.mkdtemp()
    filename = os.path.join(temp, "storage")

    storage = PDStorage(filename, MagicMock(), None)

    # Constructor should have called this start function.
    assert storage.repeater.start.called

    # PDStorage needs to be subclassed; the base class always returns not
    # saveable.
    assert storage.attrSaveable() is False
    
    storage = TestStorage(filename)
    data = {"key": "value"}

    with open(filename, "w") as output:
        output.write("BAD CONTENTS")

    # The first attempt to read it will fail and try to delete the file.  We
    # will cause the unlink to fail on the first try and let it succeed on the
    # second try.
    with patch("paradrop.lib.utils.pdos.unlink", side_effect=Exception("Boom!")): 
        storage.loadFromDisk()
        assert os.path.exists(filename)
    storage.loadFromDisk()
    assert not os.path.exists(filename)

    # The first write will fail because we have not provided data yet.
    storage.saveToDisk()
    assert not os.path.exists(filename)

    # Now we will save some data and verify that we can reload it.
    storage.setAttr(data)

    # Cause the save to fail on the first try, then let it succeed.
    with patch("paradrop.lib.utils.pdos.open", side_effect=Exception("Boom!")):
        storage.saveToDisk()
        assert not os.path.exists(filename)
    storage.saveToDisk()
    assert os.path.exists(filename)

    storage.setAttr(None)
    storage.loadFromDisk()
    assert storage.getAttr() == data

    # Clean up
    pdos.remove(temp)