add SafeUsername function
This commit is contained in:
parent
0edbff13cd
commit
2e1713db49
11
common/utils.go
Normal file
11
common/utils.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SafeUsername makes a string lowercase and replaces all spaces with
|
||||
// underscores.
|
||||
func SafeUsername(s string) string {
|
||||
return strings.Replace(strings.ToLower(s), " ", "_", -1)
|
||||
}
|
20
common/utils_test.go
Normal file
20
common/utils_test.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package common
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSafeUsername(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
arg string
|
||||
want string
|
||||
}{
|
||||
{"noChange", "no_change", "no_change"},
|
||||
{"toLower", "Change_Me", "change_me"},
|
||||
{"complete", "La_M a m m a_putt na", "la_m_a_m_m_a_putt_na"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := SafeUsername(tt.arg); got != tt.want {
|
||||
t.Errorf("%q. SafeUsername() = %v, want %v", tt.name, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -29,7 +29,7 @@ func Test_generateQuestionMarks(t *testing.T) {
|
|||
func TestWhereClause_In(t *testing.T) {
|
||||
type args struct {
|
||||
initial string
|
||||
fields []interface{}
|
||||
fields []string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
|
@ -40,13 +40,13 @@ func TestWhereClause_In(t *testing.T) {
|
|||
{
|
||||
"simple",
|
||||
&WhereClause{},
|
||||
args{"users.id", []interface{}{"1", "2", "3"}},
|
||||
args{"users.id", []string{"1", "2", "3"}},
|
||||
&WhereClause{"WHERE users.id IN (?, ?, ?)", []interface{}{"1", "2", "3"}},
|
||||
},
|
||||
{
|
||||
"withExisting",
|
||||
Where("users.username = ?", "Howl").Where("users.xd > ?", "6"),
|
||||
args{"users.id", []interface{}{"1"}},
|
||||
args{"users.id", []string{"1"}},
|
||||
&WhereClause{
|
||||
"WHERE users.username = ? AND users.xd > ? AND users.id IN (?)",
|
||||
[]interface{}{"Howl", "6", "1"},
|
||||
|
|
Loading…
Reference in New Issue
Block a user