Skip to content

Commit

Permalink
json-schema instead of a bunch of asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
plessbd committed May 14, 2019
1 parent d534811 commit cfc3b71
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 31 deletions.
126 changes: 126 additions & 0 deletions tests/artifacts/xdmod/schema/dw_descripter.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "GetDWDescriptors",
"type": "object",
"additionalProperties": false,
"properties": {
"totalCount": {
"type": "integer"
},
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/Datum"
}
}
},
"required": [
"data",
"totalCount"
],
"definitions": {
"Datum": {
"type": "object",
"additionalProperties": false,
"properties": {
"realms": {
"$ref": "#/definitions/Realms"
}
},
"required": [
"realms"
],
"title": "Datum"
},
"Realms": {
"type": "object",
"additionalProperties": {
"type": "object",
"$ref": "#/definitions/realm"
},
"title": "Realms",
"minProperties": 1
},
"realm": {
"type": "object",
"additionalProperties": false,
"properties": {
"metrics": {
"$ref": "#/definitions/metrics"
},
"dimensions": {
"$ref": "#/definitions/dimensions"
},
"text": {
"type": "string"
},
"category": {
"type": "string"
}
},
"required": [
"category",
"dimensions",
"metrics",
"text"
],
"title": "realm"
},
"dimensions": {
"type": "object",
"additionalProperties": {
"type": "object",
"$ref": "#/definitions/dimension"
},
"minProperties": 1,
"title": "Dimensions"
},
"metrics": {
"type": "object",
"additionalProperties": {
"type": "object",
"$ref": "#/definitions/metric"
},
"minProperties": 1,
"title": "metrics"
},
"dimension": {
"type": "object",
"additionalProperties": false,
"properties": {
"text": {
"type": "string"
},
"info": {
"type": "string"
}
},
"required": [
"info",
"text"
],
"title": "dimension"
},
"metric": {
"type": "object",
"additionalProperties": false,
"properties": {
"text": {
"type": "string"
},
"info": {
"type": "string"
},
"std_err": {
"type": "boolean"
}
},
"required": [
"info",
"std_err",
"text"
],
"title": "metric"
}
}
}
43 changes: 12 additions & 31 deletions tests/integration/lib/Controllers/UserAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace IntegrationTests\Controllers;

use CCR\Json;
use JsonSchema\Validator;
use TestHarness\TestFiles;

class UserAdminTest extends BaseUserAdminTest
Expand Down Expand Up @@ -416,39 +417,19 @@ public function testGetDwDescripters($username)
);

$response = $this->helper->post('controllers/metric_explorer.php', null, $data);
$this->validateResponse($response);

$actual = $response[0];
$this->assertArrayHasKey('data', $actual);
$this->assertArrayHasKey('totalCount', $actual);

$this->assertArrayHasKey(0, $actual['data']);
$this->assertArrayHasKey('realms', $actual['data'][0]);
$this->assertNotEmpty($actual['data'][0]['realms']);
foreach($actual['data'][0]['realms'] as $realm){
$this->assertArrayHasKey('metrics', $realm);
$this->assertNotEmpty($realm['metrics']);
$this->assertArrayHasKey('dimensions', $realm);
$this->assertNotEmpty($realm['dimensions']);
$this->assertArrayHasKey('text', $realm);
$this->assertNotEmpty($realm['text']);
$this->assertArrayHasKey('category', $realm);
$this->assertNotEmpty($realm['category']);
foreach($realm['metrics'] as $metric){
$this->assertArrayHasKey('text', $metric);
$this->assertNotEmpty($metric['text']);
$this->assertArrayHasKey('info', $metric);
$this->assertNotEmpty($metric['info']);
$this->assertArrayHasKey('std_err', $metric);
$this->assertInternalType('boolean', $metric['std_err']);
}
foreach($realm['dimensions'] as $dimension){
$this->assertArrayHasKey('text', $dimension);
$this->assertNotEmpty($dimension['text']);
$this->assertArrayHasKey('info', $dimension);
$this->assertNotEmpty($dimension['info']);
}
$schemaObject = JSON::loadFile(
$this->getTestFiles()->getFile('schema', 'dw_descripter.spec', ''),
false
);
$validator = new Validator();
$validator->check(json_decode(json_encode($actual)), $schemaObject);
$errors = array();
foreach ($validator->getErrors() as $err) {
$errors[] = sprintf("[%s] %s\n", $err['property'], $err['message']);
}
$this->assertEmpty($errors, implode("\n", $errors) . "\n" . json_encode($actual, JSON_PRETTY_PRINT));
$this->validateResponse($response);
if (!$isPublicUser) {
$this->helper->logout();
}
Expand Down

0 comments on commit cfc3b71

Please sign in to comment.