def test_parse_string_concatenation(self):
     self.assertEqual(
         parse("SELECT 'a' || 'b'"),
         [
             ast.SelectStatement(columns=[
                 ast.Infix("||", ast.String("a"), ast.String("b"))
             ])
         ],
     )
示例#2
0
    def test_parse_create_table_statement_with_simple_check_constraint(self):
        sql = """
        CREATE TABLE people(
          name TEXT CHECK(name != '')
        );
        """

        self.assertEqual(
            parse(sql),
            [
                ast.CreateTableStatement(
                    name="people",
                    columns=[
                        ast.Column(
                            name="name",
                            definition=ast.ColumnDefinition(
                                type="TEXT",
                                constraints=[
                                    ast.CheckConstraint(
                                        ast.Infix(
                                            "!=",
                                            ast.Identifier("name"),
                                            ast.String(""),
                                        ))
                                ],
                            ),
                        ),
                    ],
                ),
            ],
        )
示例#3
0
    def test_parse_column_with_constraints(self):
        self.assertEqual(
            parse_column("id INTEGER PRIMARY KEY"),
            ast.Column(
                name="id",
                definition=ast.ColumnDefinition(
                    type="INTEGER", constraints=[ast.PrimaryKeyConstraint()]),
            ),
        )

        self.assertEqual(
            parse_column("id INTEGER UNIQUE"),
            ast.Column(
                name="id",
                definition=ast.ColumnDefinition(
                    type="INTEGER", constraints=[ast.UniqueConstraint()]),
            ),
        )

        self.assertEqual(
            parse_column("id INTEGER not NULL"),
            ast.Column(
                name="id",
                definition=ast.ColumnDefinition(
                    type="INTEGER", constraints=[ast.NotNullConstraint()]),
            ),
        )

        self.assertEqual(
            parse_column("model INTEGER REFERENCES models"),
            ast.Column(
                name="model",
                definition=ast.ColumnDefinition(
                    type="INTEGER",
                    constraints=[
                        ast.ForeignKeyConstraint(columns=[],
                                                 foreign_table="models",
                                                 foreign_columns=[])
                    ],
                ),
            ),
        )

        self.assertEqual(
            parse_column("name TEXT NOT NULL check(name != '')"),
            ast.Column(
                name="name",
                definition=ast.ColumnDefinition(
                    type="TEXT",
                    constraints=[
                        ast.NotNullConstraint(),
                        ast.CheckConstraint(
                            ast.Infix("!=", ast.Identifier("name"),
                                      ast.String(""))),
                    ],
                ),
            ),
        )
示例#4
0
    def test_parse_create_table_statement_with_default_clause(self):
        sql = """
        CREATE TABLE people(
          name TEXT DEFAULT '',
          age INTEGER DEFAULT ( 2 + 2 ),
          employed BOOLEAN DEFAULT TRUE,
          last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        );
        """

        self.assertEqual(
            parse(sql),
            [
                ast.CreateTableStatement(
                    name="people",
                    columns=[
                        ast.Column(
                            name="name",
                            definition=ast.ColumnDefinition(
                                type="TEXT", default=ast.String("")),
                        ),
                        ast.Column(
                            name="age",
                            definition=ast.ColumnDefinition(
                                type="INTEGER",
                                default=ast.Infix("+", ast.Integer(2),
                                                  ast.Integer(2)),
                            ),
                        ),
                        ast.Column(
                            name="employed",
                            definition=ast.ColumnDefinition(
                                type="BOOLEAN", default=ast.Boolean(True)),
                        ),
                        ast.Column(
                            name="last_updated",
                            definition=ast.ColumnDefinition(
                                type="TIMESTAMP",
                                default=ast.DefaultValue.CURRENT_TIMESTAMP,
                            ),
                        ),
                    ],
                ),
            ],
        )
示例#5
0
 def test_parse_string_literal_with_backslashes_and_single_quotes(self):
     self.assertEqual(
         parse(r"SELECT '\ \n '' '"),
         [ast.SelectStatement(columns=[ast.String("\\ \\n ' ")])],
     )
示例#6
0
 def test_parse_string_literal(self):
     self.assertEqual(
         parse(r"SELECT 'abc'"),
         [ast.SelectStatement(columns=[ast.String("abc")])],
     )