Skip to content

Commit 0f7fc40

Browse files
committed
Added coverage & some tests for catch raise exceptions
1 parent a50bf6b commit 0f7fc40

File tree

16 files changed

+150
-35
lines changed

16 files changed

+150
-35
lines changed

.DS_Store

8 KB
Binary file not shown.

.github/workflows/tests.yml

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Check code
1+
name: Testing code & Coverage
22

33
on:
44
push:
@@ -11,7 +11,7 @@ on:
1111
- '**.py'
1212

1313
jobs:
14-
code-checks:
14+
tests:
1515
runs-on: ubuntu-latest
1616
strategy:
1717
matrix:
@@ -31,4 +31,28 @@ jobs:
3131
3232
- name: Run tests
3333
run: |
34-
python run.py test
34+
python manage.py test
35+
36+
coverage:
37+
runs-on: ubuntu-latest
38+
steps:
39+
- uses: actions/checkout@v4
40+
41+
- name: Set up Python 3.10
42+
uses: actions/setup-python@v4
43+
with:
44+
python-version: 3.10
45+
46+
- name: Install dependencies
47+
run: |
48+
pip install -r requirements.txt
49+
pip install coverage
50+
51+
- name: Run tests with coverage
52+
run: |
53+
python -m coverage run --source='.' manage.py test
54+
55+
- name: Upload coverage reports to Codecov
56+
uses: codecov/codecov-action@v3
57+
env:
58+
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,3 @@ cython_debug/
158158
# and can be added to the global gitignore or merged into this file. For a more nuclear
159159
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160160
.idea/
161-
162-
# hide other django apps
163-
config/
164-
manage.py

MANIFEST.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
include LICENSE
22
include README.md
33
recursive-include simple_components/templates *
4+
5+
exclude manage.py
6+
recursive-exclude tests *

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,41 @@ Django Simple Components is a small package to easily create components inside y
44

55
## Quick start
66

7-
0. Install package from pypi:
7+
### 0. Install package from pypi:
88
```bash
99
pip install django-simple-components
1010
```
1111

12-
1. Add `simple_components` to your INSTALLED_APPS setting like this:
12+
### 1. Add `simple_components` to your INSTALLED_APPS setting like this:
1313
```python
1414
INSTALLED_APPS = [
1515
...,
1616
"simple_components",
1717
]
1818
```
1919

20-
2. Add `simple_components` to your template like this:
20+
Optionally, you can specify `simple_components` as builtins and this will be available in any of your templates without additionally specifying `{% load simple_components %}`:
21+
```python
22+
TEMPLATES = [
23+
{
24+
"BACKEND": "django.template.backends.django.DjangoTemplates",
25+
"DIRS": [BASE_DIR / "templates"],
26+
"APP_DIRS": True,
27+
"OPTIONS": {
28+
"context_processors": [
29+
"django.template.context_processors.debug",
30+
"django.template.context_processors.request",
31+
"django.contrib.auth.context_processors.auth",
32+
"django.contrib.messages.context_processors.messages",
33+
],
34+
"builtins": ["simple_components.templatetags.simple_components"],
35+
},
36+
},
37+
]
38+
```
39+
If you choose not use it as a built-in, you will need to add `{% load simple_components %}` to the top of your template whenever you want to use simple components.
40+
41+
### 2. Create component inside your template:
2142
```html
2243
{% load simple_components %}
2344

@@ -41,7 +62,7 @@ INSTALLED_APPS = [
4162
</div>
4263
```
4364

44-
3. Hooray! Everything is ready to use it.
65+
### 3. Hooray! Everything is ready to use it.
4566

4667
## Contributing
4768
If you would like to suggest a new feature, you can create an issue on the GitHub repository for this project.

run.py renamed to manage.py

File renamed without changes.

pyproject.toml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ requires = [
44
"setuptools-scm>=8.0"]
55
build-backend = "setuptools.build_meta"
66

