github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/sink/cloudstorage/path_key_test.go (about) 1 // Copyright 2023 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 cloudstorage 15 16 import ( 17 "testing" 18 19 "github.com/stretchr/testify/require" 20 ) 21 22 func TestSchemaPathKey(t *testing.T) { 23 t.Parallel() 24 25 testCases := []struct { 26 path string 27 schemakey SchemaPathKey 28 checksum uint32 29 }{ 30 // Test for database schema path: <schema>/meta/schema_{tableVersion}_{checksum}.json 31 { 32 path: "test_schema/meta/schema_1_2.json", 33 schemakey: SchemaPathKey{ 34 Schema: "test_schema", 35 Table: "", 36 TableVersion: 1, 37 }, 38 checksum: 2, 39 }, 40 // Test for table schema path: <schema>/<table>/meta/schema_{tableVersion}_{checksum}.json 41 { 42 path: "test_schema/test_table/meta/schema_11_22.json", 43 schemakey: SchemaPathKey{ 44 Schema: "test_schema", 45 Table: "test_table", 46 TableVersion: 11, 47 }, 48 checksum: 22, 49 }, 50 } 51 for _, tc := range testCases { 52 var schemaKey SchemaPathKey 53 checksum, err := schemaKey.ParseSchemaFilePath(tc.path) 54 require.NoError(t, err) 55 require.Equal(t, tc.schemakey, schemaKey) 56 require.Equal(t, tc.checksum, checksum) 57 } 58 } 59 60 func TestDmlPathKey(t *testing.T) { 61 t.Parallel() 62 63 testCases := []struct { 64 index int 65 fileIndexWidth int 66 extension string 67 path string 68 dmlkey DmlPathKey 69 }{ 70 { 71 index: 10, 72 fileIndexWidth: 20, 73 extension: ".csv", 74 path: "schema1/table1/123456/2023-05-09/CDC00000000000000000010.csv", 75 dmlkey: DmlPathKey{ 76 SchemaPathKey: SchemaPathKey{ 77 Schema: "schema1", 78 Table: "table1", 79 TableVersion: 123456, 80 }, 81 PartitionNum: 0, 82 Date: "2023-05-09", 83 }, 84 }, 85 } 86 87 for _, tc := range testCases { 88 var dmlkey DmlPathKey 89 idx, err := dmlkey.ParseDMLFilePath("day", tc.path) 90 require.NoError(t, err) 91 require.Equal(t, tc.dmlkey, dmlkey) 92 93 fileName := dmlkey.GenerateDMLFilePath(idx, tc.extension, tc.fileIndexWidth) 94 require.Equal(t, tc.path, fileName) 95 } 96 }