Skip to content

Commit

Permalink
(tests): Restructure HighlightsSpec (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
mihajlo-ra92 authored Jun 24, 2023
1 parent beb1fca commit da9479e
Showing 1 changed file with 111 additions and 104 deletions.
215 changes: 111 additions & 104 deletions modules/library/src/test/scala/zio/elasticsearch/HighlightsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,102 +12,142 @@ import zio.test.{Spec, TestEnvironment, ZIOSpecDefault, assert}
object HighlightsSpec extends ZIOSpecDefault {
def spec: Spec[TestEnvironment, Any] =
suite("Highlight")(
suite("creating Highlight")(
test("successfully create Highlight with only field given without config") {
assert(highlight("day_of_week"))(
suite("constructing")(
test("highlight") {
val highlightObject =
highlight(field = "day_of_week")
val highlightWithGlobalConfig =
highlight(field = "day_of_week").withGlobalConfig(field = "type", Str("plain"))
val highlightWithHighlight =
highlight(field = "day_of_week").withHighlight(field = "first_name")
val highlightWithConfig =
highlight(field = "day_of_week", config = Map("type" -> Str("plain")))
val highlightWithConfigAndGlobalConfig =
highlight(field = "day_of_week", config = Map("type" -> Str("plain")))
.withGlobalConfig(field = "pre_tags", Arr(Str("<tag1>")))
val highlightOfStringField =
highlight(field = TestSubDocument.stringField)
val highlightOfStringFieldWithHighlight =
highlight(field = TestSubDocument.stringField).withHighlight(TestSubDocument.intField)
val highlightOfStringFieldWithGlobalConfig =
highlight(field = TestSubDocument.stringField).withGlobalConfig("type", value = Str("plain"))
val highlightOfNestedFieldWithGlobalConfig =
highlight(field = TestSubDocument.nestedField / TestNestedField.stringField)
.withGlobalConfig("type", value = Str("plain"))

assert(highlightObject)(
equalTo(
Highlights(
fields = Chunk(HighlightField("day_of_week"))
)
)
)
},
test("successfully create Highlight with two fields given without config") {
assert(highlight("day_of_week").withHighlight("first_name"))(
) &&
assert(highlightWithHighlight)(
equalTo(
Highlights(
fields = Chunk(HighlightField("first_name"), HighlightField("day_of_week"))
)
)
)
},
test("successfully create Highlight with only one field given with global config") {
assert(highlight("day_of_week").withGlobalConfig("type", Str("plain")))(
) &&
assert(highlightWithGlobalConfig)(
equalTo(
Highlights(
fields = Chunk(HighlightField("day_of_week")),
config = Map("type" -> Str("plain"))
)
)
)
},
test("successfully create Highlight with only one field given with field config") {
assert(highlight("day_of_week", Map("type" -> Str("plain"))))(
) &&
assert(highlightWithConfig)(
equalTo(
Highlights(
fields = Chunk(HighlightField("day_of_week", Map("type" -> Str("plain"))))
)
)
)
},
test("successfully create Highlight with only field given with both global and field config") {
) &&
assert(
highlight("day_of_week", Map("type" -> Str("plain"))).withGlobalConfig("pre_tags", Arr(Str("<tag1>")))
highlightWithConfigAndGlobalConfig
)(
equalTo(
Highlights(
fields = Chunk(HighlightField("day_of_week", Map("type" -> Str("plain")))),
config = Map("pre_tags" -> Arr(Str("<tag1>")))
)
)
)
}
),
suite("creating Highlight using accessors from Schema")(
test("successfully create Highlight with only field given without config") {
assert(highlight(TestSubDocument.stringField))(
) &&
assert(highlightOfStringField)(
equalTo(
Highlights(
fields = Chunk(HighlightField("stringField"))
)
)
)
},
test("successfully create Highlight with two fields given without config") {
assert(highlight(TestSubDocument.stringField).withHighlight(TestSubDocument.intField))(
) &&
assert(highlightOfStringFieldWithHighlight)(
equalTo(
Highlights(
fields = Chunk(HighlightField("intField"), HighlightField("stringField"))
)
)
)
},
test("successfully create Highlight with nested field ") {
) &&
assert(
highlight(TestSubDocument.nestedField / TestNestedField.stringField).withGlobalConfig("type", Str("plain"))
highlightOfNestedFieldWithGlobalConfig
)(
equalTo(
Highlights(
fields = Chunk(HighlightField("nestedField.stringField")),
config = Map("type" -> Str("plain"))
)
)
)
},
test("successfully create Highlight with one field given with global config") {
assert(highlight(TestSubDocument.stringField).withGlobalConfig("type", Str("plain")))(
) &&
assert(highlightOfStringFieldWithGlobalConfig)(
equalTo(
Highlights(
fields = Chunk(HighlightField("stringField")),
config = Map("type" -> Str("plain"))
)
)
)

}
),
suite("encoding Highlight as JSON")(
test("properly encode Highlight with only field given without config") {
val highlightObject = highlight("day_of_week")
suite("encoding as JSON")(
test("highlight") {
val highlightObject =
highlight(field = "day_of_week")
val highlightWithHighlight =
highlight(field = "day_of_week").withHighlight(field = "first_name")
val highlightWithHighlightAndGlobalConfig =
highlight(field = "day_of_week")
.withHighlight(field = "first_name")
.withGlobalConfig(field = "type", Str("plain"))
val highlightWithConfig =
highlight("day_of_week", config = Map("require_field_match" -> Bool(false)))
.withGlobalConfig("type", Str("plain"))
val highlightWithConfigAndHighlight =
highlight("day_of_week", config = Map("require_field_match" -> Bool(false)))
.withGlobalConfig("type", Str("plain"))
.withHighlight(
"first_name",
Map("matched_fields" -> Arr(Str("comment"), Str("comment.plain")), "type" -> Str("fvh"))
)
val highlightWithConfigHighlightAndExplicitFieldOrder =
highlight("day_of_week", config = Map("require_field_match" -> Bool(false)))
.withGlobalConfig("type", Str("plain"))
.withHighlight(
"first_name",
Map("matched_fields" -> Arr(Str("comment"), Str("comment.plain")), "type" -> Str("fvh"))
)
.withExplicitFieldOrder
val highlightWithMultipleConfig =
highlight("day_of_week", Map("require_field_match" -> Bool(false)))
.withGlobalConfig("type", Str("plain"))
.withGlobalConfig("type", Str("fvh"))
.withGlobalConfig("fragment_size", Num(150))
.withHighlight(
field = "first_name",
config = Map("matched_fields" -> Arr(Str("comment"), Str("comment.plain")), "type" -> Str("fvh"))
)
.withHighlight("last_name")

val expected =
"""
|{
Expand All @@ -116,12 +156,7 @@ object HighlightsSpec extends ZIOSpecDefault {
| }
|}
|""".stripMargin

assert(highlightObject.toJson)(equalTo(expected.toJson))
},
test("properly encode Highlight with two fields given without config") {
val highlightObject = highlight("day_of_week").withHighlight("first_name")
val expected =
val expectedWithFirstName =
"""
|{
| "fields" : {
Expand All @@ -130,13 +165,7 @@ object HighlightsSpec extends ZIOSpecDefault {
| }
|}
|""".stripMargin

assert(highlightObject.toJson)(equalTo(expected.toJson))
},
test("properly encode Highlight with two fields given with global config") {
val highlightObject =
highlight("day_of_week").withHighlight("first_name").withGlobalConfig("type", Str("plain"))
val expected =
val expectedPlainWithFirstName =
"""
|{
| "type" : "plain",
Expand All @@ -146,14 +175,7 @@ object HighlightsSpec extends ZIOSpecDefault {
| }
|}
|""".stripMargin

assert(highlightObject.toJson)(equalTo(expected.toJson))
},
test("properly encode Highlight with one field given with both global and field config") {
val highlightObject =
highlight("day_of_week", config = Map("require_field_match" -> Bool(false)))
.withGlobalConfig("type", Str("plain"))
val expected =
val expectedPlainWithRequiredFieldMatch =
"""
|{
| "type" : "plain",
Expand All @@ -164,18 +186,7 @@ object HighlightsSpec extends ZIOSpecDefault {
| }
|}
|""".stripMargin

assert(highlightObject.toJson)(equalTo(expected.toJson))
},
test("properly encode Highlight with two fields given with both having global and field config") {
val highlightObject =
highlight("day_of_week", Map("require_field_match" -> Bool(false)))
.withGlobalConfig("type", Str("plain"))
.withHighlight(
"first_name",
Map("matched_fields" -> Arr(Str("comment"), Str("comment.plain")), "type" -> Str("fvh"))
)
val expected =
val expectedPlainWithMatchedFields =
"""
|{
| "type" : "plain",
Expand All @@ -190,21 +201,7 @@ object HighlightsSpec extends ZIOSpecDefault {
| }
|}
|""".stripMargin

assert(highlightObject.toJson)(equalTo(expected.toJson))
},
test(
"properly encode Highlight with two fields given with both having global and field config and explicit order"
) {
val highlightObject =
highlight("day_of_week", Map("require_field_match" -> Bool(false)))
.withGlobalConfig("type", Str("plain"))
.withHighlight(
"first_name",
Map("matched_fields" -> Arr(Str("comment"), Str("comment.plain")), "type" -> Str("fvh"))
)
.withExplicitFieldOrder
val expected =
val expectedPlainWithArrayOfFields =
"""
|{
| "type" : "plain",
Expand All @@ -221,21 +218,7 @@ object HighlightsSpec extends ZIOSpecDefault {
| ]
|}
|""".stripMargin

assert(highlightObject.toJson)(equalTo(expected.toJson))
},
test("properly encode Highlight with three fields and multiple configurations") {
val highlightObject =
highlight("day_of_week", Map("require_field_match" -> Bool(false)))
.withGlobalConfig("type", Str("plain"))
.withGlobalConfig("type", Str("fvh"))
.withGlobalConfig("fragment_size", Num(150))
.withHighlight(
field = "first_name",
config = Map("matched_fields" -> Arr(Str("comment"), Str("comment.plain")), "type" -> Str("fvh"))
)
.withHighlight("last_name")
val expected =
val expectedFvhType =
"""
|{
| "type" : "fvh",
Expand All @@ -253,7 +236,31 @@ object HighlightsSpec extends ZIOSpecDefault {
|}
|""".stripMargin

assert(highlightObject.toJson)(equalTo(expected.toJson))
assert(highlightObject.toJson)(
equalTo(
expected.toJson
)
) &&
assert(highlightWithHighlight.toJson)(
equalTo(
expectedWithFirstName.toJson
)
) &&
assert(highlightWithHighlightAndGlobalConfig.toJson)(
equalTo(
expectedPlainWithFirstName.toJson
)
) &&
assert(highlightWithConfig.toJson)(
equalTo(expectedPlainWithRequiredFieldMatch.toJson)
) &&
assert(highlightWithConfigAndHighlight.toJson)(equalTo(expectedPlainWithMatchedFields.toJson)) &&
assert(highlightWithConfigHighlightAndExplicitFieldOrder.toJson)(
equalTo(expectedPlainWithArrayOfFields.toJson)
) &&
assert(highlightWithMultipleConfig.toJson)(
equalTo(expectedFvhType.toJson)
)
}
)
)
Expand Down

0 comments on commit da9479e

Please sign in to comment.