Skip to content

Commit

Permalink
Add test for unique named related model
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCoding committed Feb 6, 2024
1 parent 3bee8bf commit 3ebedad
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
1 change: 0 additions & 1 deletion test_app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ class Migration(migrations.Migration):
('description', models.TextField(blank=True, default='', help_text='The team description.')),
('created_by', models.ForeignKey(default=None, editable=False, help_text='The user who created this resource', null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='%(app_label)s_%(class)s_created+', to=settings.AUTH_USER_MODEL)),
('modified_by', models.ForeignKey(default=None, editable=False, help_text='The user who last modified this resource', null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='%(app_label)s_%(class)s_modified+', to=settings.AUTH_USER_MODEL)),
('scrum_master', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
('organization', models.ForeignKey(help_text='The organization of this team.', on_delete=django.db.models.deletion.CASCADE, related_name='teams', to='test_app.organization')),
('encryptioner', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='test_app.encryptionmodel'))
],
Expand Down
1 change: 0 additions & 1 deletion test_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def summary_fields(self):


class Team(AbstractTeam):
scrum_master = models.ForeignKey(User, on_delete=models.SET_NULL, null=True) # used to test related links
encryptioner = models.ForeignKey('EncryptionModel', on_delete=models.SET_NULL, null=True)


Expand Down
16 changes: 14 additions & 2 deletions test_app/tests/lib/serializers/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from ansible_base.authentication.models import AuthenticatorMap
from ansible_base.lib.serializers.common import CommonModelSerializer
from ansible_base.lib.utils.encryption import ENCRYPTED_STRING
from test_app.models import EncryptionModel
from test_app.serializers import EncryptionTestSerializer
from test_app.models import EncryptionModel, Team
from test_app.serializers import EncryptionTestSerializer, TeamSerializer


@pytest.mark.django_db
Expand Down Expand Up @@ -99,3 +99,15 @@ def test_summary_of_model_with_created_user(user, ldap_authenticator):
'created_by': f'/api/v1/users/{user.pk}/',
'modified_by': f'/api/v1/users/{user.pk}/',
}


@pytest.mark.django_db
def test_summary_of_model_with_custom_reverse(user, organization):
team = Team.objects.create(
name='foo-team', encryptioner=EncryptionModel.objects.create(name='iamencrypted', testing1='foo', testing2='bar'), organization=organization
)
serializer = TeamSerializer()

assert serializer._get_summary_fields(team)['encryptioner'] == {'id': team.encryptioner_id, 'name': 'iamencrypted'}

assert serializer._get_related(team)['encryptioner'] == f'/api/v1/encrypted_models/{team.encryptioner_id}/'

0 comments on commit 3ebedad

Please sign in to comment.