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
Show file tree
Hide file tree
Changes from 2 commits
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
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ lazy val library =
"dev.zio" %% "zio-schema" % "0.3.1",
"dev.zio" %% "zio-schema-json" % "0.3.1",
"org.apache.commons" % "commons-lang3" % "3.12.0",
"dev.zio" %% "zio-test" % "2.0.4" % IntegrationTest,
"dev.zio" %% "zio-test-sbt" % "2.0.4" % IntegrationTest
"dev.zio" %% "zio-test" % "2.0.4" % "it,test",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess IntegrationTest extends Test? Can we use Test here only?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I think that IntegrationTest does not extend Test.

"dev.zio" %% "zio-test-sbt" % "2.0.4" % "it,test"
),
testFrameworks := Seq(new TestFramework("zio.test.sbt.ZTestFramework"))
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved
)
Expand Down
18 changes: 9 additions & 9 deletions modules/library/src/main/scala/zio/elasticsearch/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ package object elasticsearch {
)
Left(
failure(
"""Index names must meet the following criteria:
| - Must be lower case only
| - Cannot include \\, /, *, ?, ", <, >, |, ` `(space character), `,`(comma), #.
| - Cannot include ":"(since 7.0).
| - Cannot start with -, _, +.
| - Cannot be `.` or `..`.
| - 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
s"""
arnoldlacko marked this conversation as resolved.
Show resolved Hide resolved
| - Must be lower case only
| - Cannot include \\, /, *, ?, ", <, >, |, ` `(space character), `,`(comma), #.
| - Cannot include ":"(since 7.0).
| - Cannot start with -, _, +.
| - Cannot be `.` or `..`.
| - 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
)
)
else
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package zio.elasticsearch

import zio.prelude.Validation
import zio.test.Assertion.equalTo
import zio.test._

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"))))
},
test("fail for string containing upper letter") {
val invalidIndexStr = "Index-name"
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved
assert(IndexName.make(invalidIndexStr))(equalTo(Validation.fail(validationFailedStr(invalidIndexStr))))
},
test("fail for string containing charachter '*'") {
val invalidIndexStr = "index*name"
assert(IndexName.make(invalidIndexStr))(equalTo(Validation.fail(validationFailedStr(invalidIndexStr))))
},
test("fail for string containing charachter ':'") {
val invalidIndexStr = "index:name"
assert(IndexName.make(invalidIndexStr))(equalTo(Validation.fail(validationFailedStr(invalidIndexStr))))
},
test("fail for string starting with charachter '-'") {
val invalidIndexStr = "-index.name"
assert(IndexName.make(invalidIndexStr))(equalTo(Validation.fail(validationFailedStr(invalidIndexStr))))
},
test("fail for string '.'") {
val invalidIndexStr = "."
assert(IndexName.make(invalidIndexStr))(equalTo(Validation.fail(validationFailedStr(invalidIndexStr))))
},
test("fail for string longer than 255 bytes") {
checkN(5)(Gen.stringN(256)(Gen.alphaChar)) { invalidIndexStr =>
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved
val lowerCaseStr = invalidIndexStr.toLowerCase()
assert(IndexName.make(lowerCaseStr.toLowerCase))(
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved
equalTo(Validation.fail(validationFailedStr(lowerCaseStr.toLowerCase)))
)
}
}
)

private def validationFailedStr(indexStr: String): String =
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved
s"""$indexStr did not satisfy
| - Must be lower case only
| - Cannot include \\, /, *, ?, ", <, >, |, ` `(space character), `,`(comma), #.
| - Cannot include ":"(since 7.0).
| - Cannot start with -, _, +.
| - Cannot be `.` or `..`.
| - 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
}