toSQL function for QueryProber struct

Make the unit test easier
This commit is contained in:
ditatompel 2024-05-30 20:14:16 +07:00
parent acc70111c8
commit 8b5bdc7523
No known key found for this signature in database
GPG key ID: 31D3D06D77950979
3 changed files with 124 additions and 29 deletions

View file

@ -85,7 +85,6 @@ func TestQueryNodes_toSQL(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotArgs, gotWhere, gotSortBy, gotSortDirection := tt.query.toSQL()
if !equalArgs(gotArgs, tt.wantArgs) {
t.Errorf("QueryNodes.toSQL() gotArgs = %v, want %v", gotArgs, tt.wantArgs)
}

View file

@ -38,7 +38,7 @@ func NewProber() ProberRepository {
}
// Add a new prober machine
func (repo *ProberRepo) Add(name string) (Prober, error) {
func (r *ProberRepo) Add(name string) (Prober, error) {
apiKey := uuid.New()
query := `
INSERT INTO tbl_prober (
@ -50,7 +50,7 @@ func (repo *ProberRepo) Add(name string) (Prober, error) {
?,
?
)`
_, err := repo.db.Exec(query, name, apiKey, 0)
_, err := r.db.Exec(query, name, apiKey, 0)
if err != nil {
return Prober{}, err
}
@ -58,9 +58,9 @@ func (repo *ProberRepo) Add(name string) (Prober, error) {
}
// Edit an existing prober
func (repo *ProberRepo) Edit(id int, name string) error {
func (r *ProberRepo) Edit(id int, name string) error {
query := `UPDATE tbl_prober SET name = ? WHERE id = ?`
res, err := repo.db.Exec(query, name, id)
res, err := r.db.Exec(query, name, id)
if err != nil {
return err
}
@ -75,8 +75,8 @@ func (repo *ProberRepo) Edit(id int, name string) error {
}
// Delete an existing prober
func (repo *ProberRepo) Delete(id int) error {
res, err := repo.db.Exec(`DELETE FROM tbl_prober WHERE id = ?`, id)
func (r *ProberRepo) Delete(id int) error {
res, err := r.db.Exec(`DELETE FROM tbl_prober WHERE id = ?`, id)
if err != nil {
return err
}
@ -96,33 +96,34 @@ type QueryProbers struct {
SortDirection string
}
func (repo *ProberRepo) Probers(q QueryProbers) ([]Prober, error) {
queryParams := []interface{}{}
whereQueries := []string{}
where := ""
func (q QueryProbers) toSQL() (args []interface{}, where, sortBy, sortDirection string) {
wq := []string{}
if q.Search != "" {
whereQueries = append(whereQueries, "(name LIKE ? OR api_key LIKE ?)")
queryParams = append(queryParams, "%"+q.Search+"%")
queryParams = append(queryParams, "%"+q.Search+"%")
wq = append(wq, "(name LIKE ? OR api_key LIKE ?)")
args = append(args, "%"+q.Search+"%", "%"+q.Search+"%")
}
if len(wq) > 0 {
where = "WHERE " + strings.Join(wq, " AND ")
}
if len(whereQueries) > 0 {
where = "WHERE " + strings.Join(whereQueries, " AND ")
}
var probers []Prober
allowedSort := []string{"id", "last_submit_ts"}
sortBy := "last_submit_ts"
if slices.Contains(allowedSort, q.SortBy) {
as := []string{"id", "last_submit_ts"}
sortBy = "last_submit_ts"
if slices.Contains(as, q.SortBy) {
sortBy = q.SortBy
}
sortDirection := "DESC"
sortDirection = "DESC"
if q.SortDirection == "asc" {
sortDirection = "ASC"
}
return args, where, sortBy, sortDirection
}
func (r *ProberRepo) Probers(q QueryProbers) ([]Prober, error) {
args, where, sortBy, sortDirection := q.toSQL()
var probers []Prober
query := fmt.Sprintf(`
SELECT
id,
@ -134,7 +135,7 @@ func (repo *ProberRepo) Probers(q QueryProbers) ([]Prober, error) {
%s -- where clause if any
ORDER BY %s %s`, where, sortBy, sortDirection)
row, err := repo.db.Query(query, queryParams...)
row, err := r.db.Query(query, args...)
if err != nil {
return probers, err
}
@ -151,7 +152,7 @@ func (repo *ProberRepo) Probers(q QueryProbers) ([]Prober, error) {
return probers, nil
}
func (repo *ProberRepo) CheckApi(key string) (Prober, error) {
func (r *ProberRepo) CheckApi(key string) (Prober, error) {
var p Prober
query := `
SELECT
@ -164,6 +165,6 @@ func (repo *ProberRepo) CheckApi(key string) (Prober, error) {
WHERE
api_key = ?
LIMIT 1`
err := repo.db.QueryRow(query, key).Scan(&p.ID, &p.Name, &p.ApiKey, &p.LastSubmitTs)
err := r.db.QueryRow(query, key).Scan(&p.ID, &p.Name, &p.ApiKey, &p.LastSubmitTs)
return p, err
}

View file

@ -4,11 +4,106 @@ import (
"testing"
)
func TestQueryProbers_toSQL(t *testing.T) {
tests := []struct {
name string
query QueryProbers
wantArgs []interface{}
wantWhere string
wantSortBy string
wantSortDirection string
}{
// TODO: Add test cases.
{
name: "Default query",
query: QueryProbers{
Search: "",
SortBy: "last_submit_ts",
SortDirection: "desc",
},
wantArgs: []interface{}{},
wantWhere: "",
wantSortBy: "last_submit_ts",
wantSortDirection: "DESC",
},
{
name: "With name or api_key query",
query: QueryProbers{
Search: "test",
SortBy: "last_submit_ts",
SortDirection: "desc",
},
wantArgs: []interface{}{"%test%", "%test%"},
wantWhere: "WHERE (name LIKE ? OR api_key LIKE ?)",
wantSortBy: "last_submit_ts",
wantSortDirection: "DESC",
},
{
name: "With sort direction",
query: QueryProbers{
Search: "test",
SortBy: "last_submit_ts",
SortDirection: "asc",
},
wantArgs: []interface{}{"%test%", "%test%"},
wantWhere: "WHERE (name LIKE ? OR api_key LIKE ?)",
wantSortBy: "last_submit_ts",
wantSortDirection: "ASC",
},
{
name: "With sort by ID",
query: QueryProbers{
Search: "test",
SortBy: "id",
SortDirection: "asc",
},
wantArgs: []interface{}{"%test%", "%test%"},
wantWhere: "WHERE (name LIKE ? OR api_key LIKE ?)",
wantSortBy: "id",
wantSortDirection: "ASC",
},
{
name: "With invalid sort by name and direction",
query: QueryProbers{
Search: "test",
SortBy: "invalid",
SortDirection: "invalid",
},
wantArgs: []interface{}{"%test%", "%test%"},
wantWhere: "WHERE (name LIKE ? OR api_key LIKE ?)",
wantSortBy: "last_submit_ts",
wantSortDirection: "DESC",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
q := QueryProbers{
Search: tt.query.Search,
SortBy: tt.query.SortBy,
SortDirection: tt.query.SortDirection,
}
gotArgs, gotWhere, gotSortBy, gotSortDirection := q.toSQL()
if !equalArgs(gotArgs, tt.wantArgs) {
t.Errorf("QueryNodes.toSQL() gotArgs = %v, want %v", gotArgs, tt.wantArgs)
}
if gotWhere != tt.wantWhere {
t.Errorf("QueryProbers.toSQL() gotWhere = %v, want %v", gotWhere, tt.wantWhere)
}
if gotSortBy != tt.wantSortBy {
t.Errorf("QueryProbers.toSQL() gotSortBy = %v, want %v", gotSortBy, tt.wantSortBy)
}
if gotSortDirection != tt.wantSortDirection {
t.Errorf("QueryProbers.toSQL() gotSortDirection = %v, want %v", gotSortDirection, tt.wantSortDirection)
}
})
}
}
// TODO: Add database test table and then clean it up
func TestProberRepo_CheckApi(t *testing.T) {
if !testMySQL {
t.Skip("Skip test, not connected to database")
t.Skip("Skip integration test, not connected to database")
}
tests := []struct {
name string