Ejemplo n.º 1
0
    def test_serialization(self, location: Location):
        """Test that serialization and deserialization of Location objects work correctly."""
        actual_location = location
        actual_location_pb = actual_location.to_pb()
        expected_location = Location.from_pb(actual_location_pb)

        assert actual_location == expected_location
Ejemplo n.º 2
0
    def from_pb(cls, relation: query_pb2.Query.Relation):
        """
        From the Relation Protobuf object to the associated
        instance of a subclass of Relation.

        :param relation: the Protobuf object that represents the relation constraint.
        :return: an instance of one of the subclasses of Relation.
        """

        relations_from_pb = {
            query_pb2.Query.Relation.GTEQ: GtEq,
            query_pb2.Query.Relation.GT: Gt,
            query_pb2.Query.Relation.LTEQ: LtEq,
            query_pb2.Query.Relation.LT: Lt,
            query_pb2.Query.Relation.NOTEQ: NotEq,
            query_pb2.Query.Relation.EQ: Eq
        }

        relation_class = relations_from_pb[relation.op]
        value_case = relation.val.WhichOneof("value")
        if value_case == "s":
            return relation_class(relation.val.s)
        elif value_case == "b":
            return relation_class(relation.val.b)
        elif value_case == "i":
            return relation_class(relation.val.i)
        elif value_case == "d":
            return relation_class(relation.val.d)
        elif value_case == "l":
            return relation_class(Location.from_pb(relation.val.l))
Ejemplo n.º 3
0
    def from_pb(cls, range_pb: query_pb2.Query.Range):
        """
        From the Range Protobuf object to the associated instance of ``Range``.

        :param range_pb: the Protobuf object that represents the range.
        :return: an instance of ``Range`` equivalent to the Protobuf object provided as input.
        """

        range_case = range_pb.WhichOneof("pair")
        if range_case == "s":
            return cls((range_pb.s.first, range_pb.s.second))
        elif range_case == "i":
            return cls((range_pb.i.first, range_pb.i.second))
        elif range_case == "d":
            return cls((range_pb.d.first, range_pb.d.second))
        elif range_case == "l":
            return cls((Location.from_pb(range_pb.l.first), Location.from_pb(range_pb.l.second)))
Ejemplo n.º 4
0
    def from_pb(cls, distance_pb: query_pb2.Query.Distance):
        """
        From the ``Distance`` Protobuf object to the associated instance of :class:`~oef.query.Distance`.

        :param distance_pb: the Protobuf object that represents the ``~oef.query.Distance`` constraint.
        :return: an instance of ``~oef.query.Distance``.
        """
        center = Location.from_pb(distance_pb.center)
        distance = distance_pb.distance
        return cls(center, distance)
Ejemplo n.º 5
0
    def from_pb(cls, set_pb: query_pb2.Query.Set):
        """
        From the Set Protobuf object to the associated instance of a subclass of :class:`~oef.query.Set`.

        :param set_pb: the Protobuf object that represents the set constraint.
        :return: the object of one of the subclasses of :class:`~oef.query.Set`.
        """
        op_from_pb = {
            query_pb2.Query.Set.IN: In,
            query_pb2.Query.Set.NOTIN: NotIn
        }
        set_class = op_from_pb[set_pb.op]
        value_case = set_pb.vals.WhichOneof("values")
        if value_case == "s":
            return set_class(set_pb.vals.s.vals)
        elif value_case == "b":
            return set_class(set_pb.vals.b.vals)
        elif value_case == "i":
            return set_class(set_pb.vals.i.vals)
        elif value_case == "d":
            return set_class(set_pb.vals.d.vals)
        elif value_case == "l":
            locations = [Location.from_pb(loc) for loc in set_pb.vals.l.vals]
            return set_class(locations)