@@ -80,6 +80,55 @@ func TestGetRandomServerID(t *testing.T) {
8080 require .NotEqual (t , 101 , serverID )
8181}
8282
83+ func TestGetTables (t * testing.T ) {
84+ t .Parallel ()
85+
86+ db , mock , err := sqlmock .New ()
87+ require .NoError (t , err )
88+
89+ schema := "test_db"
90+ tables := []string {"tbl1" , "tbl2" }
91+
92+ rows := sqlmock .NewRows ([]string {fmt .Sprintf ("Tables_in_%s" , schema ), "Table_type" })
93+ addRowsForTables (rows , tables )
94+ mock .ExpectQuery (fmt .Sprintf ("SHOW FULL TABLES IN `%s` WHERE Table_Type != 'VIEW'" , schema )).WillReturnRows (rows )
95+
96+ got , err := GetTables (context .Background (), db , schema )
97+ require .NoError (t , err )
98+ require .Equal (t , tables , got )
99+ require .NoError (t , mock .ExpectationsWereMet ())
100+
101+ rows = sqlmock .NewRows ([]string {fmt .Sprintf ("Tables_in_%s" , schema ), "Table_type" , "Auto_partition" , "Table_group" })
102+ addRowsForPolarDBXTables (rows , tables )
103+ mock .ExpectQuery (fmt .Sprintf ("SHOW FULL TABLES IN `%s` WHERE Table_Type != 'VIEW'" , schema )).WillReturnRows (rows )
104+
105+ got , err = GetTables (context .Background (), db , schema )
106+ require .NoError (t , err )
107+ require .Equal (t , tables , got )
108+ require .NoError (t , mock .ExpectationsWereMet ())
109+ }
110+
111+ func TestGetTablesErrors (t * testing.T ) {
112+ t .Parallel ()
113+
114+ db , mock , err := sqlmock .New ()
115+ require .NoError (t , err )
116+
117+ schema := "test_db"
118+ query := fmt .Sprintf ("SHOW FULL TABLES IN `%s` WHERE Table_Type != 'VIEW'" , schema )
119+
120+ mock .ExpectQuery (query ).WillReturnError (errors .New ("query failed" ))
121+ _ , err = GetTables (context .Background (), db , schema )
122+ require .ErrorContains (t , err , "query failed" )
123+ require .NoError (t , mock .ExpectationsWereMet ())
124+
125+ rows := sqlmock .NewRows ([]string {fmt .Sprintf ("Tables_in_%s" , schema )}).AddRow ("tbl1" )
126+ mock .ExpectQuery (query ).WillReturnRows (rows )
127+ _ , err = GetTables (context .Background (), db , schema )
128+ require .ErrorContains (t , err , "unexpected SHOW FULL TABLES result column count 1" )
129+ require .NoError (t , mock .ExpectationsWereMet ())
130+ }
131+
83132func TestGetMariaDBGtidDomainID (t * testing.T ) {
84133 t .Parallel ()
85134
@@ -571,6 +620,18 @@ func TestFetchAllDoTables(t *testing.T) {
571620 require .Len (t , got , 1 )
572621 require .Equal (t , []string {"tbl1" , "tbl2" }, got [doSchema ])
573622 require .NoError (t , mock .ExpectationsWereMet ())
623+
624+ rows = sqlmock .NewRows ([]string {"Database" })
625+ addRowsForSchemas (rows , schemas )
626+ mock .ExpectQuery (`SHOW DATABASES` ).WillReturnRows (rows )
627+ rows = sqlmock .NewRows ([]string {fmt .Sprintf ("Tables_in_%s" , doSchema ), "Table_type" , "Auto_partition" , "Table_group" })
628+ addRowsForPolarDBXTables (rows , tables )
629+ mock .ExpectQuery (fmt .Sprintf ("SHOW FULL TABLES IN `%s` WHERE Table_Type != 'VIEW'" , doSchema )).WillReturnRows (rows )
630+ got , err = FetchAllDoTables (context .Background (), NewBaseDBForTest (db ), ba )
631+ require .NoError (t , err )
632+ require .Len (t , got , 1 )
633+ require .Equal (t , []string {"tbl1" , "tbl2" }, got [doSchema ])
634+ require .NoError (t , mock .ExpectationsWereMet ())
574635}
575636
576637func TestFetchTargetDoTables (t * testing.T ) {
@@ -647,6 +708,12 @@ func addRowsForTables(rows *sqlmock.Rows, tables []string) {
647708 }
648709}
649710
711+ func addRowsForPolarDBXTables (rows * sqlmock.Rows , tables []string ) {
712+ for _ , table := range tables {
713+ rows .AddRow (table , "BASE TABLE" , "NO" , "single_tg" )
714+ }
715+ }
716+
650717func TestGetGTIDMode (t * testing.T ) {
651718 t .Parallel ()
652719
0 commit comments