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

feat(cts): add e2e for scala #2703

Merged
merged 5 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 templates/scala/generic_type.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
{{^isContainer}}
{{#x-is-generic}}T{{/x-is-generic}}{{^x-is-generic}}{{{datatypeWithEnum}}}{{/x-is-generic}}
{{/isContainer}}
{{/vendorExtensions}}
{{/vendorExtensions}}
2 changes: 1 addition & 1 deletion templates/scala/model.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import org.json4s.{Extraction, Formats, JField, JObject, JValue, Serializer, Typ
*/
case class {{classname}}(
{{#vars}}
{{{name}}}: {{^required}}Option[{{/required}}{{> generic_type}}{{^required}}] = scala.None{{/required}},
{{{name}}}: {{^required}}Option[{{/required}}{{#required}}{{#isNullable}}Option[{{/isNullable}}{{/required}}{{> generic_type}}{{^required}}] = scala.None{{/required}}{{#required}}{{#isNullable}}] = scala.None{{/isNullable}}{{/required}},
{{/vars}}
{{#additionalProperties}}
additionalProperties: Option[List[JField]] = None
Expand Down
2 changes: 1 addition & 1 deletion templates/scala/oneof_trait.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ object {{classname}}Serializer extends Serializer[{{classname}}] {
json match {
{{#composedSchemas.oneOf}}
{{#isModel}}
case value: JObject {{#vendorExtensions.x-discriminator-fields}}{{#-first}}if{{/-first}}{{^-first}}&&{{/-first}} value.obj.contains("{{{.}}}"){{/vendorExtensions.x-discriminator-fields}} => Extraction.extract[{{{datatypeWithEnum}}}](value)
case value: JObject {{#vendorExtensions.x-discriminator-fields}}{{#-first}}if{{/-first}}{{^-first}}&&{{/-first}} value.obj.exists(_._1 == "{{{.}}}"){{/vendorExtensions.x-discriminator-fields}} => Extraction.extract[{{{datatypeWithEnum}}}](value)
{{/isModel}}
{{#isMap}}
case value: JObject => {{classname}}.apply(Extraction.extract[{{{datatypeWithEnum}}}](value))
Expand Down
51 changes: 51 additions & 0 deletions templates/scala/tests/requests/requests.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ import algoliasearch.{{import}}.*
import org.json4s.*
import org.json4s.native.JsonParser.*
import org.scalatest.funsuite.AnyFunSuite
{{#hasE2E}}
import io.github.cdimascio.dotenv.Dotenv
import org.skyscreamer.jsonassert.JSONCompare.compareJSON
import org.skyscreamer.jsonassert.JSONCompareMode
import org.json4s.native.Serialization
import org.json4s.native.Serialization.write
{{/hasE2E}}

import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContextExecutor}
Expand All @@ -33,6 +40,27 @@ class {{clientPrefix}}Test extends AnyFunSuite {
)
}

{{#hasE2E}}
def testE2EClient(): {{client}} = {
if (System.getenv("CI") == "true") {
{{client}}(
appId = System.getenv("ALGOLIA_APPLICATION_ID"),
apiKey = System.getenv("ALGOLIA_ADMIN_KEY"){{#hasRegionalHost}},
region = {{#fallbackToAliasHost}}Some("{{defaultRegion}}"){{/fallbackToAliasHost}}{{^fallbackToAliasHost}}"{{defaultRegion}}"{{/fallbackToAliasHost}}
{{/hasRegionalHost}}
)
} else {
val dotenv = Dotenv.configure.directory("../../").load
{{client}}(
appId = dotenv.get("ALGOLIA_APPLICATION_ID"),
apiKey = dotenv.get("ALGOLIA_ADMIN_KEY"){{#hasRegionalHost}},
region = {{#fallbackToAliasHost}}Some("{{defaultRegion}}"){{/fallbackToAliasHost}}{{^fallbackToAliasHost}}"{{defaultRegion}}"{{/fallbackToAliasHost}}
{{/hasRegionalHost}}
)
}
}
{{/hasE2E}}

{{#blocksRequests}}
{{#tests}}

Expand Down Expand Up @@ -91,6 +119,29 @@ class {{clientPrefix}}Test extends AnyFunSuite {
}
{{/headers}}
{{/request}}
{{#response}}
val e2eClient = testE2EClient()
val e2eFuture = e2eClient.{{method}}{{#isCustomRequest}}[JObject]{{/isCustomRequest}}(
{{#parametersWithDataType}}
{{> tests/request_param}}{{^-last}},{{/-last}}
{{/parametersWithDataType}}
{{#hasRequestOptions}}
, requestOptions = Some(RequestOptions.builder()
{{#requestOptions.queryParameters.parametersWithDataType}}
.withQueryParameter("{{{key}}}", {{> tests/requests/requestOptionsParams}})
{{/requestOptions.queryParameters.parametersWithDataType}}
{{#requestOptions.headers.parametersWithDataType}}
.withHeader("{{{key}}}", "{{{value}}}")
{{/requestOptions.headers.parametersWithDataType}}
.build())
{{/hasRequestOptions}}
)

val response = Await.result(e2eFuture, Duration.Inf)
{{#body}}
compareJSON("""{{{body}}}""", write(response), JSONCompareMode.LENIENT)
{{/body}}
{{/response}}
}

{{/tests}}
Expand Down
2 changes: 2 additions & 0 deletions tests/output/scala/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ val algoliasearch = ProjectRef(file("../../../clients/algoliasearch-client-scala
dependsOn(algoliasearch)

libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.15" % Test
libraryDependencies += "io.github.cdimascio" % "dotenv-java" % "3.0.0" % Test
libraryDependencies += "org.skyscreamer" % "jsonassert" % "1.5.1" % Test
Loading