code.vegaprotocol.io/vega@v0.79.0/vegatools/checktx/file_test.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package checktx 17 18 import ( 19 "os" 20 "path/filepath" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestGetFilesInDirectoryRetrievesAllFiles(t *testing.T) { 27 tmpDir := t.TempDir() 28 testFiles := []string{"file1.json", "file2.json", "file3.json"} 29 filePaths := make([]string, 0, len(testFiles)) 30 31 for _, file := range testFiles { 32 filePath := filepath.Join(tmpDir, file) 33 if _, err := os.Create(filePath); err != nil { 34 t.Fatalf("failed to create test file %s: %v", filePath, err) 35 } 36 filePaths = append(filePaths, filePath) 37 } 38 39 retrievedFiles, err := GetFilesInDirectory(tmpDir) 40 assert.NoError(t, err, "GetFilesInDirectory failed") 41 assert.Len(t, retrievedFiles, len(testFiles), "GetFilesInDirectory returned incorrect number of files") 42 43 for _, filePath := range filePaths { 44 assert.Contains(t, retrievedFiles, filePath, "GetFilesInDirectory did not return expected file") 45 } 46 } 47 48 func TestGetEncodedTransactionFromFileReturnsBase64DataWhenBase64DataWritten(t *testing.T) { 49 tmpFile, err := os.CreateTemp("", "test-transaction-*.txt") 50 assert.NoErrorf(t, err, "failed to create temporary test file: %v", err) 51 defer os.Remove(tmpFile.Name()) 52 53 testData, err := CreatedEncodedTransactionData() 54 assert.NoErrorf(t, err, "failed to create temporary test file: %v", err) 55 56 _, err = tmpFile.WriteString(testData) 57 assert.NoErrorf(t, err, "failed to write test data to file: %v", err) 58 err = tmpFile.Close() 59 assert.NoErrorf(t, err, "error occurred when closing file: %v", err) 60 61 transactionData, err := GetEncodedTransactionFromFile(tmpFile.Name()) 62 assert.NoError(t, err, "GetTransactionDataFromFile failed") 63 64 assertIsBase64Encoded(t, transactionData) 65 assert.NoError(t, err, "failed to unmarshal expected data") 66 }