2016-11-13 22:01:20 +00:00
|
|
|
# Copyright 2016 Virgil Dupras
|
2016-05-29 19:02:39 +00:00
|
|
|
#
|
|
|
|
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
|
|
|
|
# which should be included with this package. The terms are also available at
|
2015-01-03 21:33:16 +00:00
|
|
|
# http://www.gnu.org/licenses/gpl-3.0.html
|
2009-06-07 14:26:46 +00:00
|
|
|
|
2011-01-01 16:17:27 +00:00
|
|
|
import logging
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-01-01 16:17:27 +00:00
|
|
|
from pytest import raises, skip
|
2011-01-11 10:59:53 +00:00
|
|
|
from hscommon.testutil import eq_
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-01-01 16:17:27 +00:00
|
|
|
try:
|
2016-11-13 22:01:20 +00:00
|
|
|
from ..pe.cache import colors_to_string, string_to_colors
|
|
|
|
from ..pe.cache_sqlite import SqliteCache
|
2016-11-16 00:58:18 +00:00
|
|
|
from ..pe.cache_shelve import ShelveCache
|
2011-01-01 16:17:27 +00:00
|
|
|
except ImportError:
|
|
|
|
skip("Can't import the cache module, probably hasn't been compiled.")
|
2009-06-07 14:26:46 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2011-01-01 16:17:27 +00:00
|
|
|
class TestCasecolors_to_string:
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_no_color(self):
|
2020-01-01 02:16:27 +00:00
|
|
|
eq_("", colors_to_string([]))
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_single_color(self):
|
2020-01-01 02:16:27 +00:00
|
|
|
eq_("000000", colors_to_string([(0, 0, 0)]))
|
|
|
|
eq_("010101", colors_to_string([(1, 1, 1)]))
|
|
|
|
eq_("0a141e", colors_to_string([(10, 20, 30)]))
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_two_colors(self):
|
2020-01-01 02:16:27 +00:00
|
|
|
eq_("000102030405", colors_to_string([(0, 1, 2), (3, 4, 5)]))
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-01-01 16:17:27 +00:00
|
|
|
class TestCasestring_to_colors:
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_empty(self):
|
2020-01-01 02:16:27 +00:00
|
|
|
eq_([], string_to_colors(""))
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_single_color(self):
|
2020-01-01 02:16:27 +00:00
|
|
|
eq_([(0, 0, 0)], string_to_colors("000000"))
|
|
|
|
eq_([(2, 3, 4)], string_to_colors("020304"))
|
|
|
|
eq_([(10, 20, 30)], string_to_colors("0a141e"))
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_two_colors(self):
|
2020-01-01 02:16:27 +00:00
|
|
|
eq_([(10, 20, 30), (40, 50, 60)], string_to_colors("0a141e28323c"))
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_incomplete_color(self):
|
|
|
|
# don't return anything if it's not a complete color
|
2020-01-01 02:16:27 +00:00
|
|
|
eq_([], string_to_colors("102"))
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2016-11-13 22:01:20 +00:00
|
|
|
class BaseTestCaseCache:
|
|
|
|
def get_cache(self, dbname=None):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_empty(self):
|
2016-11-13 22:01:20 +00:00
|
|
|
c = self.get_cache()
|
2016-05-29 19:02:39 +00:00
|
|
|
eq_(0, len(c))
|
2011-01-01 16:17:27 +00:00
|
|
|
with raises(KeyError):
|
2020-01-01 02:16:27 +00:00
|
|
|
c["foo"]
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_set_then_retrieve_blocks(self):
|
2016-11-13 22:01:20 +00:00
|
|
|
c = self.get_cache()
|
2016-05-29 19:02:39 +00:00
|
|
|
b = [(0, 0, 0), (1, 2, 3)]
|
2020-01-01 02:16:27 +00:00
|
|
|
c["foo"] = b
|
|
|
|
eq_(b, c["foo"])
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_delitem(self):
|
2016-11-13 22:01:20 +00:00
|
|
|
c = self.get_cache()
|
2020-01-01 02:16:27 +00:00
|
|
|
c["foo"] = ""
|
|
|
|
del c["foo"]
|
|
|
|
assert "foo" not in c
|
2011-01-01 16:17:27 +00:00
|
|
|
with raises(KeyError):
|
2020-01-01 02:16:27 +00:00
|
|
|
del c["foo"]
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2011-01-01 16:17:27 +00:00
|
|
|
def test_persistance(self, tmpdir):
|
2020-01-01 02:16:27 +00:00
|
|
|
DBNAME = tmpdir.join("hstest.db")
|
2016-11-13 22:01:20 +00:00
|
|
|
c = self.get_cache(str(DBNAME))
|
2020-01-01 02:16:27 +00:00
|
|
|
c["foo"] = [(1, 2, 3)]
|
2009-06-01 09:55:11 +00:00
|
|
|
del c
|
2016-11-13 22:01:20 +00:00
|
|
|
c = self.get_cache(str(DBNAME))
|
2020-01-01 02:16:27 +00:00
|
|
|
eq_([(1, 2, 3)], c["foo"])
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_filter(self):
|
2016-11-13 22:01:20 +00:00
|
|
|
c = self.get_cache()
|
2020-01-01 02:16:27 +00:00
|
|
|
c["foo"] = ""
|
|
|
|
c["bar"] = ""
|
|
|
|
c["baz"] = ""
|
|
|
|
c.filter(lambda p: p != "bar") # only 'bar' is removed
|
2016-05-29 19:02:39 +00:00
|
|
|
eq_(2, len(c))
|
2020-01-01 02:16:27 +00:00
|
|
|
assert "foo" in c
|
|
|
|
assert "baz" in c
|
|
|
|
assert "bar" not in c
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_clear(self):
|
2016-11-13 22:01:20 +00:00
|
|
|
c = self.get_cache()
|
2020-01-01 02:16:27 +00:00
|
|
|
c["foo"] = ""
|
|
|
|
c["bar"] = ""
|
|
|
|
c["baz"] = ""
|
2009-06-01 09:55:11 +00:00
|
|
|
c.clear()
|
2016-05-29 19:02:39 +00:00
|
|
|
eq_(0, len(c))
|
2020-01-01 02:16:27 +00:00
|
|
|
assert "foo" not in c
|
|
|
|
assert "baz" not in c
|
|
|
|
assert "bar" not in c
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2016-11-13 22:01:20 +00:00
|
|
|
def test_by_id(self):
|
|
|
|
# it's possible to use the cache by referring to the files by their row_id
|
|
|
|
c = self.get_cache()
|
|
|
|
b = [(0, 0, 0), (1, 2, 3)]
|
2020-01-01 02:16:27 +00:00
|
|
|
c["foo"] = b
|
|
|
|
foo_id = c.get_id("foo")
|
2016-11-13 22:01:20 +00:00
|
|
|
eq_(c[foo_id], b)
|
|
|
|
|
|
|
|
|
|
|
|
class TestCaseSqliteCache(BaseTestCaseCache):
|
|
|
|
def get_cache(self, dbname=None):
|
|
|
|
if dbname:
|
|
|
|
return SqliteCache(dbname)
|
|
|
|
else:
|
|
|
|
return SqliteCache()
|
|
|
|
|
2011-01-01 16:17:27 +00:00
|
|
|
def test_corrupted_db(self, tmpdir, monkeypatch):
|
|
|
|
# If we don't do this monkeypatching, we get a weird exception about trying to flush a
|
|
|
|
# closed file. I've tried setting logging level and stuff, but nothing worked. So, there we
|
|
|
|
# go, a dirty monkeypatch.
|
2020-01-01 02:16:27 +00:00
|
|
|
monkeypatch.setattr(logging, "warning", lambda *args, **kw: None)
|
|
|
|
dbname = str(tmpdir.join("foo.db"))
|
|
|
|
fp = open(dbname, "w")
|
|
|
|
fp.write("invalid sqlite content")
|
2009-06-01 09:55:11 +00:00
|
|
|
fp.close()
|
2020-01-01 02:16:27 +00:00
|
|
|
c = self.get_cache(dbname) # should not raise a DatabaseError
|
|
|
|
c["foo"] = [(1, 2, 3)]
|
2009-06-01 09:55:11 +00:00
|
|
|
del c
|
2016-11-13 22:01:20 +00:00
|
|
|
c = self.get_cache(dbname)
|
2020-01-01 02:16:27 +00:00
|
|
|
eq_(c["foo"], [(1, 2, 3)])
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2016-11-16 00:58:18 +00:00
|
|
|
class TestCaseShelveCache(BaseTestCaseCache):
|
|
|
|
def get_cache(self, dbname=None):
|
|
|
|
return ShelveCache(dbname)
|
|
|
|
|
|
|
|
|
2011-01-01 16:17:27 +00:00
|
|
|
class TestCaseCacheSQLEscape:
|
2016-11-13 22:01:20 +00:00
|
|
|
def get_cache(self):
|
|
|
|
return SqliteCache()
|
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_contains(self):
|
2016-11-13 22:01:20 +00:00
|
|
|
c = self.get_cache()
|
2011-01-01 16:17:27 +00:00
|
|
|
assert "foo'bar" not in c
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_getitem(self):
|
2016-11-13 22:01:20 +00:00
|
|
|
c = self.get_cache()
|
2011-01-01 16:17:27 +00:00
|
|
|
with raises(KeyError):
|
|
|
|
c["foo'bar"]
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_setitem(self):
|
2016-11-13 22:01:20 +00:00
|
|
|
c = self.get_cache()
|
2009-06-01 09:55:11 +00:00
|
|
|
c["foo'bar"] = []
|
2016-05-29 19:02:39 +00:00
|
|
|
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_delitem(self):
|
2016-11-13 22:01:20 +00:00
|
|
|
c = self.get_cache()
|
2009-06-01 09:55:11 +00:00
|
|
|
c["foo'bar"] = []
|
|
|
|
try:
|
|
|
|
del c["foo'bar"]
|
|
|
|
except KeyError:
|
2012-08-10 19:58:37 +00:00
|
|
|
assert False
|