PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/helpers/helpers_test.go

https://gitlab.com/epicglue/epicglue
Go | 141 lines | 82 code | 31 blank | 28 comment | 1 complexity | 59c0f4fe29e4e9d7faf1f30fec47b452 MD5 | raw file
  1. package helpers_test
  2. import (
  3. "github.com/stretchr/testify/assert"
  4. "gitlab.com/epicglue/epicglue/app/helpers"
  5. "gitlab.com/epicglue/epicglue/app/helpers/test"
  6. "io/ioutil"
  7. "strings"
  8. "testing"
  9. )
  10. func TestScan(t *testing.T) {
  11. }
  12. //func TestRandomNumber(t *testing.T) {
  13. // r1 := helpers.RandomNumber(8)
  14. // assert.Len(t, r1, 8)
  15. //
  16. // r2 := helpers.RandomNumber(8)
  17. // assert.Len(t, r2, 8)
  18. //
  19. // r3 := helpers.RandomNumber(1)
  20. // assert.Len(t, r3, 1)
  21. //
  22. // r4 := helpers.RandomNumber(16)
  23. // assert.Len(t, r4, 16)
  24. //
  25. // assert.NotEqual(t, r1, r2)
  26. //}
  27. func TestRandomString(t *testing.T) {
  28. r1 := helpers.RandomString(8)
  29. assert.Len(t, r1, 8)
  30. r2 := helpers.RandomString(8)
  31. assert.Len(t, r2, 8)
  32. r3 := helpers.RandomString(1)
  33. assert.Len(t, r3, 1)
  34. r4 := helpers.RandomString(16)
  35. assert.Len(t, r4, 16)
  36. assert.NotEqual(t, r1, r2)
  37. }
  38. func TestRandomEmail(t *testing.T) {
  39. e1 := helpers.RandomEmail()
  40. assert.Len(t, e1, 29)
  41. e2 := helpers.RandomEmail()
  42. assert.Len(t, e2, 29)
  43. assert.NotEqual(t, e1, e2)
  44. assert.True(t, strings.HasSuffix(e1, "@epicglue.com"))
  45. assert.True(t, strings.HasSuffix(e2, "@epicglue.com"))
  46. }
  47. func TestFlattenArrayOfStrings(t *testing.T) {
  48. assert.Equal(t, "[]", helpers.FlattenArrayOfStrings([]string{}))
  49. assert.Equal(t, `["1"]`, helpers.FlattenArrayOfStrings([]string{"1"}))
  50. assert.Equal(t, `["1","2"]`, helpers.FlattenArrayOfStrings([]string{"1", "2"}))
  51. }
  52. func TestFlattenArrayOfInts(t *testing.T) {
  53. assert.Equal(t, "[]", helpers.FlattenArrayOfInts([]int64{}))
  54. assert.Equal(t, `[1]`, helpers.FlattenArrayOfInts([]int64{1}))
  55. assert.Equal(t, `[1,2]`, helpers.FlattenArrayOfInts([]int64{1, 2}))
  56. }
  57. func TestStringToInt64(t *testing.T) {
  58. assert.Equal(t, helpers.StringToInt64("-9223372036854775808"), int64(-9223372036854775808))
  59. assert.Equal(t, helpers.StringToInt64("9223372036854775807"), int64(9223372036854775807))
  60. }
  61. func TestStringToFloat64(t *testing.T) {
  62. assert.Equal(t, helpers.StringToFloat64("4.11"), float64(4.11))
  63. }
  64. func TestConvertStringListToInterfaceList(t *testing.T) {
  65. strList := []string{"A", "B", "C"}
  66. convertedStrList := helpers.ConvertStringListToInterfaceList(strList)
  67. for i, _ := range convertedStrList {
  68. assert.Equal(t, strList[i], convertedStrList[i].(string))
  69. }
  70. }
  71. //func TestNextFullHour(t *testing.T) {
  72. // t1 := time.Date(2016, time.September, 12, 16, 10, 10, 0, time.UTC)
  73. // nt1 := helpers.NextFullHour(t1)
  74. // assert.Equal(t, time.Date(2016, time.September, 12, 17, 0, 0, 0, time.UTC), nt1)
  75. //
  76. // t2 := time.Date(2016, time.September, 10, 23, 8, 10, 0, time.UTC)
  77. // nt2 := helpers.NextFullHour(t2)
  78. // assert.Equal(t, time.Date(2016, time.September, 13, 0, 0, 0, 0, time.UTC), nt2)
  79. //
  80. // t3 := time.Date(2016, time.September, 12, 10, 0, 0, 0, time.UTC)
  81. // nt3 := helpers.NextFullHour(t3)
  82. // assert.Equal(t, t3, nt3)
  83. //}
  84. func TestCapitalizeFirstLetter(t *testing.T) {
  85. str1 := "epicglue"
  86. assert.Equal(t, "Epicglue", helpers.CapitalizeFirstLetter(str1))
  87. str2 := "EPICGLUE"
  88. assert.Equal(t, "EPICGLUE", helpers.CapitalizeFirstLetter(str2))
  89. }
  90. func TestToStringArray(t *testing.T) {
  91. arr1 := []int64{1, 2, 3}
  92. assert.Equal(t, []string{"1", "2", "3"}, helpers.ToStringArray(arr1))
  93. }
  94. func TestMakeGetRequest(t *testing.T) {
  95. }
  96. func TestMakePostRequest(t *testing.T) {
  97. r := test_helper.MakePostRequest("/", map[string]string{
  98. "testKey": "testValue",
  99. })
  100. assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
  101. assert.Equal(t, "23", r.Header.Get("Content-Length"))
  102. assert.Equal(t, "localhost:7000", r.Host)
  103. assert.Equal(t, "/", r.URL.Path)
  104. assert.Equal(t, "POST", r.Method)
  105. data, _ := ioutil.ReadAll(r.Body)
  106. assert.Equal(t, `{"testKey":"testValue"}`, string(data))
  107. }
  108. func TestIsPasswordValid(t *testing.T) {
  109. assert.True(t, helpers.IsPasswordValid("dupadupa", "3482a5ec089593947b892c137cc7668f46b19ea1", "dupa"))
  110. }