def test_rename_event_log_entry(): old_event_record = """{"__class__":"EventRecord","dagster_event":{"__class__":"DagsterEvent","event_specific_data":null,"event_type_value":"PIPELINE_SUCCESS","logging_tags":{},"message":"Finished execution of pipeline.","pid":71356,"pipeline_name":"error_monster","solid_handle":null,"step_handle":null,"step_key":null,"step_kind_value":null},"error_info":null,"level":10,"message":"error_monster - 4be295b5-fcf2-47cc-8e90-cb14d3cf3ac7 - 71356 - PIPELINE_SUCCESS - Finished execution of pipeline.","pipeline_name":"error_monster","run_id":"4be295b5-fcf2-47cc-8e90-cb14d3cf3ac7","step_key":null,"timestamp":1622659924.037028,"user_message":"Finished execution of pipeline."}""" event_log_entry = deserialize_json_to_dagster_namedtuple(old_event_record) assert isinstance(event_log_entry, EventLogEntry) dagster_event = event_log_entry.dagster_event assert isinstance(dagster_event, DagsterEvent) assert dagster_event.event_type_value == "PIPELINE_SUCCESS"
def instance_from_debug_payloads(payload_files): debug_payloads = [] for input_file in payload_files: with GzipFile(input_file, "rb") as file: blob = file.read().decode("utf-8") debug_payload = deserialize_json_to_dagster_namedtuple(blob) check.invariant(isinstance(debug_payload, DebugRunPayload)) debug_payloads.append(debug_payload) return DagsterInstance.ephemeral(preload=debug_payloads)
def test_skip_when_empty(): test_map = WhitelistMap() @_whitelist_for_serdes(whitelist_map=test_map) class SameSnapshotTuple(namedtuple("_Tuple", "foo")): def __new__(cls, foo): return super(SameSnapshotTuple, cls).__new__(cls, foo) # pylint: disable=bad-super-call old_tuple = SameSnapshotTuple(foo="A") old_serialized = serialize_dagster_namedtuple(old_tuple) old_snapshot = create_snapshot_id(old_tuple) # Without setting skip_when_empty, the ID changes @_whitelist_for_serdes(whitelist_map=test_map) class SameSnapshotTuple(namedtuple("_Tuple", "foo bar")): # pylint: disable=function-redefined def __new__(cls, foo, bar=None): return super(SameSnapshotTuple, cls).__new__( # pylint: disable=bad-super-call cls, foo, bar) new_tuple_without_serializer = SameSnapshotTuple(foo="A") new_snapshot_without_serializer = create_snapshot_id( new_tuple_without_serializer) assert new_snapshot_without_serializer != old_snapshot # By setting a custom serializer and skip_when_empty, the snapshot stays the same # as long as the new field is None class SkipWhenEmptySerializer(DefaultNamedTupleSerializer): @classmethod def skip_when_empty(cls) -> Set[str]: return {"bar"} @_whitelist_for_serdes(whitelist_map=test_map, serializer=SkipWhenEmptySerializer) class SameSnapshotTuple(namedtuple("_Tuple", "foo bar")): # pylint: disable=function-redefined def __new__(cls, foo, bar=None): return super(SameSnapshotTuple, cls).__new__( # pylint: disable=bad-super-call cls, foo, bar) new_tuple = SameSnapshotTuple(foo="A") new_snapshot = create_snapshot_id(new_tuple) assert old_snapshot == new_snapshot rehydrated_tuple = deserialize_json_to_dagster_namedtuple(old_serialized) assert rehydrated_tuple.foo == "A" assert rehydrated_tuple.bar is None new_tuple_with_bar = SameSnapshotTuple(foo="A", bar="B") assert new_tuple_with_bar.foo == "A" assert new_tuple_with_bar.bar == "B"
def from_json(json_str): return deserialize_json_to_dagster_namedtuple(json_str)
def test_deserialize_json_to_dagster_namedtuple_invalid_types(bad_obj): with pytest.raises(ParameterCheckError): deserialize_json_to_dagster_namedtuple(bad_obj)
def test_deserialize_json_to_dagster_namedtuple_non_namedtuple(): with pytest.raises(CheckError): deserialize_json_to_dagster_namedtuple('{"foo": "bar"}')
def test_deserialize_json_non_namedtuple(): with pytest.raises(DeserializationError, match="was not expected type"): deserialize_json_to_dagster_namedtuple('{"foo": "bar"}')
def test_external_job_origin_instigator_origin(): def build_legacy_whitelist_map(): legacy_env = WhitelistMap.create() @_whitelist_for_serdes(legacy_env) class ExternalJobOrigin( namedtuple("_ExternalJobOrigin", "external_repository_origin job_name")): def get_id(self): return create_snapshot_id(self) @_whitelist_for_serdes(legacy_env) class ExternalRepositoryOrigin( namedtuple("_ExternalRepositoryOrigin", "repository_location_origin repository_name")): def get_id(self): return create_snapshot_id(self) class GrpcServerOriginSerializer(DefaultNamedTupleSerializer): @classmethod def skip_when_empty(cls): return {"use_ssl"} @_whitelist_for_serdes(whitelist_map=legacy_env, serializer=GrpcServerOriginSerializer) class GrpcServerRepositoryLocationOrigin( namedtuple( "_GrpcServerRepositoryLocationOrigin", "host port socket location_name use_ssl", ), ): def __new__(cls, host, port=None, socket=None, location_name=None, use_ssl=None): return super(GrpcServerRepositoryLocationOrigin, cls).__new__(cls, host, port, socket, location_name, use_ssl) return ( legacy_env, ExternalJobOrigin, ExternalRepositoryOrigin, GrpcServerRepositoryLocationOrigin, ) legacy_env, klass, repo_klass, location_klass = build_legacy_whitelist_map( ) from dagster.core.host_representation.origin import ( ExternalInstigatorOrigin, ExternalRepositoryOrigin, GrpcServerRepositoryLocationOrigin, ) # serialize from current code, compare against old code instigator_origin = ExternalInstigatorOrigin( external_repository_origin=ExternalRepositoryOrigin( repository_location_origin=GrpcServerRepositoryLocationOrigin( host="localhost", port=1234, location_name="test_location"), repository_name="the_repo", ), instigator_name="simple_schedule", ) instigator_origin_str = serialize_dagster_namedtuple(instigator_origin) instigator_to_job = _deserialize_json(instigator_origin_str, legacy_env) assert isinstance(instigator_to_job, klass) # ensure that the origin id is stable assert instigator_to_job.get_id() == instigator_origin.get_id() # # serialize from old code, compare against current code job_origin = klass( external_repository_origin=repo_klass( repository_location_origin=location_klass( host="localhost", port=1234, location_name="test_location"), repository_name="the_repo", ), job_name="simple_schedule", ) job_origin_str = serialize_value(job_origin, legacy_env) from dagster.serdes.serdes import _WHITELIST_MAP job_to_instigator = deserialize_json_to_dagster_namedtuple(job_origin_str) assert isinstance(job_to_instigator, ExternalInstigatorOrigin) # ensure that the origin id is stable assert job_to_instigator.get_id() == job_origin.get_id()