Skip to content

Commit

Permalink
added ClearEntries function (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpatek authored Jul 2, 2024
1 parent 94dfb62 commit 38d09ee
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ kb, err := keybase.Open(keybase.WithStorage("/tmp/keybase.db"), keybase.WithTTL(
```

This will initialize a database at `/tmp/keybase.db` with a key timeout of one minute. Once
keybase is open, it is ready to store and maintain keys. Each key is assigned to a nanespace
keybase is open, it is ready to store and maintain keys. Each key is assigned to a namespace
and can be inserted using the `Put` function:

```go
Expand Down
11 changes: 11 additions & 0 deletions keybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@ func (k *Keybase) PruneEntries(ctx context.Context) error {
return nil
}

// ClearEntries removes all entries.
func (k *Keybase) ClearEntries(ctx context.Context) error {
k.mu.Lock()
defer k.mu.Unlock()
err := newClearEntriesQuery().queryExec(ctx, k.db)
if err != nil {
return fmt.Errorf("keybase.PruneEntries: failed to insert key: %v", err)
}
return nil
}

func sqlOpen(driverName string, dataSourceName string) (*sql.DB, error) {
db, _ := sql.Open(driverName, dataSourceName)
err := db.Ping()
Expand Down
18 changes: 17 additions & 1 deletion keybase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestNamespaces(t *testing.T) {
assert.Error(t, err)
}

// TestEntries tests CountEntries and PruneEntries
// TestEntries tests CountEntries, PruneEntries, and ClearEntries
func TestEntries(t *testing.T) {
keybase, err := Open(context.TODO(), WithTTL(time.Millisecond*50))
assert.NoError(t, err)
Expand Down Expand Up @@ -206,12 +206,28 @@ func TestEntries(t *testing.T) {
assert.Zero(t, count)
assert.NoError(t, err)

err = keybase.Put(context.TODO(), "namespace", "key")
assert.NoError(t, err)

count, err = keybase.CountEntries(context.TODO(), false, false)
assert.Equal(t, 1, count)
assert.NoError(t, err)

err = keybase.ClearEntries(context.TODO())
assert.NoError(t, err)

count, err = keybase.CountEntries(context.TODO(), false, false)
assert.Zero(t, count)
assert.NoError(t, err)

ctx, cancel := context.WithTimeout(context.TODO(), time.Duration(0))
defer cancel()
_, err = keybase.CountEntries(ctx, true, true)
assert.Error(t, err)
err = keybase.PruneEntries(ctx)
assert.Error(t, err)
err = keybase.ClearEntries(ctx)
assert.Error(t, err)
}

// TestStorage tests filesystem
Expand Down
6 changes: 6 additions & 0 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ func newPruneEntriesQuery(timestamp int64) *dbtx {
return tx
}

func newClearEntriesQuery() *dbtx {
return &dbtx{
query: "DELETE FROM keybase;",
}
}

func (tx dbtx) queryExec(ctx context.Context, db *sql.DB) error {
_, err := db.ExecContext(ctx, tx.query, tx.args...)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ func TestNewPruneEntriesQuery(t *testing.T) {
assert.NoError(t, err)
}

func TestNewClearEntriesQuery(t *testing.T) {
db, mock := newMock()
tx := newClearEntriesQuery()

mock.ExpectExec(regexp.QuoteMeta(tx.query)).WillReturnError(errors.New("some error"))
err := tx.queryExec(context.TODO(), db)
assert.Error(t, err)

mock.ExpectExec(regexp.QuoteMeta(tx.query)).WillReturnResult(sqlmock.NewResult(1, 1))
err = tx.queryExec(context.TODO(), db)
assert.NoError(t, err)
}

func TestQueryCount(t *testing.T) {
db, mock := newMock()
tx := &dbtx{query: ""}
Expand Down

0 comments on commit 38d09ee

Please sign in to comment.