Exemplo n.º 1
0
 def test_equal_operator(self):
     for row in ALL_CSV_ROWS:
         td1 = TransitData()
         td2 = TransitData()
         agency1 = td1.agencies.add(**row)
         agency2 = td2.agencies.add(**row)
         self.assertEqual(agency1, agency2)
Exemplo n.º 2
0
 def test_equal_operator(self):
     for row in ALL_CSV_ROWS:
         td1 = TransitData()
         td2 = TransitData()
         service1 = td1.calendar.add(**row)
         service2 = td2.calendar.add(**row)
         self.assertEqual(service1, service2)
Exemplo n.º 3
0
 def test_equal_operator(self):
     for rows in ALL_CSV_ROWS:
         td1 = TransitData()
         td2 = TransitData()
         for row in rows:
             stop1 = td1.stops.add(**row)
             stop2 = td2.stops.add(**row)
             self.assertEqual(stop1, stop2)
Exemplo n.º 4
0
 def test_equal_operator(self):
     for rows in ALL_CSV_ROWS:
         td1 = TransitData()
         td2 = TransitData()
         for row in rows:
             shape_point1 = td1.shapes.add(**row)
             shape_point2 = td2.shapes.add(**row)
             self.assertEqual(shape_point1, shape_point2)
         self.assertEqual(iter(td1.shapes).next(), iter(td2.shapes).next())
Exemplo n.º 5
0
    def test_load_from_file_path(self):
        for file_path in constants.GTFS_TEST_FILES:
            print "testing '%s'" % (file_path, )

            td1 = TransitData(gtfs_file=file_path)
            with open(file_path, "rb") as f:
                td2 = TransitData(gtfs_file=f)

            self.assertEqual(td1, td2)
Exemplo n.º 6
0
    def test_import_export(self):
        for file_path in constants.GTFS_TEST_FILES:
            print "testing '%s'" % (file_path, )

            temp_file_path = tempfile.mktemp() + ".zip"
            try:
                td1 = TransitData(gtfs_file=file_path)
                td1.save(temp_file_path)
                td2 = TransitData(temp_file_path)
                self.assertEqual(td1, td2)

                compare_gtfs_files(file_path, temp_file_path, self)
            finally:
                if os.path.exists(temp_file_path):
                    os.remove(temp_file_path)
Exemplo n.º 7
0
    def test_add_object(self):
        for row in ALL_CSV_ROWS:
            source_td = TransitData()
            dest_td = TransitData()

            source_service = source_td.calendar.add(**row)
            dest_service = dest_td.calendar.add_object(source_service)
            self.assertEqual(source_service, dest_service)

            services_num = len(dest_td.calendar)
            dest_td.calendar.add_object(source_service)
            self.assertEqual(services_num, len(dest_td.calendar))

            source_service.saturday = not source_service.saturday
            self.assertRaises(Exception, dest_td.calendar.add_object, source_service)
Exemplo n.º 8
0
    def test_minimum_properties(self):
        td = TransitData()
        agency = td.agencies.add(**MINI_AGENCY_CSV_ROW)

        self.assertTrue(hasattr(agency, "id"))
        self.assertRaises(Exception, setattr, agency, "id", 2)

        test_property(self,
                      agency,
                      property_name="agency_name",
                      new_value="test name")
        test_property(self,
                      agency,
                      property_name="agency_url",
                      new_value="http://testurl.com/")
        test_property(self,
                      agency,
                      property_name="agency_timezone",
                      new_value="Asia/Hebron")
        test_property(self,
                      agency,
                      property_name="agency_lang",
                      new_value="HE")
        test_property(self,
                      agency,
                      property_name="agency_phone",
                      new_value="*1234")
        test_property(self,
                      agency,
                      property_name="agency_email",
                      new_value="*****@*****.**")
        test_property(self,
                      agency,
                      property_name="agency_fare_url",
                      new_value="http://testurl.com/fare/")
