33import pytest
44from django .urls import reverse
55
6+ from example .factories import ArtProjectFactory , ProjectTypeFactory
7+
68pytestmark = pytest .mark .django_db
79
810
@@ -57,13 +59,22 @@ def test_polymorphism_on_polymorphic_model_detail_patch(single_art_project, clie
5759def test_polymorphism_on_polymorphic_model_list_post (client ):
5860 test_topic = 'New test topic {}' .format (random .randint (0 , 999999 ))
5961 test_artist = 'test-{}' .format (random .randint (0 , 999999 ))
62+ test_project_type = ProjectTypeFactory ()
6063 url = reverse ('project-list' )
6164 data = {
6265 'data' : {
6366 'type' : 'artProjects' ,
6467 'attributes' : {
6568 'topic' : test_topic ,
6669 'artist' : test_artist
70+ },
71+ 'relationships' : {
72+ 'projectType' : {
73+ 'data' : {
74+ 'type' : 'projectTypes' ,
75+ 'id' : test_project_type .pk
76+ }
77+ }
6778 }
6879 }
6980 }
@@ -73,6 +84,22 @@ def test_polymorphism_on_polymorphic_model_list_post(client):
7384 assert content ['data' ]['type' ] == "artProjects"
7485 assert content ['data' ]['attributes' ]['topic' ] == test_topic
7586 assert content ['data' ]['attributes' ]['artist' ] == test_artist
87+ assert content ['data' ]['relationships' ]['projectType' ]['data' ]['id' ] == \
88+ str (test_project_type .pk )
89+
90+
91+ def test_polymorphism_on_polymorphic_model_w_included_serializers (client ):
92+ test_project = ArtProjectFactory ()
93+ query = '?include=projectType'
94+ url = reverse ('project-list' )
95+ response = client .get (url + query )
96+ content = response .json ()
97+ assert content ['data' ][0 ]['id' ] == str (test_project .pk )
98+ assert content ['data' ][0 ]['type' ] == 'artProjects'
99+ assert content ['data' ][0 ]['relationships' ]['projectType' ]['data' ]['id' ] == \
100+ str (test_project .project_type .pk )
101+ assert content ['included' ][0 ]['type' ] == 'projectTypes'
102+ assert content ['included' ][0 ]['id' ] == str (test_project .project_type .pk )
76103
77104
78105def test_polymorphic_model_without_any_instance (client ):
0 commit comments