Skip to content

Commit

Permalink
fix(cts): assert that exceptions are thrown (#2713)
Browse files Browse the repository at this point in the history
Co-authored-by: Damien Couchez <damien.couchez@gmail.com>
  • Loading branch information
millotp and damcou authored Feb 15, 2024
1 parent addbd51 commit e2fd88a
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion templates/java/tests/client/suite.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class {{client}}ClientTests {
assertEquals("{{{match}}}", result.host);
{{/testHost}}
{{#testResponse}}
assertDoesNotThrow(() -> JSONAssert.assertEquals("{{#lambda.escapeQuotes}}{{{match.parameters}}}{{/lambda.escapeQuotes}}", json.writeValueAsString(res), JSONCompareMode.STRICT));
assertDoesNotThrow(() -> JSONAssert.assertEquals("{{#lambda.escapeQuotes}}{{{match.parameters}}}{{/lambda.escapeQuotes}}", json.writeValueAsString(res), JSONCompareMode.STRICT));
{{/testResponse}}
{{/match}}
{{/isError}}
Expand Down
12 changes: 0 additions & 12 deletions templates/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,6 @@ use {{invokerPackage}}\Support\Helpers;
*/
public static function create($appId = null, $apiKey = null, $region = null)
{
$allowedRegions = [{{#allowedRegions}}'{{.}}'{{^-last}},{{/-last}}{{/allowedRegions}}];

if (
{{^fallbackToAliasHost}}$region === null ||{{/fallbackToAliasHost}}
($region !== null && !in_array($region, $allowedRegions, true))
) {
throw new AlgoliaException(
'`region` {{^fallbackToAliasHost}}is required and {{/fallbackToAliasHost}}must be one of the following: ' .
implode(', ', $allowedRegions)
);
}

$config = {{configClassname}}::create($appId, $apiKey, $region);

return static::createWithConfig($config);
Expand Down
23 changes: 23 additions & 0 deletions templates/php/client_config.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,32 @@

namespace {{invokerPackage}}\Configuration;

use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;

class {{configClassname}} extends {{#hasRegionalHost}}ConfigWithRegion{{/hasRegionalHost}}{{^hasRegionalHost}}Configuration{{/hasRegionalHost}}
{
protected $clientName = '{{clientName}}';
{{#hasRegionalHost}}
public static function create($appId, $apiKey, $region = null)
{
$allowedRegions = [{{#allowedRegions}}'{{.}}'{{^-last}},{{/-last}}{{/allowedRegions}}];

if (
{{^fallbackToAliasHost}}$region === null ||{{/fallbackToAliasHost}}
($region !== null && !in_array($region, $allowedRegions, true))
) {
throw new AlgoliaException(
'`region` {{^fallbackToAliasHost}}is required and {{/fallbackToAliasHost}}must be one of the following: ' .
implode(', ', $allowedRegions)
);
}

return parent::create($appId, $apiKey, $region);
}
{{/hasRegionalHost}}

{{#isSearchClient}}private $defaultWaitTaskTimeBeforeRetry = 5000; // 5 sec in milliseconds
private $defaultMaxRetries = 50;

Expand Down
1 change: 1 addition & 0 deletions templates/php/tests/client/suite.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class {{clientPrefix}}Test extends TestCase implements HttpClientInterface
{{#isError}}
try {
{{#dynamicTemplate}}{{/dynamicTemplate}}
$this->fail('Expected exception to be thrown');
} catch (\Exception $e) {
$this->assertEquals($e->getMessage(), '{{{expectedError}}}');
}
Expand Down
5 changes: 2 additions & 3 deletions templates/php/tests/requests/requests.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,14 @@ class {{clientPrefix}}Test extends TestCase implements HttpClientInterface

protected function getClient()
{
$api = new ApiWrapper($this, {{clientPrefix}}Config::create(getenv('ALGOLIA_APP_ID'), getenv('ALGOLIA_API_KEY')), ClusterHosts::create('127.0.0.1'));
$config = {{clientPrefix}}Config::create('foo', 'bar');
$api = new ApiWrapper($this, {{clientPrefix}}Config::create(getenv('ALGOLIA_APP_ID'), getenv('ALGOLIA_API_KEY'){{#hasRegionalHost}},'{{defaultRegion}}' {{/hasRegionalHost}}), ClusterHosts::create('127.0.0.1'));
$config = {{clientPrefix}}Config::create('foo', 'bar'{{#hasRegionalHost}},'{{defaultRegion}}' {{/hasRegionalHost}});

return new {{client}}($api, $config);
}

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

/**
* Test case for {{#lambda.titlecase}}{{{method}}}{{/lambda.titlecase}}
* {{{testName}}}
Expand Down
1 change: 1 addition & 0 deletions templates/python/tests/client/suite.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Test{{#lambda.pascalcase}}{{{client}}}{{/lambda.pascalcase}}:
{{#isError}}
try:
{{#dynamicTemplate}}{{/dynamicTemplate}}
assert False
except (ValueError, Exception) as e:
assert str(e) == {{#lambda.codeSnakeCase}}"{{{expectedError}}}"{{/lambda.codeSnakeCase}}
{{/isError}}
Expand Down
1 change: 1 addition & 0 deletions templates/ruby/tests/client/suite.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class TestClient{{#lambda.pascalcase}}{{{client}}}{{/lambda.pascalcase}} < Test:
{{#isError}}
begin
{{#dynamicTemplate}}{{/dynamicTemplate}}
assert(false, 'An error should have been raised')
rescue => e
assert_equal({{#lambda.codeSnakeCase}}'{{{expectedError}}}'{{/lambda.codeSnakeCase}}, e.message)
end
Expand Down

0 comments on commit e2fd88a

Please sign in to comment.