Skip to content

Commit

Permalink
More coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Mar 31, 2024
1 parent 059b49a commit 4ac4ff9
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/DbAutoProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,36 @@ 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.
*/
public function initDbProperties(Settings $settings, string $tablePrefix): void
{
$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]
));
Expand All @@ -70,7 +82,7 @@ public function initDbProperties(Settings $settings, string $tablePrefix): void
$optimize = $attr->optimizeIfWastedMb ?? $settings->optimizeIfWastedMb;

if ($optimize !== $settings->optimizeIfWastedMb) {
$settings = new $settings(\array_merge(
$settings = new $settings(...\array_merge(
(array) $settings,
['optimizeIfWastedMb' => $optimize]
));
Expand Down
2 changes: 2 additions & 0 deletions src/Driver/DriverArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ public static function getInstance(DbArrayBuilder $config, DbArray|null $previou
$promises []= async($previous->unset(...), $key)
->map(static fn () => $instance->set($key, $value));
if (\count($promises) % 500 === 0) {
// @codeCoverageIgnoreStart
await($promises);
$promises = [];
// @codeCoverageIgnoreEnd
}
}
if ($promises) {
Expand Down
2 changes: 2 additions & 0 deletions src/Internal/Driver/PostgresArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ public function __construct(DbArrayBuilder $config, Serializer $serializer)
} elseif ($key === 'value') {
$expected = $valueType;
} else {
// @codeCoverageIgnoreStart
$connection->query("ALTER TABLE \"bytea_{$config->table}\" DROP \"$key\"");
continue;
// @codeCoverageIgnoreEnd
}
if ($expected !== $type) {
if ($expected === 'BIGINT') {
Expand Down
68 changes: 68 additions & 0 deletions tests/OrmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
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;
Expand Down Expand Up @@ -379,6 +380,27 @@ public function testKeyMigration(int $tablePostfix, Settings $settings): void
$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')]
Expand Down Expand Up @@ -486,6 +508,12 @@ public function testObject(int $tablePostfix, Settings $settings): void
$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(
Expand All @@ -505,7 +533,47 @@ public function testCache(): void
$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
Expand Down
15 changes: 15 additions & 0 deletions tests/TestObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down

0 comments on commit 4ac4ff9

Please sign in to comment.