Coverage for apis_core/uris/tests/test_uris.py: 40%

42 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-09-03 06:15 +0000

1import pytest 

2from django.contrib.contenttypes.models import ContentType 

3from faker import Faker 

4from pytest_django.asserts import assertContains, assertTemplateUsed 

5 

6from apis_core.uris.models import Uri 

7from apis_core.uris.tests.models import Dummy, Person 

8 

9fake = Faker() 

10 

11 

12@pytest.mark.django_db 

13class TestUris(object): 

14 @pytest.fixture 

15 def setup(self): 

16 # Create a couple of person objects 

17 for _ in range(0, fake.random_int(5, 20)): 

18 Person.objects.create( 

19 first_name=fake.first_name(), last_name=fake.last_name() 

20 ) 

21 

22 def test_uris_count(self, setup): 

23 """Check if the correct number of Uris was created""" 

24 assert Uri.objects.count() == Person.objects.count() 

25 

26 def test_uris_list_view_status(self, setup, admin_client): 

27 """Check if the uris list view works""" 

28 response = admin_client.get("/uris.uri/") 

29 assert response.status_code == 200 

30 

31 def test_uris_list_view_content(self, setup, admin_client): 

32 """Check if the uris list view works""" 

33 response = admin_client.get("/uris.uri/") 

34 count = Person.objects.count() 

35 # check for the correct result count in the response content 

36 assertContains(response, f"{count} results.") 

37 # check if all the persons are listed 

38 for p in Person.objects.all(): 

39 assertContains(response, str(p)) 

40 

41 def test_delete_view_template_override(self, setup, admin_client): 

42 """Check that the delete confirm view uses the right template""" 

43 response = admin_client.get("/uris.uri/delete/1") 

44 uri = Uri.objects.get(pk=1) 

45 assertTemplateUsed(response, "uris/uri_confirm_delete.html") 

46 assertContains(response, uri.uri) 

47 assertContains(response, str(uri.content_object)) 

48 

49 def test_no_uris_for_dummy(self): 

50 """Check that we don't create uris for dummy objects""" 

51 for _ in range(0, 5): 

52 Dummy.objects.create() 

53 content_type = ContentType.objects.get_for_model(Dummy) 

54 assert Uri.objects.filter(content_type=content_type).count() == 0 

55 

56 def test_custom_uri(self): 

57 d = Dummy.objects.create() 

58 uri = "http://example.org" 

59 d._uris = [uri] 

60 d.save() 

61 assert Uri.objects.filter(uri=uri).exists() 

62 assert Uri.objects.get(uri=uri).content_object == d