2025-05-16 22:51:54 -05:00
|
|
|
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
|
2025-05-26 21:05:53 -05:00
|
|
|
fixtures, _ := os.ReadFile("fixtures/functions.json")
|
|
|
|
var functionFixtures struct {
|
|
|
|
Functions []function.Function `json:"functions"`
|
|
|
|
FunctionUpdate function.Function `json:"function_update"`
|
2025-05-16 22:51:54 -05:00
|
|
|
}
|
2025-05-26 21:05:53 -05:00
|
|
|
json.Unmarshal(fixtures, &functionFixtures)
|
|
|
|
s.function = functionFixtures.Functions[0]
|
|
|
|
var functionTagIDs []uint
|
|
|
|
for _, tag := range s.function.Tags {
|
|
|
|
functionTagIDs = append(functionTagIDs, tag.ID)
|
2025-05-16 22:51:54 -05:00
|
|
|
}
|
2025-05-26 21:05:53 -05:00
|
|
|
var functionRequirementIDs []uint
|
|
|
|
for _, requirement := range s.function.Requirements {
|
|
|
|
functionRequirementIDs = append(functionRequirementIDs, requirement.ID)
|
2025-05-16 22:51:54 -05:00
|
|
|
}
|
2025-05-26 21:05:53 -05:00
|
|
|
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)
|
2025-05-16 22:51:54 -05:00
|
|
|
}
|
|
|
|
s.functionUpdateJSON, _ = json.Marshal(function.FunctionParams{
|
|
|
|
Name: s.functionUpdate.Name,
|
2025-05-26 21:05:53 -05:00
|
|
|
Tags: functionUpdateTagIDs,
|
|
|
|
Requirements: functionUpdateRequirementIDs,
|
2025-05-16 22:51:54 -05:00
|
|
|
})
|
2025-05-26 21:05:53 -05:00
|
|
|
for _, tag := range s.functionUpdate.Tags {
|
|
|
|
tagBody, _ := json.Marshal(tag)
|
|
|
|
testrequest.MakePostRequest(&tagBody, "/function-tag", s.globalAuthHeader, s.router)
|
|
|
|
}
|
2025-05-16 22:51:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2025-05-26 21:05:53 -05:00
|
|
|
fmt.Println(string(s.functionJSON))
|
2025-05-16 22:51:54 -05:00
|
|
|
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
|
2025-05-26 21:05:53 -05:00
|
|
|
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)
|
|
|
|
}
|
2025-05-16 22:51:54 -05:00
|
|
|
// 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
|
2025-05-26 21:05:53 -05:00
|
|
|
body := testrequest.MakeGetRequest("/function", s.router)
|
2025-05-16 22:51:54 -05:00
|
|
|
// Unmarshal the result
|
|
|
|
json.Unmarshal(body, &output)
|
2025-05-26 21:05:53 -05:00
|
|
|
// Check that the function list only has two entries
|
|
|
|
s.Equal(2, len(output.Result), "function list has two entries left")
|
2025-05-16 22:51:54 -05:00
|
|
|
}
|