Ejemplo n.º 1
0
class RouteSchema(mm.Schema):
    route_id = mm.fields.Str(required=True)
    agency_id = mm.fields.Int(required=True)
    route_short_name = mm.fields.Str()
    route_long_name = mm.fields.Str(required=True)
    route_desc = EnumField(mbta_models.RouteDescription)
    route_type = EnumField(mbta_models.RouteType, required=True)
    route_url = mm.fields.Url()
    route_color = mm.fields.Str()
    route_text_color = mm.fields.Str()
    route_sort_order = mm.fields.Int()
    route_fare_class = EnumField(mbta_models.FareClass)
    line_id = fk.StringForeignKey(mbta_models.Line)
    listed_route = bv.BinaryValue(missing=0)

    @mm.pre_load
    def convert_input(self, in_data: typing.Dict, **kwargs) -> typing.Dict:
        in_data["route_type"] = schema_utils.numbered_type_enum_key(
            in_data["route_type"])
        for key in ("route_desc", "route_fare_class"):
            in_data[key] = in_data[key].replace(" ", "_").lower()

        return {k: v for k, v in in_data.items() if v}

    @mm.post_load
    def make_route(self, data: typing.Dict, **kwargs) -> mbta_models.Route:
        return mbta_models.Route(
            data.pop("route_id"),
            data.pop("agency_id"),
            data.pop("route_long_name"),
            data.pop("route_type"),
            data.pop("listed_route"),
            **data,
        )
Ejemplo n.º 2
0
def test_deserialize_failure(value):
    # GIVEN
    bin_val_field = bv.BinaryValue()

    # THEN: ValidationError is raised
    with pytest.raises(mm.ValidationError):
        bin_val_field.deserialize(value)
Ejemplo n.º 3
0
class TripSchema(mm.Schema):
    route_id = fk.StringForeignKey(mbta_models.Route, required=True)
    service_id = fk.StringForeignKey(mbta_models.Calendar, required=True)
    trip_id = mm.fields.Str(required=True)
    trip_headsign = mm.fields.Str()
    trip_short_name = mm.fields.Str()
    direction_id = bv.BinaryValue()
    block_id = mm.fields.Str()
    shape_id = mm.fields.Str()
    wheelchair_accessible = EnumField(mbta_models.TripAccessibility)
    trip_route_type = EnumField(mbta_models.RouteType)
    route_pattern_id = fk.StringForeignKey(mbta_models.RoutePattern)
    bikes_allowed = EnumField(mbta_models.TripAccessibility)

    @mm.pre_load
    def convert_input(self, in_data: typing.Dict, **kwargs) -> typing.Dict:
        in_data["wheelchair_accessible"] = schema_utils.numbered_type_enum_key(
            in_data["wheelchair_accessible"], default_0=True)
        in_data["trip_route_type"] = schema_utils.numbered_type_enum_key(
            in_data["trip_route_type"])
        in_data["bikes_allowed"] = schema_utils.numbered_type_enum_key(
            in_data["bikes_allowed"], default_0=True)
        return {k: v for k, v in in_data.items() if v}

    @mm.post_load
    def make_trip(self, data: typing.Dict, **kwargs) -> mbta_models.Trip:
        return mbta_models.Trip(
            trip_id=data.pop("trip_id"),
            route_id=data.pop("route_id"),
            service_id=data.pop("service_id"),
            **data,
        )
Ejemplo n.º 4
0
class RoutePatternSchema(mm.Schema):
    route_pattern_id = mm.fields.Str(required=True)
    route_id = fk.StringForeignKey(mbta_models.Route, required=True)
    direction_id = bv.BinaryValue()
    route_pattern_name = mm.fields.Str()
    route_pattern_time_desc = mm.fields.Str()
    route_pattern_typicality = EnumField(mbta_models.RoutePatternTypicality)
    route_pattern_sort_order = mm.fields.Int()
    representative_trip_id = mm.fields.Str()

    @mm.pre_load
    def convert_input(self, in_data: typing.Dict, **kwargs) -> typing.Dict:
        in_data[
            "route_pattern_typicality"] = schema_utils.numbered_type_enum_key(
                in_data["route_pattern_typicality"])
        return {k: v for k, v in in_data.items() if v}

    @mm.post_load
    def make_route_pattern(self, data: typing.Dict,
                           **kwargs) -> mbta_models.RoutePattern:
        return mbta_models.RoutePattern(
            route_pattern_id=data.pop("route_pattern_id"),
            route_id=data.pop("route_id"),
            **data,
        )
