mirror of
https://github.com/FreshRSS/FreshRSS.git
synced 2026-12-02 13:32:43 -05:00
Closes #9658 Changes proposed in this pull request: - Use PostgreSQL `dow` instead of `day` for the day-of-week chart. - Normalize month repartitions to exactly 12 entries, from January to December. - Include today in the 30-day chart, anchor the day calculation on the oldest timestamp so integer division is always positive, and return a JSON list. - Set chart labels and build the average line as an array matching the bar data. * Fix: corriger les statistiques PostgreSQL * Remove dead code --------- Co-authored-by: upskaling <upskaling@users.noreply.github.com> Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
93 lines
3.7 KiB
PHP
93 lines
3.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class StatsDAOTest extends TestCase {
|
|
|
|
/**
|
|
* @return array{0:FreshRSS_StatsDAOSQLite,1:Minz_PdoSqlite}
|
|
*/
|
|
private static function createSQLiteDao(): array {
|
|
$pdo = new Minz_PdoSqlite('sqlite::memory:');
|
|
$pdo->setPrefix('');
|
|
$pdo->exec('CREATE TABLE entry (id INTEGER PRIMARY KEY, date INTEGER)');
|
|
$dao = new FreshRSS_StatsDAOSQLite('test', $pdo);
|
|
return [$dao, $pdo];
|
|
}
|
|
|
|
public static function testCalculateEntryCountIncludesTodayWithSQLite(): void {
|
|
[$dao, $pdo] = self::createSQLiteDao();
|
|
$midnight = mktime(0, 0, 0) ?: 0;
|
|
for ($day = 0; $day <= 30; $day++) {
|
|
$pdo->exec('INSERT INTO entry(date) VALUES (' . ($midnight - $day * 86400 + 12 * 3600) . ')');
|
|
}
|
|
self::assertSame(array_fill(0, 31, 1), $dao->calculateEntryCount());
|
|
}
|
|
|
|
public static function testCalculateDayOfWeekRepartitionWithSQLite(): void {
|
|
[$dao, $pdo] = self::createSQLiteDao();
|
|
$pdo->exec('INSERT INTO entry(date) VALUES (' . mktime(12, 0, 0, 1, 7, 2024) . ')'); // 2024-01-07 is a Sunday
|
|
$dayOfWeek = $dao->calculateEntryRepartitionPerFeedPerDayOfWeek();
|
|
self::assertCount(7, $dayOfWeek);
|
|
self::assertSame(1, $dayOfWeek[0]);
|
|
self::assertSame(1, array_sum($dayOfWeek));
|
|
}
|
|
|
|
public static function testCalculateMonthRepartitionWithSQLite(): void {
|
|
[$dao, $pdo] = self::createSQLiteDao();
|
|
$pdo->exec('INSERT INTO entry(date) VALUES (' . mktime(12, 0, 0, 1, 15, 2024) . ')');
|
|
$pdo->exec('INSERT INTO entry(date) VALUES (' . mktime(12, 0, 0, 12, 15, 2024) . ')');
|
|
$months = $dao->calculateEntryRepartitionPerFeedPerMonth();
|
|
self::assertCount(12, $months);
|
|
self::assertSame(1, $months[0]); // January
|
|
self::assertSame(1, $months[11]); // December
|
|
self::assertSame(2, array_sum($months));
|
|
}
|
|
|
|
public static function testPostgreSQLRepartitions(): void {
|
|
$dsn = getenv('FRESHRSS_TEST_PGSQL_DSN');
|
|
if ($dsn === false || $dsn === '') {
|
|
self::markTestSkipped('FRESHRSS_TEST_PGSQL_DSN is not configured.');
|
|
}
|
|
$pdo = new Minz_PdoPgsql($dsn, null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
|
|
$prefix = 'stats_test_' . getmypid() . '_';
|
|
$pdo->setPrefix($prefix);
|
|
$pdo->exec("CREATE TABLE {$prefix}entry (id BIGSERIAL PRIMARY KEY, date BIGINT)");
|
|
try {
|
|
$dao = new FreshRSS_StatsDAOPGSQL('test', $pdo);
|
|
$pdo->exec("INSERT INTO {$prefix}entry(date) VALUES (" . mktime(12, 0, 0, 1, 7, 2024) . ')'); // 2024-01-07 is a Sunday
|
|
$pdo->exec("INSERT INTO {$prefix}entry(date) VALUES (" . mktime(12, 0, 0, 12, 15, 2024) . ')');
|
|
|
|
$dayOfWeek = $dao->calculateEntryRepartitionPerFeedPerDayOfWeek();
|
|
self::assertCount(7, $dayOfWeek);
|
|
self::assertSame(2, $dayOfWeek[0]); // Both 2024-01-07 and 2024-12-15 are Sundays
|
|
self::assertSame(2, array_sum($dayOfWeek));
|
|
|
|
$months = $dao->calculateEntryRepartitionPerFeedPerMonth();
|
|
self::assertCount(12, $months);
|
|
self::assertSame(1, $months[0]); // January
|
|
self::assertSame(1, $months[11]); // December
|
|
|
|
$midnight = mktime(0, 0, 0) ?: 0;
|
|
for ($day = 0; $day <= 30; $day++) {
|
|
$pdo->exec("INSERT INTO {$prefix}entry(date) VALUES (" . ($midnight - $day * 86400 + 12 * 3600) . ')');
|
|
}
|
|
self::assertSame(array_fill(0, 31, 1), $dao->calculateEntryCount());
|
|
} finally {
|
|
$pdo->exec("DROP TABLE {$prefix}entry");
|
|
}
|
|
}
|
|
|
|
public static function testCalculateEntryAverageFromRepartition(): void {
|
|
self::assertSame(2.0, FreshRSS_StatsDAO::calculateEntryAverageFromRepartition([0, 2, 4]));
|
|
}
|
|
|
|
public static function testCalculateEntryAverageFromEmptyRepartition(): void {
|
|
self::assertSame(0.0, FreshRSS_StatsDAO::calculateEntryAverageFromRepartition([]));
|
|
}
|
|
|
|
public static function testCalculateEntryAverageFromZeroRepartition(): void {
|
|
self::assertSame(0.0, FreshRSS_StatsDAO::calculateEntryAverageFromRepartition(array_fill(0, 24, 0)));
|
|
}
|
|
}
|