-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from herzrasen/github-actions
improved tests
- Loading branch information
Showing
15 changed files
with
276 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package args | ||
|
||
type RecordCmd struct { | ||
Command string `arg:"positional"` | ||
} | ||
|
||
type ListCmd struct { | ||
NoCount bool `arg:"--no-count"` | ||
NoLastUpdate bool `arg:"--no-last-update"` | ||
WithId bool `arg:"--with-id"` | ||
Limit int `arg:"-l,--limit" default:"-1"` | ||
} | ||
|
||
type DeleteCmd struct { | ||
Ids []int64 `arg:"-i,--id"` | ||
Prefix string `arg:"-p,--prefix"` | ||
MaxCount int64 `arg:"--max-count" help:"Delete all records with a count of at most max-count"` | ||
} | ||
|
||
type Args struct { | ||
Record *RecordCmd `arg:"subcommand:record"` | ||
List *ListCmd `arg:"subcommand:list"` | ||
Delete *DeleteCmd `arg:"subcommand:delete"` | ||
Config string `arg:"--config" default:"~/.config/hist/config.yml"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,49 @@ | ||
package client | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"errors" | ||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/jmoiron/sqlx" | ||
"github.com/stretchr/testify/require" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestClient_Delete(t *testing.T) { | ||
t.Run("delete by prefix", func(t *testing.T) { | ||
c, err := CreateTestClient(t) | ||
db, mock, _ := sqlmock.New() | ||
mock.ExpectExec("DELETE FROM hist WHERE command LIKE ?"). | ||
WithArgs("test-command%"). | ||
WillReturnResult(sqlmock.NewResult(0, 2)) | ||
c := Client{Db: sqlx.NewDb(db, "sqlite3")} | ||
err := c.Delete(DeleteOptions{Prefix: "test-command"}) | ||
require.NoError(t, err) | ||
defer os.Remove(c.Path) | ||
if err = c.Update("test-command-1"); err != nil { | ||
t.FailNow() | ||
} | ||
if err = c.Update("test-command-2"); err != nil { | ||
t.FailNow() | ||
} | ||
if err = c.Update("test-command-1"); err != nil { | ||
t.FailNow() | ||
} | ||
if err = c.Update("other-command"); err != nil { | ||
t.FailNow() | ||
} | ||
err = c.Delete(DeleteOptions{Prefix: "test-command"}) | ||
require.NoError(t, err) | ||
records, err := c.List(ListOptions{}) | ||
require.NoError(t, err) | ||
assert.Len(t, records, 1) | ||
}) | ||
|
||
t.Run("delete by ids", func(t *testing.T) { | ||
c, err := CreateTestClient(t) | ||
require.NoError(t, err) | ||
defer os.Remove(c.Path) | ||
if err = c.Update("test-command-1"); err != nil { | ||
t.FailNow() | ||
} | ||
if err = c.Update("test-command-2"); err != nil { | ||
t.FailNow() | ||
} | ||
if err = c.Update("other-command"); err != nil { | ||
t.FailNow() | ||
} | ||
// get the entries to get some ids | ||
records, err := c.List(ListOptions{}) | ||
require.NoError(t, err) | ||
require.Len(t, records, 3) | ||
err = c.Delete(DeleteOptions{Ids: []int64{ | ||
records[0].Id, | ||
records[1].Id, | ||
}}) | ||
require.NoError(t, err) | ||
recordsAfterDelete, err := c.List(ListOptions{}) | ||
db, mock, _ := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) | ||
mock.ExpectExec("DELETE FROM hist WHERE id IN (?, ?)"). | ||
WithArgs(100, 101). | ||
WillReturnResult(sqlmock.NewResult(0, 2)) | ||
c := Client{Db: sqlx.NewDb(db, "sqlite3")} | ||
err := c.Delete(DeleteOptions{Ids: []int64{100, 101}}) | ||
require.NoError(t, err) | ||
assert.Len(t, recordsAfterDelete, 1) | ||
assert.Equal(t, records[2], recordsAfterDelete[0]) | ||
}) | ||
|
||
t.Run("exec returns err with ids", func(t *testing.T) { | ||
db, mock, _ := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) | ||
mock.ExpectExec("DELETE FROM hist WHERE id IN (?, ?)"). | ||
WillReturnError(errors.New("some error")) | ||
c := Client{Db: sqlx.NewDb(db, "sqlite3")} | ||
err := c.Delete(DeleteOptions{Ids: []int64{100, 101}}) | ||
require.Error(t, err) | ||
}) | ||
|
||
t.Run("exec returns err with prefix", func(t *testing.T) { | ||
db, mock, _ := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) | ||
mock.ExpectExec("DELETE FROM hist WHERE command LIKE ?"). | ||
WillReturnError(errors.New("some error")) | ||
c := Client{Db: sqlx.NewDb(db, "sqlite3")} | ||
err := c.Delete(DeleteOptions{Prefix: "test"}) | ||
require.Error(t, err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,44 @@ | ||
package client | ||
|
||
import ( | ||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/jmoiron/sqlx" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"os" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestClient_List(t *testing.T) { | ||
t.Run("succeed with empty ListOptions", func(t *testing.T) { | ||
c, err := CreateTestClient(t) | ||
require.NoError(t, err) | ||
defer os.Remove(c.Path) | ||
if err = c.Update("test-command-1"); err != nil { | ||
t.FailNow() | ||
} | ||
time.Sleep(100 * time.Millisecond) | ||
if err = c.Update("test-command-2"); err != nil { | ||
t.FailNow() | ||
} | ||
time.Sleep(100 * time.Millisecond) | ||
if err = c.Update("test-command-1"); err != nil { | ||
t.FailNow() | ||
} | ||
db, mock, _ := sqlmock.New() | ||
mock.ExpectQuery("SELECT id, command, last_update, count FROM hist ORDER BY last_update DESC"). | ||
WillReturnRows(sqlmock.NewRows([]string{"id", "command", "last_update", "count"}). | ||
AddRow(1, "test-command-1", time.Now(), 42). | ||
AddRow(2, "test-command-2", time.Now().Add(-1*time.Minute), 10)) | ||
c := Client{Db: sqlx.NewDb(db, "sqlite3")} | ||
records, err := c.List(ListOptions{}) | ||
require.NoError(t, err) | ||
assert.Len(t, records, 2) | ||
r0 := records[0] | ||
assert.Equal(t, "test-command-2", r0.Command) | ||
assert.Equal(t, uint64(1), r0.Count) | ||
assert.Equal(t, uint64(10), r0.Count) | ||
r1 := records[1] | ||
assert.Equal(t, "test-command-1", r1.Command) | ||
assert.Equal(t, uint64(2), r1.Count) | ||
assert.Equal(t, uint64(42), r1.Count) | ||
}) | ||
|
||
t.Run("succeed with selection by ids", func(t *testing.T) { | ||
c, err := CreateTestClient(t) | ||
require.NoError(t, err) | ||
defer os.Remove(c.Path) | ||
if err = c.Update("test-command-1"); err != nil { | ||
t.FailNow() | ||
} | ||
if err = c.Update("test-command-2"); err != nil { | ||
t.FailNow() | ||
} | ||
if err = c.Update("test-command-3"); err != nil { | ||
t.FailNow() | ||
} | ||
// select the records to get the ids | ||
records, err := c.List(ListOptions{}) | ||
t.Run("succeed with specified ids", func(t *testing.T) { | ||
db, mock, _ := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) | ||
mock.ExpectQuery(`SELECT id, command, last_update, count | ||
FROM hist WHERE id IN (?, ?, ?) ORDER BY last_update DESC`). | ||
WillReturnRows(sqlmock.NewRows([]string{"id", "command", "last_update", "count"})) | ||
c := Client{Db: sqlx.NewDb(db, "sqlite3")} | ||
records, err := c.List(ListOptions{ | ||
Ids: []int64{100, 101, 102}, | ||
}) | ||
require.NoError(t, err) | ||
assert.Len(t, records, 3) | ||
ids := []int64{records[1].Id, records[2].Id} | ||
recordsByIds, err := c.List(ListOptions{Ids: ids}) | ||
assert.Len(t, recordsByIds, 2) | ||
assert.Len(t, records, 0) | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package client | ||
|
||
import ( | ||
"database/sql/driver" | ||
"errors" | ||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/jmoiron/sqlx" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
"time" | ||
) | ||
|
||
type AnyTime struct{} | ||
|
||
// Match satisfies sqlmock.Argument interface | ||
func (a AnyTime) Match(v driver.Value) bool { | ||
_, ok := v.(time.Time) | ||
return ok | ||
} | ||
|
||
func TestClient_Update(t *testing.T) { | ||
t.Run("succeed", func(t *testing.T) { | ||
db, mock, _ := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) | ||
mock.ExpectExec(`INSERT INTO hist (command, last_update) | ||
VALUES (?, ?) | ||
ON CONFLICT(command) | ||
DO UPDATE SET count=count+1, last_update=excluded.last_update`). | ||
WithArgs("ls -alF", AnyTime{}). | ||
WillReturnResult(sqlmock.NewResult(1, 1)) | ||
c := Client{Db: sqlx.NewDb(db, "sqlite3")} | ||
err := c.Update("ls -alF") | ||
require.NoError(t, err) | ||
}) | ||
t.Run("succeed", func(t *testing.T) { | ||
db, mock, _ := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual)) | ||
mock.ExpectExec(`INSERT INTO hist (command, last_update) | ||
VALUES (?, ?) | ||
ON CONFLICT(command) | ||
DO UPDATE SET count=count+1, last_update=excluded.last_update`). | ||
WillReturnError(errors.New("some error")) | ||
c := Client{Db: sqlx.NewDb(db, "sqlite3")} | ||
err := c.Update("ls -alF") | ||
require.Error(t, err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.