def test_travelcat(self): # test getsubdests p = Place.objects.get(name="Krabi") sd = travelcat.getAllSubDestinations(p) self.assertEqual(len(sd), 1, "subdests...found " + str(len(sd))) p = Place.objects.get(name="Southern Thailand") sd = travelcat.getAllSubDestinations(p) self.assertEqual(len(sd), 5, "subdests...found " + str(len(sd))) p = Place.objects.get(name="Thailand") sd = travelcat.getAllSubDestinations(p) self.assertEqual(len(sd), 7, "subdests...found " + str(len(sd))) # test getratings p = Place.objects.get(name="Krabi") r = p.get_ratings() self.assertEqual(r.nature, 4.5) self.assertEqual(r.relaxing, 2) # search for location region = Place.objects.get(name="Thailand") self.userp.prefs.social = 5 # party animal time self.userp.prefs.relaxing = 2 self.userp.prefs.budget = 4 # this currently fails if the postgres stored proc is called. # worked around by specifying "django = True" to compute in django space; # that's not how prod works, and not really the path we want test coverage # on, but at least it's an approximation :/ dests = travelcat.get_destinations(self.userp, region, django=True) self.assertTrue( len(dests) > 1, "Only got {} dests back: {}".format(len(dests), dests)) # first item in tuple should be name # this depends on how name is formatted in get_destinations; is rather # arbitrary at moment. self.assertEqual(dests[0][ 0], "Ko Pha Ngan, Thailand", "Party animal wwanted KPNG; got " + dests[0][0]) self.userp.prefs.social = 1 # need to rest self.userp.prefs.relaxing = 4 dests = travelcat.get_destinations(self.userp, region, django=True) self.assertTrue(len(dests) > 1) # print(dests) self.assertEqual( dests[0][0], "Ko Lanta, Thailand", "expecting Ko Lanta; got " + dests[0][0])
def test_leafdescendants(self): p = Place.objects.get(name="Ko Tao") self.assertEqual(p.leafdescendants.count(), 1) self.assertEqual(len(travelcat.computeAllSubDestinations(p)), 1) self.assertEqual(len(travelcat.getAllSubDestinations(p)), 1) pth = Place.objects.get(name="Thailand") self.assertTrue(p in pth.leafdescendants.all()) newp = Place(name="newplace", parent=p) newp.save() self.assertEqual(newp.leafdescendants.count(), 1) self.assertEqual(len(travelcat.computeAllSubDestinations(newp)), 1) self.assertEqual(len(travelcat.getAllSubDestinations(newp)), 1) self.assertEqual(p.leafdescendants.count(), 1) self.assertEqual(len(travelcat.computeAllSubDestinations(p)), 1) self.assertEqual(len(travelcat.getAllSubDestinations(p)), 1) self.assertFalse(p in pth.leafdescendants.all()) self.assertTrue(newp in pth.leafdescendants.all()) # all ancestors, self included self.assertEqual(len(newp.leafancestors.all()), 7) pe = Place.objects.get(name="Earth") pl = Place(name="Livermore", parent=pe) pl.save() self.assertTrue(pl in pe.leafdescendants.all()) self.assertTrue(pl in pl.leafdescendants.all()) po = Place(name="Orgahousia", parent=pl) po.save() # o is the leaf node now, l is not self.assertFalse(pl in pe.leafdescendants.all()) self.assertFalse(pl in pl.leafdescendants.all()) self.assertTrue(po in pe.leafdescendants.all()) self.assertTrue(po in pl.leafdescendants.all()) pbr = Place(name="Big Room", parent=po) pbr.save() # br is the leaf now self.assertFalse(po in pe.leafdescendants.all()) self.assertFalse(po in po.leafdescendants.all()) self.assertTrue(pbr in pe.leafdescendants.all()) self.assertTrue(pbr in po.leafdescendants.all()) self.assertTrue(pbr in pl.leafdescendants.all()) # move place p = Place.objects.get(name="Ko Pha Ngan") po.parent = p po.save() self.assertTrue(pl in pe.leafdescendants.all()) self.assertTrue(pl in pl.leafdescendants.all()) self.assertTrue(pbr in pe.leafdescendants.all()) self.assertTrue(pbr in po.leafdescendants.all()) self.assertTrue(pbr in p.leafdescendants.all()) # not in livermore anymore self.assertFalse(pbr in pl.leafdescendants.all()) self.assertFalse(po in pe.leafdescendants.all()) self.assertFalse(p in pe.leafdescendants.all()) self.assertFalse(p in p.leafdescendants.all()) # delete place pbr.delete() po = Place.objects.get(name="Orgahousia") self.assertTrue(po in pe.leafdescendants.all()) self.assertTrue(po in po.leafdescendants.all())
def meow(request, template_name): ''' search for place(s) based on user input ''' vardict = {} if request.method == 'POST': f = forms.MeowForm(request.POST) if f.is_valid(): name = f.cleaned_data['destregion'] regions = search_place(name) if len(regions) == 0: # no such place in tc system -- check google try: locinfo = geocode.get_global_loc(name) regions = search_place(locinfo['addr']) if regions: # found it after all, google must have polished the name # a little for us pass else: # google recognizes it but we don't; let's clean the name a little and try again regions = search_place(locinfo['name']+", "+locinfo['country']) if regions: # found it pass else: # still don't recognize it; that means we should # add a place into our hierarchy and go directly to the # detail page locaddr = locinfo['addr'] newplaces = [] # we know we can't find it as-is, so try to go back up the # parent chain until we find something in common. # probably a country or province. try: while not regions: pname = locaddr[:locaddr.index(", ")] locaddr = locaddr[locaddr.index(", ")+2:] newplaces.insert(0, (pname, locinfo['lat'], locinfo['lng'], locinfo['radiuskm'])) # assume we are looking for a region regions = search_place(locaddr, True) if len(regions) != 1: logger.info ("Trying to add {} from Google, came up with {} existing regions for {}.". \ format(locinfo['addr'], len(regions), locaddr)) except ValueError: # if we searched through all components of the address, # that means either we don't even have the country, or # Something Awkward is happening. logger.info ("Trying to add {} from Google, can't find anything on that path.". \ format(locinfo['addr'])) raise if regions and regions[0].is_leaf(): # recognized region is a leaf. fall through and route to the leaf pass elif regions: # create link(s), so that user can verify what they # want to add. placedetail screen will note the 0 id, # and add the place based on our session data. # at present, we only look at google's best guess, so # there's only one possible place to add; either the user # verifies it, or searches differently. vardict[TC_MESSAGE] = "A new place! Click to add to the system:" vardict[TC_LINKS] = [(0, locinfo['addr'], reverse('travelcat:placedetail', args=(0,)))] newplaces.append(regions[0].id) # node where we will add new places request.session[TC_NEWPLACEDATA] = newplaces # force no results, so we stay on search screen, until user clicks regions=[] except Exception as e: # can't geolocate given address. vardict[TC_MESSAGE] = "Unable to locate any place named '{}'.".format(name) logger.exception("Unable to locate place '{}'".format(name)) if len(regions) >1: # ambiguous place -- send back for refinement vardict[TC_MESSAGE] = "{} matching areas found. Which do you mean?".format(len(regions)) vardict[TC_LINKS] = [(r.id, r.get_map_desc(), reverse('travelcat:meow', args=(r.id,)) ) for r in regions] elif len(regions) == 1: # start the pref based search try: return search_and_display_dests(request, regions[0]) except: leaves = travelcat.getAllSubDestinations(regions[0]) logger.info("Failed to redirect for {};\n but {} leaves in system\n ...maybe place or user does not have all prefs set, and SP got confused?".format(regions[0], len(leaves))) vardict[TC_MESSAGE] = "Unable to show destinations for {}; system doesn't yet have enough information about this destination.".format(regions[0]) else: # get f = forms.MeowForm() if TC_MESSAGE in request.session: vardict[TC_MESSAGE] = request.session[TC_MESSAGE] del request.session[TC_MESSAGE] vardict['form'] = f #logger.info ("after meow, passing to template: {}".format(vardict)) return render_to_response(template_name, RequestContext(request, vardict))