Exemplo n.º 9
0
    def test_add(self):
        for row in ALL_CSV_ROWS:
            td = TransitData()
            service = td.calendar.add(**row)
            self.assertIn(service, td.calendar)

            self.assertIsInstance(service.id, int)
            self.assertEqual(service.id, row["service_id"])

            self.assertEqual(service.start_date.strftime("%Y%m%d"),
                             row.get("start_date"))
            self.assertEqual(service.end_date.strftime("%Y%m%d"),
                             row.get("end_date"))
            self.assertEqual(service.sunday, row.get("sunday"))
            self.assertEqual(service.monday, row.get("monday"))
            self.assertEqual(service.tuesday, row.get("tuesday"))
            self.assertEqual(service.wednesday, row.get("wednesday"))
            self.assertEqual(service.thursday, row.get("thursday"))
            self.assertEqual(service.friday, row.get("friday"))
            self.assertEqual(service.saturday, row.get("saturday"))
            self.assertEqual(service.attributes.get("test_attribute"),
                             row.get("test_attribute"))

            self.assertEqual(len(service.attributes), len(row) - 10)

            self.assertRaises(Exception, td.calendar.add, **row)
            self.assertEqual(len(td.calendar), 1)
Exemplo n.º 10
0
    def test_add_object(self):
        for row in ALL_CSV_ROWS:
            source_td = TransitData()
            dest_td = TransitData()

            source_agency = source_td.agencies.add(**row)
            dest_agency = dest_td.agencies.add_object(source_agency)
            self.assertEqual(source_agency, dest_agency)

            agencies_num = len(dest_td.agencies)
            dest_td.agencies.add_object(source_agency)
            self.assertEqual(agencies_num, len(dest_td.agencies))

            source_agency.agency_name = "agency new name"
            self.assertRaises(Exception, dest_td.agencies.add_object,
                              source_agency)
Exemplo n.º 11
0
 def test_get_csv_line(self):
     for rows in ALL_CSV_ROWS:
         td = TransitData()
         for row in rows:
             td.shapes.add(**row)
         shape = iter(td.shapes).next()
         self.assertListEqual(list(shape.to_csv_line()), rows)
Exemplo n.º 12
0
    def test_add(self):
        for row in ALL_CSV_ROWS:
            td = TransitData()
            agency = td.agencies.add(**row)
            self.assertIn(agency, td.agencies)

            self.assertIsInstance(agency.id, str)
            self.assertEqual(agency.id, row["agency_id"])

            self.assertEqual(agency.agency_name, row.get("agency_name"))
            self.assertEqual(agency.agency_url, row.get("agency_url"))
            self.assertEqual(agency.agency_timezone,
                             row.get("agency_timezone"))
            self.assertEqual(agency.agency_lang, row.get("agency_lang"))
            self.assertEqual(agency.agency_phone, row.get("agency_phone"))
            self.assertEqual(agency.agency_email, row.get("agency_email"))
            self.assertEqual(agency.agency_fare_url,
                             row.get("agency_fare_url"))
            self.assertEqual(agency.attributes.get("test_attribute"),
                             row.get("test_attribute"))

            self.assertEqual(len(agency.attributes), len(row) - 4)

            self.assertRaises(Exception, td.agencies.add, **row)
            self.assertEqual(len(td.agencies), 1)
Exemplo n.º 13
0
 def test_clean(self):
     td = TransitData()
     for row in FULL_STOP_CSV_ROWS:
         td.stops.add(**row)
     self.assertGreater(len(td.stops), 0)
     td.stops.clean()
     self.assertEqual(len(td.stops), 0)
Exemplo n.º 14
0
    def test_maximum_properties(self):
        td = TransitData()
        shape_point = None
        for row in FULL_SHAPE_CSV_ROWS:
            shape_point = td.shapes.add(**row)

        shape = iter(td.shapes).next()
        self.assertTrue(hasattr(shape, "id"))
        self.assertRaises(Exception, setattr, shape, "id", "2")

        test_property(self, shape_point, property_name="latitude", new_value=0)
        test_property(self,
                      shape_point,
                      property_name="longitude",
                      new_value=0)
        test_property(self, shape_point, property_name="sequence", new_value=2)
        test_property(self,
                      shape_point,
                      property_name="shape_dist_traveled",
                      new_value=60.2)

        self.assertIn("test_attribute", shape_point.attributes)
        shape_point.attributes["test_attribute"] = "new test data"
        self.assertEqual(shape_point.attributes["test_attribute"],
                         "new test data")

        self.assertNotIn("test_attribute2", shape_point.attributes)
        shape_point.attributes["test_attribute2"] = "more test data"
        self.assertEqual(shape_point.attributes["test_attribute2"],
                         "more test data")
