Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(test): Support unit tests for IndexName validation #22

Merged
merged 5 commits into from
Dec 13, 2022
Merged
Changes from 1 commit
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
@@ -1,5 +1,6 @@
package zio.elasticsearch

import zio.prelude.Newtype.unsafeWrap
import zio.prelude.Validation
import zio.test.Assertion.equalTo
import zio.test._
Expand All @@ -9,33 +10,42 @@ object IndexNameSpec extends ZIOSpecDefault {
override def spec: Spec[TestEnvironment, Any] =
suite("IndexName validation")(
test("succeed for valid string") {
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved
assert(IndexName.make("index-name"))(equalTo(Validation.succeed(IndexName("index-name"))))
check(genString(5, 20)) { name =>
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved
assert(IndexName.make(name))(equalTo(Validation.succeed(unsafeWrap(IndexName)(name))))
}
},
test("fail for string containing upper letter") {
val name = "Index-name"
assert(IndexName.make(name))(equalTo(Validation.fail(indexNameFailureMessage(name))))
check(genString(5, 20)) { name =>
val invalidName = s"A$name"
assert(IndexName.make(invalidName))(equalTo(Validation.fail(indexNameFailureMessage(invalidName))))
}
},
test("fail for string containing charachter '*'") {
val name = "index*name"
assert(IndexName.make(name))(equalTo(Validation.fail(indexNameFailureMessage(name))))
check(genString(5, 20)) { name =>
val invalidName = s"*$name"
assert(IndexName.make(invalidName))(equalTo(Validation.fail(indexNameFailureMessage(invalidName))))
}
},
test("fail for string containing charachter ':'") {
val name = "index:name"
assert(IndexName.make(name))(equalTo(Validation.fail(indexNameFailureMessage(name))))
check(genString(5, 20)) { name =>
val invalidName = s":$name"
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved
assert(IndexName.make(invalidName))(equalTo(Validation.fail(indexNameFailureMessage(invalidName))))
}
},
test("fail for string starting with charachter '-'") {
val name = "-index.name"
assert(IndexName.make(name))(equalTo(Validation.fail(indexNameFailureMessage(name))))
check(genString(5, 20)) { name =>
val invalidName = s"-$name"
assert(IndexName.make(invalidName))(equalTo(Validation.fail(indexNameFailureMessage(invalidName))))
}
},
test("fail for string '.'") {
val name = "."
assert(IndexName.make(name))(equalTo(Validation.fail(indexNameFailureMessage(name))))
},
test("fail for string longer than 255 bytes") {
check(Gen.stringN(256)(Gen.alphaChar)) { name =>
val lowerCaseStr = name.toLowerCase()
assert(IndexName.make(lowerCaseStr.toLowerCase))(
equalTo(Validation.fail(indexNameFailureMessage(lowerCaseStr.toLowerCase)))
check(Gen.stringBounded(256, 300)(Gen.alphaChar).map(_.toLowerCase())) { name =>
assert(IndexName.make(name))(
equalTo(Validation.fail(indexNameFailureMessage(name)))
)
}
}
Expand All @@ -51,4 +61,7 @@ object IndexNameSpec extends ZIOSpecDefault {
| - Cannot be longer than 255 bytes (note it is bytes, so multi-byte characters will count towards the 255 limit faster).
| - Names starting with . are deprecated, except for hidden indices and internal indices managed by plugins.
|""".stripMargin

private def genString(min: Int, max: Int): Gen[Any, String] =
Gen.stringBounded(min, max)(Gen.alphaChar).map(_.toLowerCase())
}