More coverage

This commit is contained in:
Daniil Gentili 2024-03-31 20:56:18 +02:00
parent 059b49a05a
commit 4ac4ff9aa4
5 changed files with 105 additions and 6 deletions

View File

@ -44,6 +44,20 @@ trait DbAutoProperties
/** @var list<CachedArray> */
private array $properties = [];
/** @return list<\ReflectionProperty> */
private function getDbAutoProperties(): array
{
$res = [];
foreach ((new ReflectionClass(static::class))->getProperties() as $property) {
$attr = $property->getAttributes(OrmMappedArray::class);
if (!$attr) {
continue;
}
$res []= $property;
}
return $res;
}
/**
* Initialize database properties.
*/
@ -51,17 +65,15 @@ trait DbAutoProperties
{
$this->properties = [];
$promises = [];
foreach ((new ReflectionClass(static::class))->getProperties() as $property) {
foreach ($this->getDbAutoProperties() as $property) {
$attr = $property->getAttributes(OrmMappedArray::class);
if (!$attr) {
continue;
}
\assert(\count($attr) !== 0);
$attr = $attr[0]->newInstance();
if ($settings instanceof DriverSettings) {
$ttl = $attr->cacheTtl ?? $settings->cacheTtl;
if ($ttl !== $settings->cacheTtl) {
$settings = new $settings(\array_merge(
$settings = new $settings(...\array_merge(
(array) $settings,
['cacheTtl' => $ttl]
));
@ -70,7 +82,7 @@ trait DbAutoProperties
$optimize = $attr->optimizeIfWastedMb ?? $settings->optimizeIfWastedMb;
if ($optimize !== $settings->optimizeIfWastedMb) {
$settings = new $settings(\array_merge(
$settings = new $settings(...\array_merge(
(array) $settings,
['optimizeIfWastedMb' => $optimize]
));

View File

@ -98,8 +98,10 @@ abstract class DriverArray extends DbArray
$promises []= async($previous->unset(...), $key)
->map(static fn () => $instance->set($key, $value));
if (\count($promises) % 500 === 0) {
// @codeCoverageIgnoreStart
await($promises);
$promises = [];
// @codeCoverageIgnoreEnd
}
}
if ($promises) {

View File

@ -130,8 +130,10 @@ class PostgresArray extends SqlArray
} elseif ($key === 'value') {
$expected = $valueType;
} else {
// @codeCoverageIgnoreStart
$connection->query("ALTER TABLE \"bytea_{$config->table}\" DROP \"$key\"");
continue;
// @codeCoverageIgnoreEnd
}
if ($expected !== $type) {
if ($expected === 'BIGINT') {

View File

@ -33,6 +33,7 @@ use AssertionError;
use danog\AsyncOrm\DbArrayBuilder;
use danog\AsyncOrm\DbObject;
use danog\AsyncOrm\Driver\MemoryArray;
use danog\AsyncOrm\Internal\Containers\CacheContainer;
use danog\AsyncOrm\Internal\Driver\CachedArray;
use danog\AsyncOrm\Internal\Driver\ObjectArray;
use danog\AsyncOrm\KeyType;
@ -379,6 +380,27 @@ final class OrmTest extends TestCase
$this->assertEquals(1, $cnt);
$this->assertCount(0, $old);
$field = new DbArrayBuilder(
$table.'_new',
new MemorySettings,
KeyType::INT,
ValueType::INT
);
$old = $orm;
$orm = $field->build($old);
$this->assertSame(123, $orm[321]);
$this->assertTrue(isset($orm[321]));
$cnt = 0;
foreach ($orm as $kk => $vv) {
$cnt++;
$this->assertSame(321, $kk);
$this->assertSame(123, $vv);
}
$this->assertEquals(1, $cnt);
$this->assertCount(1, $old);
}
#[DataProvider('provideSettings')]
@ -486,6 +508,12 @@ final class OrmTest extends TestCase
$field->build()->clear();
}
public function testException(): void
{
$this->expectExceptionMessage("Cannot save an uninitialized object!");
(new TestObject)->save();
}
public function testCache(): void
{
$field = new DbArrayBuilder("testCache", new RedisSettings(
@ -505,7 +533,47 @@ final class OrmTest extends TestCase
$this->assertCount(0, $ormUnCached);
delay(0.9);
$this->assertCount(1, $ormUnCached);
delay(1.0);
/** @var CacheContainer */
$c = (new ReflectionProperty(CachedArray::class, 'cache'))->getValue($orm);
$this->assertCount(0, (new ReflectionProperty(CacheContainer::class, 'cache'))->getValue($c));
$f1 = async($orm->get(...), 0);
$f2 = async($orm->get(...), 0);
$this->assertSame(1, $f1->await());
$this->assertSame(1, $f2->await());
$orm->clear();
$obj = new TestObject;
$obj->initDbProperties(new RedisSettings(
RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 1
), 'testCacheMore_');
$fieldNoCache2 = new DbArrayBuilder("testCacheMore_arr2", new RedisSettings(
RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 0
), KeyType::INT, ValueType::INT);
$orm2Uncached = $fieldNoCache2->build();
$fieldNoCache4 = new DbArrayBuilder("testCacheMore_arr4", new RedisSettings(
RedisConfig::fromUri("redis://127.0.0.1"),
cacheTtl: 0
), KeyType::INT, ValueType::INT);
$orm4Uncached = $fieldNoCache4->build();
$obj->arr2->set(0, 1);
$this->assertCount(0, $orm2Uncached);
delay(0.1);
$this->assertCount(0, $orm2Uncached);
delay(0.9);
$this->assertCount(1, $orm2Uncached);
$orm2Uncached->clear();
$obj->arr4->set(0, 1);
$this->assertCount(1, $orm4Uncached);
$orm4Uncached->clear();
}
public static function provideSettingsKeysValues(): \Generator

View File

@ -52,6 +52,21 @@ final class TestObject extends DbObject
)]
public DbArray $arr2;
#[OrmMappedArray(
KeyType::INT,
ValueType::INT,
cacheTtl: 0,
)]
public DbArray $arr3;
#[OrmMappedArray(
KeyType::INT,
ValueType::INT,
cacheTtl: 0,
optimizeIfWastedMb: 0
)]
public DbArray $arr4;
public function __sleep()
{
return ['savedProp', 'arr'];