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

fix(api): changes in action.json reflection on action test #657

Merged
merged 1 commit into from
Nov 27, 2024
Merged
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
142 changes: 78 additions & 64 deletions api/internal/app/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestLoadActionsData(t *testing.T) {
Expand All @@ -35,10 +34,34 @@ func TestListActions(t *testing.T) {
err := json.Unmarshal(rec.Body.Bytes(), &response)
assert.NoError(t, err)
assert.NotEmpty(t, response)

firstAction := response[0]
assert.NotEmpty(t, firstAction.Name)
assert.NotEmpty(t, firstAction.Type)
assert.NotEmpty(t, firstAction.Description)
}
}

func TestListActionsWithSearch(t *testing.T) {
originalData := actionsData
defer func() { actionsData = originalData }()

actionsData = ActionsData{
Actions: []Action{
{
Name: "FileWriter",
Description: "Writes features to a file",
Type: ActionTypeSink,
Categories: []string{"File"},
},
{
Name: "OtherAction",
Description: "Some other action",
Type: ActionTypeProcessor,
},
},
}

e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/actions?q=file", nil)
rec := httptest.NewRecorder()
Expand All @@ -54,11 +77,33 @@ func TestListActionsWithSearch(t *testing.T) {
assert.NotEmpty(t, response, "Search should return at least one result")

for _, action := range response {
assert.Contains(t, strings.ToLower(action.Name+" "+action.Description), "file")
lowercaseContent := strings.ToLower(action.Name + " " + action.Description)
assert.Contains(t, lowercaseContent, "file",
"Each result should contain 'file' in name or description")
}
}

func TestGetSegregatedActions(t *testing.T) {
originalData := actionsData
defer func() { actionsData = originalData }()

actionsData = ActionsData{
Actions: []Action{
{
Name: "FileWriter",
Type: ActionTypeSink,
Description: "Writes features to a file",
Categories: []string{"File"},
},
{
Name: "Router",
Type: ActionTypeProcessor,
Description: "Action for last port forwarding for sub-workflows.",
Categories: []string{},
},
},
}

e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/actions/segregated", nil)
rec := httptest.NewRecorder()
Expand All @@ -71,91 +116,60 @@ func TestGetSegregatedActions(t *testing.T) {
var response SegregatedActions
err = json.Unmarshal(rec.Body.Bytes(), &response)
assert.NoError(t, err)
assert.NotEmpty(t, response.ByCategory, "Should have at least one category")
assert.NotEmpty(t, response.ByType, "Should have at least one type")

uniqueActionsByCategory := make(map[string]bool)
for _, actions := range response.ByCategory {
for _, action := range actions {
uniqueActionsByCategory[action.Name] = true
}
}
assert.NotEmpty(t, response.ByCategory)
assert.NotEmpty(t, response.ByType)

totalByType := 0
for _, actions := range response.ByType {
totalByType += len(actions)
}
assert.Contains(t, response.ByCategory, "File")
assert.Contains(t, response.ByCategory, "Uncategorized")
assert.Contains(t, response.ByType, string(ActionTypeSink))
assert.Contains(t, response.ByType, string(ActionTypeProcessor))

assert.Equal(t, len(uniqueActionsByCategory), totalByType,
"Total unique actions (including Uncategorized) should match total actions in ByType")

for typeName, typeActions := range response.ByType {
for _, action := range typeActions {
found := false
for _, categoryActions := range response.ByCategory {
for _, catAction := range categoryActions {
if catAction.Name == action.Name {
found = true
break
}
}
if found {
break
}
}
assert.True(t, found, "Action %s of type %s should be present in at least one category (including Uncategorized)", action.Name, typeName)
uncategorizedActions := response.ByCategory["Uncategorized"]
routerFound := false
for _, action := range uncategorizedActions {
if action.Name == "Router" {
routerFound = true
break
}
}

uncategorizedActions, exists := response.ByCategory["Uncategorized"]
assert.True(t, exists, "Uncategorized category should exist")
if exists {
routerFound := false
for _, action := range uncategorizedActions {
if action.Name == "Router" {
routerFound = true
break
}
}
assert.True(t, routerFound, "Router action should be in the Uncategorized category")
}
assert.True(t, routerFound, "Router should be in Uncategorized category")
}

func TestGetActionDetails(t *testing.T) {
e := echo.New()
listReq := httptest.NewRequest(http.MethodGet, "/actions", nil)
listRec := httptest.NewRecorder()
listC := e.NewContext(listReq, listRec)

err := listActions(listC)
require.NoError(t, err)
require.Equal(t, http.StatusOK, listRec.Code)
originalData := actionsData
defer func() { actionsData = originalData }()

var actionList []ActionSummary
err = json.Unmarshal(listRec.Body.Bytes(), &actionList)
require.NoError(t, err)
require.NotEmpty(t, actionList, "No actions found in the list")
testAction := Action{
Name: "TestAction",
Type: ActionTypeProcessor,
Description: "Test action description",
Categories: []string{"TestCategory"},
}

firstAction := actionList[0]
actionsData = ActionsData{
Actions: []Action{testAction},
}
Comment on lines +140 to +152
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation for error cases

Consider adding test cases for various error scenarios:

  • Action with invalid/malformed fields
  • Action with extremely long values
  • Action with special characters in name
 testAction := Action{
   Name:        "TestAction",
   Type:        ActionTypeProcessor,
   Description: "Test action description",
   Categories:  []string{"TestCategory"},
 }

+// Add test cases for error scenarios
+errorActions := []Action{
+	{
+		Name:        "",  // Invalid empty name
+		Type:        ActionTypeProcessor,
+		Description: "Action with empty name",
+	},
+	{
+		Name:        strings.Repeat("x", 1000),  // Very long name
+		Type:        ActionTypeProcessor,
+		Description: "Action with very long name",
+	},
+	{
+		Name:        "Test/Action",  // Special characters
+		Type:        ActionTypeProcessor,
+		Description: "Action with special characters",
+	},
+}
+
+for _, action := range errorActions {
+	t.Run("Error case: "+action.Name, func(t *testing.T) {
+		actionsData = ActionsData{
+			Actions: []Action{action},
+		}
+		// Test implementation here
+	})
+}

Committable suggestion skipped: line range outside the PR's diff.


e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
c.SetPath("/actions/:id")
c.SetParamNames("id")
c.SetParamValues(firstAction.Name)
c.SetParamValues(testAction.Name)

err = getActionDetails(c)
err := getActionDetails(c)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)

var response Action
err = json.Unmarshal(rec.Body.Bytes(), &response)
assert.NoError(t, err)
assert.Equal(t, firstAction.Name, response.Name)
assert.Equal(t, firstAction.Description, response.Description)
assert.Equal(t, firstAction.Type, string(response.Type))
assert.Equal(t, firstAction.Categories, response.Categories)
assert.Equal(t, testAction.Name, response.Name)
assert.Equal(t, testAction.Description, response.Description)
assert.Equal(t, testAction.Type, response.Type)
assert.Equal(t, testAction.Categories, response.Categories)
}

func TestGetActionDetailsNotFound(t *testing.T) {
Expand Down