Skip to content

Commit 91979fd

Browse files
committed
Added simple tests for code & tests workflow
1 parent 538a057 commit 91979fd

File tree

13 files changed

+187
-7
lines changed

13 files changed

+187
-7
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
runs-on: ubuntu-latest
1010

1111
steps:
12-
- uses: actions/checkout@v3
12+
- uses: actions/checkout@v4
1313

1414
- name: Set up Python 3.11
1515
uses: actions/setup-python@v4

.github/workflows/tests.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Check code
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
code-checks:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
python-version: ["3.8", "3.9", "3.10", "3.11"]
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Set up Python ${{ matrix.python-version }}
20+
uses: actions/setup-python@v4
21+
with:
22+
python-version: ${{ matrix.python-version }}
23+
24+
- name: Install dependencies
25+
run: |
26+
pip install -r requirements.txt
27+
28+
- name: Run tests
29+
run: |
30+
python run.py test
File renamed without changes.

run.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
5+
6+
def main():
7+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tests.settings')
8+
from django.core.management import execute_from_command_line
9+
10+
execute_from_command_line(sys.argv)
11+
12+
13+
if __name__ == '__main__':
14+
main()

simple_components/templatetags/simple_components.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import re
22
from django import template
33

4-
54
# allow line breaks inside tags
65
template.base.tag_re = re.compile(template.base.tag_re.pattern, re.DOTALL)
76

8-
97
register = template.Library()
108

119

1210
@register.tag
1311
def set_component(parser, token):
14-
nodelist = parser.parse(("end_set_component",))
12+
nodelist = parser.parse(('end_set_component',))
1513
parser.delete_first_token()
1614

1715
bits = token.split_contents()
1816
if len(bits) < 2:
1917
raise template.TemplateSyntaxError(
20-
"'%s' takes at least one argument (name of component)" % bits[0]
18+
'\'%s\' takes at least one argument (name of component)' % bits[0]
2119
)
2220

2321
component_name = str(parser.compile_filter(bits[1]))
@@ -68,7 +66,7 @@ class ComponentNode(template.Node):
6866
def __init__(self, component_name=None, kwargs=None):
6967
if component_name is None:
7068
raise template.TemplateSyntaxError(
71-
"Component template nodes must be given a name to return."
69+
'Component template nodes must be given a name to return.'
7270
)
7371

7472
self.component_name = component_name
@@ -79,7 +77,13 @@ def __init__(self, component_name=None, kwargs=None):
7977
self.kwargs = kwargs
8078

8179
def render(self, context):
82-
nodelist = context['components'][self.component_name]
80+
try:
81+
nodelist = context['components'][self.component_name]
82+
except KeyError:
83+
raise template.TemplateSyntaxError(
84+
'The component \'%s\' has not been previously defined.Check that the component is named correctly.'
85+
% self.component_name
86+
)
8387

8488
for key, value in self.kwargs.items():
8589
context[key] = value.resolve(context)

tests/__init__.py

Whitespace-only changes.

tests/settings.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from pathlib import Path
2+
3+
BASE_DIR = Path(__file__).resolve().parent.parent
4+
5+
SECRET_KEY = 'secret_key'
6+
DEBUG = False
7+
8+
ALLOWED_HOSTS = []
9+
10+
INSTALLED_APPS = [
11+
'tests',
12+
'simple_components'
13+
]
14+
15+
ROOT_URLCONF = 'tests.urls'
16+
17+
TEMPLATES = [
18+
{
19+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
20+
'DIRS': [],
21+
'APP_DIRS': True,
22+
'OPTIONS': {
23+
'context_processors': [
24+
'django.template.context_processors.request',
25+
],
26+
},
27+
},
28+
]
29+
30+
DATABASES = {
31+
'default': {
32+
'ENGINE': 'django.db.backends.sqlite3',
33+
'NAME': ':memory:'
34+
}
35+
}

tests/templates/context.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% load simple_components %}
2+
3+
{% set_component "simple_v4" %}
4+
<p>{{ c_list }}, {{ c_list.1 }}</p>
5+
<p>{{ c_string }}, {{ c_string.4 }}</p>
6+
<p>{{ c_dict }}, {{ c_dict.a.b.c }}</p>
7+
{% end_set_component %}
8+
9+
{% component "simple_v4" c_list=list c_string=string c_dict=dict %}

tests/templates/newlines.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{% load simple_components %}
2+
3+
{% set_component "simple_v3" %}
4+
<div>
5+
<h3>{{ title }}</h3>
6+
<p>{{ description }}</p>
7+
8+
{{ other }}
9+
</div>
10+
{% end_set_component %}
11+
12+
{% component
13+
"simple_v3"
14+
15+
title="Hello"
16+
description="World"
17+
18+
other="..."
19+
%}

tests/templates/setup.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% load simple_components %}
2+
3+
{% set_component "simple_v1" %}
4+
simple text
5+
{% end_set_component %}

0 commit comments

Comments
 (0)