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