Skip to content

Commit

Permalink
New API to work with web2pytest module
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciusban committed Oct 6, 2013
1 parent 251053a commit bec7740
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
28 changes: 7 additions & 21 deletions models/db.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
56 changes: 45 additions & 11 deletions modules/web2pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bec7740

Please sign in to comment.