code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/sqlstore_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 sqlstore_test 17 18 import ( 19 "bytes" 20 "context" 21 "crypto/sha256" 22 "encoding/base64" 23 "encoding/hex" 24 "math/rand" 25 "os" 26 "path/filepath" 27 "strconv" 28 "testing" 29 30 "code.vegaprotocol.io/vega/datanode/entities" 31 "code.vegaprotocol.io/vega/datanode/sqlstore" 32 "code.vegaprotocol.io/vega/datanode/utils/databasetest" 33 34 "github.com/stretchr/testify/require" 35 ) 36 37 var ( 38 connectionSource *sqlstore.ConnectionSource 39 testDBPort int 40 testConfig sqlstore.Config 41 ) 42 43 func TestMain(m *testing.M) { 44 ctx, cfunc := context.WithCancel(context.Background()) 45 defer cfunc() 46 tempDir, err := os.MkdirTemp("", "datanode") 47 if err != nil { 48 panic(err) 49 } 50 postgresRuntimePath := filepath.Join(tempDir, "sqlstore") 51 defer os.RemoveAll(tempDir) 52 53 databasetest.TestMain(m, ctx, func(cfg sqlstore.Config, source *sqlstore.ConnectionSource, 54 postgresLog *bytes.Buffer, 55 ) { 56 // ensures nested transactions execute the post-commit hooks while the parent transaction still rolls back the overall changes. 57 source.ToggleTest() 58 testDBPort = cfg.ConnectionConfig.Port 59 connectionSource = source 60 testConfig = cfg 61 }, postgresRuntimePath, sqlstore.EmbedMigrations) 62 } 63 64 func generateEthereumAddress() string { 65 randomString := strconv.FormatInt(rand.Int63(), 10) 66 hash := sha256.Sum256([]byte(randomString)) 67 return "0x" + hex.EncodeToString(hash[1:21]) 68 } 69 70 func generateTendermintPublicKey() string { 71 randomString := strconv.FormatInt(rand.Int63(), 10) 72 hash := sha256.Sum256([]byte(randomString)) 73 return base64.StdEncoding.EncodeToString(hash[:]) 74 } 75 76 func tempTransaction(t *testing.T) context.Context { 77 t.Helper() 78 79 ctx, err := connectionSource.WithTransaction(context.Background()) 80 require.NoError(t, err) 81 82 t.Cleanup(func() { 83 _ = connectionSource.Rollback(ctx) 84 }) 85 86 return ctx 87 } 88 89 func getEnums[T entities.ProtoEnum](t *testing.T, enumsT T) map[int32]string { 90 t.Helper() 91 return enumsT.GetEnums() 92 }