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

(dsl): Provide createIndex methods #101

Merged
merged 1 commit into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion modules/example/src/main/scala/example/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object Main extends ZIOAppDefault {
for {
_ <- ZIO.logInfo(s"Creating index '$Index'...")
mapping <- ZIO.fromTry(Using(Source.fromURL(getClass.getResource("/mapping.json")))(_.mkString))
_ <- Elasticsearch.execute(ElasticRequest.createIndex(Index, Some(mapping)))
_ <- Elasticsearch.execute(ElasticRequest.createIndex(Index, mapping))
} yield ()

val populate: RIO[SttpBackend[Task, Any] with Elasticsearch, Unit] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ object HttpExecutorSpec extends IntegrationSpec {
),
suite("creating index")(
test("successfully create index") {
assertZIO(ElasticExecutor.execute(ElasticRequest.createIndex(createIndexTestName, None)))(equalTo(Created))
assertZIO(ElasticExecutor.execute(ElasticRequest.createIndex(createIndexTestName)))(equalTo(Created))
},
test("return 'AlreadyExists' if index already exists") {
for {
_ <- ElasticExecutor.execute(ElasticRequest.createIndex(createIndexTestName, None))
res <- ElasticExecutor.execute(ElasticRequest.createIndex(createIndexTestName, None))
_ <- ElasticExecutor.execute(ElasticRequest.createIndex(createIndexTestName))
res <- ElasticExecutor.execute(ElasticRequest.createIndex(createIndexTestName))
} yield assert(res)(equalTo(AlreadyExists))
}
) @@ after(ElasticExecutor.execute(ElasticRequest.deleteIndex(createIndexTestName)).orDie),
Expand Down Expand Up @@ -100,7 +100,7 @@ object HttpExecutorSpec extends IntegrationSpec {
test("successfully delete existing index") {
checkOnce(genIndexName) { name =>
for {
_ <- ElasticExecutor.execute(ElasticRequest.createIndex(name, None))
_ <- ElasticExecutor.execute(ElasticRequest.createIndex(name))
res <- ElasticExecutor.execute(ElasticRequest.deleteIndex(name))
} yield assert(res)(equalTo(Deleted))
}
Expand Down Expand Up @@ -174,7 +174,7 @@ object HttpExecutorSpec extends IntegrationSpec {
} yield assert(res)(isNonEmpty)
}
} @@ around(
ElasticExecutor.execute(ElasticRequest.createIndex(firstSearchIndex, None)),
ElasticExecutor.execute(ElasticRequest.createIndex(firstSearchIndex)),
ElasticExecutor.execute(ElasticRequest.deleteIndex(firstSearchIndex)).orDie
),
test("fail if any of results cannot be decoded") {
Expand Down Expand Up @@ -205,7 +205,7 @@ object HttpExecutorSpec extends IntegrationSpec {
)
}
} @@ around(
ElasticExecutor.execute(ElasticRequest.createIndex(secondSearchIndex, None)),
ElasticExecutor.execute(ElasticRequest.createIndex(secondSearchIndex)),
ElasticExecutor.execute(ElasticRequest.deleteIndex(secondSearchIndex)).orDie
),
test("search for a document which contains a term using a wildcard query") {
Expand All @@ -228,7 +228,7 @@ object HttpExecutorSpec extends IntegrationSpec {
} yield assert(res)(Assertion.contains(firstCustomer))
}
} @@ around(
ElasticExecutor.execute(ElasticRequest.createIndex(firstSearchIndex, None)),
ElasticExecutor.execute(ElasticRequest.createIndex(firstSearchIndex)),
ElasticExecutor.execute(ElasticRequest.deleteIndex(firstSearchIndex)).orDie
),
test("search for a document which starts with a term using a wildcard query") {
Expand All @@ -251,7 +251,7 @@ object HttpExecutorSpec extends IntegrationSpec {
} yield assert(res)(Assertion.contains(firstCustomer))
}
} @@ around(
ElasticExecutor.execute(ElasticRequest.createIndex(firstSearchIndex, None)),
ElasticExecutor.execute(ElasticRequest.createIndex(firstSearchIndex)),
ElasticExecutor.execute(ElasticRequest.deleteIndex(firstSearchIndex)).orDie
),
test("search for a document which conforms to a pattern using a wildcard query") {
Expand All @@ -275,7 +275,7 @@ object HttpExecutorSpec extends IntegrationSpec {
} yield assert(res)(Assertion.contains(firstCustomer))
}
} @@ around(
ElasticExecutor.execute(ElasticRequest.createIndex(firstSearchIndex, None)),
ElasticExecutor.execute(ElasticRequest.createIndex(firstSearchIndex)),
ElasticExecutor.execute(ElasticRequest.deleteIndex(firstSearchIndex)).orDie
)
) @@ shrinks(0),
Expand Down Expand Up @@ -318,7 +318,7 @@ object HttpExecutorSpec extends IntegrationSpec {
} yield assert(res)(hasSameElements(List(firstCustomer.copy(balance = 150))))
}
} @@ around(
ElasticExecutor.execute(ElasticRequest.createIndex(deleteByQueryIndex, None)),
ElasticExecutor.execute(ElasticRequest.createIndex(deleteByQueryIndex)),
ElasticExecutor.execute(ElasticRequest.deleteIndex(deleteByQueryIndex)).orDie
),
test("returns NotFound when provided index is missing") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ trait IntegrationSpec extends ZIOSpecDefault {
val createIndexTestName: IndexName = IndexName("create-index-test-name")

val prepareElasticsearchIndexForTests: TestAspect[Nothing, Any, Throwable, Any] = beforeAll((for {
_ <- ElasticExecutor.execute(ElasticRequest.createIndex(index, None))
_ <- ElasticExecutor.execute(ElasticRequest.createIndex(index))
_ <- ElasticExecutor.execute(ElasticRequest.deleteByQuery(index, matchAll).refreshTrue)
} yield ()).provide(elasticsearchLayer))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ object ElasticRequest {
def create[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[CreationOutcome, CreateWithId] =
CreateWithIdRequest(index = index, id = id, document = Document.from(doc), refresh = false, routing = None)

def createIndex(name: IndexName, definition: Option[String]): ElasticRequest[CreationOutcome, CreateIndex] =
CreateIndexRequest(name, definition)
def createIndex(name: IndexName): ElasticRequest[CreationOutcome, CreateIndex] =
CreateIndexRequest(name = name, definition = None)

def createIndex(name: IndexName, definition: String): ElasticRequest[CreationOutcome, CreateIndex] =
CreateIndexRequest(name = name, definition = Some(definition))

def deleteById(index: IndexName, id: DocumentId): ElasticRequest[DeletionOutcome, DeleteById] =
DeleteByIdRequest(index = index, id = id, refresh = false, routing = None)
Expand Down Expand Up @@ -95,7 +98,7 @@ object ElasticRequest {
}

def upsert[A: Schema](index: IndexName, id: DocumentId, doc: A): ElasticRequest[Unit, Upsert] =
CreateOrUpdateRequest(index, id, Document.from(doc), refresh = false, routing = None)
CreateOrUpdateRequest(index = index, id = id, document = Document.from(doc), refresh = false, routing = None)

private[elasticsearch] final case class BulkableRequest private (request: ElasticRequest[_, _])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@
package zio.elasticsearch

import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock.{aResponse, delete, get, head, post, put, urlEqualTo}
import com.github.tomakehurst.wiremock.client.WireMock.{
aResponse,
delete,
equalToJson,
get,
head,
post,
put,
urlEqualTo
}
import sttp.model.StatusCode
import zio.ZIO
import zio.elasticsearch.ElasticQuery.matchAll
Expand Down Expand Up @@ -117,15 +126,74 @@ object HttpElasticExecutorSpec extends WireMockSpec {
)
)(equalTo(Created))
},
test("creating index request") {
test("creating index request without mapping") {
val addStubMapping = ZIO.serviceWith[WireMockServer](
_.addStubMapping(
put(urlEqualTo("/repositories")).willReturn(aResponse.withStatus(StatusCode.Ok.code)).build
)
)

assertZIO(
addStubMapping *> ElasticExecutor.execute(ElasticRequest.createIndex(name = index, definition = None))
addStubMapping *> ElasticExecutor.execute(ElasticRequest.createIndex(name = index))
)(
equalTo(Created)
)
},
test("creating index request with mapping") {
val mapping =
"""
|{
| "settings": {
| "index": {
| "number_of_shards": 1
| }
| },
| "mappings": {
| "_routing": {
| "required": true
| },
| "properties": {
| "id": {
| "type": "keyword"
| }
| }
| }
|}
|""".stripMargin

val addStubMapping = ZIO.serviceWith[WireMockServer](
_.addStubMapping(
put(urlEqualTo("/repositories"))
.withRequestBody(
equalToJson(
"""
|{
| "settings": {
| "index": {
| "number_of_shards": 1
| }
| },
| "mappings": {
| "_routing": {
| "required": true
| },
| "properties": {
| "id": {
| "type": "keyword"
| }
| }
| }
|}
|""".stripMargin
)
)
.willReturn(aResponse.withStatus(StatusCode.Ok.code))
.build
)
)

assertZIO(
addStubMapping *> ElasticExecutor.execute(ElasticRequest.createIndex(name = index, definition = mapping))
)(
equalTo(Created)
)
Expand Down