packages/sql-semantics-postgres/tests/Unit/Rules/Invocation/WindowChecksTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Invocation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\PostgreSql\Platform;
15use SqlSemantics\Platform\PostgreSql\Rules\Invocation\WindowChecks;
16use SqlSemantics\Platform\PostgreSql\Statement\Invocation\FrameBound;
17use SqlSemantics\Platform\PostgreSql\Statement\Invocation\FrameBoundKind;
18use SqlSemantics\Platform\PostgreSql\Statement\Invocation\FrameMode;
19use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Problem\WindowProblem;
20use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Problem\WindowProblemKind;
21use SqlSemantics\Platform\PostgreSql\Statement\Invocation\WindowFrame;
22use SqlSemantics\Platform\PostgreSql\Statement\Invocation\WindowSpecification;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
25use SqlSemantics\Platform\PostgreSql\Statement\Query\SortItem;
26
27#[CoversClass(WindowChecks::class)]
28#[Small]
29final class WindowChecksTest extends TestCase
30{
31 public function testSpecificationReportsARangeOffsetWithoutASingleOrdering(): void
32 {
33 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
34 (new WindowChecks())->specification($derivation, new WindowSpecification(null, [], [], new WindowFrame(FrameMode::Range, new FrameBound(FrameBoundKind::OffsetPreceding, new Constant(new IntegerConstant('1'))))));
35 self::assertEquals([new WindowProblem(WindowProblemKind::RangeOffsetOrder)], $derivation->facts()->diagnostics);
36 }
37
38 public function testSpecificationReportsGroupsWithoutOrdering(): void
39 {
40 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
41 (new WindowChecks())->specification($derivation, new WindowSpecification(null, [], [], new WindowFrame(FrameMode::Groups, new FrameBound(FrameBoundKind::CurrentRow))));
42 self::assertEquals([new WindowProblem(WindowProblemKind::GroupsWithoutOrder)], $derivation->facts()->diagnostics);
43 }
44
45 public function testSpecificationLeavesARefinedWindowToTheWindowClause(): void
46 {
47 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
48 (new WindowChecks())->specification($derivation, new WindowSpecification(new Name('w'), [], [], new WindowFrame(FrameMode::Groups, new FrameBound(FrameBoundKind::CurrentRow))));
49 (new WindowChecks())->specification($derivation, new WindowSpecification(null, [], [new SortItem(new Constant(new IntegerConstant('1')))], new WindowFrame(FrameMode::Range, new FrameBound(FrameBoundKind::OffsetPreceding, new Constant(new IntegerConstant('1'))))));
50 self::assertSame([], $derivation->facts()->diagnostics);
51 }
52}
53