def check_tfl_destination_codes(): """ Audit codes we have recorded and make sure that they are all fine """ # Check to see if destination is in our database geodata = RailStationLocations() database = WMTDatabase("whensmytube.destinationcodes.db") rows = database.get_rows("SELECT destination_name, destination_code, line_code FROM destination_codes") for (destination_name, destination_code, line_code) in rows: # Hack: Fake a ElementTree object to use the XML parser's tube train filter function fake_tag = lambda x: 1 fake_tag.attrib = {'Destination': destination_name, 'DestCode': str(destination_code)} if not filter_tube_train(fake_tag): continue train = TubeTrain(destination_name, "Northbound", "1200", "C", "001") destination = train.get_destination_no_via() if not destination.endswith("Train") and not geodata.find_fuzzy_match(destination, {}): print "Destination %s (%s) on %s not found in locations database" % (destination, destination_code, line_code) via = train.get_via() if via and not geodata.find_fuzzy_match(via, {}): print "Via %s (%s) on %s not found in locations database" % (via, destination_code, line_code)
def test_models(self): """ Unit tests for train, bus, station and bus stop objects """ # Location fundamentals location_name = "Trafalgar Square" location = Location(location_name) self.assertEqual(str(location), location_name) self.assertEqual(repr(location), location_name) self.assertEqual(len(location), len(location_name)) # BusStop fundamentals bus_stop = BusStop("TRAFALGAR SQUARE / CHARING CROSS STATION <> # [DLR] >T<", bus_stop_code='10000', distance=2.0, run=1) bus_stop2 = BusStop("CHARING CROSS STATION <> # [DLR} >T< / TRAFALGAR SQUARE", bus_stop_code='10001', distance=1.0, run=2) self.assertLess(bus_stop2, bus_stop) self.assertEqual(len(bus_stop), 26) self.assertEqual(hash(bus_stop), hash(BusStop("TRAFALGAR SQUARE / CHARING CROSS STATION <> # [DLR] >T<", run=1))) # BusStop complex functions for undesirable in ('<>', '#', r'\[DLR\]', '>T<'): self.assertNotRegexpMatches(bus_stop.get_clean_name(), undesirable) self.assertEqual(bus_stop.get_clean_name(), "Trafalgar Square / Charing Cross Station") self.assertEqual(bus_stop.get_normalised_name(), "TRAFALGARSQCHARINGCROSSSTN") self.assertEqual(bus_stop.get_similarity(bus_stop.name), 100) self.assertEqual(bus_stop.get_similarity("Charing Cross Station"), 94) self.assertEqual(bus_stop2.get_similarity("Charing Cross Station"), 95) self.assertEqual(bus_stop.get_similarity("Charing Cross"), 90) self.assertEqual(bus_stop2.get_similarity("Charing Cross"), 91) # RailStation complex functions station = RailStation("King's Cross St. Pancras", "KXX", 530237, 182944) station2 = RailStation("Earl's Court", "ECT") self.assertEqual(station.get_abbreviated_name(), "Kings X St P") self.assertEqual(station2.get_abbreviated_name(), "Earls Ct") self.assertEqual(station.get_similarity(station.name), 100) self.assertGreaterEqual(station.get_similarity("Kings Cross St Pancras"), 95) self.assertGreaterEqual(station.get_similarity("Kings Cross St Pancreas"), 90) self.assertGreaterEqual(station.get_similarity("Kings Cross"), 90) # Departure departure = Departure("Trafalgar Square", "2359") departure2 = Departure("Trafalgar Square", "0001") self.assertLess(departure, departure2) # Fails if test run at 0000-0059 self.assertEqual(hash(departure), hash(Departure("Trafalgar Square", "2359"))) self.assertEqual(str(departure), "Trafalgar Square 2359") self.assertEqual(departure.get_destination(), "Trafalgar Square") self.assertEqual(departure.get_departure_time(), "2359") # NullDeparture null_departure = NullDeparture("East") self.assertEqual(null_departure.get_destination(), "None shown going East") self.assertEqual(null_departure.get_departure_time(), "") # Bus bus = Bus("Blackwall", "2359") bus2 = Bus("Blackwall", "0001") self.assertLess(bus, bus2) # Fails if test run at 0000-0059 self.assertEqual(bus.get_destination(), "Blackwall") # Train train = Train("Charing Cross via Bank", "2359") train2 = Train("Charing Cross via Bank", "0001") self.assertLess(train, train2) # Fails if test run at 0000-0059 self.assertEqual(train.get_destination(), "Charing Cross via Bank") # TubeTrain tube_train = TubeTrain("Charing Cross via Bank", "Northbound", "2359", "N", "001") tube_train2 = TubeTrain("Charing Cross via Bank then depot", "Northbound", "2359", "N", "001") tube_train3 = TubeTrain("Northern Line", "Northbound", "2359", "N", "001") tube_train4 = TubeTrain("Heathrow T123 + 5", "Westbound", "2359", "P", "001") self.assertEqual(hash(tube_train), hash(tube_train2)) self.assertEqual(tube_train.get_destination(), "Charing Cross via Bank") self.assertEqual(tube_train3.get_destination(), "Northbound Train") self.assertEqual(tube_train4.get_destination(), "Heathrow Terminal 5") self.assertEqual(tube_train.get_destination_no_via(), "Charing Cross") self.assertEqual(tube_train.get_via(), "Bank") # DLRTrain dlr_train = DLRTrain("Beckton", "1200") self.assertEqual(dlr_train.line_code, "DLR") # DepartureCollection fundamentals departures = DepartureCollection() departures[bus_stop] = [bus] self.assertEqual(departures[bus_stop], [bus]) self.assertEqual(len(departures), 1) del departures[bus_stop] self.assertFalse(bus_stop in departures) # DepartureCollection for trains departures.add_to_slot(bus_stop, bus) departures.add_to_slot(bus_stop, bus2) self.assertEqual(str(departures), "Trafalgar Square / Charing Cross Station to Blackwall 2359 0001") departures[bus_stop2] = [] departures.cleanup(lambda stop: NullDeparture("West")) self.assertEqual(str(departures), "None shown going West; Trafalgar Square / Charing Cross Station to Blackwall 2359 0001") departures.add_to_slot(bus_stop, Bus("Leamouth", "2358")) self.assertEqual(str(departures), "None shown going West; Trafalgar Square / Charing Cross Station to Leamouth 2358, Blackwall 2359 0001") # DepartureCollection for trains departures = DepartureCollection() departures["P1"] = [Train("Bank", "1210"), Train("Tower Gateway", "1203"), Train("Bank", "1200")] departures["P2"] = [Train("Tower Gateway", "1205"), Train("Tower Gateway", "1212"), Train("Bank", "1207")] departures["P3"] = [Train("Lewisham", "1200"), Train("Lewisham", "1204"), Train("Lewisham", "1208")] departures["P4"] = [] departures.merge_common_slots() departures.cleanup(lambda platform: NullDeparture("from %s" % platform)) self.assertEqual(str(departures), "Bank 1200 1207 1210, Tower Gateway 1203 1205; Lewisham 1200 1204 1208; None shown going from P4") departures.filter(lambda train: train.get_destination() != "Lewisham") self.assertEqual(str(departures), "Bank 1200 1207 1210, Tower Gateway 1203 1205; None shown going from P4") departures["P4"] = [] departures.filter(lambda train: train.get_destination() != "Tower Gateway", True) self.assertEqual(str(departures), "Bank 1200 1207 1210")