github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/sys/sqlquery/utils_test.go (about) 1 /* 2 * Copyright (c) 2024-present Sigma-Soft, Ltd. 3 * @author: Nikolay Nikitin 4 */ 5 6 package sqlquery 7 8 import ( 9 "reflect" 10 "testing" 11 12 "github.com/voedger/voedger/pkg/istructs" 13 ) 14 15 func Test_parseQueryAppWs(t *testing.T) { 16 type args struct { 17 query string 18 } 19 tests := []struct { 20 name string 21 args args 22 wantApp istructs.AppQName 23 wantWs istructs.WSID 24 wantClean string 25 wantErr bool 26 }{ 27 {"fail: empty", args{""}, istructs.NullAppQName, 0, "", true}, 28 {"fail: no table", args{"select * from"}, istructs.NullAppQName, 0, "", true}, 29 {"fail: missed from", args{"select * table"}, istructs.NullAppQName, 0, "", true}, 30 31 {"OK:", args{"select * from pkg.table"}, istructs.NullAppQName, 0, "select * from pkg.table", false}, 32 {"OK:", args{"select * from owner.app.pkg.table"}, istructs.MustParseAppQName("owner/app"), 0, "select * from pkg.table", false}, 33 {"OK:", args{"select * from owner.app.123.pkg.table"}, istructs.MustParseAppQName("owner/app"), 123, "select * from pkg.table", false}, 34 {"OK:", args{"select * from 123.pkg.table"}, istructs.NullAppQName, 123, "select * from pkg.table", false}, 35 36 {"OK:", args{"select f1, f2 from pkg.table where f3 is null"}, istructs.NullAppQName, 0, "select f1, f2 from pkg.table where f3 is null", false}, 37 {"OK:", args{"select f1, f2 from owner.app.123.pkg.table where f3 is null"}, istructs.MustParseAppQName("owner/app"), 123, "select f1, f2 from pkg.table where f3 is null", false}, 38 39 {"fail: invalid app name", args{"select * from naked.🔫.pkg.table"}, istructs.NullAppQName, 0, "", true}, 40 {"fail: invalid table name", args{"select * from naked.🔫"}, istructs.NullAppQName, 0, "", true}, 41 {"fail: invalid (unqualified) table name", args{"select * from table"}, istructs.NullAppQName, 0, "", true}, 42 {"fail: invalid ws", args{"select -123.pkg.table"}, istructs.NullAppQName, 0, "", true}, 43 {"fail: invalid app or ws", args{"select owner.app.ooo.pkg.table"}, istructs.NullAppQName, 0, "", true}, 44 } 45 for _, tt := range tests { 46 t.Run(tt.name, func(t *testing.T) { 47 gotApp, gotWs, gotClean, err := parseQueryAppWs(tt.args.query) 48 if (err != nil) != tt.wantErr { 49 t.Errorf("parseQueryAppWs() error = %v, wantErr %v", err, tt.wantErr) 50 return 51 } 52 if !reflect.DeepEqual(gotApp, tt.wantApp) { 53 t.Errorf("parseQueryAppWs() gotApp = %v, want %v", gotApp, tt.wantApp) 54 } 55 if !reflect.DeepEqual(gotWs, tt.wantWs) { 56 t.Errorf("parseQueryAppWs() gotWs = %v, want %v", gotWs, tt.wantWs) 57 } 58 if gotClean != tt.wantClean { 59 t.Errorf("parseQueryAppWs() gotClean = %v, want %v", gotClean, tt.wantClean) 60 } 61 }) 62 } 63 }