kunwp1 commented on code in PR #9202:
URL: https://github.com/apache/texera/pull/9202#discussion_r4418689839


##########
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:
   Small suggestion: `operatorType` is the one value here that saved workflows 
persist, and it's now derived from the class name rather than written out like 
the model name and import. That changes what the round-trip test pins, from 
"the JSON name is `SklearnSDG`" to "the JSON name equals the class name". For 
example, renaming `SklearnSDGOpDesc` to fix the SDG/SGD typo while keeping 
`name = "SklearnSDG"` for compatibility would now fail this test, which nudges 
the fix toward renaming the persisted name instead. Passing `operatorType` as a 
literal, like the other two values, would keep the old guarantee for one extra 
argument per spec.



-- 
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]

Reply via email to