Skip to content

Commit 11b5e08

Browse files
authored
Merge pull request #60 from HackSoftware/test-examples
Test examples
2 parents a18ddab + 155fa55 commit 11b5e08

32 files changed

+893
-7
lines changed

.github/workflows/django.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
run: |
2525
python -m pip install --upgrade pip
2626
pip install -r requirements/local.txt
27+
pip install -r requirements/tests.txt
2728
- name: Run migrations
2829
run: python manage.py migrate
2930
- name: Run tests

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Few important things:
99
* Linux / Ubuntu is our primary OS and things are tested for that. It will mostly not work on Mac & certainly not work on Windows.
1010
* It uses Postgres as primary database.
1111
* It comes with GitHub Actions support, [based on that article](https://hacksoft.io/github-actions-in-action-setting-up-django-and-postgres/)
12+
* It comes with examples for writing tests with fakes & factories, based on the following articles - <https://www.hacksoft.io/blog/improve-your-tests-django-fakes-and-factories>, <https://www.hacksoft.io/blog/improve-your-tests-django-fakes-and-factories-advanced-usage>
1213
* It comes with [`whitenoise`](http://whitenoise.evans.io/en/stable/) setup.
1314
* It can be easily deployed to Heroku.
1415
* It comes with an example list API, that uses [`django-filter`](https://django-filter.readthedocs.io/en/stable/) for filtering & pagination from DRF.

config/django/base.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@
4040
'styleguide_example.api.apps.ApiConfig',
4141
'styleguide_example.users.apps.UsersConfig',
4242
'styleguide_example.errors.apps.ErrorsConfig',
43+
'styleguide_example.testing_examples.apps.TestingExamplesConfig',
4344
]
4445

4546
THIRD_PARTY_APPS = [
4647
'rest_framework',
4748
'django_celery_results',
4849
'django_celery_beat',
4950
'django_filters',
50-
'corsheaders'
51+
'corsheaders',
52+
'django_extensions',
5153
]
5254

5355
INSTALLED_APPS = [
@@ -105,12 +107,12 @@
105107
if os.environ.get('GITHUB_WORKFLOW'):
106108
DATABASES = {
107109
'default': {
108-
'ENGINE': 'django.db.backends.postgresql',
109-
'NAME': 'github_actions',
110-
'USER': 'postgres',
111-
'PASSWORD': 'postgres',
112-
'HOST': '127.0.0.1',
113-
'PORT': '5432',
110+
'ENGINE': 'django.db.backends.postgresql',
111+
'NAME': 'github_actions',
112+
'USER': 'postgres',
113+
'PASSWORD': 'postgres',
114+
'HOST': '127.0.0.1',
115+
'PORT': '5432',
114116
}
115117
}
116118

requirements/base.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ whitenoise==5.3.0
1111

1212
django-filter==21.1
1313
django-cors-headers==3.10.1
14+
django-extensions==3.1.5

requirements/local.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ pytest==6.2.5
44
pytest-django==4.5.1
55
flake8==4.0.1
66
ipdb==0.13.9
7+
ipython==7.29.0

requirements/tests.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-r base.txt
2+
3+
pytest==6.2.5
4+
pytest-django==4.4.0
5+
factory-boy==3.2.1
6+
Faker==9.8.2

styleguide_example/common/apps.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33

44
class CommonConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
56
name = 'styleguide_example.common'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 3.2.9 on 2021-11-18 09:46
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('common', '0001_initial'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='randommodel',
15+
name='id',
16+
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
17+
),
18+
]

styleguide_example/testing_examples/__init__.py

Whitespace-only changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from django.contrib import admin
2+
3+
from styleguide_example.testing_examples.models import (
4+
School,
5+
SchoolCourse,
6+
Student,
7+
Roster
8+
)
9+
10+
11+
@admin.register(School)
12+
class SchoolAdmin(admin.ModelAdmin):
13+
list_display = ('name', 'slug', )
14+
15+
16+
@admin.register(Roster)
17+
class RosterAdmin(admin.ModelAdmin):
18+
list_display = ('student', 'school_course', 'start_date', 'end_date', 'active', )
19+
list_filter = ('student', 'school_course')
20+
21+
22+
class RosterStudentInline(admin.TabularInline):
23+
model = Roster
24+
fields = ('school_course', 'start_date', 'end_date', 'active', 'deactivated_at', )
25+
extra = 1
26+
27+
28+
class RosterSchoolCourseInline(admin.TabularInline):
29+
model = Roster
30+
fields = ('student', 'start_date', 'end_date', 'active', 'deactivated_at', )
31+
extra = 1
32+
33+
34+
@admin.register(SchoolCourse)
35+
class SchoolCourseAdmin(admin.ModelAdmin):
36+
list_display = ('name', 'slug', 'school', 'start_date', 'end_date', )
37+
list_filter = ('school', )
38+
inlines = (RosterSchoolCourseInline, )
39+
40+
41+
@admin.register(Student)
42+
class StudentAdmin(admin.ModelAdmin):
43+
list_display = ('email', 'identifier', 'school', )
44+
list_filter = ('school', )
45+
inlines = (RosterStudentInline, )

0 commit comments

Comments
 (0)