Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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.visualization.candlestickChart

import org.apache.texera.amber.core.tuple.{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 CandlestickChartOpDescSpec extends AnyFlatSpec with Matchers {

private def configured(): CandlestickChartOpDesc = {
val d = new CandlestickChartOpDesc
d.date = "day"
d.open = "o"
d.high = "h"
d.low = "l"
d.close = "c"
d
}

"CandlestickChartOpDesc.operatorInfo" should
"advertise the name and Financial visualization group" in {
val info = (new CandlestickChartOpDesc).operatorInfo
info.userFriendlyName shouldBe "Candlestick Chart"
info.operatorGroupName shouldBe OperatorGroupConstants.VISUALIZATION_FINANCIAL_GROUP
info.inputPorts should have length 1
info.outputPorts should have length 1
}

"CandlestickChartOpDesc" should "default all OHLC column fields to the empty string" in {
val d = new CandlestickChartOpDesc
d.date shouldBe ""
d.open shouldBe ""
d.high shouldBe ""
d.low shouldBe ""
d.close shouldBe ""
}

"CandlestickChartOpDesc.getOutputSchemas" should
"produce a single html-content STRING column keyed by the declared output port" in {
val op = new CandlestickChartOpDesc
val out = op.getOutputSchemas(Map(op.operatorInfo.inputPorts.head.id -> Schema()))
out shouldBe Map(
op.operatorInfo.outputPorts.head.id -> Schema().add("html-content", AttributeType.STRING)
)
}

"CandlestickChartOpDesc.generatePythonCode" should "emit a Plotly Candlestick figure" in {
val code = configured().generatePythonCode()
code should include("go.Candlestick(")
code should include("html-content")
}

"CandlestickChartOpDesc" should "round-trip its OHLC fields through the polymorphic base" in {
val restored =
objectMapper.readValue(objectMapper.writeValueAsString(configured()), classOf[LogicalOp])
restored shouldBe a[CandlestickChartOpDesc]
val c = restored.asInstanceOf[CandlestickChartOpDesc]
c.date shouldBe "day"
c.open shouldBe "o"
c.high shouldBe "h"
c.low shouldBe "l"
c.close shouldBe "c"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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.visualization.histogram2d

import org.apache.texera.amber.core.tuple.{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 Histogram2DOpDescSpec extends AnyFlatSpec with Matchers {

private def configured(): Histogram2DOpDesc = {
val d = new Histogram2DOpDesc
d.xColumn = "x"
d.yColumn = "y"
d
}

"Histogram2DOpDesc.operatorInfo" should
"advertise the name and Statistical visualization group" in {
val info = (new Histogram2DOpDesc).operatorInfo
info.userFriendlyName shouldBe "Histogram2D"
info.operatorGroupName shouldBe OperatorGroupConstants.VISUALIZATION_STATISTICAL_GROUP
info.inputPorts should have length 1
info.outputPorts should have length 1
}

"Histogram2DOpDesc" should "default bins to 10 and normalization to DENSITY" in {
val d = new Histogram2DOpDesc
d.xBins shouldBe 10
d.yBins shouldBe 10
d.normalize shouldBe NormalizationType.DENSITY
d.xColumn shouldBe ""
d.yColumn shouldBe ""
}

"Histogram2DOpDesc.getOutputSchemas" should
"produce a single html-content STRING column keyed by the declared output port" in {
val op = new Histogram2DOpDesc
val out = op.getOutputSchemas(Map(op.operatorInfo.inputPorts.head.id -> Schema()))
out shouldBe Map(
op.operatorInfo.outputPorts.head.id -> Schema().add("html-content", AttributeType.STRING)
)
}

"Histogram2DOpDesc.generatePythonCode" should "reject a non-positive bin count" in {
val d = configured()
d.xBins = 0
intercept[AssertionError] {
d.generatePythonCode()
}
}

it should "emit a Plotly density-heatmap figure for a valid config" in {
val code = configured().generatePythonCode()
code should include("px.density_heatmap(")
code should include("html-content")
}

"Histogram2DOpDesc" should "round-trip its fields through the polymorphic base" in {
val d = configured()
d.xBins = 20
d.yBins = 5
d.normalize = NormalizationType.PROBABILITY
val restored = objectMapper.readValue(objectMapper.writeValueAsString(d), classOf[LogicalOp])
restored shouldBe a[Histogram2DOpDesc]
val h = restored.asInstanceOf[Histogram2DOpDesc]
h.xColumn shouldBe "x"
h.yColumn shouldBe "y"
h.xBins shouldBe 20
h.yBins shouldBe 5
h.normalize shouldBe NormalizationType.PROBABILITY
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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.visualization.htmlviz

import com.fasterxml.jackson.annotation.JsonProperty
import javax.validation.constraints.NotNull
import org.apache.texera.amber.core.executor.OpExecWithClassName
import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema}
import org.apache.texera.amber.core.virtualidentity.{ExecutionIdentity, WorkflowIdentity}
import org.apache.texera.amber.operator.LogicalOp
import org.apache.texera.amber.operator.metadata.OperatorGroupConstants
import org.apache.texera.amber.operator.metadata.annotations.AutofillAttributeName
import org.apache.texera.amber.util.JSONUtils.objectMapper
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class HtmlVizOpDescSpec extends AnyFlatSpec with Matchers {

private val workflowId = WorkflowIdentity(1L)
private val executionId = ExecutionIdentity(1L)

"HtmlVizOpDesc.operatorInfo" should "advertise the name and Media visualization group" in {
val info = (new HtmlVizOpDesc).operatorInfo
info.userFriendlyName shouldBe "HTML Visualizer"
info.operatorGroupName shouldBe OperatorGroupConstants.VISUALIZATION_MEDIA_GROUP
info.inputPorts should have length 1
info.outputPorts should have length 1
}

"HtmlVizOpDesc.htmlContentAttrName" should "default to the empty string" in {
(new HtmlVizOpDesc).htmlContentAttrName shouldBe ""
}
Comment thread
aglinxinyuan marked this conversation as resolved.

it should "carry @JsonProperty(required = true) + @AutofillAttributeName + @NotNull" in {
val field = classOf[HtmlVizOpDesc].getDeclaredField("htmlContentAttrName")
val jp = field.getAnnotation(classOf[JsonProperty])
jp should not be null
jp.required shouldBe true
field.getAnnotation(classOf[AutofillAttributeName]) should not be null
val notNull = field.getAnnotation(classOf[NotNull])
notNull should not be null
notNull.message shouldBe "HTML content cannot be empty"
}

"HtmlVizOpDesc" should "round-trip htmlContentAttrName through the polymorphic base" in {
val op = new HtmlVizOpDesc
op.htmlContentAttrName = "myCol"
val restored = objectMapper.readValue(objectMapper.writeValueAsString(op), classOf[LogicalOp])
restored shouldBe a[HtmlVizOpDesc]
restored.asInstanceOf[HtmlVizOpDesc].htmlContentAttrName shouldBe "myCol"
}

"HtmlVizOpDesc.getPhysicalOp" should "wire HtmlVizOpExec and carry port identities" in {
val op = new HtmlVizOpDesc
op.htmlContentAttrName = "html"
val physical = op.getPhysicalOp(workflowId, executionId)
physical.opExecInitInfo match {
case OpExecWithClassName(className, descString) =>
className shouldBe "org.apache.texera.amber.operator.visualization.htmlviz.HtmlVizOpExec"
descString should not be empty
case other => fail(s"expected OpExecWithClassName, got $other")
}
physical.inputPorts.keySet shouldBe op.operatorInfo.inputPorts.map(_.id).toSet
physical.outputPorts.keySet shouldBe op.operatorInfo.outputPorts.map(_.id).toSet
}

"HtmlVizOpDesc schema propagation" should
"emit a single html-content STRING column keyed by the declared output port" in {
val op = new HtmlVizOpDesc
op.htmlContentAttrName = "html"
val input = Schema().add(new Attribute("html", AttributeType.STRING))
val out = op.getExternalOutputSchemas(Map(op.operatorInfo.inputPorts.head.id -> input))
out shouldBe Map(
op.operatorInfo.outputPorts.head.id -> Schema().add("html-content", AttributeType.STRING)
)
}
}
Loading