'Testing the \'relate\' lookup type.'

@skipUnlessDBFeature('supports_relate_lookup')
def test_relate_lookup(self):

    pnt1 = fromstr('POINT    (649287.0363174    4177429.4494686)', srid=2847)
    pnt2 = fromstr('POINT(-98.4919715741052    29.4333344025053)', srid=4326)
    with self.assertRaises(ValueError):
        Country.objects.filter(mpoly__relate=(23, 'foo'))
    for (bad_args, e) in [((pnt1, 0), ValueError), ((pnt2, 'T*T***FF*', 0), ValueError)]:
        qs = Country.objects.filter(mpoly__relate=bad_args)
        with self.assertRaises(e):
            qs.count()
    if (postgis or spatialite):
        contains_mask = 'T*T***FF*'
        within_mask = 'T*F**F***'
        intersects_mask = 'T********'
    elif oracle:
        contains_mask = 'contains'
        within_mask = 'inside'
        intersects_mask = 'overlapbdyintersect'
    self.assertEqual('Texas', Country.objects.get(mpoly__relate=(pnt1, contains_mask)).name)
    self.assertEqual('Texas', Country.objects.get(mpoly__relate=(pnt2, contains_mask)).name)
    ks = State.objects.get(name='Kansas')
    self.assertEqual('Lawrence', City.objects.get(point__relate=(ks.poly, within_mask)).name)
    if (not oracle):
        self.assertEqual('Texas', Country.objects.get(mpoly__relate=(pnt1, intersects_mask)).name)
        self.assertEqual('Texas', Country.objects.get(mpoly__relate=(pnt2, intersects_mask)).name)
        self.assertEqual('Lawrence', City.objects.get(point__relate=(ks.poly, intersects_mask)).name)
    mask = ('anyinteract' if oracle else within_mask)
    self.assertFalse(City.objects.exclude(point__relate=(functions.Union('point', 'point'), mask)))
