def test_shelve(tmpdir):
    """Test if shelve can be caches information
    retrieved after file is deleted"""
    test_string = 'test information'
    tmp_cache = str(tmpdir)
    with tempfile.NamedTemporaryFile('w', delete=False) as f:
        f.write(test_string)
    try:
        # recovers data from temporary file and caches it in the shelve
        file_data = sg.get_data(f.name, tmp_cache)
    finally:
        os.remove(f.name)
    # tests recovered data matches
    assert file_data == test_string

    # test if cached data is available after temporary file has vanished
    assert sg.get_data(f.name, tmp_cache) == test_string

    # shelve keys need to be str in python 2, deal with unicode input
    if sys.version_info[0] == 2:
        assert sg.get_data(f.name, tmp_cache) == test_string
Example #2
0
def test_shelve():
    """Test if shelve can be caches information
    retrieved after file is deleted"""
    test_string = 'test information'
    tmp_cache = tempfile.mkdtemp()
    with tempfile.NamedTemporaryFile('w') as fid:
        fid.write(test_string)
        fid.seek(0)

        # recovers data from temporary file and caches it in the shelve
        file_data = sg.get_data(fid.name, tmp_cache)
        # tests recovered data matches
        assert_equal(file_data, test_string)

    # test if cached data is available after temporary file has vanished
    assert_equal(sg.get_data(fid.name, tmp_cache), test_string)

    # shelve keys need to be str in python 2, deal with unicode input
    if sys.version_info[0] == 2:
        unicode_name = unicode(fid.name)
        assert_equal(sg.get_data(unicode_name, tmp_cache), test_string)
def test_shelve():
    """Test if shelve can be caches information
    retrieved after file is deleted"""
    test_string = 'test information'
    tmp_cache = _TempDir()
    with tempfile.NamedTemporaryFile('w', delete=False) as f:
        f.write(test_string)
    try:
        # recovers data from temporary file and caches it in the shelve
        file_data = sg.get_data(f.name, tmp_cache)
    finally:
        os.remove(f.name)
    # tests recovered data matches
    assert file_data == test_string

    # test if cached data is available after temporary file has vanished
    assert sg.get_data(f.name, tmp_cache) == test_string

    # shelve keys need to be str in python 2, deal with unicode input
    if sys.version_info[0] == 2:
        unicode_name = unicode(f.name)
        assert sg.get_data(unicode_name, tmp_cache) == test_string