-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
76 lines (63 loc) · 2.51 KB
/
server_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"github.com/deepmap/oapi-codegen/pkg/testutil"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
"testing"
api2 "webrunner_configurator/internal"
"webrunner_configurator/internal/gen/model"
"webrunner_configurator/internal/gen/server"
"webrunner_configurator/internal/repository/simple"
)
func TestCRUDHandler(t *testing.T) {
e := echo.New()
/* db, mock, err := sqlmock.New()
connector, err := gorm.Open("mysql", db)
connector.LogMode(true)*/
repoBuilder := simple.NewBuilder()
dbRepository, err := repoBuilder.Build()
require.NoError(t, err)
handler := api2.NewCRUDHandler(dbRepository)
e.Use(middleware.Logger())
server.RegisterHandlers(e, handler)
newConfig := model.NewConfig{
Category: new(string),
Cluster: new(string),
Container: "Container",
Desc: new(string),
Script: "script",
TaskDefinition: new(string),
Path: "Path",
}
result := testutil.NewRequest().Post("/configs").WithJsonBody(newConfig).Go(t, e)
// We expect 201 code on successful pet insertion
assert.Equal(t, http.StatusCreated, result.Code())
var taskConfig = new(model.TaskConfig)
err = result.UnmarshalBodyToObject(taskConfig)
assert.NoError(t, err, "error unmarshaling response")
assert.Equal(t, newConfig, taskConfig.NewConfig)
assert.Equal(t, int64(0), taskConfig.Id)
result = testutil.NewRequest().Get("/configs").Go(t, e)
assert.Equal(t, http.StatusOK, result.Code())
var configs []model.TaskConfig
err = result.UnmarshalBodyToObject(&configs)
assert.NoError(t, err, "error unmarshaling response")
assert.Equal(t, 1, len(configs))
id := configs[0].Id
result = testutil.NewRequest().Put(fmt.Sprintf("/configs/%d", id)).WithJsonBody(newConfig).Go(t, e)
assert.Equal(t, http.StatusOK, result.Code())
taskConfig = new(model.TaskConfig)
err = result.UnmarshalBodyToObject(taskConfig)
assert.NoError(t, err, "error unmarshaling response")
assert.Equal(t, model.TaskConfig{NewConfig: newConfig, Id: id}, *taskConfig)
result = testutil.NewRequest().Delete(fmt.Sprintf("/configs/%d", id)).Go(t, e)
assert.Equal(t, http.StatusNoContent, result.Code())
result = testutil.NewRequest().Put(fmt.Sprintf("/configs/%d", id)).WithJsonBody(newConfig).Go(t, e)
assert.Equal(t, http.StatusNotFound, result.Code())
result = testutil.NewRequest().Delete(fmt.Sprintf("/configs/%d", id)).Go(t, e)
assert.Equal(t, http.StatusNotFound, result.Code())
}