Exemplo n.º 15
0
    def test_add(self):
        for rows in ALL_CSV_ROWS:
            td = TransitData()
            for row in rows:
                stop = td.stops.add(**row)
                self.assertIn(stop, td.stops)

                self.assertIsInstance(stop.id, str)
                self.assertEqual(stop.id, row["stop_id"])

                self.assertEqual(stop.stop_name, row.get("stop_name"))
                self.assertEqual(stop.stop_lat, row.get("stop_lat"))
                self.assertEqual(stop.stop_lon, row.get("stop_lon"))
                self.assertEqual(stop.location_type, row.get("location_type"))
                self.assertEqual(stop.stop_code, row.get("stop_code"))
                self.assertEqual(stop.stop_desc, row.get("stop_desc"))
                self.assertEqual(stop.zone_id, row.get("zone_id"))
                self.assertEqual(stop.stop_url, row.get("stop_url"))
                self.assertEqual(stop.parent_station,
                                 td.stops[row["parent_station"]] if "parent_station" in row else None)
                self.assertEqual(stop.stop_timezone, row.get("stop_timezone"))
                self.assertEqual(stop.wheelchair_boarding, row.get("wheelchair_boarding"))
                self.assertEqual(stop.attributes.get("test_attribute"), row.get("test_attribute"))

                self.assertEqual(len(stop.attributes), len(row) - 4)

                self.assertRaises(Exception, td.stops.add, **row)

            self.assertEqual(len(td.stops), len(rows))
Exemplo n.º 16
0
    def test_add(self):
        for rows in ALL_CSV_ROWS:
            td = TransitData()
            for row in rows:
                shape_point = td.shapes.add(**row)

                self.assertEqual(shape_point.latitude, row.get("shape_pt_lat"))
                self.assertEqual(shape_point.longitude,
                                 row.get("shape_pt_lon"))
                self.assertEqual(shape_point.sequence,
                                 row.get("shape_pt_sequence"))
                self.assertEqual(shape_point.shape_dist_traveled,
                                 row.get("shape_dist_traveled"))
                self.assertEqual(shape_point.attributes.get("test_attribute"),
                                 row.get("test_attribute"))

                self.assertEqual(len(shape_point.attributes), len(row) - 4)

                # self.assertRaises(Exception, td.shapes.add, **row)
                self.assertEqual(len(td.shapes), 1)

            shape_id = rows[-1]["shape_id"]
            shape = td.shapes[shape_id]
            self.assertIn(shape, td.shapes)

            self.assertIsInstance(shape.id, int)
            self.assertEqual(shape.id, shape_id)
Exemplo n.º 17
0
    def test_maximum_properties(self):
        td = TransitData()
        service = td.calendar.add(**FULL_SERVICE_CSV_ROW)

        self.assertTrue(hasattr(service, "id"))
        self.assertRaises(Exception, setattr, service, "id", "2")

        test_property(self,
                      service,
                      property_name="start_date",
                      new_value=TOMORROW_DATE)
        test_property(self,
                      service,
                      property_name="end_date",
                      new_value=TOMORROW_DATE + timedelta(days=1))
        test_property(self, service, property_name="sunday", new_value=False)
        test_property(self, service, property_name="monday", new_value=False)
        test_property(self, service, property_name="tuesday", new_value=False)
        test_property(self,
                      service,
                      property_name="wednesday",
                      new_value=False)
        test_property(self, service, property_name="thursday", new_value=False)
        test_property(self, service, property_name="friday", new_value=False)
        service.sunday = True
        test_property(self, service, property_name="saturday", new_value=False)

        self.assertIn("test_attribute", service.attributes)
        service.attributes["test_attribute"] = "new test data"
        self.assertEqual(service.attributes["test_attribute"], "new test data")

        self.assertNotIn("test_attribute2", service.attributes)
        service.attributes["test_attribute2"] = "more test data"
        self.assertEqual(service.attributes["test_attribute2"],
                         "more test data")
