From 4d82b945792ecf46d80154947be9d77d1be543c1 Mon Sep 17 00:00:00 2001 From: Wiser Software Engineer Date: Mon, 27 Mar 2023 05:51:04 -0300 Subject: [PATCH] #56: Test to simulate a view without model - Increasing the test scenarios --- tests/test_views.py | 19 +++++++++++++++++-- tests/urls.py | 4 +++- tests/views.py | 18 ++++++++++++------ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/tests/test_views.py b/tests/test_views.py index 8bf2f65..7200657 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -1,3 +1,5 @@ +import pytest + from datetime import timedelta from unittest.mock import patch from uuid import uuid4 @@ -509,8 +511,21 @@ def test_user_update(client, db, method, user): assert result["email"] == "something@example.com" -def test_when_using_a_view_without_model_must_return_expected_result(client): - response = client.get("/without-model/") +def test_when_using_a_detail_view_without_model_must_return_expected_result(client): + response = client.get("/without-model/detail") assert response assert response.status_code == 200, response assert response.json() == {"field_name": "field_value"} + + +def test_when_using_a_list_view_without_model_must_return_expected_result(client): + with pytest.raises(AttributeError): + response = client.get("/without-model/") + + +def test_when_using_a_list_view_without_model_but_with_queryset_must_return_expected_result(client): + with pytest.raises(AttributeError): + response = client.get("/without-model-queryset/") + + + diff --git a/tests/urls.py b/tests/urls.py index 285ab7f..9e78e85 100644 --- a/tests/urls.py +++ b/tests/urls.py @@ -10,5 +10,7 @@ path("user/", views.UserSelf.as_view()), path("users/", views.UserList.as_view()), path("users//", views.UserDetail.as_view()), - path("without-model/", views.ViewWithoutModel.as_view()) + path("without-model/", views.ViewWithoutModelList.as_view()), + path("without-model/detail", views.ViewWithoutModelDetail.as_view()), + path("without-model-queryset/", views.ViewWithoutModelListWithQuerySet.as_view()), ] diff --git a/tests/views.py b/tests/views.py index 806128b..0aa730b 100644 --- a/tests/views.py +++ b/tests/views.py @@ -112,12 +112,18 @@ def get_instance(self): return self.request.user -class ViewWithoutModel(DetailAPI): - model = Model - serializer = None - - def get_instance(self): - return None +class ViewWithoutModelDetail(DetailAPI): + model = None def get(self, *args, **kwargs): return self.render_to_response(data={"field_name": "field_value"}) + + +class ViewWithoutModelList(ListAPI): + + def get(self, *args, **kwargs): + return self.render_to_response(data={"data": [{"field_name": "field_value"}]}) + + +class ViewWithoutModelListWithQuerySet(ListAPI): + pass