|
1 |
| -/* |
2 |
| - * Copyright 2022 LambdaWorks |
3 |
| - * |
4 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
| - * you may not use this file except in compliance with the License. |
6 |
| - * You may obtain a copy of the License at |
7 |
| - * |
8 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
9 |
| - * |
10 |
| - * Unless required by applicable law or agreed to in writing, software |
11 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
12 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
| - * See the License for the specific language governing permissions and |
14 |
| - * limitations under the License. |
15 |
| - */ |
16 |
| - |
17 |
| -package zio.elasticsearch |
18 |
| - |
19 |
| -import sttp.client4.httpclient.zio.HttpClientZioBackend |
20 |
| -import zio._ |
21 |
| -import zio.elasticsearch.ElasticQuery.matchAll |
22 |
| -import zio.elasticsearch.data.GeoPoint |
23 |
| -import zio.elasticsearch.domain._ |
24 |
| -import zio.elasticsearch.executor.Executor |
25 |
| -import zio.elasticsearch.utils.unsafeWrap |
26 |
| -import zio.test.Assertion.{containsString, hasMessage} |
27 |
| -import zio.test.CheckVariants.CheckN |
28 |
| -import zio.test.TestAspect.beforeAll |
29 |
| -import zio.test.{Assertion, Gen, TestAspect, ZIOSpecDefault, checkN} |
30 |
| - |
31 |
| -import java.time.LocalDate |
32 |
| - |
33 |
| -trait IntegrationSpec extends ZIOSpecDefault { |
34 |
| - |
35 |
| - val elasticsearchLayer: TaskLayer[Executor] = HttpClientZioBackend.layer() >>> ElasticExecutor.local |
36 |
| - |
37 |
| - val index: IndexName = IndexName("users") |
38 |
| - |
39 |
| - val deleteByQueryIndex: IndexName = IndexName("delete-by-query-index") |
40 |
| - |
41 |
| - val firstSearchIndex: IndexName = IndexName("search-index-1") |
42 |
| - |
43 |
| - val secondSearchIndex: IndexName = IndexName("search-index-2") |
44 |
| - |
45 |
| - val createIndexTestName: IndexName = IndexName("create-index-test-name") |
46 |
| - |
47 |
| - val firstCountIndex: IndexName = IndexName("count-index-1") |
48 |
| - |
49 |
| - val secondCountIndex: IndexName = IndexName("count-index-2") |
50 |
| - |
51 |
| - val updateByQueryIndex: IndexName = IndexName("update-by-query-index") |
52 |
| - |
53 |
| - val geoDistanceIndex: IndexName = IndexName("geo-distance-index") |
54 |
| - |
55 |
| - val refreshFailIndex: IndexName = IndexName("refresh-fail") |
56 |
| - |
57 |
| - val IndexPatternAll: IndexPattern = IndexPattern("_all") |
58 |
| - |
59 |
| - val geoPolygonIndex: IndexName = IndexName("geo-polygon-index") |
60 |
| - |
61 |
| - val prepareElasticsearchIndexForTests: TestAspect[Nothing, Any, Throwable, Any] = beforeAll((for { |
62 |
| - _ <- Executor.execute(ElasticRequest.createIndex(index)) |
63 |
| - _ <- Executor.execute(ElasticRequest.deleteByQuery(index, matchAll).refreshTrue) |
64 |
| - } yield ()).provide(elasticsearchLayer)) |
65 |
| - |
66 |
| - def genIndexName: Gen[Any, IndexName] = |
67 |
| - Gen.stringBounded(10, 40)(Gen.alphaChar).map(name => unsafeWrap(name.toLowerCase)(IndexName)) |
68 |
| - |
69 |
| - def genDocumentId: Gen[Any, DocumentId] = |
70 |
| - Gen.stringBounded(10, 40)(Gen.alphaNumericChar).map(DocumentId(_)) |
71 |
| - |
72 |
| - def genGeoPoint: Gen[Any, GeoPoint] = |
73 |
| - for { |
74 |
| - latitude <- Gen.bigDecimal(10, 90).map(_.setScale(2, BigDecimal.RoundingMode.HALF_UP).toDouble) |
75 |
| - longitude <- Gen.bigDecimal(10, 90).map(_.setScale(2, BigDecimal.RoundingMode.HALF_UP).toDouble) |
76 |
| - } yield GeoPoint(latitude, longitude) |
77 |
| - |
78 |
| - def genTestDocument: Gen[Any, TestDocument] = for { |
79 |
| - stringField <- Gen.stringBounded(5, 10)(Gen.alphaChar) |
80 |
| - dateField <- Gen.localDate(LocalDate.parse("2010-12-02"), LocalDate.parse("2022-12-05")) |
81 |
| - subDocumentList <- Gen.listOfBounded(1, 3)(genTestSubDocument) |
82 |
| - intField <- Gen.int(1, 2000) |
83 |
| - doubleField <- Gen.double(100, 2000) |
84 |
| - booleanField <- Gen.boolean |
85 |
| - geoPointField <- genGeoPoint |
86 |
| - vectorField <- Gen.listOfN(5)(Gen.int(-10, 10)) |
87 |
| - } yield TestDocument( |
88 |
| - stringField = stringField, |
89 |
| - dateField = dateField, |
90 |
| - subDocumentList = subDocumentList, |
91 |
| - intField = intField, |
92 |
| - doubleField = doubleField, |
93 |
| - booleanField = booleanField, |
94 |
| - geoPointField = geoPointField, |
95 |
| - vectorField = vectorField |
96 |
| - ) |
97 |
| - |
98 |
| - def genTestSubDocument: Gen[Any, TestSubDocument] = for { |
99 |
| - stringField1 <- Gen.stringBounded(5, 10)(Gen.alphaChar) |
100 |
| - stringField2 <- Gen.stringBounded(5, 10)(Gen.alphaChar) |
101 |
| - longField <- Gen.long(1, 75) |
102 |
| - intField <- Gen.int(1, 200) |
103 |
| - } yield TestSubDocument( |
104 |
| - stringField = stringField1, |
105 |
| - nestedField = TestNestedField(stringField2, longField), |
106 |
| - intField = intField, |
107 |
| - intFieldList = Nil |
108 |
| - ) |
109 |
| - |
110 |
| - def checkOnce: CheckN = checkN(1) |
111 |
| - |
112 |
| - def assertException(substring: String): Assertion[Throwable] = hasMessage(containsString(substring)) |
113 |
| -} |
| 1 | +/* |
| 2 | + * Copyright 2022 LambdaWorks |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package zio.elasticsearch |
| 18 | + |
| 19 | +import sttp.client4.httpclient.zio.HttpClientZioBackend |
| 20 | +import zio._ |
| 21 | +import zio.elasticsearch.ElasticQuery.matchAll |
| 22 | +import zio.elasticsearch.data.GeoPoint |
| 23 | +import zio.elasticsearch.domain._ |
| 24 | +import zio.elasticsearch.executor.Executor |
| 25 | +import zio.elasticsearch.utils.unsafeWrap |
| 26 | +import zio.test.Assertion.{containsString, hasMessage} |
| 27 | +import zio.test.CheckVariants.CheckN |
| 28 | +import zio.test.TestAspect.beforeAll |
| 29 | +import zio.test.{Assertion, Gen, TestAspect, ZIOSpecDefault, checkN} |
| 30 | + |
| 31 | +import java.time.LocalDate |
| 32 | + |
| 33 | +trait IntegrationSpec extends ZIOSpecDefault { |
| 34 | + |
| 35 | + val elasticsearchLayer: TaskLayer[Executor] = HttpClientZioBackend.layer() >>> ElasticExecutor.local |
| 36 | + |
| 37 | + val index: IndexName = IndexName("users") |
| 38 | + |
| 39 | + val deleteByQueryIndex: IndexName = IndexName("delete-by-query-index") |
| 40 | + |
| 41 | + val firstSearchIndex: IndexName = IndexName("search-index-1") |
| 42 | + |
| 43 | + val secondSearchIndex: IndexName = IndexName("search-index-2") |
| 44 | + |
| 45 | + val createIndexTestName: IndexName = IndexName("create-index-test-name") |
| 46 | + |
| 47 | + val firstCountIndex: IndexName = IndexName("count-index-1") |
| 48 | + |
| 49 | + val secondCountIndex: IndexName = IndexName("count-index-2") |
| 50 | + |
| 51 | + val updateByQueryIndex: IndexName = IndexName("update-by-query-index") |
| 52 | + |
| 53 | + val geoDistanceIndex: IndexName = IndexName("geo-distance-index") |
| 54 | + |
| 55 | + val refreshFailIndex: IndexName = IndexName("refresh-fail") |
| 56 | + |
| 57 | + val IndexPatternAll: IndexPattern = IndexPattern("_all") |
| 58 | + |
| 59 | + val geoPolygonIndex: IndexName = IndexName("geo-polygon-index") |
| 60 | + |
| 61 | + val prepareElasticsearchIndexForTests: TestAspect[Nothing, Any, Throwable, Any] = beforeAll((for { |
| 62 | + _ <- Executor.execute(ElasticRequest.createIndex(index)) |
| 63 | + _ <- Executor.execute(ElasticRequest.deleteByQuery(index, matchAll).refreshTrue) |
| 64 | + } yield ()).provide(elasticsearchLayer)) |
| 65 | + |
| 66 | + def genIndexName: Gen[Any, IndexName] = |
| 67 | + Gen.stringBounded(10, 40)(Gen.alphaChar).map(name => unsafeWrap(name.toLowerCase)(IndexName)) |
| 68 | + |
| 69 | + def genDocumentId: Gen[Any, DocumentId] = |
| 70 | + Gen.stringBounded(10, 40)(Gen.alphaNumericChar).map(DocumentId(_)) |
| 71 | + |
| 72 | + def genGeoPoint: Gen[Any, GeoPoint] = |
| 73 | + for { |
| 74 | + latitude <- Gen.bigDecimal(10, 90).map(_.setScale(2, BigDecimal.RoundingMode.HALF_UP).toDouble) |
| 75 | + longitude <- Gen.bigDecimal(10, 90).map(_.setScale(2, BigDecimal.RoundingMode.HALF_UP).toDouble) |
| 76 | + } yield GeoPoint(latitude, longitude) |
| 77 | + |
| 78 | + def genTestDocument: Gen[Any, TestDocument] = for { |
| 79 | + stringField <- Gen.stringBounded(5, 10)(Gen.alphaChar) |
| 80 | + dateField <- Gen.localDate(LocalDate.parse("2010-12-02"), LocalDate.parse("2022-12-05")) |
| 81 | + subDocumentList <- Gen.listOfBounded(1, 3)(genTestSubDocument) |
| 82 | + intField <- Gen.int(1, 2000) |
| 83 | + doubleField <- Gen.double(100, 2000) |
| 84 | + booleanField <- Gen.boolean |
| 85 | + geoPointField <- genGeoPoint |
| 86 | + vectorField <- Gen.listOfN(5)(Gen.int(-10, 10)) |
| 87 | + } yield TestDocument( |
| 88 | + stringField = stringField, |
| 89 | + dateField = dateField, |
| 90 | + subDocumentList = subDocumentList, |
| 91 | + intField = intField, |
| 92 | + doubleField = doubleField, |
| 93 | + booleanField = booleanField, |
| 94 | + geoPointField = geoPointField, |
| 95 | + vectorField = vectorField |
| 96 | + ) |
| 97 | + |
| 98 | + def genTestSubDocument: Gen[Any, TestSubDocument] = for { |
| 99 | + stringField1 <- Gen.stringBounded(5, 10)(Gen.alphaChar) |
| 100 | + stringField2 <- Gen.stringBounded(5, 10)(Gen.alphaChar) |
| 101 | + longField <- Gen.long(1, 75) |
| 102 | + intField <- Gen.int(1, 200) |
| 103 | + } yield TestSubDocument( |
| 104 | + stringField = stringField1, |
| 105 | + nestedField = TestNestedField(stringField2, longField), |
| 106 | + intField = intField, |
| 107 | + intFieldList = Nil |
| 108 | + ) |
| 109 | + |
| 110 | + def checkOnce: CheckN = checkN(1) |
| 111 | + |
| 112 | + def assertException(substring: String): Assertion[Throwable] = hasMessage(containsString(substring)) |
| 113 | +} |
0 commit comments