tests for modules NEED to be in a separate unittest

This commit is contained in:
niphlod
2015-12-30 21:20:24 +01:00
parent 1ca0c9b0c0
commit 13e3adf22d
4 changed files with 45 additions and 19 deletions
+4 -18
View File
@@ -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()
+1
View File
@@ -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 *
+40
View File
@@ -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()
-1
View File
@@ -1 +0,0 @@