diff --git a/modules/library/src/test/scala/zio/elasticsearch/AggregationSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/AggregationSpec.scala deleted file mode 100644 index 8b77da88f..000000000 --- a/modules/library/src/test/scala/zio/elasticsearch/AggregationSpec.scala +++ /dev/null @@ -1,407 +0,0 @@ -package zio.elasticsearch - -import zio.Scope -import zio.elasticsearch.ElasticAggregation.{multipleAggregations, termsAggregation} -import zio.elasticsearch.aggregation._ -import zio.elasticsearch.domain.TestSubDocument -import zio.elasticsearch.query.sort.SortOrder.{Asc, Desc} -import zio.elasticsearch.utils._ -import zio.test.Assertion.equalTo -import zio.test._ - -object AggregationSpec extends ZIOSpecDefault { - def spec: Spec[Environment with TestEnvironment with Scope, Any] = - suite("Aggregations")( - suite("creating ElasticAggregation")( - test("successfully create Terms aggregation using `terms` method") { - val aggregation = termsAggregation(name = "aggregation", field = "day_of_week") - - assert(aggregation)( - equalTo( - Terms(name = "aggregation", field = "day_of_week", order = Set.empty, subAggregations = Nil, size = None) - ) - ) - }, - test("successfully create type-safe Terms aggregation using `terms` method") { - val aggregation = termsAggregation(name = "aggregation", field = TestSubDocument.stringField) - - assert(aggregation)( - equalTo( - Terms(name = "aggregation", field = "stringField", order = Set.empty, subAggregations = Nil, size = None) - ) - ) - }, - test("successfully create type-safe Terms aggregation with multi-field using `terms` method") { - val aggregation = - termsAggregation(name = "aggregation", field = TestSubDocument.stringField.keyword) - - assert(aggregation)( - equalTo( - Terms( - name = "aggregation", - field = "stringField.keyword", - order = Set.empty, - subAggregations = Nil, - size = None - ) - ) - ) - }, - test( - "successfully create type-safe Terms aggregation with multi-field using `terms` method and order parameter" - ) { - val aggregation = - termsAggregation( - name = "aggregation", - field = TestSubDocument.stringField.keyword - ).orderByKeyDesc.orderByCountAsc - - assert(aggregation)( - equalTo( - Terms( - name = "aggregation", - field = "stringField.keyword", - order = Set(AggregationOrder("_key", Desc), AggregationOrder("_count", Asc)), - subAggregations = Nil, - size = None - ) - ) - ) - }, - test( - "successfully create type-safe Terms aggregation with multi-field using `terms` method and size parameter" - ) { - val aggregation = - termsAggregation(name = "aggregation", field = TestSubDocument.stringField.keyword) - .size(5) - - assert(aggregation)( - equalTo( - Terms( - name = "aggregation", - field = "stringField.keyword", - order = Set.empty, - subAggregations = Nil, - size = Some(5) - ) - ) - ) - }, - test( - "successfully create type-safe Terms aggregation with multi-field with all params" - ) { - val aggregation = - termsAggregation( - name = "aggregation", - field = TestSubDocument.stringField.keyword - ).orderByCountDesc.orderByKeyAsc - .size(5) - - assert(aggregation)( - equalTo( - Terms( - name = "aggregation", - field = "stringField.keyword", - order = Set(AggregationOrder("_count", Desc), AggregationOrder("_key", Asc)), - subAggregations = Nil, - size = Some(5) - ) - ) - ) - }, - test("successfully create Multiple aggregations using `multipleAggregations` method with two `terms`") { - val aggregation = multipleAggregations.aggregations( - termsAggregation(name = "firstAggregation", field = "day_of_week"), - termsAggregation(name = "secondAggregation", field = "customer_age") - ) - - assert(aggregation)( - equalTo( - Multiple( - List( - Terms( - name = "firstAggregation", - field = "day_of_week", - order = Set.empty, - subAggregations = Nil, - size = None - ), - Terms( - name = "secondAggregation", - field = "customer_age", - order = Set.empty, - subAggregations = Nil, - size = None - ) - ) - ) - ) - ) - }, - test("successfully create Multiple aggregations using `withAgg`") { - val aggregation1 = termsAggregation(name = "firstAggregation", field = "day_of_week") - .withAgg(termsAggregation(name = "secondAggregation", field = "customer_age")) - val aggregation2 = multipleAggregations - .aggregations( - termsAggregation(name = "firstAggregation", field = "day_of_week"), - termsAggregation(name = "secondAggregation", field = "customer_age") - ) - .withAgg(termsAggregation(name = "thirdAggregation", field = "day_of_month")) - - assert(aggregation1)( - equalTo( - Multiple( - List( - Terms( - name = "firstAggregation", - field = "day_of_week", - order = Set.empty, - subAggregations = Nil, - size = None - ), - Terms( - name = "secondAggregation", - field = "customer_age", - order = Set.empty, - subAggregations = Nil, - size = None - ) - ) - ) - ) - ) && assert(aggregation2)( - equalTo( - Multiple( - List( - Terms( - name = "thirdAggregation", - field = "day_of_month", - order = Set.empty, - subAggregations = Nil, - size = None - ), - Terms( - name = "firstAggregation", - field = "day_of_week", - order = Set.empty, - subAggregations = Nil, - size = None - ), - Terms( - name = "secondAggregation", - field = "customer_age", - order = Set.empty, - subAggregations = Nil, - size = None - ) - ) - ) - ) - ) - }, - test("successfully create nested aggregation using `withSubAgg`") { - val aggregation1 = termsAggregation(name = "firstAggregation", field = "day_of_week").withSubAgg( - termsAggregation(name = "secondAggregation", field = "customer_age") - ) - val aggregation2 = multipleAggregations - .aggregations( - termsAggregation(name = "firstAggregation", field = "day_of_week"), - termsAggregation(name = "secondAggregation", field = "customer_age").withSubAgg( - termsAggregation(name = "thirdAggregation", field = "day_of_month") - ) - ) - - assert(aggregation1)( - equalTo( - Terms( - name = "firstAggregation", - field = "day_of_week", - order = Set.empty, - subAggregations = List( - Terms( - name = "secondAggregation", - field = "customer_age", - order = Set.empty, - subAggregations = Nil, - size = None - ) - ), - size = None - ) - ) - ) && assert(aggregation2)( - equalTo( - Multiple( - List( - Terms( - name = "firstAggregation", - field = "day_of_week", - order = Set.empty, - subAggregations = Nil, - size = None - ), - Terms( - name = "secondAggregation", - field = "customer_age", - order = Set.empty, - subAggregations = List( - Terms( - name = "thirdAggregation", - field = "day_of_month", - order = Set.empty, - subAggregations = Nil, - size = None - ) - ), - size = None - ) - ) - ) - ) - ) - } - ), - suite("encoding ElasticAggregation as JSON")( - test("properly encode Terms aggregation") { - val aggregation = termsAggregation(name = "aggregation", field = "day_of_week") - val expected = - """ - |{ - | "aggs": { - | "aggregation": { - | "terms": { - | "field": "day_of_week" - | } - | } - | } - |} - |""".stripMargin - - assert(aggregation.toJson)(equalTo(expected.toJson)) - }, - test("properly encode Terms aggregation with order") { - val aggregation = - termsAggregation(name = "aggregation", field = "day_of_week").orderBy(AggregationOrder("_key", Desc)) - val expected = - """ - |{ - | "aggs": { - | "aggregation": { - | "terms": { - | "field": "day_of_week", - | "order": { - | "_key": "desc" - | } - | } - | } - | } - |} - |""".stripMargin - - assert(aggregation.toJson)(equalTo(expected.toJson)) - }, - test("properly encode Terms aggregation with size") { - val aggregation = termsAggregation(name = "aggregation", field = "day_of_week").size(5) - val expected = - """ - |{ - | "aggs": { - | "aggregation": { - | "terms": { - | "field": "day_of_week", - | "size": 5 - | } - | } - | } - |} - |""".stripMargin - - assert(aggregation.toJson)(equalTo(expected.toJson)) - }, - test("properly encode Multiple aggregations with two Terms aggregations") { - val aggregation = multipleAggregations.aggregations( - termsAggregation(name = "firstAggregation", field = "day_of_week"), - termsAggregation(name = "secondAggregation", field = "customer_age") - ) - val expected = - """ - |{ - | "aggs": { - | "firstAggregation": { - | "terms": { - | "field": "day_of_week" - | } - | }, - | "secondAggregation": { - | "terms": { - | "field": "customer_age" - | } - | } - | } - |} - |""".stripMargin - - assert(aggregation.toJson)(equalTo(expected.toJson)) - }, - test("properly encode nested aggregation") { - val aggregation = termsAggregation(name = "firstAggregation", field = "day_of_week").withSubAgg( - termsAggregation(name = "secondAggregation", field = "customer_age") - ) - val expected = - """ - |{ - | "aggs": { - | "firstAggregation": { - | "terms": { - | "field": "day_of_week" - | }, - | "aggs": { - | "secondAggregation": { - | "terms": { - | "field": "customer_age" - | } - | } - | } - | } - | } - |} - |""".stripMargin - - assert(aggregation.toJson)(equalTo(expected.toJson)) - }, - test("properly encode multiple aggregation with nested aggregation") { - val aggregation = termsAggregation(name = "firstAggregation", field = "day_of_week") - .withSubAgg( - termsAggregation(name = "secondAggregation", field = "customer_age") - ) - .withAgg(termsAggregation(name = "thirdAggregation", field = "day_of_month")) - val expected = - """ - |{ - | "aggs": { - | "firstAggregation": { - | "terms": { - | "field": "day_of_week" - | }, - | "aggs": { - | "secondAggregation": { - | "terms": { - | "field": "customer_age" - | } - | } - | } - | }, - | "thirdAggregation": { - | "terms": { - | "field": "day_of_month" - | } - | } - | } - |} - |""".stripMargin - - assert(aggregation.toJson)(equalTo(expected.toJson)) - } - ) - ) -} diff --git a/modules/library/src/test/scala/zio/elasticsearch/ElasticAggregationSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ElasticAggregationSpec.scala new file mode 100644 index 000000000..b5d338939 --- /dev/null +++ b/modules/library/src/test/scala/zio/elasticsearch/ElasticAggregationSpec.scala @@ -0,0 +1,388 @@ +package zio.elasticsearch + +import zio.elasticsearch.ElasticAggregation.{multipleAggregations, termsAggregation} +import zio.elasticsearch.aggregation._ +import zio.elasticsearch.domain.{TestDocument, TestSubDocument} +import zio.elasticsearch.query.sort.SortOrder +import zio.elasticsearch.query.sort.SortOrder.{Asc, Desc} +import zio.elasticsearch.utils._ +import zio.test.Assertion.equalTo +import zio.test._ + +object ElasticAggregationSpec extends ZIOSpecDefault { + def spec: Spec[TestEnvironment, Any] = + suite("ElasticAggregation")( + suite("constructing")( + test("multiple") { + val aggregation = + multipleAggregations.aggregations( + termsAggregation("first", TestDocument.stringField).orderByKeyDesc, + termsAggregation("second", "testField").size(5) + ) + val aggregationWithSubAggregation = + termsAggregation("first", "testField") + .withSubAgg(termsAggregation("second", TestSubDocument.stringField.raw)) + .withAgg(termsAggregation("third", TestDocument.stringField)) + + assert(aggregation)( + equalTo( + Multiple( + List( + Terms( + name = "first", + field = "stringField", + order = Set(AggregationOrder(value = "_key", order = Desc)), + subAggregations = Nil, + size = None + ), + Terms( + name = "second", + field = "testField", + order = Set.empty, + subAggregations = Nil, + size = Some(5) + ) + ) + ) + ) + ) && + assert(aggregationWithSubAggregation)( + equalTo( + Multiple( + List( + Terms( + name = "first", + field = "testField", + order = Set.empty, + subAggregations = List( + Terms( + name = "second", + field = "stringField.raw", + order = Set.empty, + subAggregations = Nil, + size = None + ) + ), + size = None + ), + Terms( + name = "third", + field = "stringField", + order = Set.empty, + subAggregations = Nil, + size = None + ) + ) + ) + ) + ) + }, + test("subAggregation") { + val aggregation1 = termsAggregation("first", TestDocument.stringField).withSubAgg( + termsAggregation("second", TestSubDocument.stringField.raw) + ) + val aggregation2 = termsAggregation("first", TestDocument.stringField).withAgg( + termsAggregation("second", "testField").withSubAgg(termsAggregation("third", "anotherTestField")) + ) + + assert(aggregation1)( + equalTo( + Terms( + name = "first", + field = "stringField", + order = Set.empty, + subAggregations = List( + Terms( + name = "second", + field = "stringField.raw", + order = Set.empty, + subAggregations = Nil, + size = None + ) + ), + size = None + ) + ) + ) && assert(aggregation2)( + equalTo( + Multiple( + List( + Terms( + name = "first", + field = "stringField", + order = Set.empty, + subAggregations = Nil, + size = None + ), + Terms( + name = "second", + field = "testField", + order = Set.empty, + subAggregations = List( + Terms( + name = "third", + field = "anotherTestField", + order = Set.empty, + subAggregations = Nil, + size = None + ) + ), + size = None + ) + ) + ) + ) + ) + }, + test("terms") { + val aggregation = termsAggregation("aggregation", "testField") + val aggregationTs = termsAggregation("aggregation", TestSubDocument.stringField) + val aggregationTsRaw = termsAggregation("aggregation", TestSubDocument.stringField.raw) + val aggregationWithOrders = + termsAggregation("aggregation", TestSubDocument.stringField).orderByKeyDesc + .orderBy(AggregationOrder("test", Desc)) + .orderByCountAsc + val aggregationWithSize = termsAggregation("aggregation", TestSubDocument.stringField).size(10) + val aggregationWithAllParams = + termsAggregation("aggregation", TestSubDocument.stringField.suffix("test")).orderByCountDesc.orderByKeyAsc + .size(5) + + assert(aggregation)( + equalTo( + Terms(name = "aggregation", field = "testField", order = Set.empty, subAggregations = Nil, size = None) + ) + ) && + assert(aggregationTs)( + equalTo( + Terms( + name = "aggregation", + field = "stringField", + order = Set.empty, + subAggregations = Nil, + size = None + ) + ) + ) && + assert(aggregationTsRaw)( + equalTo( + Terms( + name = "aggregation", + field = "stringField.raw", + order = Set.empty, + subAggregations = Nil, + size = None + ) + ) + ) && + assert(aggregationWithOrders)( + equalTo( + Terms( + name = "aggregation", + field = "stringField", + order = + Set(AggregationOrder("_key", Desc), AggregationOrder("test", Desc), AggregationOrder("_count", Asc)), + subAggregations = Nil, + size = None + ) + ) + ) && + assert(aggregationWithSize)( + equalTo( + Terms( + name = "aggregation", + field = "stringField", + order = Set.empty, + subAggregations = Nil, + size = Some(10) + ) + ) + ) && + assert(aggregationWithAllParams)( + equalTo( + Terms( + name = "aggregation", + field = "stringField.test", + order = Set(AggregationOrder("_count", Desc), AggregationOrder("_key", Asc)), + subAggregations = Nil, + size = Some(5) + ) + ) + ) + } + ), + suite("encoding as JSON")( + test("multiple") { + val aggregation = + multipleAggregations.aggregations( + termsAggregation("first", TestDocument.stringField), + termsAggregation("second", "testField") + ) + val aggregationWithSubAggregation = + termsAggregation("first", "testField") + .withSubAgg(termsAggregation("second", TestSubDocument.stringField.raw)) + .withAgg(termsAggregation("third", TestDocument.stringField)) + + val expected = + """ + |{ + | "aggs": { + | "first": { + | "terms": { + | "field": "stringField" + | } + | }, + | "second": { + | "terms": { + | "field": "testField" + | } + | } + | } + |} + |""".stripMargin + + val expectedWithSubAggregation = + """ + |{ + | "aggs": { + | "first": { + | "terms": { + | "field": "testField" + | }, + | "aggs": { + | "second": { + | "terms": { + | "field": "stringField.raw" + | } + | } + | } + | }, + | "third": { + | "terms": { + | "field": "stringField" + | } + | } + | } + |} + |""".stripMargin + + assert(aggregation.toJson)(equalTo(expected.toJson)) && + assert(aggregationWithSubAggregation.toJson)(equalTo(expectedWithSubAggregation.toJson)) + }, + test("subAggregation") { + val aggregation = + termsAggregation("first", TestDocument.stringField) + .withSubAgg(termsAggregation("second", TestSubDocument.stringField.keyword)) + + val expected = + """ + |{ + | "aggs": { + | "first": { + | "terms": { + | "field": "stringField" + | }, + | "aggs": { + | "second": { + | "terms": { + | "field": "stringField.keyword" + | } + | } + | } + | } + | } + |} + |""".stripMargin + + assert(aggregation.toJson)(equalTo(expected.toJson)) + }, + test("terms") { + val aggregation = termsAggregation("aggregation", "testField") + val aggregationTs = termsAggregation("aggregation", TestDocument.stringField) + val aggregationWithOrder = termsAggregation("aggregation", TestDocument.stringField).orderByKeyDesc + val aggregationWithSize = termsAggregation("aggregation", TestDocument.stringField).size(10) + val aggregationWithAllParams = termsAggregation("aggregation", TestDocument.stringField) + .orderBy(AggregationOrder("test", SortOrder.Asc)) + .size(20) + + val expected = + """ + |{ + | "aggs": { + | "aggregation": { + | "terms": { + | "field": "testField" + | } + | } + | } + |} + |""".stripMargin + + val expectedTs = + """ + |{ + | "aggs": { + | "aggregation": { + | "terms": { + | "field": "stringField" + | } + | } + | } + |} + |""".stripMargin + + val expectedWithOrder = + """ + |{ + | "aggs": { + | "aggregation": { + | "terms": { + | "field": "stringField", + | "order": { + | "_key": "desc" + | } + | } + | } + | } + |} + |""".stripMargin + + val expectedWithSize = + """ + |{ + | "aggs": { + | "aggregation": { + | "terms": { + | "field": "stringField", + | "size": 10 + | } + | } + | } + |} + |""".stripMargin + + val expectedWithAllParams = + """ + |{ + | "aggs": { + | "aggregation": { + | "terms": { + | "field": "stringField", + | "order": { + | "test": "asc" + | }, + | "size": 20 + | } + | } + | } + |} + |""".stripMargin + + assert(aggregation.toJson)(equalTo(expected.toJson)) && + assert(aggregationTs.toJson)(equalTo(expectedTs.toJson)) && + assert(aggregationWithOrder.toJson)(equalTo(expectedWithOrder.toJson)) && + assert(aggregationWithSize.toJson)(equalTo(expectedWithSize.toJson)) && + assert(aggregationWithAllParams.toJson)(equalTo(expectedWithAllParams.toJson)) + } + ) + ) +} diff --git a/modules/library/src/test/scala/zio/elasticsearch/ElasticPrimitiveSpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ElasticPrimitiveSpec.scala index bb6963f95..421bbf59c 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ElasticPrimitiveSpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ElasticPrimitiveSpec.scala @@ -9,28 +9,28 @@ import java.util.UUID object ElasticPrimitiveSpec extends ZIOSpecDefault { override def spec: Spec[TestEnvironment, Any] = - suite("Elastic Primitives")( - test("successfully create matches query with String") { - assert(matches(FieldName, "string"))(equalTo(Match[Any, String](FieldName, "string", boost = None))) - }, - test("successfully create matches query with BigDecimal") { + suite("ElasticPrimitive")( + test("BigDecimal") { assert(matches(FieldName, BigDecimal(1)))( equalTo(Match[Any, BigDecimal](FieldName, BigDecimal(1), boost = None)) ) }, - test("successfully create matches query with Boolean") { + test("Boolean") { assert(matches(FieldName, true))(equalTo(Match[Any, Boolean](FieldName, true, boost = None))) }, - test("successfully create matches query with Double") { + test("Double") { assert(matches(FieldName, 1.00))(equalTo(Match[Any, Double](FieldName, 1.00, boost = None))) }, - test("successfully create matches query with Int") { + test("Int") { assert(matches(FieldName, 1))(equalTo(Match[Any, Int](FieldName, 1, boost = None))) }, - test("successfully create matches query with Long") { + test("Long") { assert(matches(FieldName, 1L))(equalTo(Match[Any, Long](FieldName, 1L, boost = None))) }, - test("successfully create matches query with UUID") { + test("String") { + assert(matches(FieldName, "string"))(equalTo(Match[Any, String](FieldName, "string", boost = None))) + }, + test("UUID") { val uuid = UUID.randomUUID() assert(matches(FieldName, uuid))(equalTo(Match[Any, UUID](FieldName, uuid, boost = None))) } diff --git a/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala b/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala index 2b4e5cd9f..97bc6638b 100644 --- a/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala +++ b/modules/library/src/test/scala/zio/elasticsearch/ElasticQuerySpec.scala @@ -16,7 +16,6 @@ package zio.elasticsearch -import zio.Scope import zio.elasticsearch.ElasticQuery._ import zio.elasticsearch.ElasticRequest.Bulk import zio.elasticsearch.domain._ @@ -27,7 +26,7 @@ import zio.test.Assertion.equalTo import zio.test.{Spec, TestEnvironment, ZIOSpecDefault, assert} object ElasticQuerySpec extends ZIOSpecDefault { - def spec: Spec[Environment with TestEnvironment with Scope, Any] = + def spec: Spec[TestEnvironment, Any] = suite("ElasticQuery")( suite("constructing")( suite("bool")(