test Hashstore.

This commit is contained in:
chris 2014-02-09 21:46:45 +01:00
parent b7d26d1d3d
commit c64b50aba8
2 changed files with 50 additions and 1 deletions

View file

@ -249,7 +249,7 @@ class Hashstore(object):
:param blocksize: read file in chunks of blocksize
:return: sha256 hexdigest
"""
with open(filename, 'r') as afile:
with open(filename, 'rb') as afile:
hasher = hashlib.sha256()
buf = afile.read(blocksize)
while len(buf) > 0:

View file

@ -1,6 +1,8 @@
from __future__ import unicode_literals
from __future__ import absolute_import
import os
import codecs
import hashlib
import unittest
@ -17,3 +19,50 @@ class TestTag(unittest.TestCase):
self.assertEqual(self.tag1.colour, colour)
self.assertEqual(self.tag2.colour, 'f32af7')
class TestHashstore(unittest.TestCase):
def setUp(self):
self.hashstore = blogtopoid.Hashstore('testfile.json')
def tearDown(self):
try:
os.remove('testfile.json')
except OSError: # it's ok if the file is not existant
pass
def test_init(self):
# new hashstore should be empty
self.assertEqual(self.hashstore.store, {})
# set something, delete obj, instanciate new hashstore
self.hashstore.set('testkey', 'blah')
del self.hashstore
raised = False
try:
self.hashstore
except AttributeError:
raised = True
self.assertTrue(raised)
self.hashstore = blogtopoid.Hashstore('testfile.json')
# new hashstore needs to have old key loaded
self.assertIn('testkey', self.hashstore.store)
def test_get(self):
self.assertIsNone(self.hashstore.get('not existant'))
def test_set(self):
self.assertFalse(os.path.isfile('testfile.json'))
self.hashstore.set('testkey', 'hashvalue')
self.assertTrue(os.path.isfile('testfile.json'))
self.assertEqual(self.hashstore.get('testkey'), 'hashvalue')
def test_hashfile(self):
with codecs.open('hashme.txt', 'w', 'utf-8') as afile:
afile.write('abc123def456')
self.assertEqual(
'e861b2eab679927cfa36fe256e9deb1969b0468ad0744d61064f9d188333aec6',
self.hashstore.hashfile('hashme.txt')
)
os.remove('hashme.txt')