Test that templates render, at least somewhat
authorKarl O. Pinc <kop@karlpinc.com>
Wed, 11 Sep 2024 00:17:51 +0000 (19:17 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Wed, 11 Sep 2024 00:17:51 +0000 (19:17 -0500)
setup.py
tests/templates/test_templates.py [new file with mode: 0644]

index 4c8fb69bb5fa64177b5250f9788d95000fb2233b..57fc4fb2c745f0366e5c2bbf2ce1d43a93d85af9 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -76,6 +76,7 @@ install_requires = [
 extras_require = {
     "test": [
         "pgwui_develop==" + version,
+        "pgwui_server==" + version,
         "WebTest >= 1.3.1",  # py3 compat
         "pytest>=3.7.4",
         "pytest-cov",
diff --git a/tests/templates/test_templates.py b/tests/templates/test_templates.py
new file mode 100644 (file)
index 0000000..4d85598
--- /dev/null
@@ -0,0 +1,87 @@
+# Copyright (C) 2024 The Meme Factory, Inc.  http://www.karlpinc.com/
+
+# This file is part of PGWUI_SQL.
+#
+# This program is free software: you can redistribute it and/or
+# modify it under the terms of the GNU Affero General Public License
+# as published by the Free Software Foundation, either version 3 of
+# the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public
+# License along with this program.  If not, see
+# <http://www.gnu.org/licenses/>.
+#
+
+# Karl O. Pinc <kop@karlpinc.com>
+
+import mako.lookup
+import mako.template
+import pytest
+import pgwui_server.pgwui_server
+import pgwui_develop.testing
+import pyramid.testing
+
+# A dict of "standard" settings
+pgwui_settings = {'dry_run': False,
+                  'pgwui_sql':
+                  {'menu_label': 'Test menu label'}}
+config = pgwui_server.pgwui_server.pgwui_server_config(
+    {'pgwui': pgwui_settings,
+     'session.secret': '1234567890123456789012345678901234567890'})
+request = pyramid.testing.DummyRequest()
+request.registry = config.registry
+
+stock_template_args = {
+    'pgwui': config.get_settings()['pgwui'],
+    'havecreds': False,
+    'errors': [],
+    'hosts': 'localhost',
+    'db': 'template1',
+    'user': '',
+    'csrf_token': 'somecsrftoken',
+    'sql': 'select 1;',
+}
+
+# The templates to test
+TEMPLATES = ['sql.mak', 'sql_edit.mak']
+logged_in_args = pgwui_develop.testing.update_copy(
+    stock_template_args, {'havecreds': True})
+
+# Add the request to all the arguments.
+# (The request won't deepcopy, so we don't try.)
+stock_template_args['request'] = request
+logged_in_args['request'] = request
+
+# The variables with which to test each template
+TEMPLATE_ARGS = {
+    'sql.mak': [stock_template_args,
+                logged_in_args],
+    'sql_edit.mak': [stock_template_args,
+                     logged_in_args]
+}
+
+# Transform TEMPLATE_ARGS into something pytest can use with parametrize.
+parameter_args = []
+for tfile, values in iter(TEMPLATE_ARGS.items()):
+    for value in values:
+        parameter_args.append((tfile, value))
+
+# All imports are through asset paths, which become fully qualified paths.
+lookup_dirs = mako.lookup.TemplateLookup(directories=['/'])
+
+
+# Template rendering tests
+
+@pytest.mark.parametrize('tfile,values', parameter_args)
+def test_templates(tfile, values):
+    '''All the templates render without error
+    '''
+    template = mako.template.Template(
+        filename=f'src/pgwui_sql/templates/{tfile}',
+        lookup=lookup_dirs)
+    template.render(**values)