github.com/decred/dcrlnd@v0.7.6/channeldb/migration_01_to_11/meta_test.go (about) 1 package migration_01_to_11 2 3 import ( 4 "testing" 5 6 "github.com/decred/dcrlnd/kvdb" 7 "github.com/go-errors/errors" 8 ) 9 10 // applyMigration is a helper test function that encapsulates the general steps 11 // which are needed to properly check the result of applying migration function. 12 func applyMigration(t *testing.T, beforeMigration, afterMigration func(d *DB), 13 migrationFunc migration, shouldFail bool) { 14 15 cdb, cleanUp, err := makeTestDB() 16 defer cleanUp() 17 if err != nil { 18 t.Fatal(err) 19 } 20 21 // Create a test node that will be our source node. 22 testNode, err := createTestVertex(cdb) 23 if err != nil { 24 t.Fatal(err) 25 } 26 graph := cdb.ChannelGraph() 27 if err := graph.SetSourceNode(testNode); err != nil { 28 t.Fatal(err) 29 } 30 31 // beforeMigration usually used for populating the database 32 // with test data. 33 beforeMigration(cdb) 34 35 defer func() { 36 if r := recover(); r != nil { 37 err = errors.New(r) 38 } 39 40 if err == nil && shouldFail { 41 t.Fatal("error wasn't received on migration stage") 42 } else if err != nil && !shouldFail { 43 t.Fatalf("error was received on migration stage: %v", err) 44 } 45 46 // afterMigration usually used for checking the database state and 47 // throwing the error if something went wrong. 48 afterMigration(cdb) 49 }() 50 51 // Apply migration. 52 err = kvdb.Update(cdb, func(tx kvdb.RwTx) error { 53 return migrationFunc(tx) 54 }, func() {}) 55 if err != nil { 56 log.Error(err) 57 } 58 }