|
2 | 2 |
|
3 | 3 | import pytest
|
4 | 4 | from django.db import models
|
| 5 | +from django.http import Http404 |
5 | 6 | from django.shortcuts import get_object_or_404
|
6 | 7 | from django.test import TestCase
|
7 | 8 | from django.utils import six
|
|
10 | 11 | from rest_framework.response import Response
|
11 | 12 | from rest_framework.test import APIRequestFactory
|
12 | 13 | from tests.models import (
|
13 |
| - BasicModel, ForeignKeySource, ForeignKeyTarget, RESTFrameworkModel |
| 14 | + BasicModel, ForeignKeySource, ForeignKeyTarget, RESTFrameworkModel, |
| 15 | + UUIDForeignKeyTarget |
14 | 16 | )
|
15 | 17 |
|
16 | 18 | factory = APIRequestFactory()
|
@@ -647,3 +649,19 @@ def destroy(self, request, *args, **kwargs):
|
647 | 649 | view.delete('test request', 'test arg', test_kwarg='test')
|
648 | 650 | assert view.called is True
|
649 | 651 | assert view.call_args == data
|
| 652 | + |
| 653 | + |
| 654 | +class GetObjectOr404Tests(TestCase): |
| 655 | + def setUp(self): |
| 656 | + super(GetObjectOr404Tests, self).setUp() |
| 657 | + self.uuid_object = UUIDForeignKeyTarget.objects.create(name='bar') |
| 658 | + |
| 659 | + def test_get_object_or_404_with_valid_uuid(self): |
| 660 | + obj = generics.get_object_or_404( |
| 661 | + UUIDForeignKeyTarget, pk=self.uuid_object.pk |
| 662 | + ) |
| 663 | + assert obj == self.uuid_object |
| 664 | + |
| 665 | + def test_get_object_or_404_with_invalid_string_for_uuid(self): |
| 666 | + with pytest.raises(Http404): |
| 667 | + generics.get_object_or_404(UUIDForeignKeyTarget, pk='not-a-uuid') |
0 commit comments