Merge pull request #504 from niphlod/fix/storage_and_tests

fix for StorageList and tests added
This commit is contained in:
mdipierro
2014-09-24 13:34:20 -05:00
19 changed files with 218 additions and 441 deletions
+2 -2
View File
@@ -151,10 +151,10 @@ class StorageList(Storage):
def __getattr__(self, key): def __getattr__(self, key):
if key in self: if key in self:
return getattr(self, key) return self.get(key)
else: else:
r = [] r = []
setattr(self, key, r) self[key] = r
return r return r
+11 -8
View File
@@ -2,14 +2,8 @@ import os, sys
from test_http import * from test_http import *
from test_cache import * from test_cache import *
from test_contenttype import *
NOSQL = any([name in (os.getenv("DB") or "") from test_fileutils import *
for name in ("datastore", "mongodb", "imap")])
if NOSQL:
from test_dal_nosql import *
else:
from test_dal import *
from test_html import * from test_html import *
from test_is_url import * from test_is_url import *
from test_languages import * from test_languages import *
@@ -25,3 +19,12 @@ from test_web import *
if sys.version[:3] == '2.7': if sys.version[:3] == '2.7':
from test_old_doctests import * from test_old_doctests import *
NOSQL = any([name in (os.getenv("DB") or "")
for name in ("datastore", "mongodb", "imap")])
if NOSQL:
from test_dal_nosql import *
else:
from test_dal import *
+30
View File
@@ -0,0 +1,30 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
def fix_sys_path(current_path):
"""
logic to have always the correct sys.path
'', web2py/gluon, web2py/site-packages, web2py/ ...
"""
def add_path_first(path):
sys.path = [path] + [p for p in sys.path if (
not p == path and not p == (path + '/'))]
path = os.path.dirname(os.path.abspath(current_path))
if not os.path.isfile(os.path.join(path,'web2py.py')):
i = 0
while i<10:
i += 1
if os.path.exists(os.path.join(path,'web2py.py')):
break
path = os.path.abspath(os.path.join(path, '..'))
paths = [path,
os.path.abspath(os.path.join(path, 'site-packages')),
os.path.abspath(os.path.join(path, 'gluon')),
'']
[add_path_first(path) for path in paths]
+2 -30
View File
@@ -4,39 +4,11 @@
""" """
Unit tests for gluon.cache Unit tests for gluon.cache
""" """
import sys
import os import os
import unittest import unittest
from fix_path import fix_sys_path
fix_sys_path(__file__)
def fix_sys_path():
"""
logic to have always the correct sys.path
'', web2py/gluon, web2py/site-packages, web2py/ ...
"""
def add_path_first(path):
sys.path = [path] + [p for p in sys.path if (
not p == path and not p == (path + '/'))]
path = os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(os.path.join(path,'web2py.py')):
i = 0
while i<10:
i += 1
if os.path.exists(os.path.join(path,'web2py.py')):
break
path = os.path.abspath(os.path.join(path, '..'))
paths = [path,
os.path.abspath(os.path.join(path, 'site-packages')),
os.path.abspath(os.path.join(path, 'gluon')),
'']
[add_path_first(path) for path in paths]
fix_sys_path()
from storage import Storage from storage import Storage
+43
View File
@@ -0,0 +1,43 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Unit tests for gluon.contenttype
"""
import unittest
from fix_path import fix_sys_path
fix_sys_path(__file__)
from contenttype import contenttype
class TestContentType(unittest.TestCase):
def testTypeRecognition(self):
rtn = contenttype('.png')
self.assertEqual(rtn, 'image/png')
rtn = contenttype('.gif')
self.assertEqual(rtn, 'image/gif')
rtn = contenttype('.tar.bz2')
self.assertEqual(rtn, 'application/x-bzip-compressed-tar')
# test overrides and additions
mapping = {
'.load': 'text/html; charset=utf-8',
'.json': 'application/json',
'.jsonp': 'application/jsonp',
'.pickle': 'application/python-pickle',
'.w2p': 'application/w2p',
'.md': 'text/x-markdown; charset=utf-8'
}
for k, v in mapping.iteritems():
self.assertEqual(contenttype(k), v)
# test without dot extension
rtn = contenttype('png')
self.assertEqual(rtn, 'text/plain; charset=utf-8')
if __name__ == '__main__':
unittest.main()
+2 -29
View File
@@ -3,37 +3,10 @@
""" Unit tests for contribs """ """ Unit tests for contribs """
import sys
import os
import unittest import unittest
from fix_path import fix_sys_path
def fix_sys_path(): fix_sys_path(__file__)
"""
logic to have always the correct sys.path
'', web2py/gluon, web2py/site-packages, web2py/ ...
"""
def add_path_first(path):
sys.path = [path] + [p for p in sys.path if (
not p == path and not p == (path + '/'))]
path = os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(os.path.join(path,'web2py.py')):
i = 0
while i<10:
i += 1
if os.path.exists(os.path.join(path,'web2py.py')):
break
path = os.path.abspath(os.path.join(path, '..'))
paths = [path,
os.path.abspath(os.path.join(path, 'site-packages')),
os.path.abspath(os.path.join(path, 'gluon')),
'']
[add_path_first(path) for path in paths]
fix_sys_path()
from utils import md5_hash from utils import md5_hash
+5 -30
View File
@@ -15,40 +15,17 @@ try:
except: except:
from io import StringIO from io import StringIO
def fix_sys_path(): from fix_path import fix_sys_path
"""
logic to have always the correct sys.path
'', web2py/gluon, web2py/site-packages, web2py/ ...
"""
def add_path_first(path): fix_sys_path(__file__)
sys.path = [path] + [p for p in sys.path if (
not p == path and not p == (path + '/'))]
path = os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(os.path.join(path,'web2py.py')):
i = 0
while i<10:
i += 1
if os.path.exists(os.path.join(path,'web2py.py')):
break
path = os.path.abspath(os.path.join(path, '..'))
paths = [path,
os.path.abspath(os.path.join(path, 'site-packages')),
os.path.abspath(os.path.join(path, 'gluon')),
'']
[add_path_first(path) for path in paths]
fix_sys_path()
#for travis-ci #for travis-ci
DEFAULT_URI = os.environ.get('DB', 'sqlite:memory') DEFAULT_URI = os.getenv('DB', 'sqlite:memory')
print 'Testing against %s engine (%s)' % (DEFAULT_URI.partition(':')[0], DEFAULT_URI) print 'Testing against %s engine (%s)' % (DEFAULT_URI.partition(':')[0], DEFAULT_URI)
from dal import DAL, Field, Table, SQLALL from dal import DAL, Field, Table, SQLALL
from gluon.cache import CacheInRam
ALLOWED_DATATYPES = [ ALLOWED_DATATYPES = [
'string', 'string',
@@ -564,9 +541,8 @@ class TestMinMaxSumAvg(unittest.TestCase):
db.tt.drop() db.tt.drop()
class TestSelectCache(unittest.TestCase): class TestCacheSelect(unittest.TestCase):
def testRun(self): def testRun(self):
from cache import CacheInRam
cache = CacheInRam() cache = CacheInRam()
db = DAL(DEFAULT_URI, check_reserved=['all']) db = DAL(DEFAULT_URI, check_reserved=['all'])
db.define_table('tt', Field('aa')) db.define_table('tt', Field('aa'))
@@ -1459,7 +1435,6 @@ class TestQuoting(unittest.TestCase):
db._adapter.types[key]=db._adapter.types[key].replace( db._adapter.types[key]=db._adapter.types[key].replace(
'%(on_delete_action)s','NO ACTION') '%(on_delete_action)s','NO ACTION')
t0 = db.define_table('t0', t0 = db.define_table('t0',
Field('f', 'string')) Field('f', 'string'))
t1 = db.define_table('b', t1 = db.define_table('b',
+2 -27
View File
@@ -19,34 +19,9 @@ try:
except: except:
from io import StringIO from io import StringIO
from fix_path import fix_sys_path
def fix_sys_path(): fix_sys_path(__file__)
"""
logic to have always the correct sys.path
'', web2py/gluon, web2py/site-packages, web2py/ ...
"""
def add_path_first(path):
sys.path = [path] + [p for p in sys.path if (
not p == path and not p == (path + '/'))]
path = os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(os.path.join(path,'web2py.py')):
i = 0
while i<10:
i += 1
if os.path.exists(os.path.join(path,'web2py.py')):
break
path = os.path.abspath(os.path.join(path, '..'))
paths = [path,
os.path.abspath(os.path.join(path, 'site-packages')),
os.path.abspath(os.path.join(path, 'gluon')),
'']
[add_path_first(path) for path in paths]
fix_sys_path()
#for travis-ci #for travis-ci
DEFAULT_URI = os.environ.get('DB', 'sqlite:memory') DEFAULT_URI = os.environ.get('DB', 'sqlite:memory')
+25
View File
@@ -0,0 +1,25 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
import datetime
from fix_path import fix_sys_path
fix_sys_path(__file__)
from fileutils import parse_version
class TestFileUtils(unittest.TestCase):
def testParseVersion(self):
rtn = parse_version('Version 1.99.0-rc.1+timestamp.2011.09.19.08.23.26')
self.assertEqual(rtn, (1, 99, 0, 'rc.1', datetime.datetime(2011, 9, 19, 8, 23, 26)))
rtn = parse_version('Version 2.9.11-stable+timestamp.2014.09.15.18.31.17')
self.assertEqual(rtn, (2, 9, 11, 'stable', datetime.datetime(2014, 9, 15, 18, 31, 17)))
rtn = parse_version('Version 1.99.0 (2011-09-19 08:23:26)')
self.assertEqual(rtn, (1, 99, 0, 'dev', datetime.datetime(2011, 9, 19, 8, 23, 26)))
if __name__ == '__main__':
unittest.main()
+2 -29
View File
@@ -5,37 +5,10 @@
Unit tests for gluon.html Unit tests for gluon.html
""" """
import sys
import os
import unittest import unittest
from fix_path import fix_sys_path
def fix_sys_path(): fix_sys_path(__file__)
"""
logic to have always the correct sys.path
'', web2py/gluon, web2py/site-packages, web2py/ ...
"""
def add_path_first(path):
sys.path = [path] + [p for p in sys.path if (
not p == path and not p == (path + '/'))]
path = os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(os.path.join(path,'web2py.py')):
i = 0
while i<10:
i += 1
if os.path.exists(os.path.join(path,'web2py.py')):
break
path = os.path.abspath(os.path.join(path, '..'))
paths = [path,
os.path.abspath(os.path.join(path, 'site-packages')),
os.path.abspath(os.path.join(path, 'gluon')),
'']
[add_path_first(path) for path in paths]
fix_sys_path()
from html import * from html import *
from storage import Storage from storage import Storage
+2 -33
View File
@@ -3,38 +3,10 @@
"""Unit tests for http.py """ """Unit tests for http.py """
import sys
import os
import unittest import unittest
from fix_path import fix_sys_path
fix_sys_path(__file__)
def fix_sys_path():
"""
logic to have always the correct sys.path
'', web2py/gluon, web2py/site-packages, web2py/ ...
"""
def add_path_first(path):
sys.path = [path] + [p for p in sys.path if (
not p == path and not p == (path + '/'))]
path = os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(os.path.join(path,'web2py.py')):
i = 0
while i<10:
i += 1
if os.path.exists(os.path.join(path,'web2py.py')):
break
path = os.path.abspath(os.path.join(path, '..'))
paths = [path,
os.path.abspath(os.path.join(path, 'site-packages')),
os.path.abspath(os.path.join(path, 'gluon')),
'']
[add_path_first(path) for path in paths]
fix_sys_path()
from http import HTTP, defined_status from http import HTTP, defined_status
@@ -68,8 +40,5 @@ class TestHTTP(unittest.TestCase):
# test wrong call detection # test wrong call detection
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
+5 -33
View File
@@ -4,42 +4,14 @@
Unit tests for IS_URL() Unit tests for IS_URL()
""" """
import sys
import os
import unittest import unittest
from fix_path import fix_sys_path
fix_sys_path(__file__)
def fix_sys_path(): from validators import IS_URL, IS_HTTP_URL, IS_GENERIC_URL
""" from validators import unicode_to_ascii_authority
logic to have always the correct sys.path
'', web2py/gluon, web2py/site-packages, web2py/ ...
"""
def add_path_first(path):
sys.path = [path] + [p for p in sys.path if (
not p == path and not p == (path + '/'))]
path = os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(os.path.join(path,'web2py.py')):
i = 0
while i<10:
i += 1
if os.path.exists(os.path.join(path,'web2py.py')):
break
path = os.path.abspath(os.path.join(path, '..'))
paths = [path,
os.path.abspath(os.path.join(path, 'site-packages')),
os.path.abspath(os.path.join(path, 'gluon')),
'']
[add_path_first(path) for path in paths]
fix_sys_path()
from validators import IS_URL, IS_HTTP_URL, IS_GENERIC_URL, \
unicode_to_ascii_authority
class TestIsUrl(unittest.TestCase): class TestIsUrl(unittest.TestCase):
+5 -36
View File
@@ -7,38 +7,11 @@
import sys import sys
import os import os
import unittest
import tempfile import tempfile
import threading import unittest
import logging from fix_path import fix_sys_path
def fix_sys_path(): fix_sys_path(__file__)
"""
logic to have always the correct sys.path
'', web2py/gluon, web2py/site-packages, web2py/ ...
"""
def add_path_first(path):
sys.path = [path] + [p for p in sys.path if (
not p == path and not p == (path + '/'))]
path = os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(os.path.join(path,'web2py.py')):
i = 0
while i<10:
i += 1
if os.path.exists(os.path.join(path,'web2py.py')):
break
path = os.path.abspath(os.path.join(path, '..'))
paths = [path,
os.path.abspath(os.path.join(path, 'site-packages')),
os.path.abspath(os.path.join(path, 'gluon')),
'']
[add_path_first(path) for path in paths]
fix_sys_path()
#support skipif also in python 2.6 #support skipif also in python 2.6
def _skipIf(cond, message=''): def _skipIf(cond, message=''):
@@ -55,7 +28,6 @@ else:
skipIf = _skipIf skipIf = _skipIf
import languages import languages
from storage import Storage
MP_WORKING = 0 MP_WORKING = 0
try: try:
import multiprocessing import multiprocessing
@@ -63,10 +35,6 @@ try:
#due to http://bugs.python.org/issue10845, testing multiprocessing in python is impossible #due to http://bugs.python.org/issue10845, testing multiprocessing in python is impossible
if sys.platform.startswith('win'): if sys.platform.startswith('win'):
MP_WORKING = 0 MP_WORKING = 0
#multiprocessing is also not available on GAE. Since tests randomly
#fail, let's not make them on it too
if 'datastore' in os.getenv('DB', ''):
MP_WORKING = 0
except ImportError: except ImportError:
pass pass
@@ -80,6 +48,7 @@ def read_write(args):
languages.write_dict(filename, content) languages.write_dict(filename, content)
return True return True
class TestLanguagesParallel(unittest.TestCase): class TestLanguagesParallel(unittest.TestCase):
def setUp(self): def setUp(self):
@@ -112,6 +81,7 @@ class TestLanguagesParallel(unittest.TestCase):
for result in results: for result in results:
self.assertTrue(result) self.assertTrue(result)
class TestTranslations(unittest.TestCase): class TestTranslations(unittest.TestCase):
def setUp(self): def setUp(self):
@@ -151,4 +121,3 @@ class TestTranslations(unittest.TestCase):
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
+3 -29
View File
@@ -7,41 +7,15 @@
""" """
import sys import sys
import os import os
import unittest
import doctest import doctest
import unittest
from fix_path import fix_sys_path
fix_sys_path(__file__)
def fix_sys_path():
"""
logic to have always the correct sys.path
'', web2py/gluon, web2py/site-packages, web2py/ ...
"""
def add_path_first(path):
sys.path = [path] + [p for p in sys.path if (
not p == path and not p == (path + '/'))]
path = os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(os.path.join(path,'web2py.py')):
i = 0
while i<10:
i += 1
if os.path.exists(os.path.join(path,'web2py.py')):
break
path = os.path.abspath(os.path.join(path, '..'))
paths = [path,
os.path.abspath(os.path.join(path, 'site-packages')),
os.path.abspath(os.path.join(path, 'gluon')),
'']
[add_path_first(path) for path in paths]
fix_sys_path()
def load_tests(loader, tests, ignore): def load_tests(loader, tests, ignore):
tests.addTests( tests.addTests(
doctest.DocTestSuite('html') doctest.DocTestSuite('html')
) )
+62 -31
View File
@@ -3,40 +3,13 @@
""" Unit tests for storage.py """ """ Unit tests for storage.py """
import sys
import os
import unittest import unittest
from fix_path import fix_sys_path
fix_sys_path(__file__)
def fix_sys_path(): from storage import Storage, StorageList, List
""" from http import HTTP
logic to have always the correct sys.path
'', web2py/gluon, web2py/site-packages, web2py/ ...
"""
def add_path_first(path):
sys.path = [path] + [p for p in sys.path if (
not p == path and not p == (path + '/'))]
path = os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(os.path.join(path,'web2py.py')):
i = 0
while i<10:
i += 1
if os.path.exists(os.path.join(path,'web2py.py')):
break
path = os.path.abspath(os.path.join(path, '..'))
paths = [path,
os.path.abspath(os.path.join(path, 'site-packages')),
os.path.abspath(os.path.join(path, 'gluon')),
'']
[add_path_first(path) for path in paths]
fix_sys_path()
from storage import Storage
import pickle import pickle
@@ -105,5 +78,63 @@ class TestStorage(unittest.TestCase):
news = pickle.loads(sd) news = pickle.loads(sd)
self.assertEqual(news.a, 1) self.assertEqual(news.a, 1)
def test_getlist(self):
# usually used with request.vars
a = Storage()
a.x = 'abc'
a.y = ['abc', 'def']
self.assertEqual(a.getlist('x'), ['abc'])
self.assertEqual(a.getlist('y'), ['abc', 'def'])
self.assertEqual(a.getlist('z'), [])
def test_getfirst(self):
# usually with request.vars
a = Storage()
a.x = 'abc'
a.y = ['abc', 'def']
self.assertEqual(a.getfirst('x'), 'abc')
self.assertEqual(a.getfirst('y'), 'abc')
self.assertEqual(a.getfirst('z'), None)
def test_getlast(self):
# usually with request.vars
a = Storage()
a.x = 'abc'
a.y = ['abc', 'def']
self.assertEqual(a.getlast('x'), 'abc')
self.assertEqual(a.getlast('y'), 'def')
self.assertEqual(a.getlast('z'), None)
class TestStorageList(unittest.TestCase):
""" Tests storage.StorageList """
def test_attribute(self):
s = StorageList(a=1)
self.assertEqual(s.a, 1)
self.assertEqual(s['a'], 1)
self.assertEqual(s.b, [])
s.b.append(1)
self.assertEqual(s.b, [1])
class TestList(unittest.TestCase):
""" Tests Storage.List (fast-check for request.args()) """
def test_listcall(self):
a = List((1, 2, 3))
self.assertEqual(a(1), 2)
self.assertEqual(a(-1), 3)
self.assertEqual(a(-5), None)
self.assertEqual(a(-5, default='x'), 'x')
self.assertEqual(a(-3, cast=str), '1')
a.append('1234')
self.assertEqual(a(3), '1234')
self.assertEqual(a(3, cast=int), 1234)
a.append('x')
self.assertRaises(HTTP, a, 4, cast=int)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
+2 -30
View File
@@ -4,38 +4,10 @@
Unit tests for gluon.template Unit tests for gluon.template
""" """
import sys
import os
import unittest import unittest
from fix_path import fix_sys_path
fix_sys_path(__file__)
def fix_sys_path():
"""
logic to have always the correct sys.path
'', web2py/gluon, web2py/site-packages, web2py/ ...
"""
def add_path_first(path):
sys.path = [path] + [p for p in sys.path if (
not p == path and not p == (path + '/'))]
path = os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(os.path.join(path,'web2py.py')):
i = 0
while i<10:
i += 1
if os.path.exists(os.path.join(path,'web2py.py')):
break
path = os.path.abspath(os.path.join(path, '..'))
paths = [path,
os.path.abspath(os.path.join(path, 'site-packages')),
os.path.abspath(os.path.join(path, 'gluon')),
'']
[add_path_first(path) for path in paths]
fix_sys_path()
from template import render from template import render
+2 -30
View File
@@ -3,38 +3,10 @@
""" Unit tests for utils.py """ """ Unit tests for utils.py """
import sys
import os
import unittest import unittest
from fix_path import fix_sys_path
fix_sys_path(__file__)
def fix_sys_path():
"""
logic to have always the correct sys.path
'', web2py/gluon, web2py/site-packages, web2py/ ...
"""
def add_path_first(path):
sys.path = [path] + [p for p in sys.path if (
not p == path and not p == (path + '/'))]
path = os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(os.path.join(path,'web2py.py')):
i = 0
while i<10:
i += 1
if os.path.exists(os.path.join(path,'web2py.py')):
break
path = os.path.abspath(os.path.join(path, '..'))
paths = [path,
os.path.abspath(os.path.join(path, 'site-packages')),
os.path.abspath(os.path.join(path, 'gluon')),
'']
[add_path_first(path) for path in paths]
fix_sys_path()
from utils import md5_hash from utils import md5_hash
+7 -33
View File
@@ -3,43 +3,17 @@
"""Unit tests for http.py """ """Unit tests for http.py """
import sys
import os
import unittest import unittest
def fix_sys_path():
"""
logic to have always the correct sys.path
'', web2py/gluon, web2py/site-packages, web2py/ ...
"""
def add_path_first(path):
sys.path = [path] + [p for p in sys.path if (
not p == path and not p == (path + '/'))]
path = os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(os.path.join(path,'web2py.py')):
i = 0
while i<10:
i += 1
if os.path.exists(os.path.join(path,'web2py.py')):
break
path = os.path.abspath(os.path.join(path, '..'))
paths = [path,
os.path.abspath(os.path.join(path, 'site-packages')),
os.path.abspath(os.path.join(path, 'gluon')),
'']
[add_path_first(path) for path in paths]
fix_sys_path()
import datetime import datetime
import decimal import decimal
from gluon.validators import *
import re import re
from fix_path import fix_sys_path
fix_sys_path(__file__)
from gluon.validators import *
class TestValidators(unittest.TestCase): class TestValidators(unittest.TestCase):
+2 -27
View File
@@ -13,34 +13,9 @@ import subprocess
import time import time
import signal import signal
from fix_path import fix_sys_path
def fix_sys_path(): fix_sys_path(__file__)
"""
logic to have always the correct sys.path
'', web2py/gluon, web2py/site-packages, web2py/ ...
"""
def add_path_first(path):
sys.path = [path] + [p for p in sys.path if (
not p == path and not p == (path + '/'))]
path = os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(os.path.join(path,'web2py.py')):
i = 0
while i<10:
i += 1
if os.path.exists(os.path.join(path,'web2py.py')):
break
path = os.path.abspath(os.path.join(path, '..'))
paths = [path,
os.path.abspath(os.path.join(path, 'site-packages')),
os.path.abspath(os.path.join(path, 'gluon')),
'']
[add_path_first(path) for path in paths]
fix_sys_path()
from contrib.webclient import WebClient from contrib.webclient import WebClient
from urllib2 import HTTPError from urllib2 import HTTPError