packages/sql-semantics-sqlite/tests/Unit/Statement/Schema/Column/DefaultLiteralTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Schema\Column;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
11use SqlSemantics\Platform\Sqlite\Semantics;
12use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\CurrentTime;
13use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\IntegerLiteral;
14use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\DefaultLiteral;
15use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateTable;
16use SqlSemantics\Platform\Sqlite\Statement\Type\NumberSign;
17use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
18
19#[CoversClass(DefaultLiteral::class)]
20#[Medium]
21final class DefaultLiteralTest extends TestCase
22{
23 public function testDeriveConstraintDerivesTheLiteral(): void
24 {
25 $operation = (new Semantics())->analyze('CREATE TABLE t (a DEFAULT 5)', []);
26 $statement = $operation->statement;
27
28 self::assertInstanceOf(CreateTable::class, $statement);
29 $default = $statement->columns[0]->constraints[0];
30 self::assertInstanceOf(DefaultLiteral::class, $default);
31 self::assertInstanceOf(IntegerLiteral::class, $default->literal);
32 self::assertEquals(new Known(Storage::Integer), $operation->facts->scalar($default->literal)->type);
33 self::assertSame([], $operation->facts->diagnostics);
34 }
35
36 public function testRenderKeepsTheWrittenSign(): void
37 {
38 $semantics = new Semantics();
39 $negative = $semantics->analyze('create table t (a default -1.5)');
40 $positive = $semantics->analyze("create table t (a default +'x')");
41 $statement = $negative->statement;
42 $other = $positive->statement;
43
44 self::assertInstanceOf(CreateTable::class, $statement);
45 self::assertInstanceOf(CreateTable::class, $other);
46 self::assertInstanceOf(DefaultLiteral::class, $statement->columns[0]->constraints[0]);
47 self::assertInstanceOf(DefaultLiteral::class, $other->columns[0]->constraints[0]);
48 self::assertSame(NumberSign::Minus, $statement->columns[0]->constraints[0]->sign);
49 self::assertSame(NumberSign::Plus, $other->columns[0]->constraints[0]->sign);
50 self::assertSame('CREATE TABLE t (a DEFAULT - 1.5)', $negative->toString());
51 self::assertSame("CREATE TABLE t (a DEFAULT + 'x')", $positive->toString());
52 }
53
54 public function testRenderWritesEveryKindOfLiteral(): void
55 {
56 $operation = (new Semantics())->analyze("CREATE TABLE t (a DEFAULT NULL, b DEFAULT 'x', c DEFAULT x'0A', d DEFAULT CURRENT_TIMESTAMP)");
57 $statement = $operation->statement;
58
59 self::assertInstanceOf(CreateTable::class, $statement);
60 self::assertInstanceOf(DefaultLiteral::class, $statement->columns[3]->constraints[0]);
61 self::assertInstanceOf(CurrentTime::class, $statement->columns[3]->constraints[0]->literal);
62 self::assertSame("CREATE TABLE t (a DEFAULT NULL, b DEFAULT 'x', c DEFAULT x'0A', d DEFAULT CURRENT_TIMESTAMP)", $operation->toString());
63 }
64}
65