Skip to content

Commit

Permalink
Merge pull request #166 from zhaoblake/refactor-test
Browse files Browse the repository at this point in the history
  • Loading branch information
greyli authored Dec 14, 2023
2 parents 27885b2 + 56e06db commit 2727263
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 438 deletions.
13 changes: 0 additions & 13 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +0,0 @@
import sys, os

# In order to test the Flask-Script command, Flask-Script needs to be
# installed. If this is the case, we won't be able to import from our
# local src/flaskext directory that nose puts on sys.path, due to the
# way Flask extensions employ pkg_resources to have multiple directories
# contribute to the same module. We fix it by manually adding the
# directory to an already existing virtual flaskext module.
try:
sys.modules['flaskext'].__path__.append(
os.path.join(os.path.dirname(__file__), '../src/flaskext'))
except KeyError:
pass
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""This is here so that the tests have a Python package available
that can serve as the base for Flask modules used during testing.
that can serve as the base for Flask blueprints used during testing.
"""
File renamed without changes.
34 changes: 34 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import shutil
import tempfile

import pytest
from flask import Flask

from flask_assets import Environment
from tests.helpers import new_blueprint


@pytest.fixture
def app():
app = Flask(__name__, static_url_path="/app_static")
bp = new_blueprint("bp", static_url_path="/bp_static", static_folder="static")
app.register_blueprint(bp)
return app


@pytest.fixture
def env(app):
env = Environment(app)
return env


@pytest.fixture
def no_app_env():
return Environment()


@pytest.fixture
def temp_dir():
temp = tempfile.mkdtemp()
yield temp
shutil.rmtree(temp, ignore_errors=True)
51 changes: 20 additions & 31 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
from flask.app import Flask
from webassets.test import TempEnvironmentHelper as BaseTempEnvironmentHelper
from flask_assets import Environment
import os

try:
from flask import Blueprint
Module = None
except ImportError:
# Blueprints only available starting with 0.7,
# fall back to old Modules otherwise.
Blueprint = None
from flask import Module
from flask import Blueprint

__all__ = ("create_files", "new_blueprint")

__all__ = ('TempEnvironmentHelper', 'Module', 'Blueprint')

def create_files(parent, *files):
result = []
for file in files:
path = os.path.join(parent, file)
dir_path = os.path.dirname(path)
if not os.path.exists(dir_path):
os.mkdir(dir_path)
f = open(path, "w", encoding="utf-8")
f.close()
result.append(path)

class TempEnvironmentHelper(BaseTempEnvironmentHelper):
return result

def _create_environment(self, **kwargs):
if not hasattr(self, 'app'):
self.app = Flask(__name__, static_folder=self.tempdir, **kwargs)
self.env = Environment(self.app)
return self.env


try:
from test.test_support import check_warnings
except ImportError:
# Python < 2.6
import contextlib

@contextlib.contextmanager
def check_warnings(*filters, **kwargs):
# We cannot reasonably support this, we'd have to copy to much code.
# (or write our own). Since this is only testing warnings output,
# we might slide by ignoring it.
yield
def new_blueprint(name, import_name=None, **kwargs):
if import_name is None:
from tests import bp_for_test
import_name = bp_for_test.__name__
bp = Blueprint(name, import_name, **kwargs)
return bp
165 changes: 51 additions & 114 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,116 +1,53 @@
"""The Environment configuration is hooked up to the Flask config dict.
"""

from __future__ import absolute_import

import pytest
from flask import Flask
from flask_assets import Environment

try:
from webassets.updater import BaseUpdater
except ImportError:
BaseUpdater = None # older webassets versions (<=0.5)


if BaseUpdater:
class MooUpdater(BaseUpdater):
id = 'MOO'


class TestConfigAppBound:
"""The extension is bound to a specific app.
"""

def setup(self):
self.app = Flask(__name__)
self.env = Environment(self.app)

def test_set_environment(self):
"""Setting a config value on the environment works.
"""
self.env.updater = 'foo'
assert self.app.config['ASSETS_UPDATER'] == 'foo'

def test_set_config(self):
"""Setting a value in the Flask config works.
"""
self.app.config['ASSETS_UPDATER'] = 'MOO'
assert self.env.updater == 'MOO'

def test_custom_values(self):
"""Custom config values are relayed to the Flask config as.is.
"""
self.app.config['LESS_PATH'] = '/usr/bin/less'
assert self.env.config['LESS_PATH'] == '/usr/bin/less'

def test_no_override(self):
"""Ensure that the webassets defaults do not override existing
Flask config values.
"""
app = Flask(__name__)
app.config['ASSETS_UPDATER'] = 'MOO'
env = Environment(app)
assert env.updater == 'MOO'
assert app.config['ASSETS_UPDATER'] == 'MOO'

# Neither do the defaults that flask-assets set.
app = Flask(__name__)
app.config['ASSETS_URL'] = 'MOO'
env = Environment(app)
assert env.url == 'MOO'
assert app.config['ASSETS_URL'] == 'MOO'


class TestConfigNoAppBound:
"""The application is not bound to a specific app.
"""

def setup(self):
self.env = Environment()

def test_no_app_available(self):
"""Without an application bound, we can't do much."""
with pytest.raises(RuntimeError):
setattr(self.env, 'debug', True)
with pytest.raises(RuntimeError):
self.env.config.get('debug')

