TestParser_CastExpression_WithPrecision tests CAST with type precision
(t *testing.T)
| 79 | |
| 80 | // TestParser_CastExpression_WithPrecision tests CAST with type precision |
| 81 | func TestParser_CastExpression_WithPrecision(t *testing.T) { |
| 82 | // SELECT CAST(price AS DECIMAL(10,2)) FROM products |
| 83 | tokens := []token.Token{ |
| 84 | {Type: models.TokenTypeSelect, Literal: "SELECT"}, |
| 85 | {Type: models.TokenTypeCast, Literal: "CAST"}, |
| 86 | {Type: models.TokenTypeLParen, Literal: "("}, |
| 87 | {Type: models.TokenTypeIdentifier, Literal: "price"}, |
| 88 | {Type: models.TokenTypeAs, Literal: "AS"}, |
| 89 | {Type: models.TokenTypeIdentifier, Literal: "DECIMAL"}, |
| 90 | {Type: models.TokenTypeLParen, Literal: "("}, |
| 91 | {Type: models.TokenTypeNumber, Literal: "10"}, |
| 92 | {Type: models.TokenTypeComma, Literal: ","}, |
| 93 | {Type: models.TokenTypeNumber, Literal: "2"}, |
| 94 | {Type: models.TokenTypeRParen, Literal: ")"}, |
| 95 | {Type: models.TokenTypeRParen, Literal: ")"}, |
| 96 | {Type: models.TokenTypeFrom, Literal: "FROM"}, |
| 97 | {Type: models.TokenTypeIdentifier, Literal: "products"}, |
| 98 | } |
| 99 | |
| 100 | parser := NewParser() |
| 101 | defer parser.Release() |
| 102 | |
| 103 | tree, err := parser.Parse(tokens) |
| 104 | if err != nil { |
| 105 | t.Fatalf("unexpected error: %v", err) |
| 106 | } |
| 107 | defer ast.ReleaseAST(tree) |
| 108 | |
| 109 | stmt := tree.Statements[0].(*ast.SelectStatement) |
| 110 | castExpr := stmt.Columns[0].(*ast.CastExpression) |
| 111 | |
| 112 | if castExpr.Type != "DECIMAL(10,2)" { |
| 113 | t.Errorf("expected type DECIMAL(10,2), got %s", castExpr.Type) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // TestParser_CastExpression_VarcharWithLength tests CAST to VARCHAR with length |
| 118 | func TestParser_CastExpression_VarcharWithLength(t *testing.T) { |