package tiertest import ( "encoding/json" "fmt" "os" function "example.com/database/function" functionset "example.com/database/functionset" "example.com/database/functiontag" tier "example.com/database/tier" testrequest "example.com/test/testrequest" testsetup "example.com/test/testsetup" "github.com/gin-gonic/gin" "github.com/stretchr/testify/suite" "gorm.io/gorm" ) type TestTier struct { Result []tier.Tier `json:"result"` } type TierTestSuite struct { suite.Suite globalAuthHeader string router *gin.Engine tier tier.Tier tierJSON []byte tierUpdate tier.Tier tierUpdateJSON []byte } func (s *TierTestSuite) SetupTestSuite() { s.globalAuthHeader, s.router = testsetup.SetupTestSuite() // Create Test Data testFunctionTag1 := functiontag.FunctionTag{ Model: gorm.Model{ID: 1}, Name: "tag1", } testFunctionTag2 := functiontag.FunctionTag{ Model: gorm.Model{ID: 2}, Name: "tag2", } testFunctionTag3 := functiontag.FunctionTag{ Model: gorm.Model{ID: 3}, Name: "tag3", } testFunction1 := function.Function{ Model: gorm.Model{ID: 1}, Name: "function1", Tags: []functiontag.FunctionTag{ testFunctionTag1, testFunctionTag2, }, Requirements: []function.Function{}, } testFunction1JSON, _ := json.Marshal(function.FunctionParams{ Name: testFunction1.Name, Tags: []uint{ testFunctionTag1.ID, testFunctionTag2.ID, }, Requirements: []uint{}, }) testFunction2 := function.Function{ Model: gorm.Model{ID: 2}, Name: "function2", Tags: []functiontag.FunctionTag{}, Requirements: []function.Function{ testFunction1, }, } testFunction2JSON, _ := json.Marshal(function.FunctionParams{ Name: testFunction2.Name, Tags: []uint{}, Requirements: []uint{ testFunction1.ID, }, }) testFunction3 := function.Function{ Model: gorm.Model{ID: 3}, Name: "function3", Tags: []functiontag.FunctionTag{ testFunctionTag2, testFunctionTag3, }, Requirements: []function.Function{ testFunction1, testFunction2, }, } testFunction3JSON, _ := json.Marshal(function.FunctionParams{ Name: testFunction3.Name, Tags: []uint{ testFunctionTag2.ID, testFunctionTag3.ID, }, Requirements: []uint{ testFunction1.ID, testFunction2.ID, }, }) testFunctionSet1 := functionset.FunctionSet{ Model: gorm.Model{ID: 1}, Functions: []function.Function{ testFunction1, testFunction2, }, } testFunctionSet1JSON, _ := json.Marshal(functionset.FunctionSetParams{ Functions: []uint{ testFunction1.ID, testFunction2.ID, }, }) testFunctionSet2 := functionset.FunctionSet{ Model: gorm.Model{ID: 2}, Functions: []function.Function{ testFunction2, testFunction3, }, } testFunctionSet2JSON, _ := json.Marshal(functionset.FunctionSetParams{ Functions: []uint{ testFunction2.ID, testFunction3.ID, }, }) // Set Test Variables s.tier = tier.Tier{ Model: gorm.Model{ID: 1}, FunctionSets: []functionset.FunctionSet{ testFunctionSet1, }, } s.tierJSON, _ = json.Marshal(tier.TierParams{ FunctionSets: []uint{ testFunctionSet1.ID, }, }) s.tierUpdate = tier.Tier{ Model: gorm.Model{ID: 1}, FunctionSets: []functionset.FunctionSet{ testFunctionSet2, }, } s.tierUpdateJSON, _ = json.Marshal(tier.TierParams{ FunctionSets: []uint{ testFunctionSet2.ID, }, }) // Setup variables var target string var body []byte target = "/function-tag" // Setup variables body, _ = json.Marshal(testFunctionTag1) // Attempt to create function tag 1 testrequest.MakePostRequest(&body, target, s.globalAuthHeader, s.router) // Setup variables body, _ = json.Marshal(testFunctionTag2) // Attempt to create function tag 2 testrequest.MakePostRequest(&body, target, s.globalAuthHeader, s.router) // Setup variables body, _ = json.Marshal(testFunctionTag3) // Attempt to create function tag 3 testrequest.MakePostRequest(&body, target, s.globalAuthHeader, s.router) target = "/function" // Attempt to create function 1 testrequest.MakePostRequest(&testFunction1JSON, target, s.globalAuthHeader, s.router) // Attempt to create function 2 testrequest.MakePostRequest(&testFunction2JSON, target, s.globalAuthHeader, s.router) // Attempt to create function 3 testrequest.MakePostRequest(&testFunction3JSON, target, s.globalAuthHeader, s.router) target = "/function-set" // Attempt to create function set 1 testrequest.MakePostRequest(&testFunctionSet1JSON, target, s.globalAuthHeader, s.router) // Attempt to create function set 2 testrequest.MakePostRequest(&testFunctionSet2JSON, target, s.globalAuthHeader, s.router) } func (s *TierTestSuite) TearDownTestSuite() { os.Remove("db/main.db") } func (s *TierTestSuite) Test01CreateTier() { // Setup variables var output TestTier target := fmt.Sprintf("/tier?id=%d", s.tier.ID) // Attempt to create tier status := testrequest.MakePostRequest(&s.tierJSON, "/tier", s.globalAuthHeader, s.router) // Check that the request was successful s.Equal(status, "200 OK", "function created successfully") // Attempt to get tier body := testrequest.MakeGetRequest(target, s.router) json.Unmarshal(body, &output) fmt.Println(string(body)) // Check that the tier listed matches the created tier s.Equal(s.tier.FunctionSets[0].ID, output.Result[0].FunctionSets[0].ID, "input tier exists and function set id matches output function set id") } func (s *TierTestSuite) Test02GetTier() { // Setup variables var output TestTier // Attempt to get tiers body := testrequest.MakeGetRequest("/tier", s.router) json.Unmarshal(body, &output) // Check that the tiers listed match the created tiers s.Equal(s.tier.FunctionSets[0].ID, output.Result[0].FunctionSets[0].ID, "input tier exists and function set id matches output function set id") } func (s *TierTestSuite) Test03UpdateTier() { // Setup variables var output TestTier target := fmt.Sprintf("/tier?id=%d", s.tier.ID) // Attempt to update tier status := testrequest.MakePutRequest(&s.tierUpdateJSON, target, s.globalAuthHeader, s.router) // Check that the request was successful s.Equal(status, "200 OK", "tier created successfully") // Attempt to get tier body := testrequest.MakeGetRequest(target, s.router) json.Unmarshal(body, &output) // Check that the tier listed matches the updated function s.Equal(s.tierUpdate.FunctionSets[0].ID, output.Result[0].FunctionSets[0].ID, "updated tier function set id matches output function set id") } func (s *TierTestSuite) Test04DeleteTier() { // Setup variables var output TestTier target := fmt.Sprintf("/tier?id=%d", s.tier.ID) // Attempt to delete tier 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 tier body := testrequest.MakeGetRequest("/tier", s.router) json.Unmarshal(body, &output) // Check that the tier list is empty s.Equal(0, len(output.Result), "tier list is empty") }