mirror of
https://github.com/danog/AsyncOrm.git
synced 2024-11-29 20:29:48 +01:00
More coverage
This commit is contained in:
parent
059b49a05a
commit
4ac4ff9aa4
@ -44,6 +44,20 @@ trait DbAutoProperties
|
|||||||
/** @var list<CachedArray> */
|
/** @var list<CachedArray> */
|
||||||
private array $properties = [];
|
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.
|
* Initialize database properties.
|
||||||
*/
|
*/
|
||||||
@ -51,17 +65,15 @@ trait DbAutoProperties
|
|||||||
{
|
{
|
||||||
$this->properties = [];
|
$this->properties = [];
|
||||||
$promises = [];
|
$promises = [];
|
||||||
foreach ((new ReflectionClass(static::class))->getProperties() as $property) {
|
foreach ($this->getDbAutoProperties() as $property) {
|
||||||
$attr = $property->getAttributes(OrmMappedArray::class);
|
$attr = $property->getAttributes(OrmMappedArray::class);
|
||||||
if (!$attr) {
|
\assert(\count($attr) !== 0);
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$attr = $attr[0]->newInstance();
|
$attr = $attr[0]->newInstance();
|
||||||
|
|
||||||
if ($settings instanceof DriverSettings) {
|
if ($settings instanceof DriverSettings) {
|
||||||
$ttl = $attr->cacheTtl ?? $settings->cacheTtl;
|
$ttl = $attr->cacheTtl ?? $settings->cacheTtl;
|
||||||
if ($ttl !== $settings->cacheTtl) {
|
if ($ttl !== $settings->cacheTtl) {
|
||||||
$settings = new $settings(\array_merge(
|
$settings = new $settings(...\array_merge(
|
||||||
(array) $settings,
|
(array) $settings,
|
||||||
['cacheTtl' => $ttl]
|
['cacheTtl' => $ttl]
|
||||||
));
|
));
|
||||||
@ -70,7 +82,7 @@ trait DbAutoProperties
|
|||||||
$optimize = $attr->optimizeIfWastedMb ?? $settings->optimizeIfWastedMb;
|
$optimize = $attr->optimizeIfWastedMb ?? $settings->optimizeIfWastedMb;
|
||||||
|
|
||||||
if ($optimize !== $settings->optimizeIfWastedMb) {
|
if ($optimize !== $settings->optimizeIfWastedMb) {
|
||||||
$settings = new $settings(\array_merge(
|
$settings = new $settings(...\array_merge(
|
||||||
(array) $settings,
|
(array) $settings,
|
||||||
['optimizeIfWastedMb' => $optimize]
|
['optimizeIfWastedMb' => $optimize]
|
||||||
));
|
));
|
||||||
|
@ -98,8 +98,10 @@ abstract class DriverArray extends DbArray
|
|||||||
$promises []= async($previous->unset(...), $key)
|
$promises []= async($previous->unset(...), $key)
|
||||||
->map(static fn () => $instance->set($key, $value));
|
->map(static fn () => $instance->set($key, $value));
|
||||||
if (\count($promises) % 500 === 0) {
|
if (\count($promises) % 500 === 0) {
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
await($promises);
|
await($promises);
|
||||||
$promises = [];
|
$promises = [];
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($promises) {
|
if ($promises) {
|
||||||
|
@ -130,8 +130,10 @@ class PostgresArray extends SqlArray
|
|||||||
} elseif ($key === 'value') {
|
} elseif ($key === 'value') {
|
||||||
$expected = $valueType;
|
$expected = $valueType;
|
||||||
} else {
|
} else {
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
$connection->query("ALTER TABLE \"bytea_{$config->table}\" DROP \"$key\"");
|
$connection->query("ALTER TABLE \"bytea_{$config->table}\" DROP \"$key\"");
|
||||||
continue;
|
continue;
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
}
|
}
|
||||||
if ($expected !== $type) {
|
if ($expected !== $type) {
|
||||||
if ($expected === 'BIGINT') {
|
if ($expected === 'BIGINT') {
|
||||||
|
@ -33,6 +33,7 @@ use AssertionError;
|
|||||||
use danog\AsyncOrm\DbArrayBuilder;
|
use danog\AsyncOrm\DbArrayBuilder;
|
||||||
use danog\AsyncOrm\DbObject;
|
use danog\AsyncOrm\DbObject;
|
||||||
use danog\AsyncOrm\Driver\MemoryArray;
|
use danog\AsyncOrm\Driver\MemoryArray;
|
||||||
|
use danog\AsyncOrm\Internal\Containers\CacheContainer;
|
||||||
use danog\AsyncOrm\Internal\Driver\CachedArray;
|
use danog\AsyncOrm\Internal\Driver\CachedArray;
|
||||||
use danog\AsyncOrm\Internal\Driver\ObjectArray;
|
use danog\AsyncOrm\Internal\Driver\ObjectArray;
|
||||||
use danog\AsyncOrm\KeyType;
|
use danog\AsyncOrm\KeyType;
|
||||||
@ -379,6 +380,27 @@ final class OrmTest extends TestCase
|
|||||||
$this->assertEquals(1, $cnt);
|
$this->assertEquals(1, $cnt);
|
||||||
|
|
||||||
$this->assertCount(0, $old);
|
$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')]
|
#[DataProvider('provideSettings')]
|
||||||
@ -486,6 +508,12 @@ final class OrmTest extends TestCase
|
|||||||
$field->build()->clear();
|
$field->build()->clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testException(): void
|
||||||
|
{
|
||||||
|
$this->expectExceptionMessage("Cannot save an uninitialized object!");
|
||||||
|
(new TestObject)->save();
|
||||||
|
}
|
||||||
|
|
||||||
public function testCache(): void
|
public function testCache(): void
|
||||||
{
|
{
|
||||||
$field = new DbArrayBuilder("testCache", new RedisSettings(
|
$field = new DbArrayBuilder("testCache", new RedisSettings(
|
||||||
@ -505,7 +533,47 @@ final class OrmTest extends TestCase
|
|||||||
$this->assertCount(0, $ormUnCached);
|
$this->assertCount(0, $ormUnCached);
|
||||||
delay(0.9);
|
delay(0.9);
|
||||||
$this->assertCount(1, $ormUnCached);
|
$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();
|
$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
|
public static function provideSettingsKeysValues(): \Generator
|
||||||
|
@ -52,6 +52,21 @@ final class TestObject extends DbObject
|
|||||||
)]
|
)]
|
||||||
public DbArray $arr2;
|
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()
|
public function __sleep()
|
||||||
{
|
{
|
||||||
return ['savedProp', 'arr'];
|
return ['savedProp', 'arr'];
|
||||||
|
Loading…
Reference in New Issue
Block a user