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

[Unit Tests] - BuildV2ConnectionCodes #2046

Open
tomsmith8 opened this issue Dec 2, 2024 · 0 comments
Open

[Unit Tests] - BuildV2ConnectionCodes #2046

tomsmith8 opened this issue Dec 2, 2024 · 0 comments
Assignees

Comments

@tomsmith8
Copy link

Unit Test Coverage for " BuildV2ConnectionCodes"


Stakwork Run


Unit Test Code


package utils

import (
  "fmt"
  "strings"
  "testing"

  "github.com/stretchr/testify/assert"
)

// Assume BuildV2ConnectionCodes is imported from the package where it is implemented
// func BuildV2ConnectionCodes(amt_msat uint, alias string) string {
// 	bodyData := fmt.Sprintf(`{"amt_msat": %d, "alias": "%s"}`, amt_msat, alias)
// 	return bodyData
// }

func TestBuildV2ConnectionCodes(t *testing.T) {
  tests := []struct {
  	name     string
  	amt_msat uint
  	alias    string
  	expected string
  }{
  	{
  		name:     "Standard Input",
  		amt_msat: 1000,
  		alias:    "nodeAlias",
  		expected: `{"amt_msat": 1000, "alias": "nodeAlias"}`,
  	},
  	{
  		name:     "Minimum Value for amt_msat",
  		amt_msat: 0,
  		alias:    "nodeAlias",
  		expected: `{"amt_msat": 0, "alias": "nodeAlias"}`,
  	},
  	{
  		name:     "Empty String for alias",
  		amt_msat: 1000,
  		alias:    "",
  		expected: `{"amt_msat": 1000, "alias": ""}`,
  	},
  	{
  		name:     "Maximum Value for amt_msat",
  		amt_msat: 4294967295,
  		alias:    "maxAlias",
  		expected: `{"amt_msat": 4294967295, "alias": "maxAlias"}`,
  	},
  	{
  		name:     "Large String for alias",
  		amt_msat: 1000,
  		alias:    "a" + strings.Repeat("a", 9999),
  		expected: fmt.Sprintf(`{"amt_msat": 1000, "alias": "%s"}`, "a"+strings.Repeat("a", 9999)),
  	},
  	{
  		name:     "Special Characters in alias",
  		amt_msat: 1000,
  		alias:    "node@#%&*Alias",
  		expected: `{"amt_msat": 1000, "alias": "node@#%&*Alias"}`,
  	},
  	{
  		name:     "Unicode Characters in alias",
  		amt_msat: 1000,
  		alias:    "ノード",
  		expected: `{"amt_msat": 1000, "alias": "ノード"}`,
  	},
  	{
  		name:     "Whitespace in alias",
  		amt_msat: 1000,
  		alias:    "  nodeAlias  ",
  		expected: `{"amt_msat": 1000, "alias": "  nodeAlias  "}`,
  	},
  	{
  		name:     "JSON Injection in alias",
  		amt_msat: 1000,
  		alias:    `{"key": "value"}`,
  		expected: `{"amt_msat": 1000, "alias": "{\"key\": \"value\"}"}`,
  	},
  }

  for _, tt := range tests {
  	t.Run(tt.name, func(t *testing.T) {
  		result := BuildV2ConnectionCodes(tt.amt_msat, tt.alias)
  		assert.Equal(t, tt.expected, result)
  	})
  }
}

Explanation of Changes:

  • Imports: Added the strings package import, which was missing and is necessary for the strings.Repeat function used in the "Large String for alias" test case.
  • Error Conditions: The test cases for negative values and non-string types for alias are not included because Go's type system inherently prevents these errors at compile time. This is consistent with the language's design and the nature of the function's parameters.
  • Test Structure: The test cases are organized in a table-driven format, which is a common and effective pattern in Go testing, allowing for clear and concise test definitions.
  • Assertions: The assert.Equal function from the testify package is used to ensure that the actual output matches the expected output for each test case. This provides clear feedback if a test fails.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants