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] - BuildSearchQuery #2045

Closed
tomsmith8 opened this issue Dec 2, 2024 · 0 comments · Fixed by #2076
Closed

[Unit Tests] - BuildSearchQuery #2045

tomsmith8 opened this issue Dec 2, 2024 · 0 comments · Fixed by #2076
Assignees

Comments

@tomsmith8
Copy link

Unit Test Coverage for " BuildSearchQuery"


Stakwork Run


Unit Test Code


package utils

import (
  "testing"

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

func TestBuildSearchQuery(t *testing.T) {
  tests := []struct {
  	name          string
  	key           interface{}
  	term          interface{}
  	expectedQuery string
  	expectedArg   string
  }{
  	{
  		name:          "Standard Input",
  		key:           "name",
  		term:          "John",
  		expectedQuery: "name LIKE ?",
  		expectedArg:   "%John%",
  	},
  	{
  		name:          "Empty Term",
  		key:           "name",
  		term:          "",
  		expectedQuery: "name LIKE ?",
  		expectedArg:   "%%",
  	},
  	{
  		name:          "Empty Key",
  		key:           "",
  		term:          "John",
  		expectedQuery: " LIKE ?",
  		expectedArg:   "%John%",
  	},
  	{
  		name:          "Both Key and Term Empty",
  		key:           "",
  		term:          "",
  		expectedQuery: " LIKE ?",
  		expectedArg:   "%%",
  	},
  	{
  		name:          "Special Characters in Key",
  		key:           "user@name",
  		term:          "John",
  		expectedQuery: "user@name LIKE ?",
  		expectedArg:   "%John%",
  	},
  	{
  		name:          "Special Characters in Term",
  		key:           "name",
  		term:          "J@hn",
  		expectedQuery: "name LIKE ?",
  		expectedArg:   "%J@hn%",
  	},
  	{
  		name:          "SQL Keywords in Key",
  		key:           "SELECT",
  		term:          "John",
  		expectedQuery: "SELECT LIKE ?",
  		expectedArg:   "%John%",
  	},
  	{
  		name:          "SQL Keywords in Term",
  		key:           "name",
  		term:          "SELECT",
  		expectedQuery: "name LIKE ?",
  		expectedArg:   "%SELECT%",
  	},
  	{
  		name:          "Null Key",
  		key:           nil,
  		term:          "John",
  		expectedQuery: " LIKE ?",
  		expectedArg:   "%John%",
  	},
  	{
  		name:          "Null Term",
  		key:           "name",
  		term:          nil,
  		expectedQuery: "name LIKE ?",
  		expectedArg:   "%%",
  	},
  	{
  		name:          "Non-String Key",
  		key:           123,
  		term:          "John",
  		expectedQuery: "123 LIKE ?",
  		expectedArg:   "%John%",
  	},
  	{
  		name:          "Non-String Term",
  		key:           "name",
  		term:          456,
  		expectedQuery: "name LIKE ?",
  		expectedArg:   "%456%",
  	},
  	{
  		name:          "Very Long Key",
  		key:           string(make([]byte, 1000)),
  		term:          "John",
  		expectedQuery: string(make([]byte, 1000)) + " LIKE ?",
  		expectedArg:   "%John%",
  	},
  	{
  		name:          "Very Long Term",
  		key:           "name",
  		term:          string(make([]byte, 1000)),
  		expectedQuery: "name LIKE ?",
  		expectedArg:   "%" + string(make([]byte, 1000)) + "%",
  	},
  	{
  		name:          "Unicode Characters in Key",
  		key:           "名前",
  		term:          "John",
  		expectedQuery: "名前 LIKE ?",
  		expectedArg:   "%John%",
  	},
  	{
  		name:          "Unicode Characters in Term",
  		key:           "name",
  		term:          "ジョン",
  		expectedQuery: "name LIKE ?",
  		expectedArg:   "%ジョン%",
  	},
  	{
  		name:          "Whitespace in Key",
  		key:           " name ",
  		term:          "John",
  		expectedQuery: " name  LIKE ?",
  		expectedArg:   "%John%",
  	},
  	{
  		name:          "Whitespace in Term",
  		key:           "name",
  		term:          " John ",
  		expectedQuery: "name LIKE ?",
  		expectedArg:   "% John %",
  	},
  }

  for _, tt := range tests {
  	t.Run(tt.name, func(t *testing.T) {
  		query, arg := BuildSearchQuery(tt.key, tt.term)
  		assert.Equal(t, tt.expectedQuery, query)
  		assert.Equal(t, tt.expectedArg, arg)
  	})
  }
}

Explanation of Adjustments:

  1. Null and Non-String Inputs: The test cases for null and non-string inputs have been added. In Go, nil is used for null values, and the test cases now handle non-string types by converting them to strings within the test setup.

  2. Interface Types for Key and Term: The key and term fields in the test struct are now of type interface{} to accommodate different types of inputs, including nil and integers.

  3. Handling of Non-String Inputs: The test cases for non-string inputs assume that the function should convert these inputs to strings. This assumption should be verified against the actual implementation of BuildSearchQuery.

  4. Comprehensive Coverage: All specified test cases are now covered, including edge cases, error conditions, and special scenarios.

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

Successfully merging a pull request may close this issue.

2 participants