Skip to content

Commit

Permalink
Adapt more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yevhenii-nadtochii committed Dec 27, 2024
1 parent 1b3a5d9 commit a4eacf0
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 147 deletions.
38 changes: 0 additions & 38 deletions java-tests/runtime/src/test/kotlin/io/spine/validate/Assertions.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,15 @@ internal class EnclosedMessageValidationSpec : ValidationOfConstraintTest() {

val violation = singleViolation()
violation.message.formatUnsafe() shouldContain "is invalid"
assertFieldPathIs(
violation,
ENCLOSED_FIELD_NAME
)
violation.fieldName shouldBe ENCLOSED_FIELD_NAME

val innerViolations = violation.violationList
innerViolations shouldHaveSize 1
val innerViolation = innerViolations[0]
innerViolation.message.format() shouldStartWith Diags.Regex.prefix

assertFieldPathIs(
innerViolation,
EMAIL
)

innerViolation.violationList.shouldBeEmpty()
with(innerViolations.first()) {
message.format() shouldStartWith Diags.Regex.prefix
fieldName shouldBe EMAIL
violationList.shouldBeEmpty()
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,72 +44,10 @@ import org.junit.jupiter.api.assertDoesNotThrow
@DisplayName(VALIDATION_SHOULD + "validate an entity ID")
internal class EntityIdSpec : ValidationOfConstraintTest() {

@Nested inner class
`in a command file and` {

@Test
fun `find out that non-default message is valid`() {
val state = assertDoesNotThrow {
entityIdMsgFieldValue {
value = newStringValue()
}
}
assertValid(state)
}

@Test
fun `find out that default message is NOT valid`() {
val msg = EntityIdMsgFieldValue.getDefaultInstance()
assertNotValid(msg)
}

@Test
fun `find out that empty string is NOT valid`() {
val msg = EntityIdStringFieldValue.getDefaultInstance()
assertNotValid(msg)
}

@Test
fun `find out that non-empty string is valid`() {
val state = assertDoesNotThrow {
entityIdStringFieldValue {
value = Identifier.newUuid()
}
}
assertValid(state)
}

@Test
fun `provide a violation if is not valid`() {
val msg = EntityIdMsgFieldValue.getDefaultInstance()
assertSingleViolation(msg, MessageValidatorTestEnv.VALUE)
}
}

@Nested inner class
`in a state and` {

@Test
fun `consider it required by default`() {
val stateWithDefaultId = AggregateState.getDefaultInstance()
assertNotValid(stateWithDefaultId)
}

@Test
fun `match only the first field named 'id' or ending with '_id'`() {
val onlyEntityIdSet = assertDoesNotThrow {
// Only ID set.
aggregateState {
entityId = Identifier.newUuid()
}
}
assertValid(onlyEntityIdSet)
}

@Test
fun `not consider it '(required)' if the option is set explicitly to false`() {
val stateWithDefaultId = ProjectionState.getDefaultInstance()
assertValid(stateWithDefaultId)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ import com.google.protobuf.Message
import io.kotest.matchers.collections.shouldBeEmpty
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.string.shouldNotBeEmpty
import io.kotest.matchers.string.shouldNotContain
import io.spine.validate.Validate.violationsOf
import org.junit.jupiter.api.Assertions.assertDoesNotThrow
import org.junit.jupiter.api.assertThrows
Expand Down Expand Up @@ -96,17 +94,17 @@ abstract class ValidationOfConstraintTest {
assertIsValid(false)
}

protected fun assertNotValid(msg: Message, checkFieldPath: Boolean) {
protected fun assertNotValid(msg: Message, checkFieldName: Boolean) {
validate(msg)
assertIsValid(false, checkFieldPath)
assertIsValid(false, checkFieldName)
}

@JvmOverloads
protected fun assertIsValid(isValid: Boolean, checkFieldPath: Boolean = true) {
protected fun assertIsValid(isValid: Boolean, checkFieldName: Boolean = true) {
if (isValid) {
assertThat(violations).isEmpty()
} else {
assertViolations(violations, checkFieldPath)
assertViolations(violations, checkFieldName)
}
}

Expand All @@ -125,42 +123,40 @@ abstract class ValidationOfConstraintTest {
val violation = firstViolation()
val actualErrorMessage = violation.message.format()
actualErrorMessage shouldBe expectedErrMsg
assertFieldPathIs(violation, invalidFieldName)
violation.fieldName shouldBe invalidFieldName
violation.violationList.shouldBeEmpty()
}

protected fun assertSingleViolation(message: Message, invalidFieldName: String) {
assertNotValid(message)
assertThat(violations).hasSize(1)
assertFieldPathIs(firstViolation(), invalidFieldName)
firstViolation().fieldName shouldBe invalidFieldName
}

companion object {

const val VALIDATION_SHOULD: String = "Validation should "

private fun assertViolations(
violations: List<ConstraintViolation?>?,
checkFieldPath: Boolean
violations: List<ConstraintViolation>?,
checkFieldName: Boolean
) {
assertThat(violations)
.isNotEmpty()
for (violation in violations!!) {
assertHasCorrectFormat(violation)
if (checkFieldPath) {
assertHasFieldPath(violation)
if (checkFieldName) {
if (violation.fieldName.isEmpty()) {
println(violation)
}
violation.fieldName.shouldNotBeEmpty()
}
}
}

private fun assertHasCorrectFormat(violation: ConstraintViolation?) {
val template = violation!!.message.withPlaceholders
private fun assertHasCorrectFormat(violation: ConstraintViolation) {
val template = violation.message.withPlaceholders
template.shouldNotBeEmpty()
}

private fun assertHasFieldPath(violation: ConstraintViolation?) {
assertThat(violation!!.fieldPath.fieldNameList)
.isNotEmpty()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ package io.spine.test.options
import com.google.common.truth.extensions.proto.ProtoTruth.assertThat
import io.kotest.matchers.optional.shouldBeEmpty
import io.kotest.matchers.optional.shouldBePresent
import io.spine.base.fieldPath
import io.spine.protobuf.TypeConverter.toAny
import io.spine.test.tools.validate.ProtoSet
import io.spine.test.tools.validate.protoSet
Expand All @@ -54,7 +53,7 @@ internal class DistinctITest {

val violations = error.get().constraintViolationList
val expected = constraintViolation {
fieldPath = fieldPath { fieldName.add("element") }
fieldName = "element"
}

assertThat(violations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ internal class ExternalConstraintITest {
val violations = error.get().constraintViolationList

violations.size shouldBe 1
violations[0].fieldPath.fieldNameList[0] shouldBe "shipping_address"
violations[0].fieldName shouldBe "shipping_address"
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ internal class ValidateConstraintTest {
// See https://github.com/SpineEventEngine/validation/issues/148
violations.size shouldBe 1

val receiverViolation = violations[0]
receiverViolation!!.fieldPath.getFieldName(0) shouldBe "address"
val receiverViolation = violations.first()
receiverViolation.fieldName shouldBe "address"

val nestedViolations = receiverViolation.violationList
nestedViolations.size shouldBe 2
nestedViolations[0].fieldPath.getFieldName(0) shouldBe "first_line"
nestedViolations[1].fieldPath.getFieldName(0) shouldBe "town"
nestedViolations[0].fieldName shouldBe "first_line"
nestedViolations[1].fieldName shouldBe "town"
}

@Test
Expand Down Expand Up @@ -108,7 +108,7 @@ internal class ValidateConstraintTest {

for (violation in violations) {
violation.violationList.size shouldBe 1
violation.fieldPath.getFieldName(0) shouldBe "contact"
violation.fieldName shouldBe "contact"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fun assertInvalid(builder: Message.Builder): List<ConstraintViolation> {

private val fieldName: Correspondence<ConstraintViolation, String> =
Correspondence.transforming(
{ it.fieldPath.getFieldName(0) },
{ it.fieldName },
"field name"
)

Expand All @@ -97,7 +97,7 @@ fun assertViolation(
* Obtains a violation for the field with the given name.
*/
fun List<ConstraintViolation>.atField(fieldName: String): ConstraintViolation {
return find { it.fieldPath.fieldNameList[0] == fieldName }
return find { it.fieldName == fieldName }
?: fail("Cannot find a violation for the field `$fieldName`. Violations: `$this`.")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import com.google.common.truth.Truth8.assertThat
import com.google.common.truth.extensions.proto.ProtoTruth.assertThat
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.optional.shouldBePresent
import io.spine.base.FieldPath
import io.spine.base.Identifier
import io.spine.base.Time.currentTime
import io.spine.type.TypeName
Expand Down Expand Up @@ -65,10 +64,7 @@ internal class GoesConstraintSpec {
.isEqualTo(
ConstraintViolation.newBuilder()
.setTypeName(TypeName.of(paper).toUrl().value())
.setFieldPath(
FieldPath.newBuilder()
.addFieldName("when_archived")
)
.setFieldName("when_archived")
.build()
)
}
Expand Down

0 comments on commit a4eacf0

Please sign in to comment.