add SafeUsername function

This commit is contained in:
Howl 2016-10-16 18:27:12 +02:00
parent 0edbff13cd
commit 2e1713db49
3 changed files with 34 additions and 3 deletions

11
common/utils.go Normal file
View 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
View 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)
}
}
}

View File

@ -29,7 +29,7 @@ func Test_generateQuestionMarks(t *testing.T) {
func TestWhereClause_In(t *testing.T) { func TestWhereClause_In(t *testing.T) {
type args struct { type args struct {
initial string initial string
fields []interface{} fields []string
} }
tests := []struct { tests := []struct {
name string name string
@ -40,13 +40,13 @@ func TestWhereClause_In(t *testing.T) {
{ {
"simple", "simple",
&WhereClause{}, &WhereClause{},
args{"users.id", []interface{}{"1", "2", "3"}}, args{"users.id", []string{"1", "2", "3"}},
&WhereClause{"WHERE users.id IN (?, ?, ?)", []interface{}{"1", "2", "3"}}, &WhereClause{"WHERE users.id IN (?, ?, ?)", []interface{}{"1", "2", "3"}},
}, },
{ {
"withExisting", "withExisting",
Where("users.username = ?", "Howl").Where("users.xd > ?", "6"), Where("users.username = ?", "Howl").Where("users.xd > ?", "6"),
args{"users.id", []interface{}{"1"}}, args{"users.id", []string{"1"}},
&WhereClause{ &WhereClause{
"WHERE users.username = ? AND users.xd > ? AND users.id IN (?)", "WHERE users.username = ? AND users.xd > ? AND users.id IN (?)",
[]interface{}{"Howl", "6", "1"}, []interface{}{"Howl", "6", "1"},