github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/tests/utils/gen_kafka_big_messages/main_test.go (about) 1 // Copyright 2021 PingCAP, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package main 15 16 import ( 17 "testing" 18 19 "github.com/stretchr/testify/require" 20 ) 21 22 func TestValidateOptions(t *testing.T) { 23 testCases := []struct { 24 o *options 25 expectedErr string 26 }{ 27 { 28 &options{ 29 rowBytes: 0, 30 }, 31 ".*rowBytes must be greater than zero.*", 32 }, 33 { 34 &options{ 35 rowBytes: 1024 * 1024, 36 rowCount: 0, 37 }, 38 ".*rowCount must be greater than zero.*", 39 }, 40 { 41 &options{ 42 rowBytes: 1024 * 1024, 43 rowCount: 1, 44 sqlFilePath: "", 45 }, 46 ".*please specify the correct file path.*", 47 }, 48 { 49 &options{ 50 rowBytes: 1024 * 1024, 51 rowCount: 1, 52 sqlFilePath: "./test.sql", 53 databaseName: "", 54 }, 55 ".*please specify the database name.*", 56 }, 57 { 58 &options{ 59 rowBytes: 1024 * 1024, 60 rowCount: 1, 61 sqlFilePath: "./test.sql", 62 databaseName: "kafka-big-messages", 63 tableName: "", 64 }, 65 ".*please specify the table name.*", 66 }, 67 { 68 &options{ 69 rowBytes: 1024 * 1024, 70 rowCount: 10, 71 sqlFilePath: "./test.sql", 72 databaseName: "kafka-big-messages", 73 tableName: "test", 74 }, 75 "", 76 }, 77 } 78 79 for _, tc := range testCases { 80 err := tc.o.validate() 81 if tc.expectedErr != "" { 82 require.Error(t, err) 83 require.Regexp(t, tc.expectedErr, tc.o.validate().Error()) 84 } else { 85 require.Nil(t, err) 86 } 87 } 88 } 89 90 func TestGenDatabaseSql(t *testing.T) { 91 database := "test" 92 93 sql := genDatabaseSql(database) 94 95 require.Equal(t, "DROP DATABASE IF EXISTS test;\nCREATE DATABASE test;\nUSE test;\n\n", sql) 96 } 97 98 func TestGenCreateTableSql(t *testing.T) { 99 rawBytes := varcharColumnMaxLen 100 tableName := "test" 101 102 sql := genCreateTableSql(rawBytes, tableName) 103 require.Equal(t, "CREATE TABLE test(id int primary key , a0 VARCHAR(16383));\n", sql) 104 } 105 106 func TestGenInsertSql(t *testing.T) { 107 // Override the col value to test. 108 oldColValue := colValue 109 colValue = "a" 110 defer func() { 111 colValue = oldColValue 112 }() 113 114 rawBytes := varcharColumnMaxLen 115 tableName := "test" 116 id := 1 117 118 sql := genInsertSql(rawBytes, tableName, id) 119 println(sql) 120 require.Equal(t, "INSERT INTO test VALUES (1, 'a');\n", sql) 121 }