1

使用 django rest framworks 视图集和模型序列化程序时,您需要编写测试吗?

使用 djangos 或 django rest 框架基类(如 API 视图或序列化程序)的自定义实现代码,我可以看到需要为其编写测试,但是当您使用模型序列化程序或视图集时,它们彼此紧密连接并且需要最少的代码我们写测试吗?

我想继续测试驱动开发,但我不知道如何为这些案例编写测试。

这里的示例是从 URL 到 View 到序列化程序的代码 要建模如何进行测试?

网址.py

router = DefaultRouter()
router.register(r'city-list', CityListViewSet, base_name='city-list')
router.register(r'venue-filter-options-list/(?P<city>[a-zA-Z]+)',VenueFilterOptionsViewSet, base_name='venue-filter-options'),
router.register(r'neighborhood-list/(?P<city>[a-zA-Z]+)', NeighborhoodListViewSet,
 base_name='neighborhood-list',


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^api/', include(router.urls)),
]

视图.py

"""
grabs all filter params for the browse venues page
"""
class VenueFilterOptionsViewSet(viewsets.ViewSet):


    def list(self, request, city):
        self.city = city
        values = Values(
            city = City.objects.all(),
            neighborhood = Neighborhood.objects.filter(city= self.city),
            cuisine=Cuisine.objects.all(),
            privacy= Privacy.objects.all(),
            venueType=VenueType.objects.all(),
            amenities=Amenities.objects.all()
        )
        serializer = VenueFilterOptionsSerializer(values)
        return response(serializer.data)

序列化程序.py

class NeighborhoodSerializer(serializers.ModelSerializer):
    class Meta:
        model = Neighborhood
        fields = 'neighborhood'

class VenueSerializer(serializers.ModelSerializer):
    class Meta:
        model = Venue
        fields = '__all__'


class CuisineSerializer(serializers.ModelSerializer):
    class Meta:
        model = Cuisine

class PrivacySerializer(serializers.ModelSerializer):
    class Meta:
        model = Privacy

class VenueTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = VenueType

class AmenitiesSerializer(serializers.ModelSerializer):
    class Meta:
        model = Amenities
# end child serializers for VenueFilterOptionsSerializer


class VenueFilterOptionsSerializer(serializers.Serializer):
    city = CitySerializer(many=True)
    ''' reduntant use of city as cost is low, and needed in case of non site api call, or if user navigates
    to browse venues page before heading to landing page'''

    neighborhood = NeighborhoodSerializer(many=True)
    cuisine = CuisineSerializer(many=True)
    privacy = PrivacySerializer(many=True)
    venueType = VenueTypeSerializer(many=True)
    amenities = AmenitiesSerializer(many=True)

模型.py

# models
class Venue(models.Model):
    name = models.CharField(max_length=150, blank=False)
    description = models.CharField(max_length=1000)
    image = models.ImageField(upload_to=imgUnique('venueMedia/venueImages'))
    streetAddress= models.CharField(max_length=100)
    city=models.ForeignKey(City)
    neighborhood=models.ForeignKey(Neighborhood)
    state = models.CharField(max_length=100, blank=False)
    cuisine = models.ManyToManyField(Cuisine)
    venueType = models.ManyToManyField(VenueType)
    Amenities= models.ManyToManyField(Amenities)
4

0 回答 0