|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit\Adapter; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use PDO; |
| 7 | +use PDOStatement; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Utopia\Database\Adapter\MariaDB; |
| 11 | +use Utopia\Database\Adapter\MySQL; |
| 12 | +use Utopia\Database\Adapter\Postgres; |
| 13 | +use Utopia\Database\Adapter\SQL; |
| 14 | +use Utopia\Database\Attribute; |
| 15 | +use Utopia\Database\Collection; |
| 16 | +use Utopia\Database\Query; |
| 17 | +use Utopia\Database\Validator\Authorization; |
| 18 | + |
| 19 | +/** |
| 20 | + * MariaDB and MySQL send a double sum that overflowed as `0`, so sum() over grouped or distinct rows adds them up |
| 21 | + * through the engine's range check, as it does over documents. |
| 22 | + */ |
| 23 | +final class RowSumStatementTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @return iterable<string, array{Closure(PDO): SQL, string}> |
| 27 | + */ |
| 28 | + public static function adapters(): iterable |
| 29 | + { |
| 30 | + yield 'mariadb' => [static fn (PDO $pdo): SQL => new MariaDB($pdo), 'SUM(`reading`) + 0 AS `sum`']; |
| 31 | + yield 'mysql' => [static fn (PDO $pdo): SQL => new MySQL($pdo), 'SUM(`reading`) + 0 AS `sum`']; |
| 32 | + yield 'postgres' => [static fn (PDO $pdo): SQL => new Postgres($pdo), 'SUM("reading") AS "sum"']; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @return iterable<string, array{list<Query>}> |
| 37 | + */ |
| 38 | + public static function shapes(): iterable |
| 39 | + { |
| 40 | + yield 'groupBy' => [[Query::count('*', 'n'), Query::groupBy(['reading'])]]; |
| 41 | + yield 'distinct' => [[Query::distinct(), Query::select(['reading'])]]; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @return iterable<string, array{Closure(PDO): SQL, string, list<Query>}> |
| 46 | + */ |
| 47 | + public static function sums(): iterable |
| 48 | + { |
| 49 | + foreach (self::adapters() as $adapter => [$create, $selection]) { |
| 50 | + foreach (self::shapes() as $shape => [$queries]) { |
| 51 | + yield "{$shape} on {$adapter}" => [$create, $selection, $queries]; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param Closure(PDO): SQL $create |
| 58 | + * @param list<Query> $queries |
| 59 | + */ |
| 60 | + #[DataProvider('sums')] |
| 61 | + public function testTheRowsAreAddedUpThroughTheRangeCheck(Closure $create, string $selection, array $queries): void |
| 62 | + { |
| 63 | + $statements = []; |
| 64 | + $pdo = $this->createStub(PDO::class); |
| 65 | + $pdo->method('prepare')->willReturnCallback(function (string $sql) use (&$statements): PDOStatement { |
| 66 | + $statements[] = $sql; |
| 67 | + $statement = $this->createStub(PDOStatement::class); |
| 68 | + $statement->method('execute')->willReturn(true); |
| 69 | + $statement->method('fetchAll')->willReturn([['sum' => '3']]); |
| 70 | + $statement->method('closeCursor')->willReturn(true); |
| 71 | + |
| 72 | + return $statement; |
| 73 | + }); |
| 74 | + |
| 75 | + $adapter = $create($pdo); |
| 76 | + $adapter->setDatabase('database'); |
| 77 | + $adapter->setNamespace('namespace'); |
| 78 | + $adapter->setAuthorization(new Authorization()); |
| 79 | + |
| 80 | + $collection = Collection::create(id: 'readings', attributes: [Attribute::double(key: 'reading', required: true)]); |
| 81 | + |
| 82 | + $this->assertSame(3, $adapter->sum($collection, 'reading', $queries)); |
| 83 | + $this->assertCount(1, $statements); |
| 84 | + $this->assertStringStartsWith('SELECT '.$selection.' FROM (', $statements[0]); |
| 85 | + } |
| 86 | +} |
0 commit comments