From bd75a56855bca14863329ae32476aba2f6dcea66 Mon Sep 17 00:00:00 2001 From: bluetegu Date: Wed, 26 Sep 2018 11:27:30 -0400 Subject: [PATCH] Add unit test for db Wipe. --- channeldb/db_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/channeldb/db_test.go b/channeldb/db_test.go index f3e3c96e..872e6307 100644 --- a/channeldb/db_test.go +++ b/channeldb/db_test.go @@ -33,3 +33,41 @@ func TestOpenWithCreate(t *testing.T) { t.Fatalf("channeldb failed to create data directory") } } + +// TestWipe tests that the database wipe operation completes successfully +// and that the buckets are deleted. It also checks that attempts to fetch +// information while the buckets are not set return the correct errors. +func TestWipe(t *testing.T) { + t.Parallel() + + // First, create a temporary directory to be used for the duration of + // this test. + tempDirName, err := ioutil.TempDir("", "channeldb") + if err != nil { + t.Fatalf("unable to create temp dir: %v", err) + } + defer os.RemoveAll(tempDirName) + + // Next, open thereby creating channeldb for the first time. + dbPath := filepath.Join(tempDirName, "cdb") + cdb, err := Open(dbPath) + if err != nil { + t.Fatalf("unable to create channeldb: %v", err) + } + defer cdb.Close() + + if err := cdb.Wipe(); err != nil { + t.Fatalf("unable to wipe channeldb: %v", err) + } + // Check correct errors are returned + _, err = cdb.FetchAllOpenChannels() + if err != ErrNoActiveChannels { + t.Fatalf("fetching open channels: expected '%v' instead got '%v'", + ErrNoActiveChannels, err) + } + _, err = cdb.FetchClosedChannels(false) + if err != ErrNoClosedChannels { + t.Fatalf("fetching closed channels: expected '%v' instead got '%v'", + ErrNoClosedChannels, err) + } +}