I’m unable to track why this test is failing:
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)
This is the model:
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()
I’m getting back two errors from the form: “Invalid geometry value” and “Invalid choice” for fields geometry and type respectively. I’m passing those to the post, but it’s failing.
Any tips?
EDIT: i’ve tracked the failure of “invalid geometry value” is that my form is expecting text (WKT form) instead of an actual GEOS point. I’ve added the acutal .wkt to it and it worked. But how about the foreign key? Should I pass the pk?
Why?
When you are creating django unit tests, you cannot pass the full objects (foreigned key objects) to the post data. You should pass primitives (id/pk).
For geometries it’s the same. You should pass it’s WKT representation.
So: