|
| 1 | +package ydb_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 9 | + "github.com/sqlc-dev/sqlc/internal/engine/ydb" |
| 10 | + "github.com/sqlc-dev/sqlc/internal/sql/ast" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAlterUser(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + stmt string |
| 16 | + expected ast.Node |
| 17 | + }{ |
| 18 | + { |
| 19 | + stmt: `ALTER USER alice RENAME TO queen`, |
| 20 | + expected: &ast.Statement{ |
| 21 | + Raw: &ast.RawStmt{ |
| 22 | + Stmt: &ast.AlterRoleStmt{ |
| 23 | + Role: &ast.RoleSpec{ |
| 24 | + Rolename: strPtr("alice"), |
| 25 | + Roletype: ast.RoleSpecType(1), |
| 26 | + }, |
| 27 | + Action: 1, |
| 28 | + Options: &ast.List{ |
| 29 | + Items: []ast.Node{ |
| 30 | + &ast.DefElem{ |
| 31 | + Defname: strPtr("rename"), |
| 32 | + Arg: &ast.String{Str: "queen"}, |
| 33 | + Defaction: ast.DefElemAction(1), |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + stmt: `ALTER USER bob LOGIN`, |
| 43 | + expected: &ast.Statement{ |
| 44 | + Raw: &ast.RawStmt{ |
| 45 | + Stmt: &ast.AlterRoleStmt{ |
| 46 | + Role: &ast.RoleSpec{ |
| 47 | + Rolename: strPtr("bob"), |
| 48 | + Roletype: ast.RoleSpecType(1), |
| 49 | + }, |
| 50 | + Action: 1, |
| 51 | + Options: &ast.List{ |
| 52 | + Items: []ast.Node{ |
| 53 | + &ast.DefElem{ |
| 54 | + Defname: strPtr("login"), |
| 55 | + Arg: &ast.Boolean{Boolval: true}, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + stmt: `ALTER USER charlie NOLOGIN`, |
| 65 | + expected: &ast.Statement{ |
| 66 | + Raw: &ast.RawStmt{ |
| 67 | + Stmt: &ast.AlterRoleStmt{ |
| 68 | + Role: &ast.RoleSpec{ |
| 69 | + Rolename: strPtr("charlie"), |
| 70 | + Roletype: ast.RoleSpecType(1), |
| 71 | + }, |
| 72 | + Action: 1, |
| 73 | + Options: &ast.List{ |
| 74 | + Items: []ast.Node{ |
| 75 | + &ast.DefElem{ |
| 76 | + Defname: strPtr("nologin"), |
| 77 | + Arg: &ast.Boolean{Boolval: false}, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + stmt: `ALTER USER dave PASSWORD 'qwerty'`, |
| 87 | + expected: &ast.Statement{ |
| 88 | + Raw: &ast.RawStmt{ |
| 89 | + Stmt: &ast.AlterRoleStmt{ |
| 90 | + Role: &ast.RoleSpec{ |
| 91 | + Rolename: strPtr("dave"), |
| 92 | + Roletype: ast.RoleSpecType(1), |
| 93 | + }, |
| 94 | + Action: 1, |
| 95 | + Options: &ast.List{ |
| 96 | + Items: []ast.Node{ |
| 97 | + &ast.DefElem{ |
| 98 | + Defname: strPtr("password"), |
| 99 | + Arg: &ast.String{Str: "qwerty"}, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + { |
| 108 | + stmt: `ALTER USER elena HASH 'abc123'`, |
| 109 | + expected: &ast.Statement{ |
| 110 | + Raw: &ast.RawStmt{ |
| 111 | + Stmt: &ast.AlterRoleStmt{ |
| 112 | + Role: &ast.RoleSpec{ |
| 113 | + Rolename: strPtr("elena"), |
| 114 | + Roletype: ast.RoleSpecType(1), |
| 115 | + }, |
| 116 | + Action: 1, |
| 117 | + Options: &ast.List{ |
| 118 | + Items: []ast.Node{ |
| 119 | + &ast.DefElem{ |
| 120 | + Defname: strPtr("hash"), |
| 121 | + Arg: &ast.String{Str: "abc123"}, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + p := ydb.NewParser() |
| 132 | + for _, tc := range tests { |
| 133 | + t.Run(tc.stmt, func(t *testing.T) { |
| 134 | + stmts, err := p.Parse(strings.NewReader(tc.stmt)) |
| 135 | + if err != nil { |
| 136 | + t.Fatalf("Ошибка парсинга запроса %q: %v", tc.stmt, err) |
| 137 | + } |
| 138 | + if len(stmts) == 0 { |
| 139 | + t.Fatalf("Запрос %q не распарсен", tc.stmt) |
| 140 | + } |
| 141 | + |
| 142 | + diff := cmp.Diff(tc.expected, &stmts[0], |
| 143 | + cmpopts.IgnoreFields(ast.RawStmt{}, "StmtLocation", "StmtLen"), |
| 144 | + cmpopts.IgnoreFields(ast.DefElem{}, "Location"), |
| 145 | + cmpopts.IgnoreFields(ast.RoleSpec{}, "Location"), |
| 146 | + cmpopts.IgnoreFields(ast.A_Const{}, "Location"), |
| 147 | + ) |
| 148 | + if diff != "" { |
| 149 | + t.Errorf("Несовпадение AST (-ожидалось +получено):\n%s", diff) |
| 150 | + } |
| 151 | + }) |
| 152 | + } |
| 153 | +} |
0 commit comments