1

我无法追踪此测试失败的原因:

def test_CreateViewPost(self):
    user = self.client.login(username="g",password="g")

    # this method creates a poi type and returns it for me
    poiType = self.stubPOIType()
    # this creates a point (django.contrib.gis.geos.Point)
    point = Point(10,10)

    response = self.client.post(reverse("createPOI"),{"name":"testPOI",
                                                      "description":"description",
                                                      "type":poiType,
                                                      "geometry":point})

    # the post is failing. I have errors in my form
    self.assertTemplateUsed(response,"gazetteer/detailPOI.html")
    self.assertEqual(response.status_code,302)

    self.assertEqual(POI.objects.count(),1)

这是模型:

class POI(models.Model):
    name = models.CharField(max_length=128,verbose_name=u"Nome")
    description = models.CharField(max_length=1024,verbose_name=u"Descrição",null=True,blank=True)
    type = models.ForeignKey(POIType,verbose_name=u"Tipo de Ponto")
    geometry = models.PointField(verbose_name=u"Geometria",srid=4291)
    geohash = models.CharField(max_length=64,verbose_name=u"GeoHash",editable=False)

    objects = models.GeoManager()

我从表单中返回两个错误:字段几何和类型的“无效几何值”和“无效选择”。我正在将这些传递给帖子,但它失败了。

有小费吗?

编辑:我已经跟踪到“无效几何值”的失败是我的表单需要文本(WKT 表单)而不是实际的 GEOS 点。我已经将实际的 .wkt 添加到它并且它有效。但是外键呢?我应该通过pk吗?

为什么?

4

1 回答 1

0

创建 django 单元测试时,不能将完整对象(外键对象)传递给发布数据。您应该传递原语(id/pk)。

对于几何来说,它是一样的。你应该通过它的 WKT 表示。

所以:

def test_CreateViewPost(self):
    user = self.client.login(username="g",password="g")

    # this method creates a poi type and returns it for me
    poiType = self.stubPOIType()
    # this creates a point (django.contrib.gis.geos.Point)
    point = Point(10,10)

    response = self.client.post(reverse("createPOI"),{"name":"testPOI",
                                                      "description":"description",
                                                      "type":poiType.pk,
                                                      "geometry":point.wkt})

    # the post is failing. I have errors in my form
    self.assertTemplateUsed(response,"gazetteer/detailPOI.html")
    self.assertEqual(response.status_code,302)

    self.assertEqual(POI.objects.count(),1)
于 2012-04-12T03:31:32.067 回答