From 22ca5309cdeb9525fd5fc1f8b7c8b9534ff27ba4 Mon Sep 17 00:00:00 2001 From: Jeff Ortel Date: Thu, 22 Aug 2024 13:06:42 -0700 Subject: [PATCH] add unit test. Signed-off-by: Jeff Ortel --- database/db_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/database/db_test.go b/database/db_test.go index 37102df9..2326ed73 100644 --- a/database/db_test.go +++ b/database/db_test.go @@ -69,3 +69,60 @@ func TestConcurrent(t *testing.T) { fmt.Printf("Done %d\n", id) } } + +func TestKeyGen(t *testing.T) { + pid := os.Getpid() + Settings.DB.Path = fmt.Sprintf("/tmp/keygen-%d.db", pid) + defer func() { + _ = os.Remove(Settings.DB.Path) + }() + db, err := Open(true) + if err != nil { + panic(err) + } + // ids 1-7 created. + N = 8 + for n := 1; n < N; n++ { + m := &model.Setting{Key: fmt.Sprintf("key-%d", n), Value: n} + err := db.Create(m).Error + if err != nil { + panic(err) + } + fmt.Printf("CREATED: %d/%d\n", m.ID, n) + if uint(n) != m.ID { + t.Errorf("id:%d but expected: %d", m.ID, n) + return + } + } + // delete ids=2,4. + err = db.Delete(&model.Setting{}, 2).Error + if err != nil { + panic(err) + } + err = db.Delete(&model.Setting{}, 4).Error + if err != nil { + panic(err) + } + + var count int64 + err = db.Model(&model.Setting{}).Where([]uint{2, 4}).Count(&count).Error + if err != nil { + panic(err) + } + if count > 0 { + t.Errorf("DELETED ids: 2,4 found.") + return + } + // id=8 (next) created. + next := N + m := &model.Setting{Key: fmt.Sprintf("key-%d", next), Value: next} + err = db.Create(m).Error + if err != nil { + panic(err) + } + fmt.Printf("CREATED: %d/%d\n", m.ID, next) + if uint(N) != m.ID { + t.Errorf("id:%d but expected: %d", m.ID, next) + return + } +}