aglinxinyuan commented on code in PR #5402: URL: https://github.com/apache/texera/pull/5402#discussion_r3366972649
########## amber/src/test/scala/org/apache/texera/web/resource/dashboard/hub/HubEntityModelSpec.scala: ########## @@ -0,0 +1,221 @@ +/* + * 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.web.resource.dashboard.hub + +import org.apache.texera.amber.util.JSONUtils.objectMapper +import org.apache.texera.dao.jooq.generated.Tables._ +import org.scalatest.flatspec.AnyFlatSpec + +class HubEntityModelSpec extends AnyFlatSpec { + + // --------------------------------------------------------------------------- + // ActionType + // --------------------------------------------------------------------------- + + "ActionType subtypes" should "expose their lowercase string value" in { + assert(ActionType.View.value == "view") + assert(ActionType.Like.value == "like") + assert(ActionType.Clone.value == "clone") + assert(ActionType.Unlike.value == "unlike") + } + + it should "have toString equal to value (override pin)" in { + // The trait override `toString = value` is what reaches log lines and + // tracking pipelines. Pin so a regression that returns the case-object + // name (e.g. "View" instead of "view") breaks here. + val all: List[ActionType] = + List(ActionType.View, ActionType.Like, ActionType.Clone, ActionType.Unlike) + all.foreach(a => assert(a.toString == a.value, s"$a.toString != $a.value")) + } Review Comment: Done in d04e516529 — switched the failure message to use `a.getClass.getSimpleName` (stable across the SUT) instead of the toString-interpolation that was itself the contract being pinned. A regression in toString now produces a readable diagnostic naming the case object. ########## amber/src/test/scala/org/apache/texera/web/resource/dashboard/hub/HubEntityModelSpec.scala: ########## @@ -0,0 +1,221 @@ +/* + * 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.web.resource.dashboard.hub + +import org.apache.texera.amber.util.JSONUtils.objectMapper +import org.apache.texera.dao.jooq.generated.Tables._ +import org.scalatest.flatspec.AnyFlatSpec + +class HubEntityModelSpec extends AnyFlatSpec { + + // --------------------------------------------------------------------------- + // ActionType + // --------------------------------------------------------------------------- + + "ActionType subtypes" should "expose their lowercase string value" in { + assert(ActionType.View.value == "view") + assert(ActionType.Like.value == "like") + assert(ActionType.Clone.value == "clone") + assert(ActionType.Unlike.value == "unlike") + } + + it should "have toString equal to value (override pin)" in { + // The trait override `toString = value` is what reaches log lines and + // tracking pipelines. Pin so a regression that returns the case-object + // name (e.g. "View" instead of "view") breaks here. + val all: List[ActionType] = + List(ActionType.View, ActionType.Like, ActionType.Clone, ActionType.Unlike) + all.foreach(a => assert(a.toString == a.value, s"$a.toString != $a.value")) + } + + "ActionType.fromString" should "match each subtype exactly" in { + assert(ActionType.fromString("view") == ActionType.View) + assert(ActionType.fromString("like") == ActionType.Like) + assert(ActionType.fromString("clone") == ActionType.Clone) + assert(ActionType.fromString("unlike") == ActionType.Unlike) + } + + it should "match case-insensitively" in { + assert(ActionType.fromString("VIEW") == ActionType.View) + assert(ActionType.fromString("Like") == ActionType.Like) + assert(ActionType.fromString("CLONE") == ActionType.Clone) + assert(ActionType.fromString("uNlIkE") == ActionType.Unlike) + } + + it should "throw IllegalArgumentException for an unknown action, naming the input" in { + val ex = intercept[IllegalArgumentException] { + ActionType.fromString("delete") + } + assert(ex.getMessage.contains("delete"), s"unexpected message: ${ex.getMessage}") + } + + it should "throw IllegalArgumentException for an empty string" in { + intercept[IllegalArgumentException] { + ActionType.fromString("") + } + } + + "ActionType Jackson round-trip" should "serialize each subtype as its lowercase string value" in { + // `@JsonValue` on `value` instructs Jackson to emit the field's value + // string instead of a wrapping object form. + assert(objectMapper.writeValueAsString(ActionType.View: ActionType) == "\"view\"") + assert(objectMapper.writeValueAsString(ActionType.Like: ActionType) == "\"like\"") + assert(objectMapper.writeValueAsString(ActionType.Clone: ActionType) == "\"clone\"") + assert(objectMapper.writeValueAsString(ActionType.Unlike: ActionType) == "\"unlike\"") + } + + it should "deserialize from the lowercase string back to the canonical subtype" in { + // `@JsonCreator` on `fromString` lets Jackson reconstruct the subtype + // from a raw string field. + assert(objectMapper.readValue("\"view\"", classOf[ActionType]) == ActionType.View) + assert(objectMapper.readValue("\"like\"", classOf[ActionType]) == ActionType.Like) + assert(objectMapper.readValue("\"clone\"", classOf[ActionType]) == ActionType.Clone) + assert(objectMapper.readValue("\"unlike\"", classOf[ActionType]) == ActionType.Unlike) + } + + it should "honor the case-insensitive deserialization via @JsonCreator" in { + assert(objectMapper.readValue("\"VIEW\"", classOf[ActionType]) == ActionType.View) + } + + // --------------------------------------------------------------------------- + // EntityType + // --------------------------------------------------------------------------- + + "EntityType subtypes" should "expose their lowercase string value" in { + assert(EntityType.Workflow.value == "workflow") + assert(EntityType.Dataset.value == "dataset") + } + + it should "have toString equal to value (override pin)" in { + val all: List[EntityType] = List(EntityType.Workflow, EntityType.Dataset) + all.foreach(e => assert(e.toString == e.value, s"$e.toString != $e.value")) + } Review Comment: Done in d04e516529 — switched the failure message to use `a.getClass.getSimpleName` (stable across the SUT) instead of the toString-interpolation that was itself the contract being pinned. A regression in toString now produces a readable diagnostic naming the case object. -- 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]
