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 4 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
7 changes: 4 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BuildHelper._
import sbt.librarymanagement.Configurations.IntegrationTest

Global / onChangedBuildSource := ReloadOnSourceChanges

Expand Down Expand Up @@ -40,10 +41,10 @@ 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"))
testFrameworks := List(new TestFramework("zio.test.sbt.ZTestFramework"))
)

lazy val example =
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,67 @@
package zio.elasticsearch

import zio.prelude.Newtype.unsafeWrap
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
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") {
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 '*'") {
check(genString(5, 20)) { name =>
val invalidName = s"*$name"
assert(IndexName.make(invalidName))(equalTo(Validation.fail(indexNameFailureMessage(invalidName))))
}
},
test("fail for string containing charachter ':'") {
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 '-'") {
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.stringBounded(256, 300)(Gen.alphaChar).map(_.toLowerCase())) { name =>
assert(IndexName.make(name))(
equalTo(Validation.fail(indexNameFailureMessage(name)))
)
}
}
)

private def indexNameFailureMessage(name: String): String =
s"""$name 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

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