Skip to content

Commit

Permalink
Refactor UT
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Dai <[email protected]>
  • Loading branch information
dai-chen committed Sep 3, 2024
1 parent c54546a commit 1aa3a31
Showing 1 changed file with 71 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package org.opensearch.flint.spark

import org.mockito.Mockito.{never, reset, times, verify, when, RETURNS_DEEP_STUBS}
import org.mockito.Mockito._
import org.opensearch.flint.common.metadata.log.FlintMetadataLogService
import org.opensearch.flint.core.FlintClient
import org.scalatest.matchers.should.Matchers
Expand All @@ -28,47 +28,42 @@ class FlintSparkTransactionSupportSuite extends FlintSuite with Matchers {
mockFlintMetadataLogService
}

override protected def beforeEach(): Unit = {
override protected def afterEach(): Unit = {
reset(mockFlintClient, mockFlintMetadataLogService)
}

test("execute transaction without force initialization when index exists") {
when(mockFlintClient.exists(testIndex)).thenReturn(true)
val result =
transactionSupport
.withTransaction[Boolean](testIndex, testOpName) { _ => true }

result shouldBe Some(true)
verify(mockFlintMetadataLogService, times(1)).startTransaction(testIndex, false)
assertIndexOperation()
.withForceInit(false)
.withResult("test")
.whenIndexDataExists()
.expectResult("test")
.verifyTransaction(forceInit = false)
}

test("execute transaction with force initialization when index exists") {
when(mockFlintClient.exists(testIndex)).thenReturn(true)
val result =
transactionSupport
.withTransaction[Boolean](testIndex, testOpName, forceInit = true) { _ => true }

result shouldBe Some(true)
verify(mockFlintMetadataLogService, times(1)).startTransaction(testIndex, true)
assertIndexOperation()
.withForceInit(true)
.withResult("test")
.whenIndexDataExists()
.expectResult("test")
.verifyTransaction(forceInit = true)
}

test("bypass transaction without force initialization when index does not exist") {
when(mockFlintClient.exists(testIndex)).thenReturn(false)
val result =
transactionSupport
.withTransaction[Boolean](testIndex, testOpName) { _ => true }

result shouldBe None
assertIndexOperation()
.withForceInit(false)
.withResult("test")
.whenIndexDataNotExist()
.expectNoResult()
}

test("execute transaction with force initialization even if index does not exist") {
when(mockFlintClient.exists(testIndex)).thenReturn(false)
val result =
transactionSupport
.withTransaction[Boolean](testIndex, testOpName, forceInit = true) { _ => true }

result shouldBe Some(true)
verify(mockFlintMetadataLogService, times(1)).startTransaction(testIndex, true)
assertIndexOperation()
.withForceInit(true)
.withResult("test")
.whenIndexDataNotExist()
.expectResult("test")
}

test("propagate original exception thrown within transaction") {
Expand All @@ -82,4 +77,51 @@ class FlintSparkTransactionSupportSuite extends FlintSuite with Matchers {
}
} should have message "Fake cause"
}

private def assertIndexOperation(): FlintIndexAssertion = new FlintIndexAssertion

class FlintIndexAssertion {
private var forceInit: Boolean = false
private var expectedResult: Option[String] = None

def withForceInit(forceInit: Boolean): FlintIndexAssertion = {
this.forceInit = forceInit
this
}

def withResult(expectedResult: String): FlintIndexAssertion = {
this.expectedResult = Some(expectedResult)
this
}

def whenIndexDataExists(): FlintIndexAssertion = {
when(mockFlintClient.exists(testIndex)).thenReturn(true)
this
}

def whenIndexDataNotExist(): FlintIndexAssertion = {
when(mockFlintClient.exists(testIndex)).thenReturn(false)
this
}

def verifyTransaction(forceInit: Boolean): FlintIndexAssertion = {
verify(mockFlintMetadataLogService, times(1)).startTransaction(testIndex, forceInit)
this
}

def expectResult(expectedResult: String): FlintIndexAssertion = {
val result = transactionSupport.withTransaction[String](testIndex, testOpName, forceInit) {
_ => expectedResult
}
result shouldBe Some(expectedResult)
this
}

def expectNoResult(): Unit = {
val result = transactionSupport.withTransaction[String](testIndex, testOpName, forceInit) {
_ => expectedResult.getOrElse("")
}
result shouldBe None
}
}
}

0 comments on commit 1aa3a31

Please sign in to comment.