Update tests for new module helpers.

This commit is contained in:
Ron DuPlain 2012-01-07 17:55:02 -05:00
parent a3b30b7e3b
commit fde6e364a4
5 changed files with 77 additions and 51 deletions

View file

@ -98,6 +98,14 @@ class InstanceTestCase(FlaskTestCase):
app = flask.Flask(__name__, instance_path=here)
self.assert_equal(app.instance_path, here)
def test_main_module_paths(self):
# Test an app with '__main__' as the import name, uses cwd.
from main_app import app
here = os.path.abspath(os.getcwd())
self.assert_equal(app.instance_path, os.path.join(here, 'instance'))
if 'main_app' in sys.modules:
del sys.modules['main_app']
def test_uninstalled_module_paths(self):
from config_module_app import app
here = os.path.abspath(os.path.dirname(__file__))
@ -109,70 +117,75 @@ class InstanceTestCase(FlaskTestCase):
self.assert_equal(app.instance_path, os.path.join(here, 'test_apps', 'instance'))
def test_installed_module_paths(self):
import types
expected_prefix = os.path.abspath('foo')
mod = types.ModuleType('myapp')
mod.__file__ = os.path.join(expected_prefix, 'lib', 'python2.5',
'site-packages', 'myapp.py')
sys.modules['myapp'] = mod
here = os.path.abspath(os.path.dirname(__file__))
expected_prefix = os.path.join(here, 'test_apps')
real_prefix, sys.prefix = sys.prefix, expected_prefix
site_packages = os.path.join(expected_prefix, 'lib', 'python2.5', 'site-packages')
sys.path.append(site_packages)
try:
mod.app = flask.Flask(mod.__name__)
self.assert_equal(mod.app.instance_path,
os.path.join(expected_prefix, 'var',
'myapp-instance'))
import site_app
self.assert_equal(site_app.app.instance_path,
os.path.join(expected_prefix, 'var',
'site_app-instance'))
finally:
sys.modules['myapp'] = None
sys.prefix = real_prefix
sys.path.remove(site_packages)
if 'site_app' in sys.modules:
del sys.modules['site_app']
def test_installed_package_paths(self):
import types
expected_prefix = os.path.abspath('foo')
package_path = os.path.join(expected_prefix, 'lib', 'python2.5',
'site-packages', 'myapp')
mod = types.ModuleType('myapp')
mod.__path__ = [package_path]
mod.__file__ = os.path.join(package_path, '__init__.py')
sys.modules['myapp'] = mod
here = os.path.abspath(os.path.dirname(__file__))
expected_prefix = os.path.join(here, 'test_apps')
real_prefix, sys.prefix = sys.prefix, expected_prefix
installed_path = os.path.join(expected_prefix, 'path')
sys.path.append(installed_path)
try:
mod.app = flask.Flask(mod.__name__)
self.assert_equal(mod.app.instance_path,
os.path.join(expected_prefix, 'var',
'myapp-instance'))
import installed_package
self.assert_equal(installed_package.app.instance_path,
os.path.join(expected_prefix, 'var',
'installed_package-instance'))
finally:
sys.modules['myapp'] = None
sys.prefix = real_prefix
sys.path.remove(installed_path)
if 'installed_package' in sys.modules:
del sys.modules['installed_package']
def test_prefix_installed_paths(self):
import types
expected_prefix = os.path.abspath(sys.prefix)
package_path = os.path.join(expected_prefix, 'lib', 'python2.5',
'site-packages', 'myapp')
mod = types.ModuleType('myapp')
mod.__path__ = [package_path]
mod.__file__ = os.path.join(package_path, '__init__.py')
sys.modules['myapp'] = mod
def test_prefix_package_paths(self):
here = os.path.abspath(os.path.dirname(__file__))
expected_prefix = os.path.join(here, 'test_apps')
real_prefix, sys.prefix = sys.prefix, expected_prefix
site_packages = os.path.join(expected_prefix, 'lib', 'python2.5', 'site-packages')
sys.path.append(site_packages)
try:
mod.app = flask.Flask(mod.__name__)
self.assert_equal(mod.app.instance_path,
os.path.join(expected_prefix, 'var',
'myapp-instance'))
import site_package
self.assert_equal(site_package.app.instance_path,
os.path.join(expected_prefix, 'var',
'site_package-instance'))
finally:
sys.modules['myapp'] = None
sys.prefix = real_prefix
sys.path.remove(site_packages)
if 'site_package' in sys.modules:
del sys.modules['site_package']
def test_egg_installed_paths(self):
import types
expected_prefix = os.path.abspath(sys.prefix)
package_path = os.path.join(expected_prefix, 'lib', 'python2.5',
'site-packages', 'MyApp.egg', 'myapp')
mod = types.ModuleType('myapp')
mod.__path__ = [package_path]
mod.__file__ = os.path.join(package_path, '__init__.py')
sys.modules['myapp'] = mod
here = os.path.abspath(os.path.dirname(__file__))
expected_prefix = os.path.join(here, 'test_apps')
real_prefix, sys.prefix = sys.prefix, expected_prefix
site_packages = os.path.join(expected_prefix, 'lib', 'python2.5', 'site-packages')
egg_path = os.path.join(site_packages, 'SiteEgg.egg')
sys.path.append(site_packages)
sys.path.append(egg_path)
try:
mod.app = flask.Flask(mod.__name__)
self.assert_equal(mod.app.instance_path,
os.path.join(expected_prefix, 'var',
'myapp-instance'))
import site_egg # in SiteEgg.egg
self.assert_equal(site_egg.app.instance_path,
os.path.join(expected_prefix, 'var',
'site_egg-instance'))
finally:
sys.modules['myapp'] = None
sys.prefix = real_prefix
sys.path.remove(site_packages)
sys.path.remove(egg_path)
if 'site_egg' in sys.modules:
del sys.modules['site_egg']
def suite():

View file

@ -0,0 +1,3 @@
import flask
app = flask.Flask(__name__)

View file

@ -0,0 +1,3 @@
import flask
app = flask.Flask(__name__)

View file

@ -0,0 +1,4 @@
import flask
# Test Flask initialization with main module.
app = flask.Flask('__main__')

View file

@ -0,0 +1,3 @@
import flask
app = flask.Flask(__name__)