|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Unit; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use PDO; |
| 7 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Utopia\Cache\Adapter\None as NoCache; |
| 10 | +use Utopia\Cache\Cache; |
| 11 | +use Utopia\Database\Adapter; |
| 12 | +use Utopia\Database\Adapter\Memory; |
| 13 | +use Utopia\Database\Adapter\SQLite; |
| 14 | +use Utopia\Database\Attribute; |
| 15 | +use Utopia\Database\Collection; |
| 16 | +use Utopia\Database\Database; |
| 17 | +use Utopia\Database\Document; |
| 18 | +use Utopia\Database\Exception\Limit as LimitException; |
| 19 | +use Utopia\Database\Hook\Permissions; |
| 20 | +use Utopia\Database\Permission; |
| 21 | +use Utopia\Database\Query; |
| 22 | +use Utopia\Database\Role; |
| 23 | +use Utopia\Database\Validator\Authorization; |
| 24 | + |
| 25 | +/** |
| 26 | + * SQLite and the adapters that sum in PHP answer a double sum that overflowed with infinity, which SQLite sends as |
| 27 | + * `INF` when it stringifies. sum() refuses it with the Limit error MariaDB, MySQL and PostgreSQL raise for it. |
| 28 | + */ |
| 29 | +final class SumOverflowTest extends TestCase |
| 30 | +{ |
| 31 | + private const string COLLECTION = 'readings'; |
| 32 | + |
| 33 | + /** |
| 34 | + * @return iterable<string, array{Closure(): Adapter}> |
| 35 | + */ |
| 36 | + public static function adapters(): iterable |
| 37 | + { |
| 38 | + yield 'sqlite' => [static fn (): Adapter => new SQLite(new PDO('sqlite::memory:', null, null, [PDO::ATTR_STRINGIFY_FETCHES => false]))]; |
| 39 | + yield 'sqlite stringified' => [static fn (): Adapter => new SQLite(new PDO('sqlite::memory:', null, null, [PDO::ATTR_STRINGIFY_FETCHES => true]))]; |
| 40 | + yield 'memory' => [static fn (): Adapter => new Memory()]; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @param Closure(): Adapter $adapter |
| 45 | + * @param list<float> $readings |
| 46 | + */ |
| 47 | + private function database(Closure $adapter, array $readings): Database |
| 48 | + { |
| 49 | + $database = new Database($adapter(), new Cache(new NoCache())); |
| 50 | + $database |
| 51 | + ->setDatabase('sum_overflow') |
| 52 | + ->setNamespace('sum_overflow_'.\uniqid()) |
| 53 | + ->setAuthorization(new Authorization()); |
| 54 | + $database->addHook(new Permissions()); |
| 55 | + $database->create(); |
| 56 | + |
| 57 | + $database->createCollection(Collection::create( |
| 58 | + id: self::COLLECTION, |
| 59 | + attributes: [Attribute::double(key: 'reading', required: true)], |
| 60 | + permissions: [Permission::create(Role::any()), Permission::read(Role::any())], |
| 61 | + )); |
| 62 | + |
| 63 | + foreach ($readings as $reading) { |
| 64 | + $database->createDocument(self::COLLECTION, new Document([ |
| 65 | + 'reading' => $reading, |
| 66 | + '$permissions' => [Permission::read(Role::any())], |
| 67 | + ])); |
| 68 | + } |
| 69 | + |
| 70 | + return $database; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param Closure(): Adapter $adapter |
| 75 | + */ |
| 76 | + #[DataProvider('adapters')] |
| 77 | + public function testASumThatOverflowsIsRefused(Closure $adapter): void |
| 78 | + { |
| 79 | + $database = $this->database($adapter, [1e308, 1e308]); |
| 80 | + |
| 81 | + $this->expectException(LimitException::class); |
| 82 | + $database->sum(self::COLLECTION, 'reading'); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @param Closure(): Adapter $adapter |
| 87 | + */ |
| 88 | + #[DataProvider('adapters')] |
| 89 | + public function testASumThatOverflowsUnderFiltersIsRefused(Closure $adapter): void |
| 90 | + { |
| 91 | + $database = $this->database($adapter, [1e308, 1e308, -1.0]); |
| 92 | + |
| 93 | + $this->expectException(LimitException::class); |
| 94 | + $database->sum(self::COLLECTION, 'reading', [Query::greaterThan('reading', 0)], 10); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @param Closure(): Adapter $adapter |
| 99 | + */ |
| 100 | + #[DataProvider('adapters')] |
| 101 | + public function testTheLargestFiniteSumIsStillAnswered(Closure $adapter): void |
| 102 | + { |
| 103 | + $database = $this->database($adapter, [1e308]); |
| 104 | + |
| 105 | + $this->assertSame(1e308, $database->sum(self::COLLECTION, 'reading')); |
| 106 | + } |
| 107 | +} |
0 commit comments