rosavox/lib/alveolata/storage/implementation-sqltablegroup/test.spec.php
2025-05-23 07:33:29 +00:00

216 lines
5.8 KiB
PHP

<?php
// require_once(DIR_ALVEOLATA . '/definitions.php');
require_once(DIR_ALVEOLATA . '/database/implementation-sqlite/wrapper-class.php');
require_once(DIR_ALVEOLATA . '/database/functions.php');
require_once(DIR_ALVEOLATA . '/storage/implementation-sqltable/functions.php');
require_once(DIR_ALVEOLATA . '/storage/implementation-sqltable/wrapper-class.php');
require_once(DIR_ALVEOLATA . '/storage/implementation-sqltablegroup/functions.php');
require_once(DIR_ALVEOLATA . '/storage/implementation-sqltablegroup/wrapper-class.php');
\alveolata\test\add(
[
'name' => 'alveolata',
'sections' => [
[
'name' => 'storage',
'sections' => [
[
'name' => 'sqltablegroup',
'setup' => function (&$environment) {
$environment['path_database'] = '/tmp/localdb.sqlite';
exec(sprintf('echo "" > %s', $environment['path_database']));
$database = \alveolata\database\make(
'sqlite',
[
'path' => $environment['path_database'],
]
);
$environment['storage'] = \alveolata\storage\implementation_sqltablegroup::make(
\alveolata\storage\implementation_sqltable::make(
$database,
'_testtable_core_',
[
[
'name' => 'foo',
'type' => 'VARCHAR(64)',
'null_allowed' => false,
'default' => '',
],
]
),
[
[
'sqltable' => \alveolata\storage\sqltable_make(
$database,
'_testtable_satellite1_',
[
[
'name' => 'foo_id',
'type' => 'INTEGER',
'null_allowed' => false,
],
[
'name' => 'val',
'type' => 'INTEGER',
'null_allowed' => false,
'default' => 0,
],
]
),
'key' => 'foo_id',
'target' => 'bar',
],
[
'sqltable' => \alveolata\storage\sqltable_make(
$database,
'_testtable_satellite2_',
[
[
'name' => 'foo_id',
'type' => 'INTEGER',
'null_allowed' => false,
],
[
'name' => 'val',
'type' => 'INTEGER',
'null_allowed' => false,
'default' => 0,
],
]
),
'key' => 'foo_id',
'target' => 'baz',
],
]
);
$environment['storage']->setup();
},
'sections' => [
[
'name' => 'create_delete',
'cases' => [
[
'name' => 'test',
'procedure' => function ($assert, $environment) {
// constants
$value = [
'foo' => 'Foo',
'bar' => [['val' => 0], ['val' => 2]],
'baz' => [['val' => 1], ['val' => 3]],
];
// execution & assertions
$assert->runs(
function () use (&$environment, &$value) {
$key = $environment['storage']->create($value);
$environment['storage']->delete($key);
}
);
}
],
]
],
[
'name' => 'read',
'cases' => [
[
'name' => 'test',
'procedure' => function ($assert, $environment) {
// constants
$value = [
'foo' => 'Foo',
'bar' => [['val' => 0], ['val' => 2]],
'baz' => [['val' => 1], ['val' => 3]],
];
// setup
$key = $environment['storage']->create($value);
// execution
$value_read = $environment['storage']->read($key);
// assertions
$assert->equal($value_read, $value);
// cleanup
$environment['storage']->delete($key);
}
],
]
],
[
'name' => 'search',
'cases' => [
[
'name' => 'test',
'procedure' => function ($assert, $environment) {
// constants
$value = [
'foo' => 'Foo',
'bar' => [['val' => 0], ['val' => 2]],
'baz' => [['val' => 1], ['val' => 3]],
];
// setup
$key = $environment['storage']->create($value);
// execution
$keys = $environment['storage']->search(['bar' => ['val' => 2]]);
// assertions
$assert->equal($keys, [$key]);
// cleanup
$environment['storage']->delete($key);
}
],
]
],
[
'name' => 'update',
'cases' => [
[
'name' => 'test',
'procedure' => function ($assert, $environment) {
// constants
$value1 = [
'foo' => 'Foo',
'bar' => [['val' => 0], ['val' => 2]],
'baz' => [['val' => 1], ['val' => 3]],
];
$value2 = [
'foo' => 'Fox',
'bar' => [['val' => 4], ['val' => 6]],
'baz' => [['val' => 5], ['val' => 7]],
];
// setup
$key = $environment['storage']->create($value1);
// execution
$environment['storage']->update($key, $value2);
$value_read = $environment['storage']->read($key);
// assertions
$assert->equal($value_read, $value2);
// cleanup
$environment['storage']->delete($key);
}
],
]
],
],
'cleanup' => function (&$environment) {
$environment['storage']->teardown();
exec(sprintf('rm -f %s', $environment['path_database']));
}
]
]
],
]
]
);