From 13e3adf22dd2f7eb479959c746314b0de656b4a9 Mon Sep 17 00:00:00 2001 From: niphlod Date: Wed, 30 Dec 2015 17:02:35 +0100 Subject: [PATCH] tests for modules NEED to be in a separate unittest --- gluon/recfile.py | 22 ++++---------------- gluon/tests/__init__.py | 1 + gluon/tests/test_recfile.py | 40 ++++++++++++++++++++++++++++++++++++ gluon/tests/tests_markmin.py | 1 - 4 files changed, 45 insertions(+), 19 deletions(-) create mode 100644 gluon/tests/test_recfile.py delete mode 100644 gluon/tests/tests_markmin.py diff --git a/gluon/recfile.py b/gluon/recfile.py index 180466e3..d5d772e5 100755 --- a/gluon/recfile.py +++ b/gluon/recfile.py @@ -9,7 +9,7 @@ Generates names for cache and session files -------------------------------------------- """ -import os, uuid +import os def generate(filename, depth=2, base=512): @@ -17,10 +17,10 @@ def generate(filename, depth=2, base=512): path, filename = os.path.split(filename) else: path = None - dummyhash = sum(ord(c)*256**(i % 4) for i, c in enumerate(filename)) % base**depth + dummyhash = sum(ord(c) * 256 ** (i % 4) for i, c in enumerate(filename)) % base ** depth folders = [] - for level in range(depth-1, -1, -1): - code, dummyhash = divmod(dummyhash, base**level) + for level in range(depth - 1, -1, -1): + code, dummyhash = divmod(dummyhash, base ** level) folders.append("%03x" % code) folders.append(filename) if path: @@ -63,17 +63,3 @@ def open(filename, mode="r", path=None): if mode.startswith('w') and not os.path.exists(os.path.dirname(fullfilename)): os.makedirs(os.path.dirname(fullfilename)) return file(fullfilename, mode) - - -def test(): - if not os.path.exists('tests'): - os.mkdir('tests') - for k in range(20): - filename = os.path.join('tests', str(uuid.uuid4()) + '.test') - open(filename, "w").write('test') - assert open(filename, "r").read() == 'test' - if exists(filename): - remove(filename) - -if __name__ == '__main__': - test() diff --git a/gluon/tests/__init__.py b/gluon/tests/__init__.py index 64a104d3..23958061 100644 --- a/gluon/tests/__init__.py +++ b/gluon/tests/__init__.py @@ -9,6 +9,7 @@ from test_html import * from test_is_url import * from test_languages import * from test_router import * +from test_recfile import * from test_routes import * from test_storage import * from test_serializers import * diff --git a/gluon/tests/test_recfile.py b/gluon/tests/test_recfile.py new file mode 100644 index 00000000..86f6e917 --- /dev/null +++ b/gluon/tests/test_recfile.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" + Unit tests for gluon.recfile +""" +import unittest +import os +import shutil +import uuid +from fix_path import fix_sys_path + +fix_sys_path(__file__) + +from gluon import recfile + + +class TestRecfile(unittest.TestCase): + + def setUp(self): + os.mkdir('tests') + + def tearDown(self): + shutil.rmtree('tests') + + def testgeneration(self): + for k in range(20): + teststring = 'test%s' % k + filename = os.path.join('tests', str(uuid.uuid4()) + '.test') + with recfile.open(filename, "w") as g: + g.write(teststring) + self.assertEqual(recfile.open(filename, "r").read(), teststring) + is_there = recfile.exists(filename) + self.assertTrue(is_there) + recfile.remove(filename) + is_there = recfile.exists(filename) + self.assertFalse(is_there) + +if __name__ == '__main__': + unittest.main() diff --git a/gluon/tests/tests_markmin.py b/gluon/tests/tests_markmin.py deleted file mode 100644 index 8b137891..00000000 --- a/gluon/tests/tests_markmin.py +++ /dev/null @@ -1 +0,0 @@ -