packages/sql-semantics-sqlite/tests/Unit/Statement/Schema/Column/DefaultWordTest.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\Identifier\Name;
11use SqlSemantics\Platform\Sqlite\Semantics;
12use SqlSemantics\Platform\Sqlite\Statement\Lexical\Word;
13use SqlSemantics\Platform\Sqlite\Statement\Lexical\WordQuote;
14use SqlSemantics\Platform\Sqlite\Statement\Schema\Column\DefaultWord;
15use SqlSemantics\Platform\Sqlite\Statement\Schema\CreateTable;
16
17#[CoversClass(DefaultWord::class)]
18#[Medium]
19final class DefaultWordTest extends TestCase
20{
21 public function testTruthReadsAnUnquotedTrueOrFalse(): void
22 {
23 self::assertTrue((new DefaultWord(new Word(new Name('TRUE'))))->truth());
24 self::assertFalse((new DefaultWord(new Word(new Name('false'))))->truth());
25 }
26
27 public function testTruthIsNullForATextDefault(): void
28 {
29 self::assertNull((new DefaultWord(new Word(new Name('unknown'))))->truth());
30 self::assertNull((new DefaultWord(new Word(new Name('true'), WordQuote::Double)))->truth());
31 }
32
33 public function testDeriveConstraintNeverReadsTheWordAsAColumn(): void
34 {
35 $operation = (new Semantics())->analyze('CREATE TABLE t (a, b DEFAULT a)', []);
36
37 self::assertSame([], $operation->facts->diagnostics);
38 }
39
40 public function testRenderKeepsTheQuotingThatDecidesTheMeaning(): void
41 {
42 $semantics = new Semantics();
43 $truth = $semantics->analyze('create table t (a default true)');
44 $text = $semantics->analyze('create table t (a default "true")');
45 $statement = $truth->statement;
46 $other = $text->statement;
47
48 self::assertInstanceOf(CreateTable::class, $statement);
49 self::assertInstanceOf(CreateTable::class, $other);
50 self::assertInstanceOf(DefaultWord::class, $statement->columns[0]->constraints[0]);
51 self::assertInstanceOf(DefaultWord::class, $other->columns[0]->constraints[0]);
52 self::assertTrue($statement->columns[0]->constraints[0]->truth());
53 self::assertNull($other->columns[0]->constraints[0]->truth());
54 self::assertSame('CREATE TABLE t (a DEFAULT true)', $truth->toString());
55 self::assertSame('CREATE TABLE t (a DEFAULT "true")', $text->toString());
56 }
57}
58