Skip to content

Commit

Permalink
Added django-pytest examples
Browse files Browse the repository at this point in the history
  • Loading branch information
theognis1002 committed Sep 10, 2022
1 parent 4165a81 commit bf3654c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
14 changes: 13 additions & 1 deletion apps/products/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
from django.test import TestCase
from django.urls import reverse
from django.contrib.auth.models import User

# Create your tests here.

class TestDownloadView(TestCase):
def test_anonymous_cannot_see_page(self):
response = self.client.get(reverse("test"))
assert response.status_code == 302

def test_authenticated_user_can_see_page(self):
user = User.objects.create_user("Juliana," "[email protected]", "some_pass")
self.client.force_login(user=user)
response = self.client.get(reverse("test"))
assert response.status_code == 200
3 changes: 2 additions & 1 deletion apps/products/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

urlpatterns = [
path("", views.index),
path("atomic/", views.atomic_test),
path("atomic/", views.atomic_test, name="atomic"),
path("test/", views.test_view, name="test"),
]
9 changes: 8 additions & 1 deletion apps/products/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from datetime import timedelta

from django.contrib.auth.decorators import login_required
from django.db import transaction
from django.db.models import Avg, F, Sum
from django.shortcuts import render

from .models import Customer, LineItem, Order, Product
from django.db import transaction


def index(request):
Expand Down Expand Up @@ -42,6 +43,7 @@ def index(request):
return render(request, "products/index.html", {"orders": orders})


@login_required
def atomic_test(request):
with transaction.atomic():
orders = Order.objects.select_related("customer").get(pk=1)
Expand All @@ -57,3 +59,8 @@ def atomic_test(request):
transaction.set_rollback(True)

return render(request, "products/index.html", {"orders": [orders]})


@login_required
def test_view(request):
return render(request, "products/index.html", {"orders": []})
4 changes: 4 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[pytest]
DJANGO_SETTINGS_MODULE = django_playground.settings
# -- recommended but optional:
python_files = tests.py test_*.py *_tests.py
10 changes: 10 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
asgiref==3.4.1
attrs==22.1.0
black==21.8b0
click==8.0.1
Django==3.2.7
django-debug-toolbar==3.2.2
django-extensions==3.1.5
django-migration-vis==2.0.1
Faker==8.12.1
graphviz==0.20
iniconfig==1.1.1
isort==5.9.3
mypy-extensions==0.4.3
packaging==21.3
pathspec==0.9.0
platformdirs==2.3.0
pluggy==1.0.0
py==1.11.0
pyparsing==3.0.9
pytest==7.1.3
pytest-django==4.5.2
python-dateutil==2.8.2
pytz==2021.1
regex==2021.8.28
Expand Down

0 comments on commit bf3654c

Please sign in to comment.