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" ) 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 fixtures, _ := os.ReadFile("fixtures/groups.json") var groupFixtures struct { Groups []group.Group `json:"groups"` GroupUpdate group.Group `json:"group_update"` } json.Unmarshal(fixtures, &groupFixtures) s.group = groupFixtures.Groups[0] s.groupJSON, _ = json.Marshal(s.group) s.groupUpdate = groupFixtures.GroupUpdate } 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") }