-
Notifications
You must be signed in to change notification settings - Fork 450
/
testing.go
138 lines (115 loc) · 5.16 KB
/
testing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package sqlitedriver
import (
"context"
"fmt"
"strings"
"testing"
"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/crypto"
"github.com/algorand/go-algorand/data/basics"
"github.com/algorand/go-algorand/ledger/store/trackerdb"
"github.com/algorand/go-algorand/logging"
"github.com/algorand/go-algorand/protocol"
"github.com/algorand/go-algorand/util/db"
"github.com/stretchr/testify/require"
)
// OpenForTesting opens a sqlite db file for testing purposes.
// It set the logging to the test logger and uses a tmp directory associated to the test for the db.
// The test tmp direction is automatically cleaned up by the golang test framework.
func OpenForTesting(t testing.TB, inMemory bool) (trackerdb.Store, string) {
fn := fmt.Sprintf("%s/%s.%d", t.TempDir(), strings.ReplaceAll(t.Name(), "/", "."), crypto.RandUint64())
store, err := Open(fn, inMemory, logging.TestingLog(t))
require.NoErrorf(t, err, "Filename : %s\nInMemory: %v", fn, inMemory)
return store, fn
}
// AccountsInitLightTest initializes an empty database for testing without the extra methods being called.
// implements Testing interface, test function only
func AccountsInitLightTest(tb testing.TB, e db.Executable, initAccounts map[basics.Address]basics.AccountData, proto config.ConsensusParams) (newDatabase bool, err error) {
newDB, err := accountsInit(e, initAccounts, proto)
require.NoError(tb, err)
return newDB, err
}
// modifyAcctBaseTest tweaks the database to move backards.
// implements Testing interface, test function only
func modifyAcctBaseTest(e db.Executable) error {
_, err := e.Exec("update acctrounds set rnd = 1 WHERE id='acctbase' ")
return err
}
// AccountsInitTest initializes an empty database for testing.
// implements Testing interface, test function only
func AccountsInitTest(tb testing.TB, e db.Executable, initAccounts map[basics.Address]basics.AccountData, proto protocol.ConsensusVersion) (newDatabase bool) {
newDB, err := accountsInit(e, initAccounts, config.Consensus[proto])
require.NoError(tb, err)
err = accountsAddNormalizedBalance(e, config.Consensus[proto])
require.NoError(tb, err)
err = accountsCreateResourceTable(context.Background(), e)
require.NoError(tb, err)
err = performResourceTableMigration(context.Background(), e, nil)
require.NoError(tb, err)
err = accountsCreateOnlineAccountsTable(context.Background(), e)
require.NoError(tb, err)
err = accountsCreateTxTailTable(context.Background(), e)
require.NoError(tb, err)
err = performOnlineAccountsTableMigration(context.Background(), e, nil, nil)
require.NoError(tb, err)
// since this is a test that starts from genesis, there is no tail that needs to be migrated.
// we'll pass a nil here in order to ensure we still call this method, although it would
// be a noop.
err = performTxTailTableMigration(context.Background(), nil, db.Accessor{})
require.NoError(tb, err)
err = accountsCreateOnlineRoundParamsTable(context.Background(), e)
require.NoError(tb, err)
err = performOnlineRoundParamsTailMigration(context.Background(), e, db.Accessor{}, true, proto)
require.NoError(tb, err)
err = accountsCreateBoxTable(context.Background(), e)
require.NoError(tb, err)
err = performKVStoreNullBlobConversion(context.Background(), e)
require.NoError(tb, err)
err = accountsAddCreatableTypeColumn(context.Background(), e, false)
require.NoError(tb, err)
return newDB
}
// AccountsUpdateSchemaTest adds some empty tables for tests to work with a "v6" store.
func AccountsUpdateSchemaTest(ctx context.Context, e db.Executable) (err error) {
if err = accountsCreateOnlineAccountsTable(ctx, e); err != nil {
return err
}
if err = accountsCreateTxTailTable(ctx, e); err != nil {
return err
}
if err = accountsCreateOnlineRoundParamsTable(ctx, e); err != nil {
return err
}
if err = accountsCreateCatchpointFirstStageInfoTable(ctx, e); err != nil {
return err
}
// this line creates kvstore table, even if it is not required in accountDBVersion 6 -> 7
// or in later version where we need kvstore table, some tests will fail
if err = accountsCreateBoxTable(ctx, e); err != nil {
return err
}
// this adds the resources table and ctype column, even if it is not required in accountDBVersion 6 -> 7
// this prevents some tests from failing.
if err = accountsCreateResourceTable(ctx, e); err != nil {
return err
}
if err = accountsAddCreatableTypeColumn(ctx, e, false); err != nil {
return err
}
return createStateProofVerificationTable(ctx, e)
}