2009-06-07 14:26:46 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2006/09/14
|
2010-01-01 20:11:34 +00:00
|
|
|
# Copyright 2010 Hardcoded Software (http://www.hardcoded.net)
|
2009-08-05 08:59:46 +00:00
|
|
|
#
|
2010-09-30 10:17:41 +00:00
|
|
|
# This software is licensed under the "BSD" License as described in the "LICENSE" file,
|
2009-08-05 08:59:46 +00:00
|
|
|
# which should be included with this package. The terms are also available at
|
2010-09-30 10:17:41 +00:00
|
|
|
# http://www.hardcoded.net/licenses/bsd_license
|
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:
|
|
|
|
from ..cache import Cache, colors_to_string, string_to_colors
|
|
|
|
except ImportError:
|
|
|
|
skip("Can't import the cache module, probably hasn't been compiled.")
|
2009-06-07 14:26:46 +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):
|
2011-01-01 16:17:27 +00:00
|
|
|
eq_('',colors_to_string([]))
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def test_single_color(self):
|
2011-01-01 16:17: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)]))
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def test_two_colors(self):
|
2011-01-01 16:17:27 +00:00
|
|
|
eq_('000102030405',colors_to_string([(0,1,2),(3,4,5)]))
|
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):
|
2011-01-01 16:17:27 +00:00
|
|
|
eq_([],string_to_colors(''))
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def test_single_color(self):
|
2011-01-01 16:17: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'))
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def test_two_colors(self):
|
2011-01-01 16:17:27 +00:00
|
|
|
eq_([(10,20,30),(40,50,60)],string_to_colors('0a141e28323c'))
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def test_incomplete_color(self):
|
|
|
|
# don't return anything if it's not a complete color
|
2011-01-01 16:17:27 +00:00
|
|
|
eq_([],string_to_colors('102'))
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
|
2011-01-01 16:17:27 +00:00
|
|
|
class TestCaseCache:
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_empty(self):
|
|
|
|
c = Cache()
|
2011-01-01 16:17:27 +00:00
|
|
|
eq_(0,len(c))
|
|
|
|
with raises(KeyError):
|
|
|
|
c['foo']
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def test_set_then_retrieve_blocks(self):
|
|
|
|
c = Cache()
|
|
|
|
b = [(0,0,0),(1,2,3)]
|
|
|
|
c['foo'] = b
|
2011-01-01 16:17:27 +00:00
|
|
|
eq_(b,c['foo'])
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def test_delitem(self):
|
|
|
|
c = Cache()
|
|
|
|
c['foo'] = ''
|
|
|
|
del c['foo']
|
2011-01-01 16:17:27 +00:00
|
|
|
assert 'foo' not in c
|
|
|
|
with raises(KeyError):
|
|
|
|
del c['foo']
|
2009-06-01 09:55:11 +00:00
|
|
|
|
2011-01-01 16:17:27 +00:00
|
|
|
def test_persistance(self, tmpdir):
|
|
|
|
DBNAME = tmpdir.join('hstest.db')
|
|
|
|
c = Cache(str(DBNAME))
|
2009-06-01 09:55:11 +00:00
|
|
|
c['foo'] = [(1,2,3)]
|
|
|
|
del c
|
2011-01-01 16:17:27 +00:00
|
|
|
c = Cache(str(DBNAME))
|
|
|
|
eq_([(1,2,3)],c['foo'])
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def test_filter(self):
|
|
|
|
c = Cache()
|
|
|
|
c['foo'] = ''
|
|
|
|
c['bar'] = ''
|
|
|
|
c['baz'] = ''
|
|
|
|
c.filter(lambda p:p != 'bar') #only 'bar' is removed
|
2011-01-01 16:17:27 +00:00
|
|
|
eq_(2,len(c))
|
|
|
|
assert 'foo' in c
|
|
|
|
assert 'baz' in c
|
|
|
|
assert 'bar' not in c
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def test_clear(self):
|
|
|
|
c = Cache()
|
|
|
|
c['foo'] = ''
|
|
|
|
c['bar'] = ''
|
|
|
|
c['baz'] = ''
|
|
|
|
c.clear()
|
2011-01-01 16:17:27 +00:00
|
|
|
eq_(0,len(c))
|
|
|
|
assert 'foo' not in c
|
|
|
|
assert 'baz' not in c
|
|
|
|
assert 'bar' not in c
|
|
|
|
|
|
|
|
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.
|
|
|
|
monkeypatch.setattr(logging, 'warning', lambda *args, **kw: None)
|
|
|
|
dbname = str(tmpdir.join('foo.db'))
|
2009-06-01 09:55:11 +00:00
|
|
|
fp = open(dbname, 'w')
|
|
|
|
fp.write('invalid sqlite content')
|
|
|
|
fp.close()
|
|
|
|
c = Cache(dbname) # should not raise a DatabaseError
|
|
|
|
c['foo'] = [(1, 2, 3)]
|
|
|
|
del c
|
|
|
|
c = Cache(dbname)
|
2011-01-01 16:17:27 +00:00
|
|
|
eq_(c['foo'], [(1, 2, 3)])
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def test_by_id(self):
|
|
|
|
# it's possible to use the cache by referring to the files by their row_id
|
|
|
|
c = Cache()
|
|
|
|
b = [(0,0,0),(1,2,3)]
|
|
|
|
c['foo'] = b
|
|
|
|
foo_id = c.get_id('foo')
|
2011-01-01 16:17:27 +00:00
|
|
|
eq_(c[foo_id], b)
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
|
2011-01-01 16:17:27 +00:00
|
|
|
class TestCaseCacheSQLEscape:
|
2009-06-01 09:55:11 +00:00
|
|
|
def test_contains(self):
|
|
|
|
c = Cache()
|
2011-01-01 16:17:27 +00:00
|
|
|
assert "foo'bar" not in c
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def test_getitem(self):
|
|
|
|
c = Cache()
|
2011-01-01 16:17:27 +00:00
|
|
|
with raises(KeyError):
|
|
|
|
c["foo'bar"]
|
2009-06-01 09:55:11 +00:00
|
|
|
|
|
|
|
def test_setitem(self):
|
|
|
|
c = Cache()
|
|
|
|
c["foo'bar"] = []
|
|
|
|
|
|
|
|
def test_delitem(self):
|
|
|
|
c = Cache()
|
|
|
|
c["foo'bar"] = []
|
|
|
|
try:
|
|
|
|
del c["foo'bar"]
|
|
|
|
except KeyError:
|
|
|
|
self.fail()
|
|
|
|
|