Skip to content

Commit

Permalink
add unit test.
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Ortel <[email protected]>
  • Loading branch information
jortel committed Aug 22, 2024
1 parent 1da5bcf commit 22ca530
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions database/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 22ca530

Please sign in to comment.