utils_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package utils
  2. import "testing"
  3. func TestStrToJsonCamel(t *testing.T) {
  4. str1 := "hello_WorLd_hey"
  5. if CamelizeStr(str1, false) != "helloWorldHey" {
  6. t.Error("string to json camel error")
  7. }
  8. if CamelizeStr(str1, true) != "HelloWorldHey" {
  9. t.Error("string to json camel error")
  10. }
  11. str1 = "iD"
  12. if CamelizeStr(str1, false) != "ID" {
  13. t.Error("string to json camel error")
  14. }
  15. str1 = "my_TeSt_id"
  16. if CamelizeStr(str1, false) != "myTestID" {
  17. t.Error("string to json camel error")
  18. }
  19. if CamelizeStr(str1, true) != "MyTestID" {
  20. t.Error("string to json camel error")
  21. }
  22. str1 = "a"
  23. if CamelizeStr(str1, false) != "a" {
  24. t.Error("string to json camel error")
  25. }
  26. if CamelizeStr(str1, true) != "A" {
  27. t.Error("string to json camel error")
  28. }
  29. }
  30. func TestGetDbNameFromDSN(t *testing.T) {
  31. str1 := "postgres://:@127.0.0.1:5432/test?sslmode=disable"
  32. dbName, err := GetDbNameFromDSN(str1)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. if dbName != "test" {
  37. t.Fatal("error db name")
  38. }
  39. str1 = "root:123456@tcp(127.0.0.1:3306)/test?parseTime=true&charset=utf8&loc=Asia%2FShanghai"
  40. dbName, err = GetDbNameFromDSN(str1)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. if dbName != "test" {
  45. t.Fatal("error db name")
  46. }
  47. str1 = "host=127.0.0.1 dbname=test sslmode=disable Timezone=Asia/Shanghai"
  48. dbName, err = GetDbNameFromDSN(str1)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. if dbName != "test" {
  53. t.Fatal("error db name")
  54. }
  55. }