1
0
Fork 0
mirror of synced 2024-06-02 10:54:44 +12:00

Parse min and max as proper types

This commit is contained in:
kodumbeats 2021-07-27 15:12:24 -04:00
parent 1eae5490df
commit 432a1a7f5c
2 changed files with 31 additions and 27 deletions

View file

@ -67,17 +67,19 @@ $attributesCallback = function ($attribute, $response, $dbForExternal, $database
}
if (!is_null($min) || !is_null($max)) { // Add range validator if either $min or $max is provided
$min = (is_null($min)) ? -INF : \intval($min);
$max = (is_null($max)) ? INF : \intval($max);
switch ($type) {
case Database::VAR_INTEGER:
$format = 'int-range';
break;
case Database::VAR_FLOAT:
$format = 'float-range';
break;
default:
throw new Exception("Format range not available for {$type} attributes.", 400);
case Database::VAR_INTEGER:
$min = (is_null($min)) ? -INF : \intval($min);
$max = (is_null($max)) ? INF : \intval($max);
$format = 'int-range';
break;
case Database::VAR_FLOAT:
$min = (is_null($min)) ? -INF : \floatval($min);
$max = (is_null($max)) ? INF : \floatval($max);
$format = 'float-range';
break;
default:
throw new Exception("Format range not available for {$type} attributes.", 400);
}
}
@ -729,13 +731,6 @@ App::delete('/v1/database/collections/:collectionId/attributes/:attributeId')
$type = $attribute->getAttribute('type', '');
$format = $attribute->getAttribute('format', '');
// Remove the range filter from ints/floats, if given
if ($type === Database::VAR_INTEGER || $type === Database::VAR_FLOAT) {
if ($format) {
Structure::removeFormat($format);
}
}
$database
->setParam('type', DELETE_TYPE_ATTRIBUTE)
->setParam('document', $attribute)

View file

@ -567,7 +567,6 @@ trait DatabaseBase
'max' => 10,
]);
// TODO@kodumbeats float validator rejects 0.0 and 1.0 as floats
// TODO@kodumbeats min and max are rounded in error message
$floatRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/float', array_merge([
'content-type' => 'application/json',
@ -576,10 +575,22 @@ trait DatabaseBase
]), [
'attributeId' => 'floatRange',
'required' => false,
'min' => 0.5,
'max' => 1.5,
'min' => 1.1,
'max' => 1.4,
]);
// TODO@kodumbeats float validator rejects 0.0 and 1.0 as floats
// $probability = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/float', array_merge([
// 'content-type' => 'application/json',
// 'x-appwrite-project' => $this->getProject()['$id'],
// 'x-appwrite-key' => $this->getProject()['apiKey']
// ]), [
// 'attributeId' => 'probability',
// 'required' => false,
// 'min' => \floatval(0.0),
// 'max' => \floatval(1.0),
// ]);
$upperBound = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/attributes/integer', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
@ -626,7 +637,7 @@ trait DatabaseBase
// $this->assertEquals('Minimum value must be lesser than maximum value', $invalidRange['body']['message']);
// wait for worker to add attributes
sleep(15);
sleep(10);
$collection = $this->client->call(Client::METHOD_GET, '/database/collections/' . $collectionId, array_merge([
'content-type' => 'application/json',
@ -634,8 +645,6 @@ trait DatabaseBase
'x-appwrite-key' => $this->getProject()['apiKey'],
]), []);
var_dump($collection);
$this->assertCount(7, $collection['body']['attributes']);
$this->assertCount(0, $collection['body']['attributesInQueue']);
@ -692,7 +701,7 @@ trait DatabaseBase
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'data' => [
'floatRange' => 0.8,
'floatRange' => 1.4,
],
'read' => ['user:'.$this->getUser()['$id']],
'write' => ['user:'.$this->getUser()['$id']],
@ -778,7 +787,7 @@ trait DatabaseBase
'write' => ['user:'.$this->getUser()['$id']],
]);
$badProbability = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
$badFloatRange = $this->client->call(Client::METHOD_POST, '/database/collections/' . $collectionId . '/documents', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
@ -816,14 +825,14 @@ trait DatabaseBase
$this->assertEquals(400, $badIp['headers']['status-code']);
$this->assertEquals(400, $badUrl['headers']['status-code']);
$this->assertEquals(400, $badRange['headers']['status-code']);
$this->assertEquals(400, $badProbability['headers']['status-code']);
$this->assertEquals(400, $badFloatRange['headers']['status-code']);
$this->assertEquals(400, $tooHigh['headers']['status-code']);
$this->assertEquals(400, $tooLow['headers']['status-code']);
$this->assertEquals('Invalid document structure: Attribute "email" has invalid format. Value must be a valid email address', $badEmail['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "ip" has invalid format. Value must be a valid IP address', $badIp['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "url" has invalid format. Value must be a valid URL', $badUrl['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "range" has invalid format. Value must be a valid range between 1 and 10', $badRange['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "floatRange" has invalid format. Value must be a valid range between 1 and 2', $badProbability['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "floatRange" has invalid format. Value must be a valid range between 1 and 1', $badFloatRange['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "upperBound" has invalid format. Value must be a valid range between inf and 10', $tooHigh['body']['message']);
$this->assertEquals('Invalid document structure: Attribute "lowerBound" has invalid format. Value must be a valid range between 5 and inf', $tooLow['body']['message']);
}