@@ -40,6 +40,7 @@ limitations under the License.
4040#include " ml_metadata/proto/metadata_store.pb.h"
4141#include " ml_metadata/proto/metadata_store_service.pb.h"
4242#include " ml_metadata/simple_types/proto/simple_types.pb.h"
43+ #include " google/protobuf/repeated_ptr_field.h"
4344
4445namespace ml_metadata {
4546namespace testing {
@@ -7263,6 +7264,118 @@ TEST_P(MetadataStoreTestSuite, PutLineageSubgraphAndVerifyLineageGraph) {
72637264 /* ignore_fields=*/ {" milliseconds_since_epoch" })));
72647265}
72657266
7267+ // Test that PutLineageSubgraph Upsert artifacts before execution.
7268+ TEST_P (MetadataStoreTestSuite, PutLineageSubgraphWithNewArtifactsExecutions) {
7269+ // Prepare types and write them to MLMD.
7270+ PutTypesRequest put_types_request = ParseTextProtoOrDie<PutTypesRequest>(R"pb(
7271+ context_types: {
7272+ name: 'context_type'
7273+ properties { key: 'property_1' value: INT }
7274+ }
7275+
7276+ execution_types: {
7277+ name: 'execution_type'
7278+ properties { key: 'property_1' value: DOUBLE }
7279+ }
7280+ artifact_types: {
7281+ name: 'artifact_type'
7282+ properties { key: 'property_1' value: STRING }
7283+ }
7284+ )pb" );
7285+ PutTypesResponse put_types_response;
7286+ ASSERT_EQ (absl::OkStatus (),
7287+ metadata_store_->PutTypes (put_types_request, &put_types_response));
7288+
7289+ // Prepare a context and write it to MLMD.
7290+ Context context;
7291+ context.set_type_id (put_types_response.context_type_ids (0 ));
7292+ context.set_name (" context" );
7293+ (*context.mutable_properties ())[" property_1" ].set_int_value (1 );
7294+ PutContextsRequest put_contexts_request;
7295+ *put_contexts_request.add_contexts () = context;
7296+ PutContextsResponse put_contexts_response;
7297+ ASSERT_EQ (absl::OkStatus (),
7298+ metadata_store_->PutContexts (put_contexts_request,
7299+ &put_contexts_response));
7300+
7301+ // Prepare a new execution and write it to MLMD.
7302+ Execution execution;
7303+ execution.set_type_id (put_types_response.execution_type_ids (0 ));
7304+ (*execution.mutable_properties ())[" property_1" ].set_double_value (1.0 );
7305+ PutExecutionRequest put_execution_request;
7306+ *put_execution_request.mutable_execution () = execution;
7307+ PutExecutionResponse put_execution_response;
7308+ ASSERT_EQ (absl::OkStatus (),
7309+ metadata_store_->PutExecution (put_execution_request,
7310+ &put_execution_response));
7311+ execution.set_id (put_execution_response.execution_id ());
7312+
7313+ // Prepare a new artifact, but don't write it to MLMD.
7314+ Artifact artifact;
7315+ artifact.set_type_id (put_types_response.artifact_type_ids (0 ));
7316+ artifact.set_uri (" testuri" );
7317+ (*artifact.mutable_properties ())[" property_1" ].set_string_value (" 1" );
7318+
7319+ // Prepare the PutLineageSubgraph request
7320+ PutLineageSubgraphRequest put_lineage_subgraph_request;
7321+ *put_lineage_subgraph_request.add_executions () = execution;
7322+ *put_lineage_subgraph_request.add_artifacts () = artifact;
7323+ *put_lineage_subgraph_request.add_contexts () = context;
7324+ put_lineage_subgraph_request.mutable_options ()
7325+ ->set_reuse_context_if_already_exist (true );
7326+
7327+ // Prepare event_edge with execution_index and artifact_index but no
7328+ // execution_id and no artifact_id
7329+ Event event;
7330+ event.set_type (Event::OUTPUT );
7331+ PutLineageSubgraphRequest::EventEdge* event_edge =
7332+ put_lineage_subgraph_request.add_event_edges ();
7333+ event_edge->set_execution_index (0 );
7334+ event_edge->set_artifact_index (0 );
7335+ *event_edge->mutable_event () = event;
7336+
7337+ PutLineageSubgraphResponse put_lineage_subgraph_response;
7338+ ASSERT_EQ (absl::OkStatus (),
7339+ metadata_store_->PutLineageSubgraph (
7340+ put_lineage_subgraph_request, &put_lineage_subgraph_response));
7341+
7342+ // Verify that new artifact and execution are correctly written.
7343+ GetExecutionsByContextRequest get_executions_by_context_request;
7344+ get_executions_by_context_request.set_context_id (
7345+ put_lineage_subgraph_response.context_ids (0 ));
7346+ GetExecutionsByContextResponse get_executions_by_context_response;
7347+ ASSERT_EQ (absl::OkStatus (), metadata_store_->GetExecutionsByContext (
7348+ get_executions_by_context_request,
7349+ &get_executions_by_context_response));
7350+ ASSERT_THAT (get_executions_by_context_response.executions (), SizeIs (1 ));
7351+ EXPECT_THAT (get_executions_by_context_response.executions (),
7352+ ElementsAre (EqualsProto (
7353+ execution,
7354+ /* ignore_fields=*/ {" id" , " type" , " create_time_since_epoch" ,
7355+ " last_update_time_since_epoch" })));
7356+
7357+ GetArtifactsByContextRequest get_artifacts_by_context_request;
7358+ get_artifacts_by_context_request.set_context_id (
7359+ put_lineage_subgraph_response.context_ids (0 ));
7360+ GetArtifactsByContextResponse get_artifacts_by_context_response;
7361+ ASSERT_EQ (absl::OkStatus (), metadata_store_->GetArtifactsByContext (
7362+ get_artifacts_by_context_request,
7363+ &get_artifacts_by_context_response));
7364+ ASSERT_THAT (get_artifacts_by_context_response.artifacts (), SizeIs (1 ));
7365+ EXPECT_THAT (get_artifacts_by_context_response.artifacts (),
7366+ ElementsAre (EqualsProto (
7367+ artifact,
7368+ /* ignore_fields=*/ {" id" , " type" , " create_time_since_epoch" ,
7369+ " last_update_time_since_epoch" })));
7370+
7371+ // Verify that the update time of execution is larger than the creation time
7372+ // of the artifact.
7373+ EXPECT_GE (
7374+ get_executions_by_context_response.executions (0 )
7375+ .last_update_time_since_epoch (),
7376+ get_artifacts_by_context_response.artifacts (0 ).create_time_since_epoch ());
7377+ }
7378+
72667379TEST_P (MetadataStoreTestSuite,
72677380 PutLineageSubgraphUpdatesArtifactsAndExecutions) {
72687381 // Prepare the metadata store with types
0 commit comments