7-
[tool.setuptools_scm]
8-
97
[project]
108
name = "django-simple-components"
119
dynamic = ["version"]
@@ -35,3 +33,13 @@ classifiers = [
3533
[project.urls]
3634
"Homepage" = "https://github.com/paqstd-dev/django-simple-components"
3735
"Bug Tracker" = "https://github.com/paqstd-dev/django-simple-components/issues"
36+
37+
38+
[tool.setuptools_scm]
39+
40+
[tool.coverage.run]
41+
omit = [
42+
"manage.py",
43+
"setup.py",
44+
"tests/*"
45+
]

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from setuptools import setup
22

3-
setup()
3+
setup()

simple_components/exceptions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from django import template
2+
3+
4+
class SetComponentNameError(template.TemplateSyntaxError):
5+
pass
6+
7+
8+
class ComponentNameError(template.TemplateSyntaxError):
9+
pass
10+
11+
12+
class ComponentArgsError(template.TemplateSyntaxError):
13+
pass
14+
15+
16+
class ComponentNotDefined(template.TemplateSyntaxError):
17+
pass

simple_components/templatetags/simple_components.py

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

4+
from ..exceptions import SetComponentNameError, ComponentNameError, ComponentArgsError, ComponentNotDefined
5+
46
# allow line breaks inside tags
57
template.base.tag_re = re.compile(template.base.tag_re.pattern, re.DOTALL)
68

@@ -14,7 +16,7 @@ def set_component(parser, token):
1416

1517
bits = token.split_contents()
1618
if len(bits) < 2:
17-
raise template.TemplateSyntaxError(
19+
raise SetComponentNameError(
1820
'\'%s\' takes at least one argument (name of component)' % bits[0]
1921
)
2022

@@ -39,9 +41,10 @@ def render(self, context):
3941
@register.tag
4042
def component(parser, token):
4143
bits = token.split_contents()
42-
if len(bits) < 2:
43-
raise template.TemplateSyntaxError(
44-
'\'%s\' takes at least one argument (name of component)' % bits[0]
44+
if len(bits) < 2 or '=' in bits[1]:
45+
example = '{% component \"name\" ' + ' '.join(bits[1:]) + ' %}'
46+
raise ComponentNameError(
47+
'Component takes at least one required argument (name of component):\n%s' % example
4548
)
4649

4750
component_name = str(parser.compile_filter(bits[1]))
@@ -55,36 +58,29 @@ def component(parser, token):
5558
if name:
5659
kwargs[name] = parser.compile_filter(value)
5760
else:
58-
raise template.TemplateSyntaxError(
59-
'\'%s\' must be takes as kwargs' % bit
61+
example = '{% component "' + component_name + '" param1=%s' % bit + ' ... %}'
62+
raise ComponentArgsError(
63+
'Argument %s must be takes as kwargs:\n%s' % (bit, example)
6064
)
6165

6266
return ComponentNode(component_name, kwargs)
6367

6468

6569
class ComponentNode(template.Node):
66-
def __init__(self, component_name=None, kwargs=None):
67-
if component_name is None:
68-
raise template.TemplateSyntaxError(
69-
'Component template nodes must be given a name to return.'
70-
)
71-
70+
def __init__(self, component_name, kwargs=None):
7271
self.component_name = component_name
73-
74-
if kwargs is None:
75-
kwargs = {}
76-
7772
self.kwargs = kwargs
7873

7974
def render(self, context):
8075
try:
8176
nodelist = context['components'][self.component_name]
8277
except KeyError:
83-
raise template.TemplateSyntaxError(
84-
'The component \'%s\' has not been previously defined.Check that the component is named correctly.'
78+
raise ComponentNotDefined(
79+
'The component \'%s\' has not been previously defined. Check that the component is named correctly.'
8580
% self.component_name
8681
)
8782

88-
for key, value in self.kwargs.items():
89-
context[key] = value.resolve(context)
83+
if self.kwargs is not None:
84+
for key, value in self.kwargs.items():
85+
context[key] = value.resolve(context)
9086
return nodelist.render(context)

0 commit comments

Comments
 (0)