diff --git a/common/utils.go b/common/utils.go new file mode 100644 index 0000000..df35d10 --- /dev/null +++ b/common/utils.go @@ -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) +} diff --git a/common/utils_test.go b/common/utils_test.go new file mode 100644 index 0000000..2769502 --- /dev/null +++ b/common/utils_test.go @@ -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) + } + } +} diff --git a/common/where_test.go b/common/where_test.go index 4dbfc2e..144f05f 100644 --- a/common/where_test.go +++ b/common/where_test.go @@ -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"},