github.com/k0marov/go-socnet@v0.0.0-20220715154813-90d07867c782/core/helpers/test_helpers/test_helpers.go (about) 1 package test_helpers 2 3 import ( 4 "encoding/json" 5 "errors" 6 "github.com/jmoiron/sqlx" 7 likeable_contexters "github.com/k0marov/go-socnet/core/abstract/ownable_likeable/contexters" 8 "github.com/k0marov/go-socnet/core/general/client_errors" 9 "github.com/k0marov/go-socnet/core/general/core_entities" 10 "github.com/k0marov/go-socnet/core/general/core_values" 11 "github.com/k0marov/go-socnet/core/general/core_values/ref" 12 "math" 13 random "math/rand" 14 "net/http" 15 "net/http/httptest" 16 "reflect" 17 "strconv" 18 "testing" 19 "time" 20 21 comment_entities "github.com/k0marov/go-socnet/features/comments/domain/entities" 22 comment_models "github.com/k0marov/go-socnet/features/comments/domain/models" 23 comment_values "github.com/k0marov/go-socnet/features/comments/domain/values" 24 post_models "github.com/k0marov/go-socnet/features/posts/domain/models" 25 post_values "github.com/k0marov/go-socnet/features/posts/domain/values" 26 profile_models "github.com/k0marov/go-socnet/features/profiles/domain/models" 27 28 post_entities "github.com/k0marov/go-socnet/features/posts/domain/entities" 29 profile_entities "github.com/k0marov/go-socnet/features/profiles/domain/entities" 30 31 auth "github.com/k0marov/golang-auth" 32 ) 33 34 var rand = random.New(random.NewSource(time.Now().UnixNano())) 35 36 func AssertStatusCode(t testing.TB, got *httptest.ResponseRecorder, want int) { 37 t.Helper() 38 if !Assert(t, got.Result().StatusCode, want, "response status code") { 39 t.Fatalf("response: %v", got.Body) 40 } 41 } 42 43 func Assert[T any](t testing.TB, got, want T, description string) bool { 44 t.Helper() 45 if !reflect.DeepEqual(got, want) { 46 t.Errorf("%s is not right:\ngot '%v',\nwant '%v'", description, got, want) 47 return false 48 } 49 return true 50 } 51 52 func AssertNoError(t testing.TB, got error) { 53 t.Helper() 54 if got != nil { 55 t.Fatalf("expected no error but got %v", got) 56 } 57 } 58 func AssertError(t testing.TB, got error, want error) { 59 t.Helper() 60 if got != want { 61 t.Errorf("expected error %v, but got %v", want, got) 62 } 63 } 64 func AssertSomeError(t testing.TB, got error) { 65 t.Helper() 66 if got == nil { 67 t.Error("expected an error, but got nil") 68 } 69 } 70 71 func AssertFatal[T comparable](t testing.TB, got, want T, description string) { 72 t.Helper() 73 if !Assert(t, got, want, description) { 74 t.Fatal() 75 } 76 } 77 78 func AssertClientError(t testing.TB, response *httptest.ResponseRecorder, err client_errors.ClientError) { 79 t.Helper() 80 var got client_errors.ClientError 81 json.NewDecoder(response.Body).Decode(&got) 82 83 AssertJSON(t, response) 84 Assert(t, got.ReadableDetail, err.ReadableDetail, "readable detail") 85 Assert(t, got.DetailCode, err.DetailCode, "detail code") 86 Assert(t, response.Code, err.HTTPCode, "status code") 87 } 88 89 func AssertJSON(t testing.TB, response *httptest.ResponseRecorder) { 90 t.Helper() 91 Assert(t, response.Result().Header.Get("contentType"), "application/json", "response content type") 92 } 93 94 func AssertJSONData[T any](t testing.TB, response *httptest.ResponseRecorder, wantData T) { 95 t.Helper() 96 AssertStatusCode(t, response, http.StatusOK) 97 AssertJSON(t, response) 98 var gotData T 99 json.NewDecoder(response.Body).Decode(&gotData) 100 Assert(t, gotData, wantData, "json encoded data") 101 } 102 103 func RandomError() error { 104 return errors.New(RandomString()) 105 } 106 107 func RandomUser() core_entities.User { 108 return core_entities.User{ 109 Id: RandomId(), 110 Username: RandomString(), 111 } 112 } 113 114 func RandomAuthUser() auth.User { 115 return auth.User{ 116 Id: RandomId(), 117 Username: RandomString(), 118 } 119 } 120 121 func RandomProfile() profile_entities.Profile { 122 return profile_entities.Profile{ 123 ProfileModel: RandomProfileModel(), 124 AvatarURL: RandomString(), 125 Follows: RandomInt(), 126 Followers: RandomInt(), 127 } 128 } 129 130 func RandomContextedProfile() profile_entities.ContextedProfile { 131 return profile_entities.ContextedProfile{ 132 Profile: RandomProfile(), 133 OwnLikeContext: RandomLikeableContext(), 134 } 135 } 136 137 func RandomId() string { 138 return strconv.Itoa(rand.Intn(100000)) 139 } 140 141 func RandomProfileModel() profile_models.ProfileModel { 142 return profile_models.ProfileModel{ 143 Id: RandomId(), 144 Username: RandomString(), 145 About: RandomString(), 146 AvatarPath: RandomString(), 147 } 148 } 149 150 func RandomFiles() []core_values.FileData { 151 return []core_values.FileData{RandomFileData(), RandomFileData(), RandomFileData()} 152 } 153 func RandomUrls() []core_values.FileURL { 154 return []core_values.FileURL{RandomString(), RandomString(), RandomString()} 155 } 156 func RandomPostImages() []post_values.PostImage { 157 return []post_values.PostImage{ 158 {URL: RandomString(), Index: 1}, 159 {URL: RandomString(), Index: 2}, 160 {URL: RandomString(), Index: 3}, 161 } 162 } 163 164 func RandomContextedPost() post_entities.ContextedPost { 165 return post_entities.ContextedPost{ 166 Post: RandomPost(), 167 Author: RandomContextedProfile(), 168 OwnLikeContext: RandomLikeableContext(), 169 } 170 } 171 func RandomNewPostData() post_values.NewPostData { 172 return post_values.NewPostData{ 173 Text: RandomString(), 174 Author: RandomString(), 175 Images: []post_values.PostImageFile{{RandomFileData(), 1}, {RandomFileData(), 2}}, 176 } 177 } 178 179 func RandomLikeableContext() likeable_contexters.OwnLikeContext { 180 return likeable_contexters.OwnLikeContext{ 181 IsMine: RandomBool(), 182 IsLiked: RandomBool(), 183 } 184 } 185 186 func RandomTime() time.Time { 187 return time.Date(2022, 6, 17, 16, 53, 42, 0, time.UTC) 188 } 189 190 func RandomPostModel() post_models.PostModel { 191 return post_models.PostModel{ 192 Id: RandomString(), 193 AuthorId: RandomString(), 194 Text: RandomString(), 195 CreatedAt: RandomTime().Unix(), 196 Images: RandomPostImageModels(), 197 } 198 } 199 func RandomPostImageModels() []post_models.PostImageModel { 200 return []post_models.PostImageModel{ 201 {Path: RandomString(), Index: 1}, 202 {Path: RandomString(), Index: 2}, 203 {Path: RandomString(), Index: 3}, 204 } 205 } 206 func RandomPost() post_entities.Post { 207 return post_entities.Post{ 208 PostModel: RandomPostModel(), 209 Images: RandomPostImages(), 210 Likes: RandomInt(), 211 } 212 } 213 214 func OpenSqliteDB(t testing.TB) *sqlx.DB { 215 t.Helper() 216 sql, err := sqlx.Open("sqlite3", "file::memory:?cache=shared") 217 if err != nil { 218 t.Fatalf("error while opening in-memory database: %v", err) 219 } 220 return sql 221 } 222 223 func TimeAlmostEqual(t, want time.Time) bool { 224 return math.Abs(t.Sub(want).Minutes()) < 1 225 } 226 227 func TimeAlmostNow(t time.Time) bool { 228 return TimeAlmostEqual(t, time.Now()) 229 } 230 231 func RandomFileData() core_values.FileData { 232 data := []byte(RandomString()) 233 ref, _ := ref.NewRef(&data) 234 return ref 235 } 236 237 func RandomClientError() client_errors.ClientError { 238 return client_errors.ClientError{ 239 DetailCode: RandomString(), 240 ReadableDetail: RandomString(), 241 HTTPCode: 400 + rand.Intn(100), 242 } 243 } 244 245 func RandomContextedComment() comment_entities.ContextedComment { 246 return comment_entities.ContextedComment{ 247 Comment: RandomComment(), 248 Author: RandomContextedProfile(), 249 OwnLikeContext: RandomLikeableContext(), 250 } 251 } 252 func RandomContextedComments() []comment_entities.ContextedComment { 253 return []comment_entities.ContextedComment{RandomContextedComment(), RandomContextedComment(), RandomContextedComment()} 254 } 255 256 func RandomCommentModel() comment_models.CommentModel { 257 return comment_models.CommentModel{ 258 Id: RandomId(), 259 AuthorId: RandomString(), 260 Text: RandomString(), 261 CreatedAt: RandomTime().Unix(), 262 } 263 } 264 func RandomComment() comment_entities.Comment { 265 return comment_entities.Comment{ 266 CommentModel: RandomCommentModel(), 267 Likes: RandomInt(), 268 } 269 } 270 271 func RandomNewComment() comment_values.NewCommentValue { 272 return comment_values.NewCommentValue{ 273 Author: RandomId(), 274 Post: RandomId(), 275 Text: RandomString(), 276 } 277 } 278 279 func RandomBool() bool { 280 return rand.Float32() > 0.5 281 } 282 283 func RandomInt() int { 284 return rand.Intn(100) 285 } 286 287 func RandomString() string { 288 str := "" 289 for i := 0; i < 2; i++ { 290 str += words[rand.Intn(len(words))] + "_" 291 } 292 return str 293 } 294 295 var words = []string{"the", "be", "to", "of", "and", "a", "in", "that", "have", "I", "it", "for", "not", "on", "with", "he", "as", "you", "do", "at", "this", "but", "his", "by", "from", "they", "we", "say", "her", "she", "or", "an", "will", "my", "one", "all", "would", "there", "their", "what", "so", "up", "out", "if", "about", "who", "get", "which", "go", "me", "when", "make", "can", "like", "time", "no", "just", "him", "know", "take", "people", "into", "year", "your", "good", "some", "could", "them", "see", "other", "than", "then", "now", "look", "only", "come", "its", "over", "think", "also", "back", "after", "use", "two", "how", "our", "work", "first", "well", "way", "even", "new", "want", "because", "any", "these", "give", "day", "most", "us"}