|
| 1 | +import pytest |
| 2 | +from django.db import models |
| 3 | +from django.test import TestCase |
| 4 | +from django.test.utils import isolate_apps |
| 5 | + |
| 6 | +from ansible_base.rbac.models import DABContentType |
| 7 | +from ansible_base.rbac.remote import RemoteObject |
| 8 | + |
| 9 | +from test_app.models import Inventory |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.django_db |
| 13 | +def test_post_migrate_creates_contenttype(): |
| 14 | + ct = DABContentType.objects.get(app_label="test_app", model="inventory") |
| 15 | + assert ct.service == "aap" |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.django_db |
| 19 | +class DABContentTypeTests(TestCase): |
| 20 | + """These tests originally came from Django contenttypes""" |
| 21 | + def setUp(self): |
| 22 | + DABContentType.objects.clear_cache() |
| 23 | + self.addCleanup(DABContentType.objects.clear_cache) |
| 24 | + |
| 25 | + def test_lookup_cache(self): |
| 26 | + with self.assertNumQueries(1): |
| 27 | + DABContentType.objects.get_for_model(Inventory) |
| 28 | + with self.assertNumQueries(0): |
| 29 | + ct = DABContentType.objects.get_for_model(Inventory) |
| 30 | + with self.assertNumQueries(0): |
| 31 | + DABContentType.objects.get_for_id(ct.id) |
| 32 | + with self.assertNumQueries(0): |
| 33 | + DABContentType.objects.get_by_natural_key( |
| 34 | + ct.service, |
| 35 | + ct.app_label, |
| 36 | + ct.model, |
| 37 | + ) |
| 38 | + DABContentType.objects.clear_cache() |
| 39 | + with self.assertNumQueries(1): |
| 40 | + DABContentType.objects.get_for_model(Inventory) |
| 41 | + |
| 42 | + @isolate_apps("tests") |
| 43 | + def test_get_for_model_create_contenttype(self): |
| 44 | + class ModelCreatedOnTheFly(models.Model): |
| 45 | + name = models.CharField(max_length=10) |
| 46 | + |
| 47 | + class Meta: |
| 48 | + app_label = "tests" |
| 49 | + |
| 50 | + ct = DABContentType.objects.get_for_model(ModelCreatedOnTheFly) |
| 51 | + assert ct.app_label == "tests" |
| 52 | + assert ct.model == "modelcreatedonthefly" |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.django_db |
| 56 | +def test_get_object_for_this_type_remote(): |
| 57 | + """Remote objects should return a remote proxy.""" |
| 58 | + ct = DABContentType.objects.create( |
| 59 | + service="remote_proj", |
| 60 | + app_label="testapp", |
| 61 | + model="book", |
| 62 | + ) |
| 63 | + |
| 64 | + obj = ct.get_object_for_this_type(pk=1) |
| 65 | + |
| 66 | + assert isinstance(obj, RemoteObject) |
| 67 | + assert obj.object_id == 1 |
| 68 | + assert obj.content_type == ct |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.django_db |
| 72 | +def test_get_all_objects_for_this_type_remote(): |
| 73 | + ct = DABContentType.objects.create( |
| 74 | + service="remote_proj2", |
| 75 | + app_label="testapp", |
| 76 | + model="book", |
| 77 | + ) |
| 78 | + |
| 79 | + objs = ct.get_all_objects_for_this_type(pk__in=[1, 2]) |
| 80 | + |
| 81 | + assert [o.object_id for o in objs] == [1, 2] |
| 82 | + assert all(isinstance(o, RemoteObject) for o in objs) |
0 commit comments