| 1 | import pytest |
| 2 | from django.test import Client |
| 3 | from django.urls import reverse |
| 4 | from someapp.models import Event |
| 5 | |
| 6 | |
| 7 | @pytest.mark.django_db |
| 8 | def test_with_client(client: Client): |
| 9 | assert Event.objects.count() == 0 |
| 10 | |
| 11 | test_item = {"start_date": "2020-01-01", "end_date": "2020-01-02", "title": "test"} |
| 12 | |
| 13 | response = client.post("/api/events/create", **json_payload(test_item)) |
| 14 | assert response.status_code == 200 |
| 15 | assert Event.objects.count() == 1 |
| 16 | |
| 17 | response = client.get("/api/events") |
| 18 | assert response.status_code == 200 |
| 19 | assert response.json() == [test_item] |
| 20 | |
| 21 | response = client.get("/api/events/1") |
| 22 | assert response.status_code == 200 |
| 23 | assert response.json() == test_item |
| 24 | |
| 25 | |
| 26 | def test_reverse(): |
| 27 | """ |
| 28 | Check that url reversing works. |
| 29 | """ |
| 30 | assert reverse("api-1.0.0:event-create-url-name") == "/api/events/create" |
| 31 | |
| 32 | |
| 33 | def test_reverse_implicit(): |
| 34 | """ |
| 35 | Check that implicit url reversing works. |
| 36 | """ |
| 37 | assert reverse("api-1.0.0:list_events") == "/api/events" |
| 38 | |
| 39 | |
| 40 | def json_payload(data): |
| 41 | import json |
| 42 | |
| 43 | return dict(data=json.dumps(data), content_type="application/json") |