Implement first db-test using go-txdb
This commit is contained in:
parent
c3db535e10
commit
6ab8a96888
@ -16,7 +16,7 @@ func main() {
|
||||
log.Fatalf("Could not load config: %v", err)
|
||||
}
|
||||
|
||||
q, err := postgres.Connect(cfg.DatabaseConnection)
|
||||
q, err := postgres.Connect("pgx", cfg.DatabaseConnection)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed connecting to DB: %v", err)
|
||||
}
|
||||
|
@ -4,16 +4,23 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.javil.eu/jacob1123/budgeteer/bcrypt"
|
||||
"git.javil.eu/jacob1123/budgeteer/jwt"
|
||||
"git.javil.eu/jacob1123/budgeteer/postgres"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
txdb "github.com/DATA-DOG/go-txdb"
|
||||
)
|
||||
|
||||
func init() {
|
||||
txdb.Register("pgtx", "pgx", "postgres://budgeteer_test:budgeteer_test@localhost:5432/budgeteer_test")
|
||||
}
|
||||
|
||||
func TestListTimezonesHandler(t *testing.T) {
|
||||
db, err := postgres.Connect("postgres://budgeteer_test:budgeteer_test@localhost:5432/budgeteer_test")
|
||||
db, err := postgres.Connect("pgtx", "example")
|
||||
if err != nil {
|
||||
t.Errorf("could not connect to db: %s", err)
|
||||
return
|
||||
@ -29,6 +36,31 @@ func TestListTimezonesHandler(t *testing.T) {
|
||||
c, engine := gin.CreateTestContext(rr)
|
||||
h.LoadRoutes(engine)
|
||||
|
||||
t.Run("RegisterUser", func(t *testing.T) {
|
||||
c.Request, err = http.NewRequest(http.MethodPost, "/api/v1/user/register", strings.NewReader(`{"password":"pass","email":"info@example.com","name":"Test"}`))
|
||||
if err != nil {
|
||||
t.Errorf("error creating request: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
h.registerPost(c)
|
||||
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Errorf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK)
|
||||
}
|
||||
|
||||
var response LoginResponse
|
||||
err = json.NewDecoder(rr.Body).Decode(&response)
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
t.Error("Error registering")
|
||||
}
|
||||
if len(response.Token) == 0 {
|
||||
t.Error("Did not get a token")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("GetTransactions", func(t *testing.T) {
|
||||
c.Request, err = http.NewRequest(http.MethodGet, "/account/accountid/transactions", nil)
|
||||
if rr.Code != http.StatusOK {
|
||||
t.Errorf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK)
|
||||
@ -43,4 +75,5 @@ func TestListTimezonesHandler(t *testing.T) {
|
||||
if len(response.Transactions) == 0 {
|
||||
t.Error("Did not get any transactions.")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -84,11 +84,13 @@ func (h *Handler) loginPost(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, struct {
|
||||
c.JSON(http.StatusOK, LoginResponse{t, user, budgets})
|
||||
}
|
||||
|
||||
type LoginResponse struct {
|
||||
Token string
|
||||
User postgres.User
|
||||
Budgets []postgres.Budget
|
||||
}{t, user, budgets})
|
||||
}
|
||||
|
||||
type registerInformation struct {
|
||||
@ -108,13 +110,13 @@ func (h *Handler) registerPost(c *gin.Context) {
|
||||
|
||||
_, err := h.Service.GetUserByUsername(c.Request.Context(), register.Email)
|
||||
if err == nil {
|
||||
c.AbortWithError(http.StatusUnauthorized, err)
|
||||
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("email is already taken"))
|
||||
return
|
||||
}
|
||||
|
||||
hash, err := h.CredentialsVerifier.Hash(register.Password)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusUnauthorized, err)
|
||||
c.AbortWithError(http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -140,9 +142,5 @@ func (h *Handler) registerPost(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, struct {
|
||||
Token string
|
||||
User postgres.User
|
||||
Budgets []postgres.Budget
|
||||
}{t, user, budgets})
|
||||
c.JSON(http.StatusOK, LoginResponse{t, user, budgets})
|
||||
}
|
||||
|
@ -18,8 +18,8 @@ type Database struct {
|
||||
}
|
||||
|
||||
// Connect to a database
|
||||
func Connect(connString string) (*Database, error) {
|
||||
conn, err := sql.Open("pgx", connString)
|
||||
func Connect(typ string, connString string) (*Database, error) {
|
||||
conn, err := sql.Open(typ, connString)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open connection: %w", err)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user