diff --git a/models/db.py b/models/db.py index 52d09ac..1ea95c1 100644 --- a/models/db.py +++ b/models/db.py @@ -1,34 +1,20 @@ # -*- coding: utf-8 -*- -# note: if you use Ubuntu, you can allocate your test database on ramdisk -# simply using the /dev/shm directory. -# There's no doubt a ramdisk is much faster than your harddisk, but use it -# carefully if you don't have enough memory. -temp_dir = '/dev/shm/'+request.application # Ubuntu's native ramdisk is faster -#temp_dir = '/tmp' +import web2pytest +import os def _i_am_running_under_tests(): - '''Check if Web2py is running under a test environment. - ''' - - test_running = False - if request.is_local: - # IMPORTANT: the temp_filename variable must be the same as the one set - # on your tests/conftest.py file. - temp_filename = '%s/tests_%s.tmp' % (temp_dir, request.application) - - import glob - if glob.glob(temp_filename): - test_running = True - - return test_running + """Check if Web2py is running under a test environment. + """ + return request.is_local and web2pytest.testfile_exists() if not request.env.web2py_runtime_gae: ## if NOT running on Google App Engine use SQLite or other DB if _i_am_running_under_tests(): - db = DAL('sqlite://test_storage.sqlite', folder=temp_dir) + db = DAL('sqlite://%s_storage.sqlite' % request.application, + folder=os.path.dirname(web2pytest.testfile_name())) else: db = DAL('sqlite://storage.sqlite', pool_size=1, check_reserved=['all']) else: diff --git a/modules/web2pytest/__init__.py b/modules/web2pytest/__init__.py index 5f67f21..76fb1c7 100644 --- a/modules/web2pytest/__init__.py +++ b/modules/web2pytest/__init__.py @@ -15,29 +15,63 @@ need to use this module. """ +import glob import os -#default_dirname = "/tmp" -default_dirname = "/dev/shm/web2py_test" # Ubuntu native ramdisk is faster +#default_path = "/tmp" +default_path = "/dev/shm/web2py_test" # Ubuntu native ramdisk is faster default_filename = "web2py_test_indicator" -test_filename = None +_test_filename = None -def create_testfile(dirname=None, filename=None): +def testfile_name(path=None, filename=None): + global _test_filename + if _test_filename and not (path or filename): + return _test_filename + + path = path if path is not None else default_path + filename = filename if filename is not None else default_filename + _test_filename = os.path.join(path, filename) + + return _test_filename + + +def create_testfile(path=None, filename=None): """Creates a temp file to tell application she's running under a test environment. """ - dirname = dirname or default_dirname - filename = filename or default_filename - test_filename = "%s/%s.tmp" % (dirname, filename) + fname = testfile_name(path, filename) - os.mkdir(dirname) + try: + os.mkdir(os.path.dirname(fname)) + except OSError as e: + pass - with open(test_filename, "w+") as f: - f.write("web2py running in test mode.") + try: + with open(fname, "w+") as f: + f.write("web2py running in test mode.") + return True + except: + return False def delete_testfile(): - os.remove(test_filename) + try: + os.remove(testfile_name()) + return True + except: + return False + + +def testfile_exists(path=None, filename=None): + fname = testfile_name(path, filename) + + try: + if glob.glob(fname): + return True + else: + return False + except: + return False