示例#1
0
    def test_properties(self, stub):
        id_ = "foo"
        obj = types.Int(1)
        parameters = {"foo": types.Str("bar")}

        job_state = job_pb2.Job.State(stage=job_pb2.Job.Stage.QUEUED)

        def create_side_effect(req, **kwargs):
            return job_pb2.Job(
                id=id_,
                parameters=req.parameters,
                serialized_graft=req.serialized_graft,
                typespec=req.typespec,
                type=req.type,
                channel=req.channel,
                state=job_state,
            )

        stub.return_value.CreateJob.side_effect = create_side_effect

        job = Job(obj, parameters)
        job_from_msg = Job._from_proto(job._message, client=job._client)

        assert job.object is obj
        utils.assert_graft_is_scope_isolated_equvalent(
            job_from_msg.object.graft, obj.graft
        )
        assert job_from_msg.type is type(job_from_msg.object) is type(obj)  # noqa: E721
        assert job.result_type == "Int"
        assert job.parameters == {"foo": graft_client.value_graft(parameters["foo"])}

        assert job.id == id_
        assert job.channel == _channel.__channel__
        assert job.stage == "QUEUED"
        assert job.created_datetime is None
        assert job.updated_datetime is None
        assert job.runtime is None
        assert job.error is None
        assert job.done is False

        job._message.state.stage = job_pb2.Job.Stage.SUCCEEDED
        job._message.timestamp = 1
        job._message.state.timestamp = 2

        assert job.stage == "SUCCEEDED"
        assert job.created_datetime == pb_milliseconds_to_datetime(1)
        assert job.updated_datetime == pb_milliseconds_to_datetime(2)
        assert job.runtime == job.updated_datetime - job.created_datetime
        assert job.error is None
        assert job.done is True

        job._message.state.stage = job_pb2.Job.Stage.FAILED
        job._message.state.error.code = errors_pb2.ERROR_INVALID
        job._message.state.error.message = "test"

        assert job.stage == "FAILED"
        assert isinstance(job.error, JobInvalid)
        assert job.done is True
def parameters_to_grafts(**parameters):
    """
    Convert a dict of parameters into a dict of grafts or literals.

    If a parameter is a graft literal (i.e., a Python primitive),
    it's JSON-encoded directly rather than wrapping it in a graft, to prevent
    graft bloat, and improve browser cache hits on query argument.

    Otherwise, ``value_graft`` is called on it, so it should be a `Proxytype`
    or a JSON literal.

    If ``value_graft`` fails, `proxify` is called as a last resort to try to
    convert the value into something that graft can represent.

    Parameters
    ----------
    parameters: JSON-serializable value, Proxytype, `proxify` compatible value
        Parameters to use while computing.

        Each argument must be the name of a parameter created with `~.identifier.parameter`.
        Each value must be a JSON-serializable type (``bool``, ``int``, ``float``,
        ``str``, ``list``, ``dict``, etc.), a `Proxytype` (like `~.geospatial.Image` or `.Timedelta`),
        or a value that `proxify` can handle (like a ``datetime.datetime``).

    Returns
    -------
    grafts: dict[str, str]
        Dict of parameters, where keys are argument names, and values
        are their graft representations. Meant to be compatible with
        `merge_value_grafts` from the graft client.

    Raises
    ------
    TypeError:
        If a parameter value can't be represented as a graft by ``value_graft`` or `proxify`.
    """
    grafts = {}
    for name, param in six.iteritems(parameters):
        if graft_syntax.is_literal(param) or graft_syntax.is_graft(param):
            graftable = param
        else:
            try:
                graftable = graft_client.value_graft(param)
            except TypeError:
                try:
                    graftable = proxify(param).graft
                except NotImplementedError:
                    raise TypeError(
                        "Invalid type for parameter {!r}: {}. "
                        "Must be a JSON-serializable value, Proxytype, "
                        "or object that `proxify` can handle. "
                        "Got: {}".format(name, type(param), param))

        grafts[name] = graftable

    return grafts
    def test_properties(self, stub):
        obj = types.Int(1)
        parameters = {"foo": types.Str("bar")}

        job = Job.build(obj, parameters, channel="foo")
        job_from_msg = Job(job._message, client=job._client)

        assert job.object is obj
        utils.assert_graft_is_scope_isolated_equvalent(
            job_from_msg.object.graft, obj.graft)
        assert job_from_msg.type is type(job_from_msg.object) is type(
            obj)  # noqa: E721
        assert job.result_type == "Int"
        assert job.parameters == {
            "foo": graft_client.value_graft(parameters["foo"])
        }

        assert job.id is None
        assert job.channel == "foo"
        assert job.status == "STATUS_UNKNOWN"
        assert job.stage == "STAGE_UNKNOWN"
        assert job.created_datetime is None
        assert job.updated_datetime is None
        assert job.runtime is None
        assert job.error is None
        assert job.done is False

        job._message.id = "foo"
        job._message.status = job_pb2.STATUS_SUCCESS
        job._message.stage = job_pb2.STAGE_DONE
        job._message.created_timestamp = 1
        job._message.updated_timestamp = 2

        assert job.id == "foo"
        assert job.status == "STATUS_SUCCESS"
        assert job.stage == "STAGE_DONE"
        assert job.created_datetime == pb_milliseconds_to_datetime(1)
        assert job.updated_datetime == pb_milliseconds_to_datetime(2)
        assert job.runtime == job.updated_datetime - job.created_datetime
        assert job.error is None
        assert job.done is True

        job._message.status = job_pb2.STATUS_FAILURE
        job._message.error.code = errors_pb2.ERROR_INVALID
        job._message.error.message = "test"

        assert job.status == "STATUS_FAILURE"
        assert job.stage == "STAGE_DONE"
        assert isinstance(job.error, JobInvalid)
        assert job.done is True
    def __init__(self, obj):
        if self._is_generic():
            raise ProxyTypeError(
                "Cannot instantiate a generic {}; use a concrete subclass".
                format(type(self).__name__))

        from .any_ import Any  # TODO circular import

        if isinstance(obj, (type(self), Any)):
            self.graft = obj.graft
        else:
            if not isinstance(obj, self._pytype):
                raise ProxyTypeError("Cannot promote {} to {}".format(
                    type(obj), type(self)))
            self.graft = client.value_graft(obj)
    def test_build(self, stub, client):
        obj = types.Int(1)
        parameters = {"foo": types.Str("bar")}

        job = Job.build(obj, parameters, channel="foo", client=client)
        message = job._message

        assert message.workflow_id == ""
        assert message.channel == "foo"

        assert json.loads(message.parameters) == utils.json_normalize(
            {"foo": graft_client.value_graft(parameters["foo"])})
        assert json.loads(message.serialized_graft) == utils.json_normalize(
            obj.graft)
        assert message.typespec == cereal.serialize_typespec(type(obj))
        assert message.type == types_pb2.Int

        if client is not None:
            assert job._client == client
        else:
            assert isinstance(job._client, Client)
示例#6
0
 def __init__(self, value):
     self.graft = client.value_graft(value)
示例#7
0
 def __init__(self, value):
     self.graft = client.value_graft(value)
     self.params = getattr(value, "params", ())