package functiontest import ( "encoding/json" "fmt" "os" function "example.com/database/function" "example.com/database/functiontag" testrequest "example.com/test/testrequest" testsetup "example.com/test/testsetup" functiontagtest "example.com/unit/functiontagtest" "github.com/gin-gonic/gin" "github.com/stretchr/testify/suite" "gorm.io/gorm" ) 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 testFunctionTag := functiontag.FunctionTag{ Model: gorm.Model{ID: 1}, Name: "test", } testFunction := function.Function{ Model: gorm.Model{ID: 2}, Name: "test", Tags: []functiontag.FunctionTag{}, Requirements: []function.Function{}, } s.function = function.Function{ Model: gorm.Model{ID: 1}, Name: "another_test", Tags: []functiontag.FunctionTag{}, Requirements: []function.Function{}, } s.functionJSON, _ = json.Marshal(s.function) s.functionUpdate = function.Function{ Model: gorm.Model{ID: 1}, Name: "yet_another_test", Tags: []functiontag.FunctionTag{ testFunctionTag, }, Requirements: []function.Function{ testFunction, }, } s.functionUpdateJSON, _ = json.Marshal(function.FunctionParams{ Name: s.functionUpdate.Name, Tags: []uint{1}, Requirements: []uint{2}, }) } 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 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 var functionTagOutput functiontagtest.TestFunctionTag functionTagBody, _ := json.Marshal(s.functionUpdate.Tags[0]) // Attempt to create function tag functionTagStatus := testrequest.MakePostRequest(&functionTagBody, "/function-tag", s.globalAuthHeader, s.router) // Check that the request was successful s.Equal(functionTagStatus, "200 OK", "function tag created successfully") // Setup variables functionTagTarget := fmt.Sprintf("/function-tag?id=%d", s.functionUpdate.Tags[0].ID) // Attempt to get function tag functionTagBody = testrequest.MakeGetRequest(functionTagTarget, s.router) // Unamrshal the result json.Unmarshal(functionTagBody, &functionTagOutput) // Check that the function tag listed matches the created function tag s.Equal(s.functionUpdate.Tags[0].Name, functionTagOutput.Result[0].Name, "input function tag name matches created function tag name") // Setup variables var functionOutput TestFunction functionBody, _ := json.Marshal(s.functionUpdate.Requirements[0]) // Attempt to create function functionStatus := testrequest.MakePostRequest(&functionBody, "/function", s.globalAuthHeader, s.router) // Check that the request was successful s.Equal(functionStatus, "200 OK", "function created successfully") // Setup variables functionTarget := fmt.Sprintf("/function?id=%d", s.functionUpdate.Requirements[0].ID) // Attempt to get function functionBody = testrequest.MakeGetRequest(functionTarget, s.router) // Unmarshal the result json.Unmarshal(functionBody, &functionOutput) // Check that the function listed matches the created function s.Equal(s.functionUpdate.Requirements[0].Name, functionOutput.Result[0].Name, "input function name matches created function name") // 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(target, s.router) // Unmarshal the result json.Unmarshal(body, &output) // Check that the function list has one entry s.Equal(1, len(output.Result), "function list has one entry left") }