From f5f6f365f9908c1c98cc6a5b7086568befa23be9 Mon Sep 17 00:00:00 2001 From: niphlod Date: Sun, 25 Aug 2013 18:35:29 +0200 Subject: [PATCH] added boilerplate to be able to run tests from anywhere. Added test for validate_and_insert too. --- gluon/tests/test_cache.py | 36 +++++++++++++++--- gluon/tests/test_contribs.py | 33 ++++++++++++++-- gluon/tests/test_dal.py | 65 +++++++++++++++++++++++++++----- gluon/tests/test_html.py | 34 ++++++++++++++--- gluon/tests/test_http.py | 48 +++++++++++++++++------ gluon/tests/test_is_url.py | 36 +++++++++++++++--- gluon/tests/test_languages.py | 37 +++++++++++++++--- gluon/tests/test_old_doctests.py | 40 ++++++++++++++++---- gluon/tests/test_storage.py | 33 ++++++++++++++-- gluon/tests/test_template.py | 35 ++++++++++++++--- gluon/tests/test_utils.py | 33 ++++++++++++++-- gluon/tests/test_web.py | 48 +++++++++++++++++++---- 12 files changed, 405 insertions(+), 73 deletions(-) diff --git a/gluon/tests/test_cache.py b/gluon/tests/test_cache.py index 2ef79103..be50e8ef 100644 --- a/gluon/tests/test_cache.py +++ b/gluon/tests/test_cache.py @@ -7,12 +7,38 @@ import sys import os -if os.path.isdir('gluon'): - sys.path.append(os.path.realpath('gluon')) -else: - sys.path.append(os.path.realpath('../')) - 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() + + from storage import Storage from cache import CacheInRam, CacheOnDisk diff --git a/gluon/tests/test_contribs.py b/gluon/tests/test_contribs.py index 534c04dd..2e8a15e6 100644 --- a/gluon/tests/test_contribs.py +++ b/gluon/tests/test_contribs.py @@ -6,10 +6,35 @@ import sys import os import unittest -if os.path.isdir('gluon'): - sys.path.append(os.path.realpath('gluon')) -else: - sys.path.append(os.path.realpath('../')) + +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 import contrib.fpdf as fpdf diff --git a/gluon/tests/test_dal.py b/gluon/tests/test_dal.py index 8a83282a..664f1d55 100644 --- a/gluon/tests/test_dal.py +++ b/gluon/tests/test_dal.py @@ -8,17 +8,41 @@ import sys import os import glob -if os.path.isdir('gluon'): - sys.path.append(os.path.realpath('gluon')) -else: - sys.path.append(os.path.realpath('../')) - import unittest import datetime try: import cStringIO as StringIO except: from io import StringIO + +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 dal import DAL, Field, Table, SQLALL #for travis-ci @@ -603,7 +627,7 @@ class TestComputedFields(unittest.TestCase): self.assertEqual(db.tt[id].cc,'zx') db.tt.drop() db.commit() - + # test checking that a compute field can refer to earlier-defined computed fields db.define_table('tt', Field('aa'), @@ -612,10 +636,10 @@ class TestComputedFields(unittest.TestCase): Field('dd',compute=lambda r: r.bb + r.cc)) db.commit() id = db.tt.insert(aa="z") - self.assertEqual(db.tt[id].dd,'xzx') + self.assertEqual(db.tt[id].dd,'xzx') db.tt.drop() db.commit() - + class TestCommonFilters(unittest.TestCase): @@ -634,7 +658,7 @@ class TestCommonFilters(unittest.TestCase): self.assertEqual(db(db.t1).count(),2) q = db.t2.b==db.t1.id self.assertEqual(db(q).count(),2) - self.assertEqual(db(q).count(),2) + self.assertEqual(db(q).count(),2) self.assertEqual(len(db(db.t1).select(left=db.t2.on(q))),3) db.t2._common_filter = lambda q: db.t2.aa<6 self.assertEqual(db(q).count(),1) @@ -788,6 +812,29 @@ class TestDALDictImportExport(unittest.TestCase): db6.commit() +class TestValidateAndInsert(unittest.TestCase): + + def testRun(self): + import datetime + from gluon.validators import IS_INT_IN_RANGE + db = DAL(DEFAULT_URI, check_reserved=['all']) + db.define_table('val_and_insert', + Field('aa'), + Field('bb', 'integer', + requires=IS_INT_IN_RANGE(1,5)) + ) + rtn = db.val_and_insert.validate_and_insert(aa='test1', bb=2) + self.assertEqual(rtn.id, 1) + #errors should be empty + self.assertEqual(len(rtn.errors.keys()), 0) + #this insert won't pass + rtn = db.val_and_insert.validate_and_insert(bb="a") + #the returned id should be None + self.assertEqual(rtn.id, None) + #an error message should be in rtn.errors.bb + self.assertNotEqual(rtn.errors.bb, None) + #cleanup table + db.val_and_insert.drop() diff --git a/gluon/tests/test_html.py b/gluon/tests/test_html.py index 82d65e62..8272adbd 100644 --- a/gluon/tests/test_html.py +++ b/gluon/tests/test_html.py @@ -7,12 +7,36 @@ import sys import os -if os.path.isdir('gluon'): - sys.path.append(os.path.realpath('gluon')) -else: - sys.path.append(os.path.realpath('../')) - 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() + from html import * diff --git a/gluon/tests/test_http.py b/gluon/tests/test_http.py index a588e03e..5257b6fa 100644 --- a/gluon/tests/test_http.py +++ b/gluon/tests/test_http.py @@ -6,10 +6,36 @@ import sys import os import unittest -if os.path.isdir('gluon'): - sys.path.append(os.path.realpath('gluon')) -else: - sys.path.append(os.path.realpath('../')) + + +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 @@ -21,12 +47,12 @@ class TestHTTP(unittest.TestCase): """ Tests http status code message """ h = HTTP - + def gen_status_str(code, message): return str(code) + ' ' + str(message) message = '1423 This is a custom message' code = 1423 - self.assertEqual(str(h(gen_status_str(code, message))), + self.assertEqual(str(h(gen_status_str(code, message))), gen_status_str(code, message)) # test predefined codes @@ -34,16 +60,16 @@ class TestHTTP(unittest.TestCase): self.assertEqual( str(h(code)), gen_status_str(code, defined_status[code])) - - # test correct use of status_message + + # test correct use of status_message for code in defined_status.keys(): - self.assertEqual(str(h(gen_status_str(code, message))), + self.assertEqual(str(h(gen_status_str(code, message))), gen_status_str(code, message)) # test wrong call detection - - + + if __name__ == '__main__': unittest.main() diff --git a/gluon/tests/test_is_url.py b/gluon/tests/test_is_url.py index 39f89610..36b802a7 100644 --- a/gluon/tests/test_is_url.py +++ b/gluon/tests/test_is_url.py @@ -6,12 +6,38 @@ Unit tests for IS_URL() import sys import os -if os.path.isdir('gluon'): - sys.path.append(os.path.realpath('gluon')) -else: - sys.path.append(os.path.realpath('../')) - 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() + + from validators import IS_URL, IS_HTTP_URL, IS_GENERIC_URL, \ unicode_to_ascii_authority diff --git a/gluon/tests/test_languages.py b/gluon/tests/test_languages.py index ece446c8..f7f9a38e 100644 --- a/gluon/tests/test_languages.py +++ b/gluon/tests/test_languages.py @@ -7,16 +7,41 @@ import sys import os -if os.path.isdir('gluon'): - sys.path.append(os.path.realpath('gluon')) -else: - sys.path.append(os.path.realpath('../')) - import unittest -import languages import tempfile import threading import logging + + +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 languages from storage import Storage try: diff --git a/gluon/tests/test_old_doctests.py b/gluon/tests/test_old_doctests.py index 95427a3e..95b92da5 100644 --- a/gluon/tests/test_old_doctests.py +++ b/gluon/tests/test_old_doctests.py @@ -7,14 +7,38 @@ """ import sys import os -if os.path.isdir('gluon'): - sys.path.append(os.path.realpath('gluon')) -else: - sys.path.append(os.path.realpath('../')) - import unittest import doctest - + + +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): tests.addTests( @@ -28,12 +52,12 @@ def load_tests(loader, tests, ignore): tests.addTests( doctest.DocTestSuite('utf8') ) - + tests.addTests( doctest.DocTestSuite('contrib.markmin.markmin2html', ) ) - + return tests if __name__ == '__main__': diff --git a/gluon/tests/test_storage.py b/gluon/tests/test_storage.py index 828137e0..ea9a6698 100644 --- a/gluon/tests/test_storage.py +++ b/gluon/tests/test_storage.py @@ -6,10 +6,35 @@ import sys import os import unittest -if os.path.isdir('gluon'): - sys.path.append(os.path.realpath('gluon')) -else: - sys.path.append(os.path.realpath('../')) + + +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 diff --git a/gluon/tests/test_template.py b/gluon/tests/test_template.py index 20fadbcd..85758339 100644 --- a/gluon/tests/test_template.py +++ b/gluon/tests/test_template.py @@ -6,12 +6,37 @@ import sys import os -if os.path.isdir('gluon'): - sys.path.append(os.path.realpath('gluon')) -else: - sys.path.append(os.path.realpath('../')) - 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() + from template import render diff --git a/gluon/tests/test_utils.py b/gluon/tests/test_utils.py index 97d4d2b2..77fd9e4e 100644 --- a/gluon/tests/test_utils.py +++ b/gluon/tests/test_utils.py @@ -6,10 +6,35 @@ import sys import os import unittest -if os.path.isdir('gluon'): - sys.path.append(os.path.realpath('gluon')) -else: - sys.path.append(os.path.realpath('../')) + + +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 diff --git a/gluon/tests/test_web.py b/gluon/tests/test_web.py index e5e68605..93ded415 100644 --- a/gluon/tests/test_web.py +++ b/gluon/tests/test_web.py @@ -5,22 +5,56 @@ """ import sys import os -if os.path.isdir('gluon'): - sys.path.append(os.path.realpath('gluon')) -else: - sys.path.append(os.path.realpath('../')) - import unittest import subprocess import time import signal + + +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 contrib.webclient import WebClient webserverprocess = None def startwebserver(): global webserverprocess - webserverprocess = subprocess.Popen([sys.executable, 'web2py.py', '-a', 'testpass']) + 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, '..')) + web2py_exec = os.path.join(path, 'web2py.py') + webserverprocess = subprocess.Popen([sys.executable, web2py_exec, '-a', 'testpass']) print 'Sleeping before web2py starts...' for a in range(1,11): time.sleep(1) @@ -29,7 +63,7 @@ def startwebserver(): def terminate_process(pid): #Taken from http://stackoverflow.com/questions/1064335/in-python-2-5-how-do-i-kill-a-subprocess - # all this shit is because we are stuck with Python 2.5 and \ + # all this **blah** is because we are stuck with Python 2.5 and \ #we cannot use Popen.terminate() if sys.platform.startswith('win'): import ctypes