test: add tests for training endpoints
This commit is contained in:
parent
9800573814
commit
103241182e
1 changed files with 113 additions and 0 deletions
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/shopspring/decimal"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"gitlab.mareshq.com/hq/yggdrasil/pkg/training"
|
"gitlab.mareshq.com/hq/yggdrasil/pkg/training"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -121,4 +122,116 @@ func TestServer(t *testing.T) {
|
||||||
assert.Equal(t, newTraining.Days, resultTraining.Days)
|
assert.Equal(t, newTraining.Days, resultTraining.Days)
|
||||||
//assert.Equal(t, newTraining.Pricing, resultTraining.Pricing)
|
//assert.Equal(t, newTraining.Pricing, resultTraining.Pricing)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("Training not found", func(t *testing.T) {
|
||||||
|
rr, _ := doGet(t, app, "/v1/trainings/97b0e954-14f3-4908-98fa-271f505056d3")
|
||||||
|
assert.Equal(t, http.StatusNotFound, rr.StatusCode)
|
||||||
|
|
||||||
|
var trainingError NotFoundError
|
||||||
|
err := json.NewDecoder(rr.Body).Decode(&trainingError)
|
||||||
|
assert.NoError(t, err, "error getting response", err)
|
||||||
|
assert.Equal(t, http.StatusNotFound, trainingError.Status)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("List all trainings", func(t *testing.T) {
|
||||||
|
tr := &training.Training{
|
||||||
|
Name: "Testing Training",
|
||||||
|
Description: "This is a test training",
|
||||||
|
Days: 1,
|
||||||
|
Pricing: []training.TrainingPrice{
|
||||||
|
{
|
||||||
|
Amount: decimal.NewFromInt(200),
|
||||||
|
Currency: "EUR",
|
||||||
|
Type: training.OpenTrainingPrice,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
_ = handlers.trainingRepository.Create(tr)
|
||||||
|
|
||||||
|
rr, _ := doGet(t, app, "/v1/trainings")
|
||||||
|
assert.Equal(t, http.StatusOK, rr.StatusCode)
|
||||||
|
|
||||||
|
var trainings []Training
|
||||||
|
err := json.NewDecoder(rr.Body).Decode(&trainings)
|
||||||
|
assert.NoError(t, err, "error unmarshalling response")
|
||||||
|
assert.Greater(t, len(trainings), 0)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Update training", func(t *testing.T) {
|
||||||
|
tr := &training.Training{
|
||||||
|
Name: "Testing Training",
|
||||||
|
Description: "This is a test training",
|
||||||
|
Days: 1,
|
||||||
|
Pricing: []training.TrainingPrice{
|
||||||
|
{
|
||||||
|
Amount: decimal.NewFromInt(200),
|
||||||
|
Currency: "EUR",
|
||||||
|
Type: training.OpenTrainingPrice,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = handlers.trainingRepository.Create(tr)
|
||||||
|
|
||||||
|
updTr := NewTraining{
|
||||||
|
Name: "Updated Training",
|
||||||
|
Description: tr.Description,
|
||||||
|
Days: tr.Days,
|
||||||
|
Pricing: []TrainingPrice{
|
||||||
|
//{
|
||||||
|
// Amount: decimal.NewFromInt(200),
|
||||||
|
// Currency: "EUR",
|
||||||
|
// Type: OPEN,
|
||||||
|
//},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
rr, _ := doPut(t, app, "/v1/trainings/"+tr.ID.String(), updTr)
|
||||||
|
assert.Equal(t, http.StatusOK, rr.StatusCode)
|
||||||
|
|
||||||
|
var trr Training
|
||||||
|
err := json.NewDecoder(rr.Body).Decode(&trr)
|
||||||
|
assert.NoError(t, err, "error unmarshalling response")
|
||||||
|
assert.Equal(t, updTr.Name, trr.Name)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Delete training", func(t *testing.T) {
|
||||||
|
tr := &training.Training{
|
||||||
|
Name: "Testing Training",
|
||||||
|
Description: "This is a test training",
|
||||||
|
Days: 1,
|
||||||
|
Pricing: []training.TrainingPrice{
|
||||||
|
{
|
||||||
|
Amount: decimal.NewFromInt(200),
|
||||||
|
Currency: "EUR",
|
||||||
|
Type: training.OpenTrainingPrice,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = handlers.trainingRepository.Create(tr)
|
||||||
|
|
||||||
|
rr, _ := doGet(t, app, "/v1/trainings/"+tr.ID.String())
|
||||||
|
assert.Equal(t, http.StatusOK, rr.StatusCode)
|
||||||
|
|
||||||
|
var trr Training
|
||||||
|
err := json.NewDecoder(rr.Body).Decode(&trr)
|
||||||
|
assert.NoError(t, err, "error unmarshalling response")
|
||||||
|
assert.Equal(t, tr.ID, trr.Id)
|
||||||
|
|
||||||
|
drr, _ := doDelete(t, app, "/v1/trainings/"+tr.ID.String())
|
||||||
|
assert.Equal(t, http.StatusNoContent, drr.StatusCode)
|
||||||
|
|
||||||
|
rr, _ = doGet(t, app, "/v1/trainings/"+tr.ID.String())
|
||||||
|
assert.Equal(t, http.StatusNotFound, rr.StatusCode)
|
||||||
|
|
||||||
|
var trainingError NotFoundError
|
||||||
|
err = json.NewDecoder(rr.Body).Decode(&trainingError)
|
||||||
|
assert.NoError(t, err, "error getting response", err)
|
||||||
|
assert.Equal(t, http.StatusNotFound, trainingError.Status)
|
||||||
|
|
||||||
|
lrr, _ := doGet(t, app, "/v1/trainings")
|
||||||
|
assert.Equal(t, http.StatusOK, lrr.StatusCode)
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Reference in a new issue