refactor(server): testing server
This commit is contained in:
parent
8fd2dffcb9
commit
05deed2452
3 changed files with 178 additions and 30 deletions
124
internal/server/server_test.go
Normal file
124
internal/server/server_test.go
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gitlab.mareshq.com/hq/yggdrasil/pkg/training"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func doGet(t *testing.T, app *fiber.App, rawURL string) (*http.Response, error) {
|
||||
|
||||
u, err := url.Parse(rawURL)
|
||||
if err != nil {
|
||||
t.Fatalf("Invalid url: %s", rawURL)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", u.RequestURI(), nil)
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Host = u.Host
|
||||
|
||||
return app.Test(req)
|
||||
}
|
||||
|
||||
func doPost(t *testing.T, app *fiber.App, rawURL string, jsonBody interface{}) (*http.Response, error) {
|
||||
u, err := url.Parse(rawURL)
|
||||
if err != nil {
|
||||
t.Fatalf("Invalid url: %s", rawURL)
|
||||
}
|
||||
|
||||
buf, err := json.Marshal(jsonBody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req := httptest.NewRequest("POST", u.RequestURI(), bytes.NewReader(buf))
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Host = u.Host
|
||||
return app.Test(req)
|
||||
}
|
||||
|
||||
func doPut(t *testing.T, app *fiber.App, rawURL string, jsonBody interface{}) (*http.Response, error) {
|
||||
u, err := url.Parse(rawURL)
|
||||
if err != nil {
|
||||
t.Fatalf("Invalid url: %s", rawURL)
|
||||
}
|
||||
|
||||
buf, err := json.Marshal(jsonBody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req := httptest.NewRequest("PUT", u.RequestURI(), bytes.NewReader(buf))
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Host = u.Host
|
||||
return app.Test(req)
|
||||
}
|
||||
|
||||
func doDelete(t *testing.T, app *fiber.App, rawURL string) (*http.Response, error) {
|
||||
u, err := url.Parse(rawURL)
|
||||
if err != nil {
|
||||
t.Fatalf("Invalid url: %s", rawURL)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("DELETE", u.RequestURI(), nil)
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Host = u.Host
|
||||
return app.Test(req)
|
||||
}
|
||||
|
||||
func TestServer(t *testing.T) {
|
||||
//var err error
|
||||
trainingRepo := training.NewInMemoryTrainingRepository()
|
||||
trainingDateRepo := training.NewInMemoryTrainingDateRepository()
|
||||
trainingDateAttendeeRepo := training.NewInMemoryTrainingDateAttendeeRepository()
|
||||
|
||||
handlers := NewAPIHandlers(trainingRepo, trainingDateRepo, trainingDateAttendeeRepo)
|
||||
app := NewTestingServer(handlers)
|
||||
|
||||
t.Run("Add training", func(t *testing.T) {
|
||||
newTraining := NewTraining{
|
||||
Name: "Testing Training",
|
||||
Description: "This is a test training",
|
||||
Days: 1,
|
||||
Pricing: []TrainingPrice{
|
||||
//{
|
||||
// Amount: decimal.NewFromInt(200),
|
||||
// Currency: "EUR",
|
||||
// Type: OPEN,
|
||||
//},
|
||||
//{
|
||||
// Amount: decimal.NewFromInt(1000),
|
||||
// Currency: "EUR",
|
||||
// Type: CORPORATE,
|
||||
//},
|
||||
//{
|
||||
// Amount: decimal.NewFromInt(4900),
|
||||
// Currency: "CZK",
|
||||
// Type: OPEN,
|
||||
//},
|
||||
//{
|
||||
// Amount: decimal.NewFromInt(24000),
|
||||
// Currency: "CZK",
|
||||
// Type: CORPORATE,
|
||||
//},
|
||||
},
|
||||
}
|
||||
|
||||
rr, _ := doPost(t, app, "/v1/trainings", newTraining)
|
||||
assert.Equal(t, http.StatusCreated, rr.StatusCode)
|
||||
|
||||
var resultTraining Training
|
||||
err := json.NewDecoder(rr.Body).Decode(&resultTraining)
|
||||
assert.NoError(t, err, "error unmarshalling response")
|
||||
assert.Equal(t, newTraining.Name, resultTraining.Name)
|
||||
assert.Equal(t, newTraining.Description, resultTraining.Description)
|
||||
assert.Equal(t, newTraining.Days, resultTraining.Days)
|
||||
//assert.Equal(t, newTraining.Pricing, resultTraining.Pricing)
|
||||
})
|
||||
}
|
||||
Reference in a new issue