106 lines
3.1 KiB
Go
106 lines
3.1 KiB
Go
package grouptest
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
|
|
group "example.com/database/group"
|
|
testrequest "example.com/test/testrequest"
|
|
testsetup "example.com/test/testsetup"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/suite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type TestGroup struct {
|
|
Result []group.Group `json:"result"`
|
|
}
|
|
|
|
type GroupTestSuite struct {
|
|
suite.Suite
|
|
globalAuthHeader string
|
|
router *gin.Engine
|
|
group group.Group
|
|
groupJSON []byte
|
|
groupUpdate group.Group
|
|
}
|
|
|
|
func (s *GroupTestSuite) SetupTestSuite() {
|
|
s.globalAuthHeader, s.router = testsetup.SetupTestSuite()
|
|
// Create Test Data
|
|
s.group = group.Group{Model: gorm.Model{ID: 1}, Name: "test"}
|
|
s.groupJSON, _ = json.Marshal(s.group)
|
|
s.groupUpdate = group.Group{Model: gorm.Model{ID: 1}, Name: "another_test"}
|
|
}
|
|
|
|
func (s *GroupTestSuite) TearDownTestSuite() {
|
|
os.Remove("db/main.db")
|
|
}
|
|
|
|
func (s *GroupTestSuite) Test01CreateGroup() {
|
|
// Setup variables
|
|
var output TestGroup
|
|
target := fmt.Sprintf("/group?id=%d", s.group.ID)
|
|
|
|
// Attempt to create group
|
|
status := testrequest.MakePostRequest(&s.groupJSON, "/group", s.globalAuthHeader, s.router)
|
|
// Check that the request was successful
|
|
s.Equal(status, "200 OK", "group created successfully")
|
|
|
|
// Attempt to get group
|
|
body := testrequest.MakeGetRequest(target, s.router)
|
|
// Unmarshal the result
|
|
json.Unmarshal(body, &output)
|
|
// Check that the group listed matches the created group
|
|
s.Equal(s.group.Name, output.Result[0].Name, "input name matches output name")
|
|
}
|
|
|
|
func (s *GroupTestSuite) Test02GetGroup() {
|
|
// Setup variables
|
|
var output TestGroup
|
|
|
|
// Attempt to get groups
|
|
body := testrequest.MakeGetRequest("/group", s.router)
|
|
// Unmarshal the result
|
|
json.Unmarshal(body, &output)
|
|
// Check that the groups listed match the created groups
|
|
s.Equal(s.group.Name, output.Result[0].Name, "input name matches output name")
|
|
}
|
|
|
|
func (s *GroupTestSuite) Test03UpdateGroup() {
|
|
// Setup variables
|
|
var output TestGroup
|
|
body, _ := json.Marshal(s.groupUpdate)
|
|
target := fmt.Sprintf("/group?id=%d", s.group.ID)
|
|
|
|
// Attempt to update group
|
|
status := testrequest.MakePutRequest(&body, target, s.globalAuthHeader, s.router)
|
|
// Check that the request was successful
|
|
s.Equal(status, "200 OK", "group created successfully")
|
|
|
|
// Attempt to get group
|
|
body = testrequest.MakeGetRequest(target, s.router)
|
|
// Unmarshal the result
|
|
json.Unmarshal(body, &output)
|
|
// Check that the group listed matches the updated group
|
|
s.Equal(s.groupUpdate.Name, output.Result[0].Name, "input name matches output name")
|
|
}
|
|
|
|
func (s *GroupTestSuite) Test04DeleteGroup() {
|
|
// Setup variables
|
|
var output TestGroup
|
|
target := fmt.Sprintf("/group?id=%d", s.group.ID)
|
|
|
|
// Attempt to delete group
|
|
status := testrequest.MakeDeleteRequest(target, s.globalAuthHeader, s.router)
|
|
// Check that the request was successful
|
|
s.Equal(status, "200 OK", "group deleted successfully")
|
|
|
|
// Attempt to get group
|
|
body := testrequest.MakeGetRequest(target, s.router)
|
|
// Unmarshal the result
|
|
json.Unmarshal(body, &output)
|
|
// Check that the group list is empty
|
|
s.Equal(len(output.Result), 0, "input name matches output name")
|
|
}
|