Skip to content

Commit

Permalink
Refactor IndexName tests (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipmilo authored Jun 24, 2023
1 parent e39a715 commit ce5ba61
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ object IndexNameValidation {
params.exists(StringUtils.contains(string, _))

name.toLowerCase == name &&
name.nonEmpty &&
!startsWithAny(name, "+", "-", "_") &&
!containsAny(string = name, params = Chunk("*", "?", "\"", "<", ">", "|", " ", ",", "#", ":")) &&
!equalsAny(name, ".", "..") &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ object IndexNameValidator
def isValid(name: String): Boolean =
name.toLowerCase == name &&
!startsWithAny(name, "+", "-", "_") &&
name.nonEmpty &&
!containsAny(string = name, params = Chunk("*", "?", "\"", "<", ">", "|", " ", ",", "#", ":")) &&
!equalsAny(name, ".", "..") &&
name.getBytes().length <= 255
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,59 @@ import zio.test._

object IndexNameSpec extends ZIOSpecDefault {
def spec: Spec[TestEnvironment, Any] =
suite("IndexName validation")(
test("succeed for valid string") {
check(genString(1, 255)) { name =>
assert(IndexName.make(name))(equalTo(Validation.succeed(unsafeWrap(name)(IndexName))))
}
},
test("fail for string containing upper letter") {
check(genString(0, 127), genString(0, 128)) { (part1, part2) =>
val invalidName = s"${part1}A$part2"
assert(IndexName.make(invalidName))(equalTo(Validation.fail(indexNameFailureMessage(invalidName))))
}
},
test("fail for string containing character '*'") {
check(genString(0, 127), genString(0, 128)) { (part1, part2) =>
val invalidName = s"$part1*$part2"
assert(IndexName.make(invalidName))(equalTo(Validation.fail(indexNameFailureMessage(invalidName))))
}
},
test("fail for string containing character ':'") {
check(genString(0, 127), genString(0, 128)) { (part1, part2) =>
val invalidName = s"$part1:$part2"
assert(IndexName.make(invalidName))(equalTo(Validation.fail(indexNameFailureMessage(invalidName))))
}
},
test("fail for empty string") {
val name = ""
assert(IndexName.make(name))(equalTo(Validation.succeed(unsafeWrap(name)(IndexName))))
},
test("fail for string starting with character '-'") {
check(genString(1, 255)) { name =>
val invalidName = s"-$name"
suite("IndexName")(
suite("constructing")(
test("fail for empty string") {
val name = ""

assert(IndexName.make(name))(equalTo(Validation.fail(indexNameFailureMessage(name))))
},
test("fail for string '.'") {
val invalidName = "."

assert(IndexName.make(invalidName))(equalTo(Validation.fail(indexNameFailureMessage(invalidName))))
},
test("fail for string containing character '*'") {
check(genString(0, 127), genString(0, 128)) { (part1, part2) =>
val invalidName = s"$part1*$part2"

assert(IndexName.make(invalidName))(equalTo(Validation.fail(indexNameFailureMessage(invalidName))))
}
},
test("fail for string containing character ':'") {
check(genString(0, 127), genString(0, 128)) { (part1, part2) =>
val invalidName = s"$part1:$part2"

assert(IndexName.make(invalidName))(equalTo(Validation.fail(indexNameFailureMessage(invalidName))))
}
},
test("fail for string containing upper letter") {
check(genString(0, 127), genString(0, 128)) { (part1, part2) =>
val invalidName = s"${part1}A$part2"

assert(IndexName.make(invalidName))(equalTo(Validation.fail(indexNameFailureMessage(invalidName))))
}
},
test("fail for string longer than 255 bytes") {
check(genString(256, 300)) { invalidName =>
assert(IndexName.make(invalidName))(
equalTo(Validation.fail(indexNameFailureMessage(invalidName)))
)
}
},
test("fail for string starting with character '-'") {
check(genString(1, 255)) { name =>
val invalidName = s"-$name"

assert(IndexName.make(invalidName))(equalTo(Validation.fail(indexNameFailureMessage(invalidName))))
}
},
test("succeed for valid string") {
check(genString(1, 255)) { name =>
assert(IndexName.make(name))(equalTo(Validation.succeed(unsafeWrap(name)(IndexName))))
}
}
},
test("fail for string '.'") {
val name = "."
assert(IndexName.make(name))(equalTo(Validation.fail(indexNameFailureMessage(name))))
},
test("fail for string longer than 255 bytes") {
check(genString(256, 300)) { name =>
assert(IndexName.make(name))(
equalTo(Validation.fail(indexNameFailureMessage(name)))
)
}
}
)
)

private def indexNameFailureMessage(name: String): String =
Expand Down

0 comments on commit ce5ba61

Please sign in to comment.