package functionsettest import ( "encoding/json" "fmt" "os" function "example.com/database/function" functionset "example.com/database/functionset" testrequest "example.com/test/testrequest" testsetup "example.com/test/testsetup" functiontest "example.com/unit/functiontest" "github.com/gin-gonic/gin" "github.com/stretchr/testify/suite" ) type TestFunctionSet struct { Result []functionset.FunctionSet `json:"result"` } type FunctionSetTestSuite struct { suite.Suite globalAuthHeader string router *gin.Engine functionSet functionset.FunctionSet functionSetJSON []byte functionSetUpdate functionset.FunctionSet functionSetUpdateJSON []byte } func (s *FunctionSetTestSuite) SetupTestSuite() { s.globalAuthHeader, s.router = testsetup.SetupTestSuite() // Create Test Data fixtures, _ := os.ReadFile("fixtures/functionset.json") var functionSetFixtures struct { FunctionSets []functionset.FunctionSet `json:"function_sets"` FunctionSetUpdate functionset.FunctionSet `json:"function_set_update"` } json.Unmarshal(fixtures, &functionSetFixtures) s.functionSet = functionSetFixtures.FunctionSets[0] var functionSetFunctionIDs []uint for _, function := range s.functionSet.Functions { functionSetFunctionIDs = append(functionSetFunctionIDs, function.ID) } s.functionSetJSON, _ = json.Marshal(functionset.FunctionSetParams{ Functions: functionSetFunctionIDs, }) s.functionSetUpdate = functionSetFixtures.FunctionSetUpdate var functionSetUpdateFunctionIDs []uint for _, function := range s.functionSetUpdate.Functions { functionSetUpdateFunctionIDs = append(functionSetUpdateFunctionIDs, function.ID) } s.functionSetUpdateJSON, _ = json.Marshal(functionset.FunctionSetParams{ Functions: functionSetUpdateFunctionIDs, }) } func (s *FunctionSetTestSuite) TearDownTestSuite() { os.Remove("db/main.db") } func (s *FunctionSetTestSuite) Test01CreateFunctionSet() { // Setup variables var output TestFunctionSet target := fmt.Sprintf("/function-set?id=%d", s.functionSet.ID) // Create needed field objects var functionOutput functiontest.TestFunction for _, function := range s.functionSet.Functions { functionBody, _ := json.Marshal(function) functionStatus := testrequest.MakePostRequest(&functionBody, "/function", s.globalAuthHeader, s.router) s.Equal(functionStatus, "200 OK", "function created successfully") functionTarget := fmt.Sprintf("/function?id=%d", function.ID) functionBody = testrequest.MakeGetRequest(functionTarget, s.router) json.Unmarshal(functionBody, &functionOutput) s.Equal(function.Name, functionOutput.Result[0].Name, "input function name matches output function name") } // Attempt to create function set status := testrequest.MakePostRequest(&s.functionSetJSON, "/function-set", s.globalAuthHeader, s.router) // Check that the request was successful s.Equal(status, "200 OK", "function created successfully") // Attempt to get function set body := testrequest.MakeGetRequest(target, s.router) json.Unmarshal(body, &output) // Check that the function set listed matches the created function set s.Equal(s.functionSet.Functions[0].Name, output.Result[0].Functions[0].Name, "first input function name matches first output function name") } func (s *FunctionSetTestSuite) Test02GetFunctionSet() { // Setup variables var output TestFunctionSet // Attempt to get function sets body := testrequest.MakeGetRequest("/function-set", s.router) json.Unmarshal(body, &output) // Check that the function sets listed match the created function sets s.Equal(s.functionSet.Functions[0].Name, output.Result[0].Functions[0].Name, "first input function name matches first output function name") } func (s *FunctionSetTestSuite) Test03UpdateFunctionSet() { // Setup variables var output TestFunctionSet target := fmt.Sprintf("/function-set?id=%d", s.functionSet.ID) // Create needed field objects var functionOutput functiontest.TestFunction for _, updateFunction := range s.functionSetUpdate.Functions { var functionTagIDs []uint for _, tag := range updateFunction.Tags { tagBody, _ := json.Marshal(tag) testrequest.MakePostRequest(&tagBody, "/function-tag", s.globalAuthHeader, s.router) functionTagIDs = append(functionTagIDs, tag.ID) } var functionRequirementIDs []uint for _, requirement := range updateFunction.Requirements { functionRequirementIDs = append(functionRequirementIDs, requirement.ID) } functionBody, _ := json.Marshal(function.FunctionParams{ Name: updateFunction.Name, Tags: functionTagIDs, Requirements: functionRequirementIDs, }) functionStatus := testrequest.MakePostRequest(&functionBody, "/function", s.globalAuthHeader, s.router) s.Equal(functionStatus, "200 OK", "function created successfully") functionTarget := fmt.Sprintf("/function?id=%d", updateFunction.ID) functionBody = testrequest.MakeGetRequest(functionTarget, s.router) json.Unmarshal(functionBody, &functionOutput) s.Equal(updateFunction.Name, functionOutput.Result[0].Name, "input function name matches output function name") } // Attempt to update function set status := testrequest.MakePutRequest(&s.functionSetUpdateJSON, target, s.globalAuthHeader, s.router) // Check that the request was successful s.Equal(status, "200 OK", "function set created successfully") // Attempt to get function set body := testrequest.MakeGetRequest(target, s.router) json.Unmarshal(body, &output) // Check that the function set listed matches the updated function s.Equal(s.functionSetUpdate.Functions[0].Name, output.Result[0].Functions[0].Name, "first updated function name matches first output function name") s.Equal(s.functionSetUpdate.Functions[1].Name, output.Result[0].Functions[1].Name, "second updated function name matches second output function name") } func (s *FunctionSetTestSuite) Test04DeleteFunctionSet() { // Setup variables var output TestFunctionSet target := fmt.Sprintf("/function-set?id=%d", s.functionSet.ID) // Attempt to delete function set 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 set body := testrequest.MakeGetRequest("/function-set", s.router) json.Unmarshal(body, &output) // Check that the function set list is empty s.Equal(0, len(output.Result), "function set list is empty") }