aglinxinyuan commented on code in PR #9202: URL: https://github.com/apache/texera/pull/9202#discussion_r4421235994
########## common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/sklearn/SklearnModelOpDescBehaviors.scala: ########## @@ -0,0 +1,146 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.texera.amber.operator.sklearn + +import org.apache.texera.amber.core.tuple.AttributeType +import org.apache.texera.amber.operator.LogicalOp +import org.apache.texera.amber.operator.metadata.OperatorGroupConstants +import org.apache.texera.amber.operator.sklearn.training.SklearnTrainingOpDesc +import org.apache.texera.amber.util.JSONUtils.objectMapper +import org.scalatest.flatspec.AnyFlatSpecLike +import org.scalatest.matchers.should.Matchers + +import scala.reflect.ClassTag + +/** + * The five tests every per-model sklearn descriptor spec shares. Each estimator + * ships a classifier descriptor (training and testing ports, Sklearn group) and a + * training-only descriptor (one training port, Sklearn Training group), and both + * pin the same contracts: operatorInfo, the config fields' defaults, the + * model_name/model output schema, the estimator import in the generated code, and + * a JSON round-trip through the polymorphic `LogicalOp` base. + * + * Each spec passes its model name and sklearn import as literals rather than + * reading them back from the descriptor, so a renamed model or a changed import + * still fails that model's own spec. The JSON `operatorType` is the class name + * without its `OpDesc` suffix, the name every sklearn descriptor registers under. + * Test names keep the `<Descriptor>[.<member>] should ...` form, so a model's + * spec reports the same names as before. + * + * `newDesc` is by-name: every test builds its own descriptor. + */ +trait SklearnModelOpDescBehaviors extends AnyFlatSpecLike with Matchers { + + /** Registers the shared tests for a classifier descriptor. */ + protected def sklearnClassifier[T <: SklearnClassifierOpDesc: ClassTag]( + newDesc: => T, + modelName: String, + estimatorImport: String, + estimatorTestName: String = "import the configured sklearn estimator" + ): Unit = + sklearnModel( + newDesc, + modelName, + estimatorImport, + estimatorTestName, + operatorInfoTestName = + "advertise the model name, Sklearn group, and the training/testing port shape", + group = OperatorGroupConstants.SKLEARN_GROUP, + inputPorts = List("training", "testing") + ) + + /** Registers the shared tests for a training-only descriptor. */ + protected def sklearnTrainer[T <: SklearnTrainingOpDesc: ClassTag]( + newDesc: => T, + modelName: String, + estimatorImport: String + ): Unit = + sklearnModel( + newDesc, + modelName, + estimatorImport, + estimatorTestName = "import the configured sklearn estimator", + operatorInfoTestName = + "advertise the model name, Sklearn Training group, and the single training port", + group = OperatorGroupConstants.SKLEARN_TRAINING_GROUP, + inputPorts = List("training") + ) + + private def sklearnModel[T <: SklearnModelOpDesc: ClassTag]( + newDesc: => T, + modelName: String, + estimatorImport: String, + estimatorTestName: String, + operatorInfoTestName: String, + group: String, + inputPorts: List[String] + ): Unit = { + val descriptor = implicitly[ClassTag[T]].runtimeClass.getSimpleName + val operatorType = descriptor.stripSuffix("OpDesc") Review Comment: Good catch, done in c5a3123a86: `sklearnClassifier` and `sklearnTrainer` now take `operatorType` as a literal next to the model name and import (each value taken from that spec's version on main), so the round-trip test pins the persisted name again instead of the class name. _🤖 Addressed by [Claude Code](https://claude.com/claude-code)_ -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
