2025-05-16 22:51:54 -05:00
|
|
|
package persontest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
person "example.com/database/person"
|
|
|
|
testrequest "example.com/test/testrequest"
|
|
|
|
testsetup "example.com/test/testsetup"
|
|
|
|
grouptest "example.com/unit/grouptest"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestPerson struct {
|
|
|
|
Result []person.Person `json:"result"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PersonTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
globalAuthHeader string
|
|
|
|
router *gin.Engine
|
|
|
|
person person.Person
|
|
|
|
personJSON []byte
|
|
|
|
personUpdate person.Person
|
|
|
|
personUpdateJSON []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PersonTestSuite) SetupTestSuite() {
|
|
|
|
s.globalAuthHeader, s.router = testsetup.SetupTestSuite()
|
|
|
|
// Create Test Data
|
2025-05-26 21:05:53 -05:00
|
|
|
fixtures, _ := os.ReadFile("fixtures/persons.json")
|
|
|
|
var personFixtures struct {
|
|
|
|
Persons []person.Person `json:"persons"`
|
|
|
|
PersonUpdate person.Person `json:"person_update"`
|
2025-05-16 22:51:54 -05:00
|
|
|
}
|
2025-05-26 21:05:53 -05:00
|
|
|
json.Unmarshal(fixtures, &personFixtures)
|
|
|
|
s.person = personFixtures.Persons[0]
|
|
|
|
var personGroupsIDs []uint
|
|
|
|
for _, group := range s.person.Groups {
|
|
|
|
personGroupsIDs = append(personGroupsIDs, group.ID)
|
2025-05-16 22:51:54 -05:00
|
|
|
}
|
2025-05-26 21:05:53 -05:00
|
|
|
s.personJSON, _ = json.Marshal(person.PersonParams{
|
|
|
|
Name: s.person.Name,
|
|
|
|
Groups: personGroupsIDs,
|
|
|
|
})
|
|
|
|
s.personUpdate = personFixtures.PersonUpdate
|
|
|
|
var personUpdateGroupsIDs []uint
|
|
|
|
for _, group := range s.personUpdate.Groups {
|
|
|
|
personUpdateGroupsIDs = append(personUpdateGroupsIDs, group.ID)
|
2025-05-16 22:51:54 -05:00
|
|
|
}
|
|
|
|
s.personUpdateJSON, _ = json.Marshal(person.PersonParams{
|
|
|
|
Name: s.personUpdate.Name,
|
2025-05-26 21:05:53 -05:00
|
|
|
Groups: personUpdateGroupsIDs,
|
2025-05-16 22:51:54 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PersonTestSuite) TearDownTestSuite() {
|
|
|
|
os.Remove("db/main.db")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PersonTestSuite) Test01CreatePerson() {
|
|
|
|
// Setup variables
|
|
|
|
var output TestPerson
|
|
|
|
target := fmt.Sprintf("/person?id=%d", s.person.ID)
|
|
|
|
|
|
|
|
// Attempt to create person
|
|
|
|
status := testrequest.MakePostRequest(&s.personJSON, "/person", s.globalAuthHeader, s.router)
|
|
|
|
// Check that the request was successful
|
|
|
|
s.Equal(status, "200 OK", "person created successfully")
|
|
|
|
|
|
|
|
// Attempt to get person
|
|
|
|
body := testrequest.MakeGetRequest(target, s.router)
|
|
|
|
// Unmarshal the result
|
|
|
|
json.Unmarshal(body, &output)
|
2025-05-26 21:05:53 -05:00
|
|
|
fmt.Println(string(body))
|
2025-05-16 22:51:54 -05:00
|
|
|
// Check that the person listed matches the created person
|
|
|
|
s.Equal(s.person.Name, output.Result[0].Name, "input name matches output name")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PersonTestSuite) Test02GetPerson() {
|
|
|
|
// Setup variables
|
|
|
|
var output TestPerson
|
|
|
|
|
|
|
|
// Attempt to get persons
|
|
|
|
body := testrequest.MakeGetRequest("/person", s.router)
|
|
|
|
// Unmarshal the result
|
|
|
|
json.Unmarshal(body, &output)
|
|
|
|
// Check that the persons listed match the created persons
|
|
|
|
s.Equal(s.person.Name, output.Result[1].Name, "input name matches output name")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PersonTestSuite) Test03UpdatePerson() {
|
|
|
|
// Setup variables
|
|
|
|
var output TestPerson
|
|
|
|
target := fmt.Sprintf("/person?id=%d", s.person.ID)
|
|
|
|
|
|
|
|
// Create needed field objects
|
|
|
|
var groupOutput grouptest.TestGroup
|
2025-05-26 21:05:53 -05:00
|
|
|
for _, group := range s.personUpdate.Groups {
|
|
|
|
groupBody, _ := json.Marshal(group)
|
|
|
|
groupStatus := testrequest.MakePostRequest(&groupBody, "/group", s.globalAuthHeader, s.router)
|
|
|
|
s.Equal(groupStatus, "200 OK", "group created successfully")
|
|
|
|
groupTarget := fmt.Sprintf("/group?id=%d", group.ID)
|
|
|
|
groupBody = testrequest.MakeGetRequest(groupTarget, s.router)
|
|
|
|
json.Unmarshal(groupBody, &groupOutput)
|
|
|
|
s.Equal(group.Name, groupOutput.Result[0].Name, "input group name matches output group name")
|
|
|
|
}
|
2025-05-16 22:51:54 -05:00
|
|
|
|
|
|
|
// Attempt to update person
|
|
|
|
status := testrequest.MakePutRequest(&s.personUpdateJSON, target, s.globalAuthHeader, s.router)
|
|
|
|
// Check that the request was successful
|
|
|
|
s.Equal(status, "200 OK", "person created successfully")
|
|
|
|
|
|
|
|
// Attempt to get person
|
|
|
|
body := testrequest.MakeGetRequest(target, s.router)
|
|
|
|
// Unmarshal the result
|
|
|
|
json.Unmarshal(body, &output)
|
|
|
|
// Check that the person listed matches the updated person
|
|
|
|
s.Equal(s.personUpdate.Name, output.Result[0].Name, "input name matches output name")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PersonTestSuite) Test04DeletePerson() {
|
|
|
|
// Setup variables
|
|
|
|
var output TestPerson
|
|
|
|
target := fmt.Sprintf("/person?id=%d", s.person.ID)
|
|
|
|
|
|
|
|
// Attempt to delete person
|
|
|
|
status := testrequest.MakeDeleteRequest(target, s.globalAuthHeader, s.router)
|
|
|
|
// Check that the request was successful
|
|
|
|
s.Equal(status, "200 OK", "person deleted successfully")
|
|
|
|
|
|
|
|
// Attempt to get person
|
|
|
|
body := testrequest.MakeGetRequest(target, s.router)
|
|
|
|
// Unmarshal the result
|
|
|
|
json.Unmarshal(body, &output)
|
|
|
|
// Check that the person list is empty
|
|
|
|
s.Equal(1, len(output.Result), "person list is empty")
|
|
|
|
}
|