From 5fb9c10d6a4a31881d7f4fee9ec83876aeed050a Mon Sep 17 00:00:00 2001 From: diesieben07 Date: Sun, 31 Jan 2021 16:37:39 +0100 Subject: [PATCH] remove standalone test, include test case in regressions #274 --- tests/test_readonly_related_field.py | 39 ----- tests/test_readonly_related_field.yml | 222 -------------------------- tests/test_regressions.py | 33 ++++ 3 files changed, 33 insertions(+), 261 deletions(-) delete mode 100644 tests/test_readonly_related_field.py delete mode 100644 tests/test_readonly_related_field.yml diff --git a/tests/test_readonly_related_field.py b/tests/test_readonly_related_field.py deleted file mode 100644 index c7af95c2..00000000 --- a/tests/test_readonly_related_field.py +++ /dev/null @@ -1,39 +0,0 @@ -from django.db import models -from rest_framework import serializers, viewsets -from rest_framework.permissions import IsAuthenticatedOrReadOnly - -from tests import assert_schema, generate_schema - - -class ReferencedModel(models.Model): - id = models.AutoField(primary_key=True) - - -class ReferencingModel(models.Model): - id = models.UUIDField(primary_key=True) - referenced_model = models.ForeignKey(ReferencedModel, on_delete=models.CASCADE) - referenced_model_readonly = models.ForeignKey(ReferencedModel, on_delete=models.CASCADE) - referenced_model_m2m = models.ManyToManyField(ReferencedModel) - referenced_model_m2m_readonly = models.ManyToManyField(ReferencedModel) - - -class ReferencingModelSerializer(serializers.ModelSerializer): - - class Meta: - fields = ['id', 'referenced_model', 'referenced_model_readonly', - 'referenced_model_m2m', 'referenced_model_m2m_readonly'] - read_only_fields = ['id', 'referenced_model_readonly', 'referenced_model_m2m_readonly'] - model = ReferencingModel - - -class ReferencingModelViewset(viewsets.ModelViewSet): - permission_classes = [IsAuthenticatedOrReadOnly] - serializer_class = ReferencingModelSerializer - queryset = ReferencingModel.objects.all() - - -def test_readonly_related_field(no_warnings): - assert_schema( - generate_schema('referencing_model', ReferencingModelViewset), - 'tests/test_readonly_related_field.yml' - ) diff --git a/tests/test_readonly_related_field.yml b/tests/test_readonly_related_field.yml deleted file mode 100644 index 96eb71a3..00000000 --- a/tests/test_readonly_related_field.yml +++ /dev/null @@ -1,222 +0,0 @@ -openapi: 3.0.3 -info: - title: '' - version: 0.0.0 -paths: - /referencing_model/: - get: - operationId: referencing_model_list - description: '' - tags: - - referencing_model - security: - - cookieAuth: [] - - basicAuth: [] - - {} - responses: - '200': - content: - application/json: - schema: - type: array - items: - $ref: '#/components/schemas/ReferencingModel' - description: '' - post: - operationId: referencing_model_create - description: '' - tags: - - referencing_model - requestBody: - content: - application/json: - schema: - $ref: '#/components/schemas/ReferencingModel' - application/x-www-form-urlencoded: - schema: - $ref: '#/components/schemas/ReferencingModel' - multipart/form-data: - schema: - $ref: '#/components/schemas/ReferencingModel' - required: true - security: - - cookieAuth: [] - - basicAuth: [] - responses: - '201': - content: - application/json: - schema: - $ref: '#/components/schemas/ReferencingModel' - description: '' - /referencing_model/{id}/: - get: - operationId: referencing_model_retrieve - description: '' - parameters: - - in: path - name: id - schema: - type: string - format: uuid - description: A UUID string identifying this referencing model. - required: true - tags: - - referencing_model - security: - - cookieAuth: [] - - basicAuth: [] - - {} - responses: - '200': - content: - application/json: - schema: - $ref: '#/components/schemas/ReferencingModel' - description: '' - put: - operationId: referencing_model_update - description: '' - parameters: - - in: path - name: id - schema: - type: string - format: uuid - description: A UUID string identifying this referencing model. - required: true - tags: - - referencing_model - requestBody: - content: - application/json: - schema: - $ref: '#/components/schemas/ReferencingModel' - application/x-www-form-urlencoded: - schema: - $ref: '#/components/schemas/ReferencingModel' - multipart/form-data: - schema: - $ref: '#/components/schemas/ReferencingModel' - required: true - security: - - cookieAuth: [] - - basicAuth: [] - responses: - '200': - content: - application/json: - schema: - $ref: '#/components/schemas/ReferencingModel' - description: '' - patch: - operationId: referencing_model_partial_update - description: '' - parameters: - - in: path - name: id - schema: - type: string - format: uuid - description: A UUID string identifying this referencing model. - required: true - tags: - - referencing_model - requestBody: - content: - application/json: - schema: - $ref: '#/components/schemas/PatchedReferencingModel' - application/x-www-form-urlencoded: - schema: - $ref: '#/components/schemas/PatchedReferencingModel' - multipart/form-data: - schema: - $ref: '#/components/schemas/PatchedReferencingModel' - security: - - cookieAuth: [] - - basicAuth: [] - responses: - '200': - content: - application/json: - schema: - $ref: '#/components/schemas/ReferencingModel' - description: '' - delete: - operationId: referencing_model_destroy - description: '' - parameters: - - in: path - name: id - schema: - type: string - format: uuid - description: A UUID string identifying this referencing model. - required: true - tags: - - referencing_model - security: - - cookieAuth: [] - - basicAuth: [] - responses: - '204': - description: No response body -components: - schemas: - PatchedReferencingModel: - type: object - properties: - id: - type: string - format: uuid - readOnly: true - referenced_model: - type: integer - referenced_model_readonly: - type: integer - readOnly: true - referenced_model_m2m: - type: array - items: - type: integer - referenced_model_m2m_readonly: - type: array - items: - type: integer - readOnly: true - ReferencingModel: - type: object - properties: - id: - type: string - format: uuid - readOnly: true - referenced_model: - type: integer - referenced_model_readonly: - type: integer - readOnly: true - referenced_model_m2m: - type: array - items: - type: integer - referenced_model_m2m_readonly: - type: array - items: - type: integer - readOnly: true - required: - - id - - referenced_model - - referenced_model_m2m - - referenced_model_m2m_readonly - - referenced_model_readonly - securitySchemes: - basicAuth: - type: http - scheme: basic - cookieAuth: - type: apiKey - in: cookie - name: Session diff --git a/tests/test_regressions.py b/tests/test_regressions.py index 44b7f3a5..6c1864b6 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -1293,3 +1293,36 @@ def view_func(request, format=None): assert 'explode' in parameter assert 'style' in parameter assert parameter['schema']['type'] == 'array' + + +def test_incorrect_foreignkey_type_on_readonly_field(no_warnings): + class ReferencingModel(models.Model): + id = models.UUIDField(primary_key=True) + referenced_model = models.ForeignKey(SimpleModel, on_delete=models.CASCADE) + referenced_model_ro = models.ForeignKey(SimpleModel, on_delete=models.CASCADE) + referenced_model_m2m = models.ManyToManyField(SimpleModel) + referenced_model_m2m_ro = models.ManyToManyField(SimpleModel) + + class ReferencingModelSerializer(serializers.ModelSerializer): + indirect_referenced_model_ro = serializers.PrimaryKeyRelatedField( + source='referenced_model', + read_only=True, + ) + + class Meta: + fields = '__all__' + read_only_fields = ['id', 'referenced_model_ro', 'referenced_model_m2m_ro'] + model = ReferencingModel + + class ReferencingModelViewset(viewsets.ModelViewSet): + serializer_class = ReferencingModelSerializer + queryset = ReferencingModel.objects.all() + + schema = generate_schema('/x/', ReferencingModelViewset) + properties = schema['components']['schemas']['ReferencingModel']['properties'] + + assert properties['referenced_model']['type'] == 'integer' + assert properties['referenced_model_ro']['type'] == 'integer' + assert properties['referenced_model_m2m']['items']['type'] == 'integer' + assert properties['referenced_model_m2m_ro']['items']['type'] == 'integer' + assert properties['indirect_referenced_model_ro']['type'] == 'integer'