@@ -150,6 +150,7 @@ func Test_convertDBType(t *testing.T) {
150150 "MongoDB" : {input : "MongoDB" , expected : "MONGODB" },
151151 "Redis" : {input : "Redis" , expected : "REDIS" },
152152 "DB2" : {input : "DB2" , expected : "DB2" },
153+ "KingBase" : {input : "KingBase" , expected : "KINGBASE" },
153154 "Unknown passthrough" : {input : "UnknownDB" , expected : "UnknownDB" },
154155 }
155156 for name , tc := range cases {
@@ -183,6 +184,7 @@ func Test_SupportDBType(t *testing.T) {
183184 "GaussDB supported" : {input : pkgConst .DBTypeGaussDB , expected : true },
184185 "GaussDBForMySQL unsupported" : {input : pkgConst .DBTypeGaussDBForMySQL , expected : false },
185186 "DB2 unsupported" : {input : pkgConst .DBTypeDB2 , expected : false },
187+ "KingBase supported" : {input : pkgConst .DBTypeKingBase , expected : true },
186188 "empty string unsupported" : {input : pkgConst .DBType ("" ), expected : false },
187189 "unknown type unsupported" : {input : pkgConst .DBType ("UnknownDBType" ), expected : false },
188190 }
@@ -390,6 +392,89 @@ func Test_buildDatasourceBaseInfo_DB2(t *testing.T) {
390392 }
391393}
392394
395+ // Test_buildDatasourceBaseInfo_KingBase 覆盖 KingBase → defaultSchema 契约(S1 / AC-1):
396+ //
397+ // (a) 正例:database_name=test → DefaultSchema=="test" 且 Type 经 convert 为 KINGBASE
398+ // (b) 负例:缺 database_name → err 含 "database_name"
399+ // (c) MySQL 回归:DefaultSchema == nil
400+ func Test_buildDatasourceBaseInfo_KingBase (t * testing.T ) {
401+ svc := & SqlWorkbenchService {}
402+ const envID = int64 (1 )
403+ const datasourceName = "proj:kingbase_odc_test"
404+
405+ cases := map [string ]struct {
406+ dbService * biz.DBService
407+ expectErr bool
408+ expectErrSubstr string
409+ expectDefaultSchema * string
410+ expectType string
411+ }{
412+ "KingBase happy path" : {
413+ dbService : & biz.DBService {
414+ Name : "kingbase_odc_test" ,
415+ DBType : string (pkgConst .DBTypeKingBase ),
416+ Host : "10.186.16.126" ,
417+ Port : "1522" ,
418+ User : "kb_dev" ,
419+ AdditionalParams : pkgParams.Params {
420+ {Key : "database_name" , Value : "test" },
421+ },
422+ },
423+ expectErr : false ,
424+ expectDefaultSchema : strPtr ("test" ),
425+ expectType : "KINGBASE" ,
426+ },
427+ "KingBase missing database_name" : {
428+ dbService : & biz.DBService {
429+ Name : "kingbase-missing-db" ,
430+ DBType : string (pkgConst .DBTypeKingBase ),
431+ AdditionalParams : pkgParams.Params {},
432+ },
433+ expectErr : true ,
434+ expectErrSubstr : "database_name" ,
435+ },
436+ "MySQL regression still no DefaultSchema" : {
437+ dbService : & biz.DBService {
438+ Name : "mysql-1" ,
439+ DBType : "MySQL" ,
440+ AdditionalParams : pkgParams.Params {},
441+ },
442+ expectErr : false ,
443+ expectDefaultSchema : nil ,
444+ expectType : "MYSQL" ,
445+ },
446+ }
447+
448+ for name , tc := range cases {
449+ t .Run (name , func (t * testing.T ) {
450+ got , err := svc .fillDatasourceBaseInfo (datasourceName , tc .dbService , envID )
451+ if tc .expectErr {
452+ if err == nil {
453+ t .Fatalf ("expected error, got nil; baseInfo=%+v" , got )
454+ }
455+ if tc .expectErrSubstr != "" && ! strings .Contains (err .Error (), tc .expectErrSubstr ) {
456+ t .Errorf ("error %q does not contain %q" , err .Error (), tc .expectErrSubstr )
457+ }
458+ return
459+ }
460+ if err != nil {
461+ t .Fatalf ("unexpected error: %v" , err )
462+ }
463+ if got == nil {
464+ t .Fatalf ("expected non-nil baseInfo" )
465+ }
466+ if tc .expectType != "" && got .Type != tc .expectType {
467+ t .Errorf ("Type = %q, want %q" , got .Type , tc .expectType )
468+ }
469+ if (got .DefaultSchema == nil ) != (tc .expectDefaultSchema == nil ) {
470+ t .Errorf ("DefaultSchema nil mismatch: got=%v, want=%v" , got .DefaultSchema , tc .expectDefaultSchema )
471+ } else if got .DefaultSchema != nil && tc .expectDefaultSchema != nil && * got .DefaultSchema != * tc .expectDefaultSchema {
472+ t .Errorf ("DefaultSchema = %q, want %q" , * got .DefaultSchema , * tc .expectDefaultSchema )
473+ }
474+ })
475+ }
476+ }
477+
393478func strPtr (s string ) * string {
394479 return & s
395480}
0 commit comments