@@ -749,6 +749,10 @@ class NamingCollisionView(generics.RetrieveUpdateDestroyAPIView):
749
749
serializer_class = BasicModelSerializer
750
750
751
751
752
+ class BasicNamingCollisionView (generics .RetrieveAPIView ):
753
+ queryset = BasicModel .objects .all ()
754
+
755
+
752
756
class NamingCollisionViewSet (GenericViewSet ):
753
757
"""
754
758
Example via: https://stackoverflow.com/questions/43778668/django-rest-framwork-occured-typeerror-link-object-does-not-support-item-ass/
@@ -779,9 +783,35 @@ def test_manually_routing_nested_routes(self):
779
783
]
780
784
781
785
generator = SchemaGenerator (title = 'Naming Colisions' , patterns = patterns )
786
+ schema = generator .get_schema ()
782
787
783
- with pytest .raises (ValueError ):
784
- generator .get_schema ()
788
+ expected = coreapi .Document (
789
+ url = '' ,
790
+ title = 'Naming Colisions' ,
791
+ content = {
792
+ 'test' : {
793
+ 'list' : {
794
+ 'list' : coreapi .Link (url = '/test/list/' , action = 'get' )
795
+ },
796
+ 'list_0' : coreapi .Link (url = '/test' , action = 'get' )
797
+ }
798
+ }
799
+ )
800
+
801
+ assert expected == schema
802
+
803
+ def _verify_cbv_links (self , loc , url , methods = None , suffixes = None ):
804
+ if methods is None :
805
+ methods = ('read' , 'update' , 'partial_update' , 'delete' )
806
+ if suffixes is None :
807
+ suffixes = (None for m in methods )
808
+
809
+ for method , suffix in zip (methods , suffixes ):
810
+ if suffix is not None :
811
+ key = '{}_{}' .format (method , suffix )
812
+ else :
813
+ key = method
814
+ assert loc [key ].url == url
785
815
786
816
def test_manually_routing_generic_view (self ):
787
817
patterns = [
@@ -797,18 +827,68 @@ def test_manually_routing_generic_view(self):
797
827
798
828
generator = SchemaGenerator (title = 'Naming Colisions' , patterns = patterns )
799
829
800
- with pytest .raises (ValueError ):
801
- generator .get_schema ()
830
+ schema = generator .get_schema ()
831
+
832
+ self ._verify_cbv_links (schema ['test' ]['delete' ], '/test/delete/' )
833
+ self ._verify_cbv_links (schema ['test' ]['put' ], '/test/put/' )
834
+ self ._verify_cbv_links (schema ['test' ]['get' ], '/test/get/' )
835
+ self ._verify_cbv_links (schema ['test' ]['update' ], '/test/update/' )
836
+ self ._verify_cbv_links (schema ['test' ]['retrieve' ], '/test/retrieve/' )
837
+ self ._verify_cbv_links (schema ['test' ], '/test' , suffixes = (None , '0' , None , '0' ))
802
838
803
839
def test_from_router (self ):
804
840
patterns = [
805
841
url (r'from-router' , include (naming_collisions_router .urls )),
806
842
]
807
843
808
844
generator = SchemaGenerator (title = 'Naming Colisions' , patterns = patterns )
845
+ schema = generator .get_schema ()
846
+ desc = schema ['detail_0' ].description # not important here
847
+
848
+ expected = coreapi .Document (
849
+ url = '' ,
850
+ title = 'Naming Colisions' ,
851
+ content = {
852
+ 'detail' : {
853
+ 'detail_export' : coreapi .Link (
854
+ url = '/from-routercollision/detail/export/' ,
855
+ action = 'get' ,
856
+ description = desc )
857
+ },
858
+ 'detail_0' : coreapi .Link (
859
+ url = '/from-routercollision/detail/' ,
860
+ action = 'get' ,
861
+ description = desc
862
+ )
863
+ }
864
+ )
865
+
866
+ assert schema == expected
867
+
868
+ def test_url_under_same_key_not_replaced (self ):
869
+ patterns = [
870
+ url (r'example/(?P<pk>\d+)/$' , BasicNamingCollisionView .as_view ()),
871
+ url (r'example/(?P<slug>\w+)/$' , BasicNamingCollisionView .as_view ()),
872
+ ]
873
+
874
+ generator = SchemaGenerator (title = 'Naming Colisions' , patterns = patterns )
875
+ schema = generator .get_schema ()
876
+
877
+ assert schema ['example' ]['read' ].url == '/example/{id}/'
878
+ assert schema ['example' ]['read_0' ].url == '/example/{slug}/'
879
+
880
+ def test_url_under_same_key_not_replaced_another (self ):
881
+
882
+ patterns = [
883
+ url (r'^test/list/' , simple_fbv ),
884
+ url (r'^test/(?P<pk>\d+)/list/' , simple_fbv ),
885
+ ]
886
+
887
+ generator = SchemaGenerator (title = 'Naming Colisions' , patterns = patterns )
888
+ schema = generator .get_schema ()
809
889
810
- with pytest . raises ( ValueError ):
811
- generator . get_schema ()
890
+ assert schema [ 'test' ][ 'list' ][ 'list' ]. url == '/test/list/'
891
+ assert schema [ 'test' ][ 'list' ][ 'list_0' ]. url == '/test/{id}/list/'
812
892
813
893
814
894
def test_is_list_view_recognises_retrieve_view_subclasses ():
0 commit comments