github.com/matrixorigin/matrixone@v1.2.0/pkg/util/export/etl/sql_test.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package etl 16 17 import ( 18 "regexp" 19 "testing" 20 "time" 21 22 "github.com/DATA-DOG/go-sqlmock" 23 db_holder "github.com/matrixorigin/matrixone/pkg/util/export/etl/db" 24 "github.com/matrixorigin/matrixone/pkg/util/export/table" 25 ) 26 27 func TestDefaultSqlWriter_WriteRowRecords(t *testing.T) { 28 db, mock, err := sqlmock.New() 29 if err != nil { 30 t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) 31 } 32 defer db.Close() 33 mock.ExpectExec(regexp.QuoteMeta(`OAD DATA INLINE FORMAT='csv', DATA='record1 34 ' INTO TABLE testDB.testTable`)).WillReturnResult(sqlmock.NewResult(1, 1)) 35 db_holder.SetDBConn(db) 36 37 // set up your DefaultSqlWriter and records 38 var dummyStrColumn = table.Column{Name: "str", ColType: table.TVarchar, Scale: 32, Default: "", Comment: "str column"} 39 40 tbl := &table.Table{ 41 Database: "testDB", 42 Table: "testTable", 43 Columns: []table.Column{dummyStrColumn}, 44 } 45 records := [][]string{ 46 {"record1"}, 47 // {"record2"}, 48 // add more records as needed 49 } 50 51 // call the function to test 52 cnt, err := db_holder.WriteRowRecords(records, tbl, 1*time.Second) 53 54 // assertions 55 if err != nil { 56 t.Errorf("expected nil error, got %v", err) 57 } 58 59 if cnt != len(records) { 60 t.Errorf("expected %d, got %d", len(records), cnt) 61 } 62 63 if err := mock.ExpectationsWereMet(); err != nil { 64 t.Errorf("there were unfulfilled expectations: %s", err) 65 } 66 }