fedimser commented on code in PR #53159:
URL: https://github.com/apache/spark/pull/53159#discussion_r2662866251


##########
sql/core/src/test/scala/org/apache/spark/sql/streaming/TwsTesterSuite.scala:
##########
@@ -0,0 +1,699 @@
+/*
+ * 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.spark.sql.streaming
+
+import org.apache.spark.SparkFunSuite
+import org.apache.spark.sql.Dataset
+import org.apache.spark.sql.execution.streaming.runtime.MemoryStream
+import org.apache.spark.sql.execution.streaming.state.RocksDBStateStoreProvider
+import org.apache.spark.sql.internal.SQLConf
+
+/** Test suite for TwsTester utility class. */
+class TwsTesterSuite extends SparkFunSuite {
+
+  test("TwsTester should correctly test RunningCountProcessor") {
+    val tester = new TwsTester(new RunningCountProcessor[String]())
+    assert(tester.test("key1", List("a")) == List(("key1", 1L)))
+    assert(tester.test("key2", List("a", "a")) == List(("key2", 2L)))
+    assert(tester.test("key3", List("a")) == List(("key3", 1L)))
+    assert(tester.test("key1", List("a", "a", "a")) == List(("key1", 4L)))
+
+    assert(tester.peekValueState[Long]("count", "key1").get == 4L)
+    assert(tester.peekValueState[Long]("count", "key2").get == 2L)
+    assert(tester.peekValueState[Long]("count", "key3").get == 1L)
+    assert(tester.peekValueState[Long]("count", "key4").isEmpty)
+  }
+
+  test("TwsTester should allow direct access to ValueState") {
+    val processor = new RunningCountProcessor[String]()
+    val tester = new TwsTester[String, String, (String, Long)](processor)
+    tester.setValueState[Long]("count", "foo", 5)
+    tester.test("foo", List("a"))
+    assert(tester.peekValueState[Long]("count", "foo").get == 6L)
+  }
+
+  test("TwsTester should correctly test TopKProcessor") {
+    val input: List[(String, (String, Double))] = List(
+      ("key2", ("c", 30.0)),
+      ("key2", ("d", 40.0)),
+      ("key2", ("a", 10.0)),
+      ("key2", ("b", 20.0)),
+      ("key3", ("a", 100.0))
+    )
+    val tester = new TwsTester(new TopKProcessor(2))
+    val ans1 = tester.test("key1", List(("b", 2.0), ("c", 3.0), ("a", 1.0)))
+    assert(ans1 == List(("key1", 3.0), ("key1", 2.0)))
+    val ans2 = tester.test("key2", List(("a", 10.0), ("b", 20.0), ("c", 30.0), 
("d", 40.0)))
+    assert(ans2 == List(("key2", 40.0), ("key2", 30.0)))
+    val ans3 = tester.test("key3", List(("a", 100.0)))
+    assert(ans3 == List(("key3", 100.0)))
+
+    assert(tester.peekListState[Double]("topK", "key1") == List(3.0, 2.0))
+    assert(tester.peekListState[Double]("topK", "key2") == List(40.0, 30.0))
+    assert(tester.peekListState[Double]("topK", "key3") == List(100.0))
+    assert(tester.peekListState[Double]("topK", "key4").isEmpty)
+
+    val ans4 = tester.test("key1", List(("a", 10.0)))
+    assert(ans4 == List(("key1", 10.0), ("key1", 3.0)))
+    assert(tester.peekListState[Double]("topK", "key1") == List(10.0, 3.0))
+  }
+
+  test("TwsTester should allow direct access to ListState") {
+    val tester = new TwsTester(new TopKProcessor(2))
+    tester.setListState("topK", "a", List(6.0, 5.0))
+    tester.setListState("topK", "b", List(8.0, 7.0))
+    tester.test("a", List(("", 10.0)))
+    tester.test("b", List(("", 7.5)))
+    tester.test("c", List(("", 1.0)))
+
+    assert(tester.peekListState[Double]("topK", "a") == List(10.0, 6.0))
+    assert(tester.peekListState[Double]("topK", "b") == List(8.0, 7.5))
+    assert(tester.peekListState[Double]("topK", "c") == List(1.0))
+    assert(tester.peekListState[Double]("topK", "d") == List())
+  }
+
+  test("TwsTester should correctly test WordFrequencyProcessor") {
+    val tester = new TwsTester(new WordFrequencyProcessor())
+    val ans1 =
+      tester.test("user1", List(("", "hello"), ("", "world"), ("", "hello"), 
("", "world")))
+    assert(
+      ans1.sorted == List(
+        ("user1", "hello", 1L),
+        ("user1", "hello", 2L),
+        ("user1", "world", 1L),
+        ("user1", "world", 2L)
+      ).sorted
+    )
+
+    val ans2 = tester.test("user2", List(("", "hello"), ("", "spark")))
+    assert(ans2.sorted == List(("user2", "hello", 1L), ("user2", "spark", 
1L)).sorted)
+
+    // Check state using peekMapState
+    assert(
+      tester.peekMapState[String, Long]("frequencies", "user1") == Map("hello" 
-> 2L, "world" -> 2L)
+    )
+    assert(
+      tester.peekMapState[String, Long]("frequencies", "user2") == Map("hello" 
-> 1L, "spark" -> 1L)
+    )
+    assert(tester.peekMapState[String, Long]("frequencies", "user3") == Map())
+    assert(tester.peekMapState[String, Long]("frequencies", "user3").isEmpty)
+
+    // Process more data for user1
+    val ans3 = tester.test("user1", List(("", "hello"), ("", "test")))
+    assert(ans3.sorted == List(("user1", "hello", 3L), ("user1", "test", 
1L)).sorted)
+    assert(
+      tester.peekMapState[String, Long]("frequencies", "user1") == Map(
+        "hello" -> 3L,
+        "world" -> 2L,
+        "test" -> 1L
+      )
+    )
+  }
+
+  test("TwsTester should allow direct access to MapState") {
+    val tester = new TwsTester(new WordFrequencyProcessor())
+
+    // Set initial state directly
+    tester.setMapState("frequencies", "user1", Map("hello" -> 5L, "world" -> 
3L))
+    tester.setMapState("frequencies", "user2", Map("spark" -> 10L))
+
+    // Process new words
+    tester.test("user1", List(("", "hello")))
+    tester.test("user1", List(("", "goodbye")))
+    tester.test("user2", List(("", "spark")))
+    tester.test("user3", List(("", "new")))
+
+    // Verify updated state
+    assert(
+      tester.peekMapState[String, Long]("frequencies", "user1") == Map(
+        "hello" -> 6L,
+        "world" -> 3L,
+        "goodbye" -> 1L
+      )
+    )
+    assert(tester.peekMapState[String, Long]("frequencies", "user2") == 
Map("spark" -> 11L))
+    assert(tester.peekMapState[String, Long]("frequencies", "user3") == 
Map("new" -> 1L))
+    assert(tester.peekMapState[String, Long]("frequencies", "user4") == Map())
+  }
+
+  test("TwsTester can be used to test step function") {
+    val processor = new RunningCountProcessor[String]()
+    val tester = new TwsTester(processor)
+
+    // Example of helper function using TwsTester to inspect how processing a 
single row changes
+    // state.
+    def testStepFunction(key: String, inputRow: String, stateIn: Long): Long = 
{
+      tester.setValueState[Long]("count", key, stateIn)
+      tester.test(key, List(inputRow))
+      tester.peekValueState("count", key).get
+    }
+
+    assert(testStepFunction("key1", "a", 10L) == 11L)
+  }
+
+  test("TwsTester should call handleInitialState") {
+    val processor = new RunningCountProcessor[String]()
+    val tester = new TwsTester(processor, initialState = List(("a", 10L), 
("b", 20L)))
+    assert(tester.peekValueState[Long]("count", "a").get == 10L)
+    assert(tester.peekValueState[Long]("count", "b").get == 20L)
+
+    val ans1 = tester.test("a", List("a"))
+    val ans2 = tester.test("c", List("c"))
+    assert(ans1 == List(("a", 11L)))
+    assert(ans2 == List(("c", 1L)))
+  }
+
+  test("TwsTester should fail when initialState is passed but not supported") {
+    val processor = new TopKProcessor(5)
+    val exception = intercept[IllegalArgumentException] {
+      new TwsTester(processor, initialState = List(("a", List(1.0, 2.0))))
+    }
+    assert(exception.getMessage.contains("stateful processor doesn't support 
initial state"))
+  }
+
+  test("TwsTester should test RunningCountProcessor row-by-row") {
+    val tester = new TwsTester(new RunningCountProcessor[String]())
+
+    // Example of helper function to test how TransformWithState processes 
rows one-by-one, which
+    // can be used to simulate real-time mode.
+    def testRowByRow(input: List[(String, String)]): List[(String, Long)] = {
+      input.flatMap { case (key, value) => tester.test(key, List(value)) }
+    }
+
+    val input: List[(String, String)] = List(
+      ("key1", "a"),
+      ("key2", "b"),
+      ("key1", "c"),
+      ("key2", "b"),
+      ("key1", "c"),
+      ("key1", "c"),
+      ("key3", "q")
+    )
+    val ans: List[(String, Long)] = testRowByRow(input)
+    assert(
+      ans == List(
+        ("key1", 1L),
+        ("key2", 1L),
+        ("key1", 2L),
+        ("key2", 2L),
+        ("key1", 3L),
+        ("key1", 4L),
+        ("key3", 1L)
+      )
+    )
+  }
+
+  test("TwsTester should exercise all state methods") {
+    val tester = new TwsTester(new AllMethodsTestProcessor())
+    val results = tester.test(
+      "k",
+      List(
+        "value-exists", // false
+        "value-set", // set to 42
+        "value-exists", // true
+        "value-clear", // clear
+        "value-exists", // false again
+        "list-exists", // false
+        "list-append", // append a, b
+        "list-exists", // true
+        "list-append-array", // append c, d
+        "list-get", // a,b,c,d

Review Comment:
   Added a test.



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to