|
| 1 | +package com.linkedin.datahub.graphql.types.mlmodel.mappers; |
| 2 | + |
| 3 | +import static org.testng.Assert.assertEquals; |
| 4 | +import static org.testng.Assert.assertNotNull; |
| 5 | +import static org.testng.Assert.assertNull; |
| 6 | + |
| 7 | +import com.linkedin.common.MLFeatureUrnArray; |
| 8 | +import com.linkedin.common.TimeStamp; |
| 9 | +import com.linkedin.common.VersionTag; |
| 10 | +import com.linkedin.common.url.Url; |
| 11 | +import com.linkedin.common.urn.MLFeatureUrn; |
| 12 | +import com.linkedin.common.urn.MLModelUrn; |
| 13 | +import com.linkedin.common.urn.Urn; |
| 14 | +import com.linkedin.data.template.StringArray; |
| 15 | +import com.linkedin.data.template.StringMap; |
| 16 | +import com.linkedin.ml.metadata.MLHyperParam; |
| 17 | +import com.linkedin.ml.metadata.MLHyperParamArray; |
| 18 | +import com.linkedin.ml.metadata.MLMetric; |
| 19 | +import com.linkedin.ml.metadata.MLMetricArray; |
| 20 | +import com.linkedin.ml.metadata.MLModelProperties; |
| 21 | +import java.net.URISyntaxException; |
| 22 | +import org.testng.annotations.Test; |
| 23 | + |
| 24 | +public class MLModelPropertiesMapperTest { |
| 25 | + |
| 26 | + @Test |
| 27 | + public void testMapMLModelProperties() throws URISyntaxException { |
| 28 | + MLModelProperties input = new MLModelProperties(); |
| 29 | + |
| 30 | + // Set basic properties |
| 31 | + input.setName("TestModel"); |
| 32 | + input.setDescription("A test ML model"); |
| 33 | + input.setType("Classification"); |
| 34 | + |
| 35 | + // Set version |
| 36 | + VersionTag versionTag = new VersionTag(); |
| 37 | + versionTag.setVersionTag("1.0.0"); |
| 38 | + input.setVersion(versionTag); |
| 39 | + |
| 40 | + // Set external URL |
| 41 | + Url externalUrl = new Url("https://example.com/model"); |
| 42 | + input.setExternalUrl(externalUrl); |
| 43 | + |
| 44 | + // Set created and last modified timestamps |
| 45 | + TimeStamp createdTimeStamp = new TimeStamp(); |
| 46 | + createdTimeStamp.setTime(1000L); |
| 47 | + Urn userUrn = Urn.createFromString("urn:li:corpuser:test"); |
| 48 | + createdTimeStamp.setActor(userUrn); |
| 49 | + input.setCreated(createdTimeStamp); |
| 50 | + |
| 51 | + TimeStamp lastModifiedTimeStamp = new TimeStamp(); |
| 52 | + lastModifiedTimeStamp.setTime(2000L); |
| 53 | + lastModifiedTimeStamp.setActor(userUrn); |
| 54 | + input.setLastModified(lastModifiedTimeStamp); |
| 55 | + |
| 56 | + // Set custom properties |
| 57 | + StringMap customProps = new StringMap(); |
| 58 | + customProps.put("key1", "value1"); |
| 59 | + customProps.put("key2", "value2"); |
| 60 | + input.setCustomProperties(customProps); |
| 61 | + |
| 62 | + // Set hyper parameters |
| 63 | + MLHyperParamArray hyperParams = new MLHyperParamArray(); |
| 64 | + MLHyperParam hyperParam1 = new MLHyperParam(); |
| 65 | + hyperParam1.setName("learning_rate"); |
| 66 | + hyperParam1.setValue("0.01"); |
| 67 | + hyperParams.add(hyperParam1); |
| 68 | + input.setHyperParams(hyperParams); |
| 69 | + |
| 70 | + // Set training metrics |
| 71 | + MLMetricArray trainingMetrics = new MLMetricArray(); |
| 72 | + MLMetric metric1 = new MLMetric(); |
| 73 | + metric1.setName("accuracy"); |
| 74 | + metric1.setValue("0.95"); |
| 75 | + trainingMetrics.add(metric1); |
| 76 | + input.setTrainingMetrics(trainingMetrics); |
| 77 | + |
| 78 | + // Set ML features |
| 79 | + MLFeatureUrnArray mlFeatures = new MLFeatureUrnArray(); |
| 80 | + MLFeatureUrn featureUrn = MLFeatureUrn.createFromString("urn:li:mlFeature:(dataset,feature)"); |
| 81 | + mlFeatures.add(featureUrn); |
| 82 | + input.setMlFeatures(mlFeatures); |
| 83 | + |
| 84 | + // Set tags |
| 85 | + StringArray tags = new StringArray(); |
| 86 | + tags.add("tag1"); |
| 87 | + tags.add("tag2"); |
| 88 | + input.setTags(tags); |
| 89 | + |
| 90 | + // Set training and downstream jobs |
| 91 | + input.setTrainingJobs( |
| 92 | + new com.linkedin.common.UrnArray(Urn.createFromString("urn:li:dataJob:train"))); |
| 93 | + input.setDownstreamJobs( |
| 94 | + new com.linkedin.common.UrnArray(Urn.createFromString("urn:li:dataJob:predict"))); |
| 95 | + |
| 96 | + // Create ML Model URN |
| 97 | + MLModelUrn modelUrn = |
| 98 | + MLModelUrn.createFromString( |
| 99 | + "urn:li:mlModel:(urn:li:dataPlatform:sagemaker,unittestmodel,PROD)"); |
| 100 | + |
| 101 | + // Map the properties |
| 102 | + com.linkedin.datahub.graphql.generated.MLModelProperties result = |
| 103 | + MLModelPropertiesMapper.map(null, input, modelUrn); |
| 104 | + |
| 105 | + // Verify mapped properties |
| 106 | + assertNotNull(result); |
| 107 | + assertEquals(result.getName(), "TestModel"); |
| 108 | + assertEquals(result.getDescription(), "A test ML model"); |
| 109 | + assertEquals(result.getType(), "Classification"); |
| 110 | + assertEquals(result.getVersion(), "1.0.0"); |
| 111 | + assertEquals(result.getExternalUrl(), "https://example.com/model"); |
| 112 | + |
| 113 | + // Verify audit stamps |
| 114 | + assertNotNull(result.getCreated()); |
| 115 | + assertEquals(result.getCreated().getTime().longValue(), 1000L); |
| 116 | + assertEquals(result.getCreated().getActor(), userUrn.toString()); |
| 117 | + |
| 118 | + assertNotNull(result.getLastModified()); |
| 119 | + assertEquals(result.getLastModified().getTime().longValue(), 2000L); |
| 120 | + assertEquals(result.getLastModified().getActor(), userUrn.toString()); |
| 121 | + |
| 122 | + // Verify custom properties |
| 123 | + assertNotNull(result.getCustomProperties()); |
| 124 | + |
| 125 | + // Verify hyper parameters |
| 126 | + assertNotNull(result.getHyperParams()); |
| 127 | + assertEquals(result.getHyperParams().size(), 1); |
| 128 | + assertEquals(result.getHyperParams().get(0).getName(), "learning_rate"); |
| 129 | + assertEquals(result.getHyperParams().get(0).getValue(), "0.01"); |
| 130 | + |
| 131 | + // Verify training metrics |
| 132 | + assertNotNull(result.getTrainingMetrics()); |
| 133 | + assertEquals(result.getTrainingMetrics().size(), 1); |
| 134 | + assertEquals(result.getTrainingMetrics().get(0).getName(), "accuracy"); |
| 135 | + assertEquals(result.getTrainingMetrics().get(0).getValue(), "0.95"); |
| 136 | + |
| 137 | + // Verify ML features |
| 138 | + assertNotNull(result.getMlFeatures()); |
| 139 | + assertEquals(result.getMlFeatures().size(), 1); |
| 140 | + assertEquals(result.getMlFeatures().get(0), featureUrn.toString()); |
| 141 | + |
| 142 | + // Verify tags |
| 143 | + assertNotNull(result.getTags()); |
| 144 | + assertEquals(result.getTags().get(0), "tag1"); |
| 145 | + assertEquals(result.getTags().get(1), "tag2"); |
| 146 | + |
| 147 | + // Verify lineage info |
| 148 | + assertNotNull(result.getMlModelLineageInfo()); |
| 149 | + assertEquals(result.getMlModelLineageInfo().getTrainingJobs().size(), 1); |
| 150 | + assertEquals(result.getMlModelLineageInfo().getTrainingJobs().get(0), "urn:li:dataJob:train"); |
| 151 | + assertEquals(result.getMlModelLineageInfo().getDownstreamJobs().size(), 1); |
| 152 | + assertEquals( |
| 153 | + result.getMlModelLineageInfo().getDownstreamJobs().get(0), "urn:li:dataJob:predict"); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void testMapWithMissingName() throws URISyntaxException { |
| 158 | + MLModelProperties input = new MLModelProperties(); |
| 159 | + MLModelUrn modelUrn = |
| 160 | + MLModelUrn.createFromString( |
| 161 | + "urn:li:mlModel:(urn:li:dataPlatform:sagemaker,missingnamemodel,PROD)"); |
| 162 | + |
| 163 | + com.linkedin.datahub.graphql.generated.MLModelProperties result = |
| 164 | + MLModelPropertiesMapper.map(null, input, modelUrn); |
| 165 | + |
| 166 | + // Verify that name is extracted from URN when not present in input |
| 167 | + assertEquals(result.getName(), "missingnamemodel"); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + public void testMapWithMinimalProperties() throws URISyntaxException { |
| 172 | + MLModelProperties input = new MLModelProperties(); |
| 173 | + MLModelUrn modelUrn = |
| 174 | + MLModelUrn.createFromString( |
| 175 | + "urn:li:mlModel:(urn:li:dataPlatform:sagemaker,minimalmodel,PROD)"); |
| 176 | + |
| 177 | + com.linkedin.datahub.graphql.generated.MLModelProperties result = |
| 178 | + MLModelPropertiesMapper.map(null, input, modelUrn); |
| 179 | + |
| 180 | + // Verify basic mapping with minimal properties |
| 181 | + assertNotNull(result); |
| 182 | + assertEquals(result.getName(), "minimalmodel"); |
| 183 | + assertNull(result.getDescription()); |
| 184 | + assertNull(result.getType()); |
| 185 | + assertNull(result.getVersion()); |
| 186 | + } |
| 187 | +} |
0 commit comments