packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/WindowFrameTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\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\Rendering\Lexical;
14use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
15use SqlSemantics\Platform\PostgreSql\Platform;
16use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
17use SqlSemantics\Platform\PostgreSql\Statement\Invocation\FrameBound;
18use SqlSemantics\Platform\PostgreSql\Statement\Invocation\FrameBoundKind;
19use SqlSemantics\Platform\PostgreSql\Statement\Invocation\FrameExclusion;
20use SqlSemantics\Platform\PostgreSql\Statement\Invocation\FrameMode;
21use SqlSemantics\Platform\PostgreSql\Statement\Invocation\WindowFrame;
22use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
24
25#[CoversClass(WindowFrame::class)]
26#[Small]
27final class WindowFrameTest extends TestCase
28{
29 public function testDeriveClauseDerivesTheOffsetsOfBothBounds(): void
30 {
31 $scalar = new Constant(new IntegerConstant('3'));
32 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
33 (new WindowFrame(FrameMode::Rows, new FrameBound(FrameBoundKind::CurrentRow), new FrameBound(FrameBoundKind::OffsetFollowing, $scalar)))->deriveClause($derivation, $derivation->environment());
34 self::assertTrue($derivation->facts()->covers($scalar));
35 }
36
37 public function testRenderWritesBetweenForTwoBounds(): void
38 {
39 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
40 (new WindowFrame(FrameMode::Groups, new FrameBound(FrameBoundKind::OffsetPreceding, new Constant(new IntegerConstant('1'))), new FrameBound(FrameBoundKind::CurrentRow), FrameExclusion::Ties))->render($out);
41 self::assertSame('GROUPS BETWEEN 1 PRECEDING AND CURRENT ROW EXCLUDE TIES', (new Lexical())->join($out->pieces()));
42 }
43
44 public function testRenderWritesAStartAlone(): void
45 {
46 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
47 (new WindowFrame(FrameMode::Range, new FrameBound(FrameBoundKind::UnboundedPreceding), null, FrameExclusion::CurrentRow))->render($out);
48 self::assertSame('RANGE UNBOUNDED PRECEDING EXCLUDE CURRENT ROW', (new Lexical())->join($out->pieces()));
49 }
50
51 public function testRejectsAnEndAtUnboundedPreceding(): void
52 {
53 $this->expectExceptionMessage('A frame does not end at UNBOUNDED PRECEDING.');
54 new WindowFrame(FrameMode::Rows, new FrameBound(FrameBoundKind::CurrentRow), new FrameBound(FrameBoundKind::UnboundedPreceding));
55 }
56
57 public function testRejectsAStartAfterTheCurrentRowWithoutAnEnd(): void
58 {
59 $this->expectExceptionMessage('A frame starting after the current row does not end at or before it.');
60 new WindowFrame(FrameMode::Rows, new FrameBound(FrameBoundKind::OffsetFollowing, new Constant(new IntegerConstant('1'))));
61 }
62}
63