@@ -3,7 +3,9 @@ package sql_workbench
33import (
44 "testing"
55
6+ "github.com/actiontech/dms/internal/dms/biz"
67 pkgConst "github.com/actiontech/dms/internal/dms/pkg/constant"
8+ pkgParams "github.com/actiontech/dms/pkg/params"
79)
810
911func Test_convertDBType (t * testing.T ) {
@@ -12,17 +14,18 @@ func Test_convertDBType(t *testing.T) {
1214 input string
1315 expected string
1416 }{
15- "DM" : {input : "达梦(DM)" , expected : "DM" },
16- "MySQL" : {input : "MySQL" , expected : "MYSQL" },
17- "PostgreSQL" : {input : "PostgreSQL" , expected : "POSTGRESQL" },
18- "Oracle" : {input : "Oracle" , expected : "ORACLE" },
19- "SQL Server" : {input : "SQL Server" , expected : "SQL_SERVER" },
20- "OB Oracle" : {input : "OceanBase For Oracle" , expected : "OB_ORACLE" },
21- "OB MySQL" : {input : "OceanBase For MySQL" , expected : "OB_MYSQL" },
17+ "DM" : {input : "达梦(DM)" , expected : "DM" },
18+ "MySQL" : {input : "MySQL" , expected : "MYSQL" },
19+ "PostgreSQL" : {input : "PostgreSQL" , expected : "POSTGRESQL" },
20+ "Oracle" : {input : "Oracle" , expected : "ORACLE" },
21+ "SQL Server" : {input : "SQL Server" , expected : "SQL_SERVER" },
22+ "OB Oracle" : {input : "OceanBase For Oracle" , expected : "OB_ORACLE" },
23+ "OB MySQL" : {input : "OceanBase For MySQL" , expected : "OB_MYSQL" },
2224 "TiDB" : {input : "TiDB" , expected : "TIDB" },
23- "TDSQL For InnoDB" : {input : "TDSQL For InnoDB" , expected : "MYSQL" },
25+ "TDSQL For InnoDB" : {input : "TDSQL For InnoDB" , expected : "MYSQL" },
2426 "GoldenDB" : {input : "GoldenDB" , expected : "MYSQL" },
2527 "PolarDB For MySQL" : {input : "PolarDB For MySQL" , expected : "MYSQL" },
28+ "MongoDB" : {input : "MongoDB" , expected : "MONGODB" },
2629 "Unknown passthrough" : {input : "UnknownDB" , expected : "UnknownDB" },
2730 }
2831 for name , tc := range cases {
@@ -41,16 +44,17 @@ func Test_SupportDBType(t *testing.T) {
4144 input pkgConst.DBType
4245 expected bool
4346 }{
44- "DM supported" : {input : pkgConst .DBTypeDM , expected : true },
45- "MySQL supported" : {input : pkgConst .DBTypeMySQL , expected : true },
46- "Oracle supported" : {input : pkgConst .DBTypeOracle , expected : true },
47- "OB MySQL supported" : {input : pkgConst .DBTypeOceanBaseMySQL , expected : true },
48- "TiDB supported" : {input : pkgConst .DBTypeTiDB , expected : true },
49- "TDSQL supported" : {input : pkgConst .DBTypeTDSQLForInnoDB , expected : true },
50- "GoldenDB supported" : {input : pkgConst .DBTypeGoldenDB , expected : true },
51- "PostgreSQL unsupported" : {input : pkgConst .DBTypePostgreSQL , expected : false },
52- "SQL Server unsupported" : {input : pkgConst .DBTypeSQLServer , expected : false },
53- "PolarDB For MySQL supported" : {input : pkgConst .DBTypePolarDBForMySQL , expected : true },
47+ "DM supported" : {input : pkgConst .DBTypeDM , expected : true },
48+ "MySQL supported" : {input : pkgConst .DBTypeMySQL , expected : true },
49+ "Oracle supported" : {input : pkgConst .DBTypeOracle , expected : true },
50+ "OB MySQL supported" : {input : pkgConst .DBTypeOceanBaseMySQL , expected : true },
51+ "TiDB supported" : {input : pkgConst .DBTypeTiDB , expected : true },
52+ "TDSQL supported" : {input : pkgConst .DBTypeTDSQLForInnoDB , expected : true },
53+ "GoldenDB supported" : {input : pkgConst .DBTypeGoldenDB , expected : true },
54+ "MongoDB unsupported" : {input : pkgConst .DBTypeMongoDB , expected : false },
55+ "PostgreSQL unsupported" : {input : pkgConst .DBTypePostgreSQL , expected : false },
56+ "SQL Server unsupported" : {input : pkgConst .DBTypeSQLServer , expected : false },
57+ "PolarDB For MySQL supported" : {input : pkgConst .DBTypePolarDBForMySQL , expected : true },
5458 }
5559 for name , tc := range cases {
5660 t .Run (name , func (t * testing.T ) {
@@ -61,3 +65,58 @@ func Test_SupportDBType(t *testing.T) {
6165 })
6266 }
6367}
68+
69+ func Test_buildMongoDatasourceOptions (t * testing.T ) {
70+ defaultDB := "appdb"
71+ defaultSchema , propertiesValue , jdbcParams := buildMongoDatasourceOptions (& biz.DBService {
72+ DBType : string (pkgConst .DBTypeMongoDB ),
73+ Host : "127.0.0.1" ,
74+ Port : "27017" ,
75+ AdditionalParams : pkgParams.Params {
76+ & pkgParams.Param {Key : mongoDefaultDatabaseParam , Value : defaultDB , Type : pkgParams .ParamTypeString },
77+ & pkgParams.Param {Key : mongoAuthDatabaseParam , Value : "admin" , Type : pkgParams .ParamTypeString },
78+ & pkgParams.Param {Key : mongoAuthMechanismParam , Value : "SCRAM-SHA-256" , Type : pkgParams .ParamTypeString },
79+ & pkgParams.Param {Key : mongoReplicaSetParam , Value : "rs0" , Type : pkgParams .ParamTypeString },
80+ & pkgParams.Param {Key : mongoTLSEnabledParam , Value : "true" , Type : pkgParams .ParamTypeBool },
81+ & pkgParams.Param {Key : mongoDirectConnectionParam , Value : "true" , Type : pkgParams .ParamTypeBool },
82+ & pkgParams.Param {Key : mongoTLSSkipVerifyParam , Value : "true" , Type : pkgParams .ParamTypeBool },
83+ },
84+ })
85+ if defaultSchema == nil || * defaultSchema != defaultDB {
86+ t .Fatalf ("unexpected default schema: %#v" , defaultSchema )
87+ }
88+ if propertiesValue != nil {
89+ t .Fatalf ("expected nil properties, got %#v" , propertiesValue )
90+ }
91+ if jdbcParams ["authSource" ] != "admin" {
92+ t .Fatalf ("unexpected authSource: %#v" , jdbcParams ["authSource" ])
93+ }
94+ if jdbcParams ["authMechanism" ] != "SCRAM-SHA-256" {
95+ t .Fatalf ("unexpected authMechanism: %#v" , jdbcParams ["authMechanism" ])
96+ }
97+ if jdbcParams ["replicaSet" ] != "rs0" {
98+ t .Fatalf ("unexpected replicaSet: %#v" , jdbcParams ["replicaSet" ])
99+ }
100+ if jdbcParams ["tls" ] != "true" {
101+ t .Fatalf ("unexpected tls: %#v" , jdbcParams ["tls" ])
102+ }
103+ if jdbcParams ["directConnection" ] != true || jdbcParams ["tlsInsecure" ] != true {
104+ t .Fatalf ("unexpected jdbc params: %#v" , jdbcParams )
105+ }
106+ }
107+
108+ func Test_buildMongoDatasourceOptions_tlsOnly (t * testing.T ) {
109+ _ , propertiesValue , jdbcParams := buildMongoDatasourceOptions (& biz.DBService {
110+ DBType : string (pkgConst .DBTypeMongoDB ),
111+ AdditionalParams : pkgParams.Params {
112+ & pkgParams.Param {Key : mongoTLSEnabledParam , Value : "true" , Type : pkgParams .ParamTypeBool },
113+ },
114+ })
115+ if propertiesValue != nil {
116+ t .Fatalf ("expected nil properties, got %#v" , propertiesValue )
117+ }
118+ if jdbcParams ["tls" ] != "true" {
119+ t .Fatalf ("expected tls in jdbcUrlParameters when only tls is configured, got %#v" , jdbcParams )
120+ }
121+ }
122+
0 commit comments