Skip to content

Commit

Permalink
gundotio#56: Test to simulate a view without model
Browse files Browse the repository at this point in the history
- Increasing the test scenarios
  • Loading branch information
wisersoftwareengineer committed Mar 27, 2023
1 parent 3e6cd37 commit 4d82b94
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
19 changes: 17 additions & 2 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from datetime import timedelta
from unittest.mock import patch
from uuid import uuid4
Expand Down Expand Up @@ -509,8 +511,21 @@ def test_user_update(client, db, method, user):
assert result["email"] == "[email protected]"


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/")



4 changes: 3 additions & 1 deletion tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@
path("user/", views.UserSelf.as_view()),
path("users/", views.UserList.as_view()),
path("users/<int:id>/", 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()),
]
18 changes: 12 additions & 6 deletions tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4d82b94

Please sign in to comment.