Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Test to API Services & Catch Some 🐛 #2

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/google/uuid v1.6.0
github.com/sirupsen/logrus v1.9.3
github.com/urfave/cli v1.22.15
go.uber.org/mock v0.4.0
gopkg.in/yaml.v2 v2.4.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4d
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/urfave/cli v1.22.15 h1:nuqt+pdC/KqswQKhETJjo7pvn/k4xMUxgW6liI7XpnM=
github.com/urfave/cli v1.22.15/go.mod h1:wSan1hmo5zeyLGBjRJbzRTNk8gwoYa2B9n4q9dmRIc0=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k=
golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
Expand Down
2 changes: 1 addition & 1 deletion internal/sbi/api_spyfamily.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *Server) HTTPSerchSpyFamilyCharacter(c *gin.Context) {

targetName := c.Param("Name")
if targetName == "" {
c.JSON(http.StatusBadRequest, "No name provided")
c.String(http.StatusBadRequest, "No name provided")
return
}

Expand Down
50 changes: 50 additions & 0 deletions internal/sbi/api_spyfamily_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package sbi_test

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/andy89923/nf-example/internal/sbi"
"github.com/andy89923/nf-example/pkg/factory"
"github.com/gin-gonic/gin"
"go.uber.org/mock/gomock"
)

func Test_HTTPSerchSpyFamilyCharacter(t *testing.T) {
mockCtrl := gomock.NewController(t)
nfApp := sbi.NewMocknfApp(mockCtrl)
nfApp.EXPECT().Config().Return(&factory.Config{
Configuration: &factory.Configuration{
Sbi: &factory.Sbi{
Port: 8000,
},
},
}).AnyTimes()
server := sbi.NewServer(nfApp, "")

t.Run("No name provided", func(t *testing.T) {
const EXPECTED_STATUS = http.StatusBadRequest
const EXPECTED_BODY = "No name provided"

httpRecorder := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(httpRecorder)

var err error
ginCtx.Request, err = http.NewRequest("GET", "/spyfamily", nil)
if err != nil {
t.Errorf("Failed to create request: %s", err)
return
}

server.HTTPSerchSpyFamilyCharacter(ginCtx)

if httpRecorder.Code != EXPECTED_STATUS {
t.Errorf("Expected status code %d, got %d", EXPECTED_STATUS, httpRecorder.Code)
}

if httpRecorder.Body.String() != EXPECTED_BODY {
t.Errorf("Expected body %s, got %s", EXPECTED_BODY, httpRecorder.Body.String())
}
})
}
143 changes: 143 additions & 0 deletions internal/sbi/processor/processor_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/sbi/processor/spy_family.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

func (p *Processor) FindSpyFamilyCharacterName(c *gin.Context, targetName string) {
if lastName, ok := p.Context().SpyFamilyData[targetName]; ok {
c.JSON(http.StatusOK, fmt.Sprintf("Character: %s %s", targetName, lastName))
c.String(http.StatusOK, fmt.Sprintf("Character: %s %s", targetName, lastName))
return
}
c.JSON(http.StatusNotFound, fmt.Sprintf("[%s] not found in SPYxFAMILY", targetName))
c.String(http.StatusNotFound, fmt.Sprintf("[%s] not found in SPYxFAMILY", targetName))
}
69 changes: 69 additions & 0 deletions internal/sbi/processor/spy_family_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package processor_test

import (
"net/http/httptest"
"testing"

nf_context "github.com/andy89923/nf-example/internal/context"
"github.com/andy89923/nf-example/internal/sbi/processor"
"github.com/gin-gonic/gin"
gomock "go.uber.org/mock/gomock"
)

func Test_FindSpyFamilyCharacterName(t *testing.T) {
mockCtrl := gomock.NewController(t)
processorNf := processor.NewMockProcessorNf(mockCtrl)
processor, err := processor.NewProcessor(processorNf)
if err != nil {
t.Errorf("Failed to create processor: %s", err)
return
}

t.Run("Find Character That Exists", func(t *testing.T) {
const INPUT_NAME = "Anya"
const EXPECTED_STATUS = 200
const EXPECTED_BODY = "Character: " + INPUT_NAME + " Forger"

processorNf.EXPECT().Context().Return(&nf_context.NFContext{
SpyFamilyData: map[string]string{
"Anya": "Forger",
},
})

httpRecorder := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(httpRecorder)
processor.FindSpyFamilyCharacterName(ginCtx, INPUT_NAME)

if httpRecorder.Code != EXPECTED_STATUS {
t.Errorf("Expected status code %d, got %d", EXPECTED_STATUS, httpRecorder.Code)
}

if httpRecorder.Body.String() != EXPECTED_BODY {
t.Errorf("Expected body %s, got %s", EXPECTED_BODY, httpRecorder.Body.String())
}
})

t.Run("Find Character That Does Not Exist", func(t *testing.T) {
const INPUT_NAME = "Andy"
const EXPECTED_STATUS = 404
const EXPECTED_BODY = "[" + INPUT_NAME + "] not found in SPYxFAMILY"

processorNf.EXPECT().Context().Return(&nf_context.NFContext{
SpyFamilyData: map[string]string{
"Anya": "Forger",
},
})

httpRecorder := httptest.NewRecorder()
ginCtx, _ := gin.CreateTestContext(httpRecorder)
processor.FindSpyFamilyCharacterName(ginCtx, INPUT_NAME)

if httpRecorder.Code != EXPECTED_STATUS {
t.Errorf("Expected status code %d, got %d", EXPECTED_STATUS, httpRecorder.Code)
}

if httpRecorder.Body.String() != EXPECTED_BODY {
t.Errorf("Expected body %s, got %s", EXPECTED_BODY, httpRecorder.Body.String())
}
})
}
Loading
Loading