github.com/angryronald/go-kit@v0.0.0-20240505173814-ff2bd9c79dbf/generic/repository/sql/generic.utils_test.go (about)

     1  package sql
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestGetCamelToSnakeCaseMapping(t *testing.T) {
     9  	type User struct {
    10  		FirstName string
    11  		LastName  string
    12  		Email     string
    13  		OTP       string
    14  		FeatureID string
    15  	}
    16  
    17  	user := User{}
    18  	expectedMapping := map[string]string{
    19  		"FirstName": "first_name",
    20  		"LastName":  "last_name",
    21  		"Email":     "email",
    22  		"OTP":       "otp",
    23  		"FeatureID": "feature_id",
    24  	}
    25  
    26  	mapping, err := GetCamelToSnakeCaseMapping(user)
    27  	if err != nil {
    28  		t.Errorf("Unexpected error: %v", err)
    29  	}
    30  
    31  	if !reflect.DeepEqual(mapping, expectedMapping) {
    32  		t.Errorf("Mapping does not match. Expected: %v, Got: %v", expectedMapping, mapping)
    33  	}
    34  }
    35  
    36  func TestGetCamelToSnakeCaseMapping_InvalidInput(t *testing.T) {
    37  	nonStruct := "not_a_struct"
    38  	_, err := GetCamelToSnakeCaseMapping(nonStruct)
    39  	if err == nil {
    40  		t.Error("Expected an error for non-struct input, but got none.")
    41  	}
    42  }
    43  
    44  func TestIsNumber(t *testing.T) {
    45  	tests := []struct {
    46  		name     string
    47  		input    interface{}
    48  		expected bool
    49  	}{
    50  		{"Integer", 42, true},
    51  		{"Float", 3.14, true},
    52  		{"String", "hello", false},
    53  		{"Slice", []int{1, 2, 3}, false},
    54  		{"Map", map[string]int{"a": 1}, false},
    55  	}
    56  
    57  	for _, tt := range tests {
    58  		t.Run(tt.name, func(t *testing.T) {
    59  			got := IsNumber(tt.input)
    60  			if got != tt.expected {
    61  				t.Errorf("IsNumber(%v) = %v; want %v", tt.input, got, tt.expected)
    62  			}
    63  		})
    64  	}
    65  }