Exemplo n.º 18
0
    def test_maximum_properties(self):
        td = TransitData()
        stop = None
        for row in FULL_STOP_CSV_ROWS:
            stop = td.stops.add(**row)

        self.assertTrue(hasattr(stop, "id"))
        self.assertRaises(Exception, setattr, stop, "id", "2")

        test_property(self, stop, property_name="stop_name", new_value="test name")
        test_property(self, stop, property_name="stop_lat", new_value=0)
        test_property(self, stop, property_name="stop_lon", new_value=0)
        test_property(self, stop, property_name="stop_code", new_value="1")
        test_property(self, stop, property_name="stop_desc", new_value="test desc")
        test_property(self, stop, property_name="zone_id", new_value=2)
        test_property(self, stop, property_name="stop_url", new_value="http://testurl.com/")
        test_property(self, stop, property_name="location_type", new_value=1)
        test_property(self, stop, property_name="parent_station", new_value=stop)
        test_property(self, stop, property_name="stop_timezone", new_value="Asia/Hebron")
        test_property(self, stop, property_name="wheelchair_boarding", new_value=False)

        self.assertIn("test_attribute", stop.attributes)
        stop.attributes["test_attribute"] = "new test data"
        self.assertEqual(stop.attributes["test_attribute"], "new test data")

        self.assertNotIn("test_attribute2", stop.attributes)
        stop.attributes["test_attribute2"] = "more test data"
        self.assertEqual(stop.attributes["test_attribute2"], "more test data")
Exemplo n.º 19
0
    def test_add_object(self):
        for rows in ALL_CSV_ROWS:
            source_td = TransitData()
            dest_td = TransitData()

            source_stop = None
            for row in rows:
                source_stop = source_td.stops.add(**row)
            dest_stop = dest_td.stops.add_object(source_stop, recursive=True)
            self.assertEqual(source_stop, dest_stop)

            stops_num = len(dest_td.stops)
            dest_td.stops.add_object(source_stop)
            self.assertEqual(stops_num, len(dest_td.stops))

            source_stop.stop_name = "test_name"
            self.assertRaises(Exception, dest_td.stops.add_object, source_stop)
Exemplo n.º 20
0
    def test_load_partial(self):
        lines = {15: ["58", "358", "458"]}
        for file_path in constants.GTFS_TEST_FILES:
            print "testing '%s'" % (file_path,)

            td1 = load_partial_transit_data(file_path, lines)
            td2 = create_partial_transit_data(TransitData(gtfs_file=file_path), lines)
            self.assertEqual(td1, td2)
Exemplo n.º 21
0
    def test_add_object(self):
        for rows in ALL_CSV_ROWS:
            source_td = TransitData()
            dest_td = TransitData()

            for row in rows:
                source_td.shapes.add(**row)
            source_shape = iter(source_td.shapes).next()
            dest_shape = dest_td.shapes.add_object(source_shape,
                                                   recursive=True)
            self.assertEqual(source_shape, dest_shape)

            shapes_num = len(dest_td.shapes)
            dest_td.shapes.add_object(source_shape)
            self.assertEqual(shapes_num, len(dest_td.shapes))

            source_shape.shape_points[-1].sequence += 1
            self.assertRaises(Exception, dest_td.shapes.add_object,
                              source_shape)
Exemplo n.º 22
0
 def test_remove(self):
     td = TransitData()
     agency = td.agencies.add(**FULL_AGENCY_CSV_ROW)
     self.assertIn(agency, td.agencies)
     td.agencies.remove(agency)
     self.assertNotIn(agency, td.agencies)
     td.agencies.add(**FULL_AGENCY_CSV_ROW)
     self.assertIn(agency, td.agencies)
     td.agencies.remove(agency.id)
     self.assertNotIn(agency, td.agencies)
Exemplo n.º 23
0
 def test_remove(self):
     td = TransitData()
     service = td.calendar.add(**FULL_SERVICE_CSV_ROW)
     self.assertIn(service, td.calendar)
     td.calendar.remove(service)
     self.assertNotIn(service, td.calendar)
     td.calendar.add(**FULL_SERVICE_CSV_ROW)
     self.assertIn(service, td.calendar)
     td.calendar.remove(service.id)
     self.assertNotIn(service, td.calendar)