Ejemplo n.º 5
0
class DirectionSchema(mm.Schema):
    route_id = fk.StringForeignKey(mbta_models.Route, required=True)
    direction_id = bv.BinaryValue(required=True)
    direction = EnumField(mbta_models.DirectionOption, required=True)
    direction_destination = mm.fields.Str(required=True)

    @mm.pre_load
    def convert_input(self, in_data: typing.Dict, **kwargs) -> typing.Dict:
        in_data["direction"] = in_data["direction"].lower()
        return {k: v for k, v in in_data.items() if v}

    def load(self, *args, **kwargs) -> typing.Optional[mbta_models.Direction]:
        """Load direction data, skipping rows that reference non-existent Routes"""
        try:
            return super().load(*args, **kwargs)
        except mm.ValidationError as ve:
            # self.route_id doesn't exist when a ValidationError has occurred
            string_fk_field = fk.StringForeignKey(mbta_models.Route)
            if string_fk_field.is_missing_instance_error(ve):
                logger.info(ve.normalized_messages())
                return None
            else:
                raise ve

    @mm.post_load
    def make_direction(self, data: typing.Dict,
                       **kwargs) -> mbta_models.Direction:
        return mbta_models.Direction(
            route_id=data.pop("route_id"),
            direction_id=data.pop("direction_id"),
            direction=data.pop("direction"),
            direction_destination=data.pop("direction_destination"),
        )
Ejemplo n.º 6
0
class LinkedDatasetSchema(mm.Schema):
    url = mm.fields.Url(required=True)
    trip_updates = bv.BinaryValue(required=True)
    vehicle_positions = bv.BinaryValue(required=True)
    service_alerts = bv.BinaryValue(required=True)
    authentication_type = EnumField(mbta_models.AuthenticationType,
                                    required=True)

    @mm.pre_load
    def convert_input(self, in_data: typing.Dict, **kwargs) -> typing.Dict:
        in_data["authentication_type"] = schema_utils.numbered_type_enum_key(
            in_data["authentication_type"])
        return {k: v for k, v in in_data.items() if v}

    @mm.post_load
    def make_linked_dataset(self, data: typing.Dict,
                            **kwargs) -> mbta_models.LinkedDataset:
        return mbta_models.LinkedDataset(
            url=data.pop("url"),
            trip_updates=data.pop("trip_updates"),
            vehicle_positions=data.pop("vehicle_positions"),
            service_alerts=data.pop("service_alerts"),
            authentication_type=data.pop("authentication_type"),
        )
Ejemplo n.º 7
0
class StopTimeSchema(mm.Schema):
    trip_id = fk.StringForeignKey(mbta_models.Trip, required=True)
    arrival_time = mm.fields.Int(required=True)
    departure_time = mm.fields.Int(required=True)
    stop_id = fk.StringForeignKey(mbta_models.Stop, required=True)
    stop_sequence = mm.fields.Int(required=True)
    stop_headsign = mm.fields.Str()
    pickup_type = EnumField(mbta_models.PickupDropOffType)
    drop_off_type = EnumField(mbta_models.PickupDropOffType)
    shape_dist_traveled = mm.fields.Float()
    timepoint = bv.BinaryValue()
    checkpoint_id = fk.StringForeignKey(mbta_models.Checkpoint)

    @mm.pre_load
    def convert_input(self, in_data: typing.Dict, **kwargs) -> typing.Dict:
        in_data["arrival_time"] = schema_utils.time_as_seconds(
            in_data["arrival_time"])
        in_data["departure_time"] = schema_utils.time_as_seconds(
            in_data["departure_time"])
        in_data["pickup_type"] = schema_utils.numbered_type_enum_key(
            in_data["pickup_type"], default_0=True)
        in_data["drop_off_type"] = schema_utils.numbered_type_enum_key(
            in_data["drop_off_type"], default_0=True)
        return {k: v for k, v in in_data.items() if v}

    @mm.post_load
    def make_stop_time(self, data: typing.Dict,
                       **kwargs) -> mbta_models.StopTime:
        return mbta_models.StopTime(
            trip_id=data.pop("trip_id"),
            arrival_time=data.pop("arrival_time"),
            departure_time=data.pop("departure_time"),
            stop_id=data.pop("stop_id"),
            stop_sequence=data.pop("stop_sequence"),
            **data,
        )
Ejemplo n.º 8
0
def test_deserialize_success(value):
    # GIVEN
    bin_val_field = bv.BinaryValue()

    # THEN: value is returned unchanged without error
    assert bin_val_field.deserialize(value) == int(value)