Skip to content

Commit

Permalink
Use database in Question test
Browse files Browse the repository at this point in the history
- add a slug field to the Question so we can look it up in the url
- add factoryboy so we can declare database state in tests
  • Loading branch information
duncanjbrown committed Mar 1, 2024
1 parent 1ad5cc4 commit fc25dff
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.2 on 2024-03-01 17:25

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("consultations", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="question",
name="slug",
field=models.CharField(default=None, max_length=256),
preserve_default=False,
),
]
1 change: 1 addition & 0 deletions consultation_analyser/consultations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

class Question(models.Model):
text = models.CharField(max_length=None) # no idea what's a sensible value for max_length
slug = models.CharField(max_length=256)
Original file line number Diff line number Diff line change
@@ -1 +1 @@
How should funding be allocated?
{{ question.text }}
2 changes: 1 addition & 1 deletion consultation_analyser/consultations/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.urls import path
from .views import show_question

urlpatterns = [path("questions/how-should-funding-be-allocated", show_question)]
urlpatterns = [path("questions/<str:slug>", show_question)]
6 changes: 4 additions & 2 deletions consultation_analyser/consultations/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.shortcuts import render
from django.http import HttpRequest
from .models import Question


def show_question(request: HttpRequest):
return render(request, "show_question.html")
def show_question(request: HttpRequest, slug: str):
context = {"question": Question.objects.filter(slug=slug).first()}
return render(request, "show_question.html", context)
59 changes: 58 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pre-commit = "^3.6.2"
files = '**/*.py'
exclude= ['^consultation_analyser/consultations/migrations/']


[tool.poetry.group.test.dependencies]
pytest-django = "^4.8.0"

Expand Down
Empty file added tests/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions tests/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import factory
from consultation_analyser.consultations import models


class QuestionFactory(factory.django.DjangoModelFactory):
class Meta:
model = models.Question

text = "Example question text?"
slug = "test-question"
9 changes: 8 additions & 1 deletion tests/integration/test_question_page.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import pytest
from tests.factories import QuestionFactory


@pytest.mark.django_db
def test_get_question_page(client):
response = client.get("/questions/how-should-funding-be-allocated")
question = QuestionFactory(text="How should funding be allocated?", slug="how-should-funding-be-allocated")

response = client.get(f"/questions/{question.slug}")
assert "How should funding be allocated?" in str(response.content)

0 comments on commit fc25dff

Please sign in to comment.