fix(tfstated): add delete handler tests
This commit is contained in:
parent
7ec826921c
commit
c4ce5e8623
3 changed files with 58 additions and 13 deletions
|
@ -1,8 +1,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
|
@ -17,15 +15,13 @@ func handleDelete(db *database.DB) http.Handler {
|
|||
return
|
||||
}
|
||||
|
||||
if err := db.DeleteState(r.URL.Path); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
_ = errorResponse(w, http.StatusNotFound,
|
||||
fmt.Errorf("state path not found: %s", r.URL.Path))
|
||||
} else {
|
||||
_ = errorResponse(w, http.StatusInternalServerError, err)
|
||||
}
|
||||
} else {
|
||||
if success, err := db.DeleteState(r.URL.Path); err != nil {
|
||||
_ = errorResponse(w, http.StatusInternalServerError, err)
|
||||
} else if success {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
} else {
|
||||
_ = errorResponse(w, http.StatusNotFound,
|
||||
fmt.Errorf("state path not found: %s", r.URL.Path))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
41
cmd/tfstated/delete_test.go
Normal file
41
cmd/tfstated/delete_test.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
tests := []struct {
|
||||
method string
|
||||
uri *url.URL
|
||||
body io.Reader
|
||||
expect string
|
||||
status int
|
||||
msg string
|
||||
}{
|
||||
{"DELETE", &url.URL{Path: "/"}, nil, "", http.StatusBadRequest, "/"},
|
||||
{"DELETE", &url.URL{Path: "/non_existent_delete"}, nil, "", http.StatusNotFound, "non existent"},
|
||||
{"POST", &url.URL{Path: "/test_delete"}, strings.NewReader("the_test_delete"), "", http.StatusOK, "/test_delete"},
|
||||
{"DELETE", &url.URL{Path: "/test_delete"}, nil, "", http.StatusOK, "/test_delete"},
|
||||
{"DELETE", &url.URL{Path: "/test_delete"}, nil, "", http.StatusNotFound, "/test_delete"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
runHTTPRequest(tt.method, tt.uri, tt.body, func(r *http.Response, err error) {
|
||||
if err != nil {
|
||||
t.Fatalf("failed %s with error: %+v", tt.method, err)
|
||||
} else if r.StatusCode != tt.status {
|
||||
t.Fatalf("%s %s should %s, got %s", tt.method, tt.msg, http.StatusText(tt.status), http.StatusText(r.StatusCode))
|
||||
} else if tt.expect != "" {
|
||||
if body, err := io.ReadAll(r.Body); err != nil {
|
||||
t.Fatalf("failed to read body with error: %+v", err)
|
||||
} else if strings.Compare(string(body), tt.expect) != 0 {
|
||||
t.Fatalf("%s should have returned \"%s\", got %s", tt.method, tt.expect, string(body))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -5,9 +5,17 @@ import (
|
|||
"fmt"
|
||||
)
|
||||
|
||||
func (db *DB) DeleteState(name string) error {
|
||||
_, err := db.Exec(`DELETE FROM states WHERE name = ?;`, name)
|
||||
return err
|
||||
// returns true in case of successful deletion
|
||||
func (db *DB) DeleteState(name string) (bool, error) {
|
||||
result, err := db.Exec(`DELETE FROM states WHERE name = ?;`, name)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
n, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return n == 1, nil
|
||||
}
|
||||
|
||||
func (db *DB) GetState(name string) ([]byte, error) {
|
||||
|
|
Loading…
Add table
Reference in a new issue