diff --git a/blogtopoid/blogtopoid.py b/blogtopoid/blogtopoid.py index 6669b62..aed3e67 100644 --- a/blogtopoid/blogtopoid.py +++ b/blogtopoid/blogtopoid.py @@ -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: diff --git a/blogtopoid/test/test_blogtopoid.py b/blogtopoid/test/test_blogtopoid.py index 31ab324..a59df91 100644 --- a/blogtopoid/test/test_blogtopoid.py +++ b/blogtopoid/test/test_blogtopoid.py @@ -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')