Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kjaymiller committed Aug 7, 2019
1 parent 66e7dcb commit 1accbce
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 14 deletions.
2 changes: 1 addition & 1 deletion render_engine/config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ def load_config(config_path, section):
config = yaml.safe_load(config_path.read_text())

if section in config:
return config['Section']
return config[section]
else:
return config
36 changes: 25 additions & 11 deletions render_engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ class Engine:
def __init__(
self,
*,
site_url='./',
site_url=None,
template_path='./templates',
config={},
config_path=None,
content_path='content',
static_path='static',
output_path='output',
routes=[],
**kwargs,
):

Expand All @@ -38,27 +42,37 @@ def __init__(
logging.info(f'{config_path} detected checking for engine variables')
config.update(load_config(config_path, 'Engine'))
logging.debug(f'config={config}')
config.update(kwargs)
logging.debug(config)

if 'Environment' not in config:
config['Environment'] = {}

config['Environment'].update(kwargs)


# Create a new environment and set the global variables to the config
# items
# items called in environment variables
self.env = Environment(
loader=FileSystemLoader(template_path),
autoescape=select_autoescape(['html', 'xml']),
)

self.env.globals.update({key: attr for key, attr in config.items()})
if 'Environment' in config:
logging.info(f'Environment section detected')
logging.debug(config['Environment'])
self.env.globals.update(config['Environment'])

# These fields are called a lot. So we pull them from config. Also,
# make it a path
self.base_content_path = config.get('content_path', 'content')
self.base_output_path = config.get('output_path', 'output/')
self.base_static_path = config.get('static_path', 'static')
self.site_url = site_url
self.routes = []
self.base_content_path = config.get('content_path', content_path)
self.base_static_path = config.get('static_path', static_path)
self.base_output_path= config.get('output_path', output_path)
self.site_url = config.get('site_url', site_url)
self.routes = routes
logging.debug(f'base_content_path - {self.base_content_path}')
logging.debug(f'base_output_path - {self.base_output_path}')
logging.debug(f'base_static_path - {self.base_static_path}')
logging.debug(f'site_url - {self.site_url}')
logging.debug(f'routes - {self.routes}')

def route(self, *routes, content_path=None, template=None, content_type=Page):
"""Used to get **kwargs for `add_route`"""
Expand All @@ -69,7 +83,7 @@ def inner(func, routes=routes, content_path=content_path):
self.routes.append(
content_type(
content_path=content_path,
url_root=self.site_url,
url_root=self.site_url if self.site_url else './',
template=template,
slug=route.lstrip('/'),
**kwargs,
Expand Down
3 changes: 3 additions & 0 deletions tests/functional_tests/content/blog_post.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Title: This is a Test

This is a test
10 changes: 10 additions & 0 deletions tests/functional_tests/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<head>
<title></title>
</head>

<body>
<strike>{{TEMPLATE_VARIABLE}}</strike>
{{ANOTHER_VARIABLE}}
</body>
</html>
10 changes: 10 additions & 0 deletions tests/functional_tests/templates/post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<head>
<title>{{Title}}</title>
<link ref="stylesheet" src="">
</head>

<body>
{{content}}
</body>
</html>
File renamed without changes.
File renamed without changes.
21 changes: 19 additions & 2 deletions tests/test_engine.py → tests/unit_tests/test_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

@pytest.fixture()
def base_engine(site_url):
return Engine(site_url=site_url)
return Engine(site_url=site_url, routes=[])

def test_engine_has_internal_env():
"""This ensures that each Engine instance has its own environment.
Expand Down Expand Up @@ -39,7 +39,9 @@ def test_engine_config_path_added_to_env(mocker):
"""When a config_path is provided parse the yaml file and add it to configs
and further the environment globals"""

custom_val='CUSTOM_KEY: CUSTOM_VALUE'
custom_val='''Engine:
Environment:
CUSTOM_KEY: CUSTOM_VALUE'''
mocker.patch(
'pathlib.Path.read_text',
return_value=custom_val,
Expand All @@ -53,3 +55,18 @@ def test_engine_build_collection(mocker, base_engine, base_collection):
assert len(base_engine.routes) == 0
base_engine.build_collection('/collection', pages=(base_collection.pages))
assert len(base_engine.routes) == 3

def test_engine_has_default_base_content_path():
"""If no base content path is presented a default content path of 'content'
is set. This is set even because a Collection with no pages defined MUST
have a content_path set"""

env = Engine()
assert env.base_content_path == 'content'

def test_engine_default_base_content_path_can_be_overridden():
"""If content_path is presented when the engine is initialized it can
overwrite the default content_path."""

env = Engine(content_path='override_the_content_path')
assert env.base_content_path == 'override_the_content_path'
File renamed without changes.

0 comments on commit 1accbce

Please sign in to comment.