From 31f72f6c7d48d85736d50a79d11b29ab62fd9aef Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Sat, 28 Sep 2019 08:22:05 +0200 Subject: [PATCH] channeldb: extract before migration func --- channeldb/migration_11_invoices_test.go | 75 +++++++++++++------------ 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/channeldb/migration_11_invoices_test.go b/channeldb/migration_11_invoices_test.go index 9739af80..b8518a35 100644 --- a/channeldb/migration_11_invoices_test.go +++ b/channeldb/migration_11_invoices_test.go @@ -24,6 +24,43 @@ var ( testCltvDelta = int32(50) ) +// beforeMigrationFuncV11 insert the test invoices in the database. +func beforeMigrationFuncV11(t *testing.T, d *DB, invoices []Invoice) { + err := d.Update(func(tx *bbolt.Tx) error { + invoicesBucket, err := tx.CreateBucketIfNotExists( + invoiceBucket, + ) + if err != nil { + return err + } + + invoiceNum := uint32(1) + for _, invoice := range invoices { + var invoiceKey [4]byte + byteOrder.PutUint32(invoiceKey[:], invoiceNum) + invoiceNum++ + + var buf bytes.Buffer + err := serializeInvoiceLegacy(&buf, &invoice) // nolint:scopelint + if err != nil { + return err + } + + err = invoicesBucket.Put( + invoiceKey[:], buf.Bytes(), + ) + if err != nil { + return err + } + } + + return nil + }) + if err != nil { + t.Fatal(err) + } +} + // TestMigrateInvoices checks that invoices are migrated correctly. func TestMigrateInvoices(t *testing.T) { t.Parallel() @@ -49,42 +86,6 @@ func TestMigrateInvoices(t *testing.T) { }, } - beforeMigrationFunc := func(d *DB) { - err := d.Update(func(tx *bbolt.Tx) error { - invoicesBucket, err := tx.CreateBucketIfNotExists( - invoiceBucket, - ) - if err != nil { - return err - } - - invoiceNum := uint32(1) - for _, invoice := range invoices { - var invoiceKey [4]byte - byteOrder.PutUint32(invoiceKey[:], invoiceNum) - invoiceNum++ - - var buf bytes.Buffer - err := serializeInvoiceLegacy(&buf, &invoice) - if err != nil { - return err - } - - err = invoicesBucket.Put( - invoiceKey[:], buf.Bytes(), - ) - if err != nil { - return err - } - } - - return nil - }) - if err != nil { - t.Fatal(err) - } - } - // Verify that all invoices were migrated. afterMigrationFunc := func(d *DB) { meta, err := d.FetchMeta(nil) @@ -120,7 +121,7 @@ func TestMigrateInvoices(t *testing.T) { } applyMigration(t, - beforeMigrationFunc, + func(d *DB) { beforeMigrationFuncV11(t, d, invoices) }, afterMigrationFunc, migrateInvoices, false)