Exemplo n.º 24
0
 def test_get_csv_fields(self):
     for rows in ALL_CSV_ROWS:
         td = TransitData()
         for row in rows:
             td.shapes.add(**row)
         shape = iter(td.shapes).next()
         self.assertListEqual(
             sorted(shape.get_csv_fields()),
             sorted(list({key
                          for row in rows for key in row.iterkeys()})))
Exemplo n.º 25
0
def load_gtfs(gtfs_file_path):
    td = TransitData(gtfs_file_path)
    print "TransitData object contains:"
    print "%d agencies" % (len(td.agencies))
    print "%d routes" % (len(td.routes))
    print "%d trips" % (len(td.trips))
    print "%d stops" % (len(td.stops))
    print "%d shapes" % (len(td.shapes))
    print "%d services" % (len(td.calendar))

    return td
Exemplo n.º 26
0
 def test_remove(self):
     td = TransitData()
     stop = None
     for row in FULL_STOP_CSV_ROWS:
         stop = td.stops.add(**row)
     self.assertIn(stop, td.stops)
     td.stops.remove(stop, clean_after=False)
     self.assertNotIn(stop, td.stops)
     td.stops.add(**FULL_STOP_CSV_ROWS[-1])
     self.assertIn(stop, td.stops)
     td.stops.remove(stop.id)
     self.assertNotIn(stop, td.stops)
Exemplo n.º 27
0
 def test_remove(self):
     td = TransitData()
     for row in FULL_SHAPE_CSV_ROWS:
         td.shapes.add(**row)
     shape_id = FULL_SHAPE_CSV_ROWS[-1]["shape_id"]
     self.assertIn(shape_id, td.shapes)
     td.shapes.remove(td.shapes[shape_id], clean_after=False)
     self.assertNotIn(shape_id, td.shapes)
     for row in FULL_SHAPE_CSV_ROWS:
         td.shapes.add(**row)
     self.assertIn(shape_id, td.shapes)
     td.shapes.remove(shape_id)
     self.assertNotIn(shape_id, td.shapes)
Exemplo n.º 28
0
    def test_minimum_properties(self):
        td = TransitData()
        service = td.calendar.add(**MINI_SERVICE_CSV_ROW)

        self.assertTrue(hasattr(service, "id"))
        self.assertRaises(Exception, setattr, service, "id", "2")

        test_property(self, service, property_name="start_date", new_value=TOMORROW_DATE)
        test_property(self, service, property_name="end_date", new_value=TOMORROW_DATE)
        test_property(self, service, property_name="saturday", new_value=True)
        test_property(self, service, property_name="sunday", new_value=False)
        test_property(self, service, property_name="monday", new_value=True)
        test_property(self, service, property_name="tuesday", new_value=True)
        test_property(self, service, property_name="wednesday", new_value=True)
        test_property(self, service, property_name="thursday", new_value=True)
        test_property(self, service, property_name="friday", new_value=True)
Exemplo n.º 29
0
    def test_create_partial(self):
        partial = {15: ["58", "358", "458"]}
        for file_path in constants.GTFS_TEST_FILES:
            print "testing '%s'" % (file_path,)

            td1 = TransitData(gtfs_file=file_path)
            td2 = create_partial_transit_data(td1, partial)
            self.assertListEqual(sorted(agency.id for agency in td2.agencies),
                                 sorted(agency for agency in partial.iterkeys() if agency in td1.agencies))
            for agency in td2.agencies:
                if partial[agency.id] is not None:
                    self.assertListEqual(sorted(line.line_number for line in agency.lines),
                                         sorted(line.line_number for line in td1.agencies[agency.id].lines
                                                if line.line_number in partial[agency.id]))
                else:
                    self.assertListEqual(sorted(line.line_number for line in agency.lines),
                                         sorted(line.line_number for line in td1.agencies[agency.id].lines))
Exemplo n.º 30
0
def load_partial_gtfs2(file_path, partial):
    """
    :type file_path: str
    :type partial: dict[int, list[str]] | dict[int, None]
    """

    td = TransitData(file_path)
    td = create_partial_transit_data(td, partial)

    print "TransitData object contains:"
    print "%d agencies" % (len(td.agencies))
    print "%d routes" % (len(td.routes))
    print "%d trips" % (len(td.trips))
    print "%d stops" % (len(td.stops))
    print "%d shapes" % (len(td.shapes))
    print "%d services" % (len(td.calendar))

    return td