def test_global_defaults(self):
"""We may set defaults even without an application, however."""
self.env.config.setdefault('FOO', 'BAR')
with Flask(__name__).test_request_context():
assert self.env.config['FOO'] == 'BAR'

def test_multiple_separate_apps(self):
"""Each app has it's own separate configuration.
"""
app1 = Flask(__name__)
self.env.init_app(app1)

# With no app yet available...
with pytest.raises(RuntimeError):
getattr(self.env, 'url')
# ...set a default
self.env.config.setdefault('FOO', 'BAR')

# When an app is available, the default is used
with app1.test_request_context():
assert self.env.config['FOO'] == 'BAR'

# If the default is overridden for this application, it
# is still valid for other apps.
self.env.config['FOO'] = '42'
assert self.env.config['FOO'] == '42'
app2 = Flask(__name__)
with app2.test_request_context():
assert self.env.config['FOO'] == 'BAR'

def test_key_error(self):
"""KeyError is raised if a config value doesn't exist.
"""
with Flask(__name__).test_request_context():
with pytest.raises(KeyError):
self.env.config['YADDAYADDA']
# The get() helper, on the other hand, simply returns None
assert self.env.config.get('YADDAYADDA') == None


def test_env_set(app, env):
env.url = "https://github.com/miracle2k/flask-assets"
assert app.config["ASSETS_URL"] == "https://github.com/miracle2k/flask-assets"


def test_env_get(app, env):
app.config["ASSETS_URL"] = "https://github.com/miracle2k/flask-assets"
assert env.url == "https://github.com/miracle2k/flask-assets"


def test_env_config(app, env):
app.config["LESS_PATH"] = "/usr/bin/less"
assert env.config["LESS_PATH"] == "/usr/bin/less"

with pytest.raises(KeyError):
_ = env.config["do_not_exist"]

assert env.config.get("do_not_exist") is None


def test_no_app_env_set(no_app_env):
with pytest.raises(RuntimeError):
no_app_env.debug = True


def test_no_app_env_get(no_app_env):
with pytest.raises(RuntimeError):
no_app_env.config.get("debug")


def test_no_app_env_config(app, no_app_env):
no_app_env.config.setdefault("foo", "bar")
with app.test_request_context():
assert no_app_env.config["foo"] == "bar"


def test_config_isolation_within_apps(no_app_env):
no_app_env.config.setdefault("foo", "bar")

app1 = Flask(__name__)
with app1.test_request_context():
assert no_app_env.config["foo"] == "bar"

no_app_env.config["foo"] = "qux"
assert no_app_env.config["foo"] == "qux"

app2 = Flask(__name__)
with app2.test_request_context():
assert no_app_env.config["foo"] == "bar"
64 changes: 24 additions & 40 deletions tests/test_env.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,34 @@
import os
import types

from flask import Flask
from flask_assets import Environment, Bundle
from flask_assets import Bundle


class TestEnv:
def test_assets_tag(app, env):
env.register("test", "file1", "file2")
template = app.jinja_env.from_string("{% assets 'test' %}{{ASSET_URL}};{% endassets %}")
assert template.render() == "/app_static/file1;/app_static/file2;"

def setup(self):
self.app = Flask(__name__)
self.env = Environment(self.app)
self.env.debug = True
self.env.register('test', 'file1', 'file2')

def test_tag_available(self):
"""Jinja tag has been made available.
"""
t = self.app.jinja_env.from_string('{% assets "test" %}{{ASSET_URL}};{% endassets %}')
assert t.render() == '/static/file1;/static/file2;'
def test_from_module(app, env):
module = types.ModuleType("test")
module.pytest = Bundle("py_file1", "py_file2")
env.from_module(module)
template = app.jinja_env.from_string('{% assets "pytest" %}{{ASSET_URL}};{% endassets %}')
assert template.render() == '/app_static/py_file1;/app_static/py_file2;'

def test_from_yaml(self):
"""YAML configuration gets loaded
"""
f = open('test.yaml', 'w')

def test_from_yaml(app, env):
with open("test.yaml", "w", encoding="utf-8") as f:
f.write("""
yamltest:
yaml_test:
contents:
- yamlfile1
- yamlfile2
- yaml_file1
- yaml_file2
""")
f.close()

self.env.from_yaml('test.yaml')

t = self.app.jinja_env.from_string('{% assets "yamltest" %}{{ASSET_URL}};{% endassets %}')
assert t.render() == '/static/yamlfile1;/static/yamlfile2;'

os.remove('test.yaml')

def test_from_python_module(self):
"""Python configuration module gets loaded
"""
import types
module = types.ModuleType('test')
module.pytest = Bundle('pyfile1', 'pyfile2')

self.env.from_module(module)

t = self.app.jinja_env.from_string('{% assets "pytest" %}{{ASSET_URL}};{% endassets %}')
assert t.render() == '/static/pyfile1;/static/pyfile2;'
try:
env.from_yaml("test.yaml")
template = app.jinja_env.from_string('{% assets "yaml_test" %}{{ASSET_URL}};{% endassets %}')
assert template.render() == "/app_static/yaml_file1;/app_static/yaml_file2;"
finally:
os.remove("test.yaml")
Loading

0 comments on commit 2727263

Please sign in to comment.