From 80540a662a218299ec74bba7212f69428a7f1504 Mon Sep 17 00:00:00 2001 From: nxshock Date: Wed, 7 Dec 2022 19:20:38 +0500 Subject: [PATCH] Add test for backup with deleted records --- README.md | 1 - zkv_test.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 12eed2b..b66f815 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,5 @@ File is log stuctured list of commands: ## TODO -- [ ] Add delete records test for `Backup()` method - [ ] Test [seekable zstd streams](https://github.com/SaveTheRbtz/zstd-seekable-format-go) - [ ] Implement optional separate index file to speedup store initialization diff --git a/zkv_test.go b/zkv_test.go index 1fc3983..a252f9d 100644 --- a/zkv_test.go +++ b/zkv_test.go @@ -274,3 +274,58 @@ func TestBackupBasic(t *testing.T) { assert.NoError(t, err) } + +func TestBackupWithDeletedRecords(t *testing.T) { + const filePath = "TestBackupWithDeletedRecords.zkv" + const newFilePath = "TestBackupWithDeletedRecords2.zkv" + const recordCount = 100 + defer os.Remove(filePath) + defer os.Remove(newFilePath) + + db, err := Open(filePath) + assert.NoError(t, err) + + for i := 1; i <= recordCount; i++ { + err = db.Set(i, i) + assert.NoError(t, err) + } + + err = db.Flush() + assert.NoError(t, err) + + for i := 1; i <= recordCount; i++ { + if i%2 == 1 { + continue + } + + err = db.Delete(i) + assert.NoError(t, err) + } + + err = db.Backup(newFilePath) + assert.NoError(t, err) + + err = db.Close() + assert.NoError(t, err) + + db, err = Open(newFilePath) + assert.NoError(t, err) + + assert.Len(t, db.dataOffset, recordCount/2) + + for i := 1; i <= recordCount; i++ { + var gotValue int + + err = db.Get(i, &gotValue) + if i%2 == 0 { + assert.ErrorIs(t, err, ErrNotExists) + } else { + assert.NoError(t, err) + assert.Equal(t, i, gotValue) + } + } + + err = db.Close() + assert.NoError(t, err) + +}