datetime_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package helpers
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/brianvoe/gofakeit/v6"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func Test_ParseDateTime(t *testing.T) {
  9. var (
  10. year = gofakeit.Year()
  11. month = gofakeit.Month()
  12. day = gofakeit.Day()
  13. hour = gofakeit.Hour()
  14. minute = gofakeit.Minute()
  15. second = gofakeit.Second()
  16. correctRes = time.Date(year, time.Month(month), day, hour, minute, second, 0, time.UTC)
  17. correctReq = correctRes.Format("2006-01-02 15:04:05")
  18. incorrectReq = gofakeit.Word()
  19. )
  20. tests := []struct {
  21. name string
  22. req string
  23. res time.Time
  24. err error
  25. }{
  26. {
  27. name: "positive case",
  28. req: correctReq,
  29. err: nil,
  30. res: correctRes,
  31. },
  32. {
  33. name: "negative case",
  34. req: incorrectReq,
  35. err: &time.ParseError{Layout: "2006-01-02 15:04:05", Value: incorrectReq, LayoutElem: "2006", ValueElem: incorrectReq, Message: ""},
  36. res: time.Time{},
  37. },
  38. }
  39. for _, tt := range tests {
  40. t.Run(tt.name, func(t *testing.T) {
  41. res, err := ParseDateTime(tt.req)
  42. assert.Equal(t, tt.res, res)
  43. assert.Equal(t, tt.err, err)
  44. })
  45. }
  46. }
  47. func Test_FormatDateForm(t *testing.T) {
  48. var (
  49. year = gofakeit.Year()
  50. month = gofakeit.Month()
  51. day = gofakeit.Day()
  52. hour = gofakeit.Hour()
  53. minute = gofakeit.Minute()
  54. second = gofakeit.Second()
  55. correctReq = time.Date(year, time.Month(month), day, hour, minute, second, 0, time.UTC)
  56. correctRes = correctReq.Format("2006-01-02")
  57. )
  58. tests := []struct {
  59. name string
  60. req time.Time
  61. res string
  62. }{
  63. {
  64. name: "positive case",
  65. req: correctReq,
  66. res: correctRes,
  67. },
  68. }
  69. for _, tt := range tests {
  70. t.Run(tt.name, func(t *testing.T) {
  71. res := FormatDateForm(tt.req)
  72. assert.Equal(t, tt.res, res)
  73. })
  74. }
  75. }
  76. func Test_FormatDateStr(t *testing.T) {
  77. var (
  78. year = gofakeit.Year()
  79. month = gofakeit.Month()
  80. day = gofakeit.Day()
  81. hour = gofakeit.Hour()
  82. minute = gofakeit.Minute()
  83. second = gofakeit.Second()
  84. correctReq = time.Date(year, time.Month(month), day, hour, minute, second, 0, time.UTC)
  85. correctRes = correctReq.Format("2") + " " + getMonthStr(correctReq.Format("01")) + " " + correctReq.Format("2006")
  86. )
  87. tests := []struct {
  88. name string
  89. req time.Time
  90. res string
  91. }{
  92. {
  93. name: "positive case",
  94. req: correctReq,
  95. res: correctRes,
  96. },
  97. }
  98. for _, tt := range tests {
  99. t.Run(tt.name, func(t *testing.T) {
  100. res := FormatDateStr(tt.req)
  101. assert.Equal(t, tt.res, res)
  102. })
  103. }
  104. }
  105. func Test_GetMonthStr(t *testing.T) {
  106. tests := []struct {
  107. name string
  108. req string
  109. res string
  110. }{
  111. {
  112. name: "Jan",
  113. req: "01",
  114. res: "января",
  115. },
  116. {
  117. name: "Feb",
  118. req: "02",
  119. res: "февраля",
  120. },
  121. {
  122. name: "Mar",
  123. req: "03",
  124. res: "марта",
  125. },
  126. {
  127. name: "Apr",
  128. req: "04",
  129. res: "апреля",
  130. },
  131. {
  132. name: "May",
  133. req: "05",
  134. res: "мая",
  135. },
  136. {
  137. name: "Jun",
  138. req: "06",
  139. res: "июня",
  140. },
  141. {
  142. name: "Jul",
  143. req: "07",
  144. res: "июля",
  145. },
  146. {
  147. name: "Aug",
  148. req: "08",
  149. res: "августа",
  150. },
  151. {
  152. name: "Sep",
  153. req: "09",
  154. res: "сентября",
  155. },
  156. {
  157. name: "Oct",
  158. req: "10",
  159. res: "октября",
  160. },
  161. {
  162. name: "Nov",
  163. req: "11",
  164. res: "ноября",
  165. },
  166. {
  167. name: "Dec",
  168. req: "12",
  169. res: "декабря",
  170. },
  171. {
  172. name: "Empty",
  173. req: "",
  174. res: "",
  175. },
  176. }
  177. for _, tt := range tests {
  178. t.Run(tt.name, func(t *testing.T) {
  179. res := getMonthStr(tt.req)
  180. assert.Equal(t, tt.res, res)
  181. })
  182. }
  183. }