package functiontest import ( "encoding/json" "fmt" "os" function "example.com/database/function" testrequest "example.com/test/testrequest" testsetup "example.com/test/testsetup" "github.com/gin-gonic/gin" "github.com/stretchr/testify/suite" ) type TestFunction struct { Result []function.Function `json:"result"` } type FunctionTestSuite struct { suite.Suite globalAuthHeader string router *gin.Engine function function.Function functionJSON []byte functionUpdate function.Function functionUpdateJSON []byte } func (s *FunctionTestSuite) SetupTestSuite() { s.globalAuthHeader, s.router = testsetup.SetupTestSuite() // Create Test Data fixtures, _ := os.ReadFile("fixtures/functions.json") var functionFixtures struct { Functions []function.Function `json:"functions"` FunctionUpdate function.Function `json:"function_update"` } json.Unmarshal(fixtures, &functionFixtures) s.function = functionFixtures.Functions[0] var functionTagIDs []uint for _, tag := range s.function.Tags { functionTagIDs = append(functionTagIDs, tag.ID) } var functionRequirementIDs []uint for _, requirement := range s.function.Requirements { functionRequirementIDs = append(functionRequirementIDs, requirement.ID) } s.functionJSON, _ = json.Marshal(function.FunctionParams{ Name: s.function.Name, Tags: functionTagIDs, Requirements: functionRequirementIDs, }) s.functionUpdate = functionFixtures.FunctionUpdate var functionUpdateTagIDs []uint for _, tag := range s.functionUpdate.Tags { functionUpdateTagIDs = append(functionUpdateTagIDs, tag.ID) } var functionUpdateRequirementIDs []uint for _, requirement := range s.functionUpdate.Requirements { functionUpdateRequirementIDs = append(functionUpdateRequirementIDs, requirement.ID) } s.functionUpdateJSON, _ = json.Marshal(function.FunctionParams{ Name: s.functionUpdate.Name, Tags: functionUpdateTagIDs, Requirements: functionUpdateRequirementIDs, }) for _, tag := range s.functionUpdate.Tags { tagBody, _ := json.Marshal(tag) testrequest.MakePostRequest(&tagBody, "/function-tag", s.globalAuthHeader, s.router) } } func (s *FunctionTestSuite) TearDownTestSuite() { os.Remove("db/main.db") } func (s *FunctionTestSuite) Test01CreateFunction() { // Setup variables var output TestFunction target := fmt.Sprintf("/function?id=%d", s.function.ID) // Attempt to create function fmt.Println(string(s.functionJSON)) status := testrequest.MakePostRequest(&s.functionJSON, "/function", s.globalAuthHeader, s.router) // Check that the request was successful s.Equal(status, "200 OK", "function created successfully") // Attempt to get function body := testrequest.MakeGetRequest(target, s.router) // Unmarshal the result json.Unmarshal(body, &output) // Check that the function listed matches the created function s.Equal(s.function.Name, output.Result[0].Name, "input name matches output name") } func (s *FunctionTestSuite) Test02GetFunction() { // Setup variables var output TestFunction // Attempt to get functions body := testrequest.MakeGetRequest("/function", s.router) // Unmarshal the result json.Unmarshal(body, &output) // Check that the functions listed match the created functions s.Equal(s.function.Name, output.Result[0].Name, "input name matches output name") } func (s *FunctionTestSuite) Test03UpdateFunction() { // Setup variables var output TestFunction target := fmt.Sprintf("/function?id=%d", s.function.ID) // Create needed field objects // Setup variables for _, requirement := range s.functionUpdate.Requirements { var requirementTagIDs []uint for _, tag := range s.function.Tags { requirementTagIDs = append(requirementTagIDs, tag.ID) } var requirementRequirementIDs []uint for _, requirement := range s.function.Requirements { requirementRequirementIDs = append(requirementRequirementIDs, requirement.ID) } requirementBody, _ := json.Marshal(function.FunctionParams{ Name: requirement.Name, Tags: requirementTagIDs, Requirements: requirementRequirementIDs, }) testrequest.MakePostRequest(&requirementBody, "/function", s.globalAuthHeader, s.router) } // Attempt to update function status := testrequest.MakePutRequest(&s.functionUpdateJSON, target, s.globalAuthHeader, s.router) // Check that the request was successful s.Equal(status, "200 OK", "function created successfully") // Attempt to get function body := testrequest.MakeGetRequest(target, s.router) // Unmarshal the result json.Unmarshal(body, &output) // Check that the function listed matches the updated function s.Equal(s.functionUpdate.Name, output.Result[0].Name, "input name matches output name") s.Equal(s.functionUpdate.Tags[0].Name, output.Result[0].Tags[0].Name, "input tags match output tags") s.Equal(s.functionUpdate.Requirements[0].Name, output.Result[0].Requirements[0].Name, "input requirements match output requirements") } func (s *FunctionTestSuite) Test04DeleteFunction() { // Setup variables var output TestFunction target := fmt.Sprintf("/function?id=%d", s.function.ID) // Attempt to delete function status := testrequest.MakeDeleteRequest(target, s.globalAuthHeader, s.router) // Check that the request was successful s.Equal(status, "200 OK", "function deleted successfully") // Attempt to get function body := testrequest.MakeGetRequest("/function", s.router) // Unmarshal the result json.Unmarshal(body, &output) // Check that the function list only has two entries s.Equal(2, len(output.Result), "function list has two entries left") }