Copilot commented on code in PR #5878: URL: https://github.com/apache/texera/pull/5878#discussion_r3449346168
########## common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/machineLearning/sklearnAdvanced/base/HyperParametersSpec.scala: ########## @@ -0,0 +1,82 @@ +/* + * 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.machineLearning.sklearnAdvanced.base + +import com.fasterxml.jackson.annotation.JsonProperty +import org.apache.texera.amber.util.JSONUtils.objectMapper +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers + +class HyperParametersSpec extends AnyFlatSpec with Matchers { + + "HyperParameters" should + "default parameter/attribute/value to null and parametersSource to false" in { + val h = new HyperParameters[String] + h.parameter shouldBe null + h.attribute shouldBe null + h.value shouldBe null + h.parametersSource shouldBe false + } + + it should "allow all fields to be assigned post-construction" in { + val h = new HyperParameters[String] + h.parameter = "alpha" + h.attribute = "colA" + h.value = "0.5" + h.parametersSource = true + h.parameter shouldBe "alpha" + h.attribute shouldBe "colA" + h.value shouldBe "0.5" + h.parametersSource shouldBe true + } + + "HyperParameters" should "expose attribute and value under their @JsonProperty wire-keys" in { + classOf[HyperParameters[_]] + .getDeclaredField("attribute") + .getAnnotation(classOf[JsonProperty]) + .value shouldBe "attribute" + classOf[HyperParameters[_]] + .getDeclaredField("value") + .getAnnotation(classOf[JsonProperty]) + .value shouldBe "value" + } Review Comment: The test is using Java reflection on the Scala compiler–generated backing fields to assert `@JsonProperty` values. This is brittle (annotation target placement can differ across Scala/Jackson configurations) and doesn’t actually validate the JSON wire format. Prefer asserting on serialized JSON keys instead. ########## common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/machineLearning/Scorer/MachineLearningScorerOpDescSpec.scala: ########## @@ -0,0 +1,89 @@ +/* + * 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.machineLearning.Scorer + +import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema} +import org.apache.texera.amber.operator.LogicalOp +import org.apache.texera.amber.operator.metadata.OperatorGroupConstants +import org.apache.texera.amber.util.JSONUtils.objectMapper +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers + +class MachineLearningScorerOpDescSpec extends AnyFlatSpec with Matchers { + + "MachineLearningScorerOpDesc.operatorInfo" should + "advertise the name and Machine Learning General group" in { + val info = (new MachineLearningScorerOpDesc).operatorInfo + info.userFriendlyName shouldBe "Machine Learning Scorer" + info.operatorDescription shouldBe "Scorer for machine learning models" + info.operatorGroupName shouldBe OperatorGroupConstants.MACHINE_LEARNING_GENERAL_GROUP + info.inputPorts should have length 1 + info.outputPorts should have length 1 + } + + "MachineLearningScorerOpDesc" should "default isRegression false and the column fields to empty" in { + val d = new MachineLearningScorerOpDesc + d.isRegression shouldBe false + d.actualValueColumn shouldBe "" + d.predictValueColumn shouldBe "" + d.classificationMetrics shouldBe empty + d.regressionMetrics shouldBe empty + } + + "MachineLearningScorerOpDesc.getOutputSchemas" should + "include a Class column for classification with no metrics" in { + val d = new MachineLearningScorerOpDesc + d.getOutputSchemas(Map.empty) shouldBe Map( + d.operatorInfo.outputPorts.head.id -> Schema( + List(new Attribute("Class", AttributeType.STRING)) + ) + ) + } + + it should "produce an empty schema for regression with no metrics" in { + val d = new MachineLearningScorerOpDesc + d.isRegression = true + val out = d.getOutputSchemas(Map.empty) + out.keySet shouldBe Set(d.operatorInfo.outputPorts.head.id) + out(d.operatorInfo.outputPorts.head.id).getAttributes shouldBe empty + } + + "MachineLearningScorerOpDesc.generatePythonCode" should "emit the scorer table operator" in { + val d = new MachineLearningScorerOpDesc + d.actualValueColumn = "y" + d.predictValueColumn = "yhat" + val code = d.generatePythonCode() + code should include("class ProcessTableOperator(UDFTableOperator)") + code should include("from sklearn.metrics import") + } Review Comment: This test sets `actualValueColumn`/`predictValueColumn`, but the assertions only check for the operator class and sklearn import. Adding a couple of assertions that the generated code actually references the configured columns would better pin the contract and catch regressions in the template interpolation. -- 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]
