def test_json_success(self): since = datetime.datetime(2013, 1, 2, 3, 0, 0) departures = [ Departure("ZOO", "HBF", datetime.datetime(2013, 1, 2, 3, 4, 0), "S5", since=since), Departure("ZOO", "ERP", datetime.datetime(2013, 1, 2, 3, 5, 0), "U2", since=since) ] json1 = [[ 'ZOO', [ { 'start': 'ZOO', 'line': 'S5', 'end': 'HBF', 'remaining': 240, }, { 'start': 'ZOO', 'line': 'U2', 'end': 'ERP', 'remaining': 300, }, ] ]] response = Response(True, "ZOO", departures) self.assertEqual(json.loads(response.json), json1)
def test_merge(self): departures = [Departure("ERP", "HBF", datetime.datetime(2013, 1, 2, 3, 4, 1), "U2"), Departure("HBF", "ERP", datetime.datetime(2013, 2, 1, 3, 4, 1), "U55")] departures2 = [Departure("ERP", "HBF", datetime.datetime(2013, 1, 2, 3, 4, 1), "U6"), Departure("HBF", "ERP", datetime.datetime(2013, 2, 1, 3, 4, 1), "U9")] allDepartures = [("Marchbrücke", departures), ("S Tiergarten", departures2)] r1 = Response(True, "Marchbrücke", departures) r2 = Response(True, "S Tiergarten", departures2) r3 = Response(False, []) self.assertRaises(ValueError, r1.merge, r3) self.assertRaises(ValueError, r3.merge, r2) self.assertRaises(TypeError, r1.merge, departures) r1.merge(r2) self.assertEqual(r1.departures, allDepartures)
def call(self): params = { 'input': self.station_enc, 'time': timeformat(datetime.datetime.now()), 'date': dateformat(datetime.datetime.now()), 'productsFilter': self.vehicles, 'maxJourneys': self.limit, 'start': 'yes' } response = requests.get(SCHEDULED_API_ENDPOINT, params=params) if response.ok: soup = BeautifulSoup(response.text) if soup.find('span', 'error'): # The station we are looking for is ambiguous or does not exist stations = soup.find('span', 'select').find_all('a') if stations: # The station is ambiguous stationlist = [s.text.strip() for s in stations] return Response(False, stationlist) else: # The station does not exist return Response(False, []) else: # The station seems to exist tbody = soup.find('tbody') if tbody is None: return Response(True, self.station, []) rows = tbody.find_all('tr') departures = [] for row in rows: tds = row.find_all('td') dep = Departure(start=self.station, end=tds[2].text.strip(), when=tds[0].text.strip(), line=tds[1].text.strip()) departures.append(dep) return Response(True, self.station, departures) else: try: response.raise_for_status() except requests.RequestException as e: return Response(False, error=e) else: return Response(False, error=Exception("An unknown error occured"))
def call(self): params = {'input': self.station_enc, 'start': 'suchen'} response = requests.get(ACTUAL_API_ENDPOINT, params=params) if response.ok: soup = BeautifulSoup(response.text) if soup.find_all('form'): # The station we are looking for is ambiguous or does not exist stations = soup.find_all('option') if stations: # The station is ambiguous stationlist = [s.get('value') for s in stations] return Response(False, stationlist) else: # The station does not exist return Response(False) else: # The station seems to exist result = soup.find('div', { 'id': '', 'class': 'ivu_result_box' }) if result is None: return Response(True, self.station, []) rows = result.find_all('tr') departures = [] for row in rows: if row.parent.name == 'tbody': td = row.find_all('td') if td: dep = Departure(start=self.station, end=td[2].text.strip(), when=td[0].text.strip(), line=td[1].text.strip()) departures.append(dep) return Response(True, self.station, departures) else: try: response.raise_for_status() except requests.RequestException as e: return Response(False, error=e) else: return Response(False, error=Exception("An unknown error occured"))
def test_merge(self): departures = [ Departure("ERP", "HBF", datetime.datetime(2013, 1, 2, 3, 4, 1), "U2"), Departure("HBF", "ERP", datetime.datetime(2013, 2, 1, 3, 4, 1), "U55") ] departures2 = [ Departure("ERP", "HBF", datetime.datetime(2013, 1, 2, 3, 4, 1), "U6"), Departure("HBF", "ERP", datetime.datetime(2013, 2, 1, 3, 4, 1), "U9") ] allDepartures = [("Marchbrücke", departures), ("S Tiergarten", departures2)] r1 = Response(True, "Marchbrücke", departures) r2 = Response(True, "S Tiergarten", departures2) r3 = Response(False, []) self.assertRaises(ValueError, r1.merge, r3) self.assertRaises(ValueError, r3.merge, r2) self.assertRaises(TypeError, r1.merge, departures) r1.merge(r2) self.assertEqual(r1.departures, allDepartures)
def test_init_error_string(self): response = Response(False, error="An error occurred") self.assertFalse(response.state) self.assertEqual(str(response.error), "An error occurred")
def test_init_not_found(self): response = Response(False) self.assertFalse(response.state) self.assertEqual(str(response.error), "Station does not exist")
def test_init_ambiguous(self): response = Response(False, ['s1', 's2']) self.assertFalse(response.state) self.assertEqual(str(response.error), "Station is ambiguous: s1, s2")
def test_departures_failure(self): response = Response(False, ['ERP', 'ZOO']) self.assertEqual(response.departures, "Station is ambiguous: ERP, ZOO")
def test_json_failure(self): response = Response(False, ['ERP', 'ZOO']) self.assertEqual(response.json, json.dumps("Station is ambiguous: ERP, ZOO"))