Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
 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"))
Exemplo n.º 3
0
 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"))
Exemplo n.º 4
0
    def test_regression_new_day(self):
        now = datetime.datetime(2013, 1, 2, 23, 59, 0)

        dep = datetime.datetime(2013, 1, 2, 0, 1, 2)
        departure = Departure("From My Station",
                              "To Your Station",
                              dep,
                              "A Line",
                              since=now)
        self.assertEqual(departure.remaining, 120)
        departure = Departure("From My Station",
                              "To Your Station",
                              dep,
                              "A Line",
                              since=now,
                              no_add_day=True)
        self.assertEqual(departure.remaining, -86280)

        mitday_current_day = datetime.datetime(2013, 1, 2, 11, 59, 0)
        departure = Departure("From My Station",
                              "To Your Station",
                              mitday_current_day,
                              "A Line",
                              since=now)
        self.assertEqual(departure.remaining, -43200)
        departure = Departure("From My Station",
                              "To Your Station",
                              mitday_current_day,
                              "A Line",
                              since=now,
                              no_add_day=True)

        mitday_next_day = datetime.datetime(2013, 1, 2, 11, 58, 59)
        departure = Departure("From My Station",
                              "To Your Station",
                              mitday_next_day,
                              "A Line",
                              since=now)
        self.assertEqual(departure.remaining, 43140)
        departure = Departure("From My Station",
                              "To Your Station",
                              mitday_next_day,
                              "A Line",
                              since=now,
                              no_add_day=True)
        self.assertEqual(departure.remaining, -43260)
Exemplo n.º 5
0
 def test_error(self):
     self.assertRaises(TypeError, Departure, "from", "to", ["when"], "line")
     self.assertIsInstance(Departure("from", "to", "16:15\n \t*", "line"),
                           Departure)
Exemplo n.º 6
0
 def test_datetime_now(self):
     when = self.since
     dep = Departure("from", "to", when, "line", since=self.since)
     self.assertEqual(dep.remaining, 0)
Exemplo n.º 7
0
 def test_string_now(self):
     when = fullformat(self.since)
     dep = Departure("from", "to", when, "line", since=self.since)
     self.assertEqual(dep.remaining, 0)