github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/model/search_params_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package model 5 6 import ( 7 "testing" 8 "time" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestSplitWords(t *testing.T) { 14 if words := splitWords(""); len(words) != 0 { 15 t.Fatalf("Incorrect output splitWords: %v", words) 16 } 17 18 if words := splitWords(" "); len(words) != 0 { 19 t.Fatalf("Incorrect output splitWords: %v", words) 20 } 21 22 if words := splitWords("word"); len(words) != 1 || words[0] != "word" { 23 t.Fatalf("Incorrect output splitWords: %v", words) 24 } 25 26 if words := splitWords("wo\"rd"); len(words) != 2 || words[0] != "wo" || words[1] != "\"rd" { 27 t.Fatalf("Incorrect output splitWords: %v", words) 28 } 29 30 if words := splitWords("wo\"rd\""); len(words) != 2 || words[0] != "wo" || words[1] != "\"rd\"" { 31 t.Fatalf("Incorrect output splitWords: %v", words) 32 } 33 34 if words := splitWords("word1 word2 word3"); len(words) != 3 || words[0] != "word1" || words[1] != "word2" || words[2] != "word3" { 35 t.Fatalf("Incorrect output splitWords: %v", words) 36 } 37 38 if words := splitWords("word1 \"word2 word3"); len(words) != 3 || words[0] != "word1" || words[1] != "\"word2" || words[2] != "word3" { 39 t.Fatalf("Incorrect output splitWords: %v", words) 40 } 41 42 if words := splitWords("\"word1 word2 word3"); len(words) != 3 || words[0] != "\"word1" || words[1] != "word2" || words[2] != "word3" { 43 t.Fatalf("Incorrect output splitWords: %v", words) 44 } 45 46 if words := splitWords("word1 word2 word3\""); len(words) != 4 || words[0] != "word1" || words[1] != "word2" || words[2] != "word3" || words[3] != "\"" { 47 t.Fatalf("Incorrect output splitWords: %v", words) 48 } 49 50 if words := splitWords("word1 #word2 ##word3"); len(words) != 3 || words[0] != "word1" || words[1] != "#word2" || words[2] != "##word3" { 51 t.Fatalf("Incorrect output splitWords: %v", words) 52 } 53 54 if words := splitWords(" word1 word2 word3 "); len(words) != 3 || words[0] != "word1" || words[1] != "word2" || words[2] != "word3" { 55 t.Fatalf("Incorrect output splitWords: %v", words) 56 } 57 58 if words := splitWords("\"quoted\""); len(words) != 1 || words[0] != "\"quoted\"" { 59 t.Fatalf("Incorrect output splitWords: %v", words) 60 } 61 62 if words := splitWords("\"quoted multiple words\""); len(words) != 1 || words[0] != "\"quoted multiple words\"" { 63 t.Fatalf("Incorrect output splitWords: %v", words) 64 } 65 66 if words := splitWords("some stuff \"quoted multiple words\" more stuff"); len(words) != 5 || words[0] != "some" || words[1] != "stuff" || words[2] != "\"quoted multiple words\"" || words[3] != "more" || words[4] != "stuff" { 67 t.Fatalf("Incorrect output splitWords: %v", words) 68 } 69 70 if words := splitWords("some \"stuff\" \"quoted multiple words\" #some \"more stuff\""); len(words) != 5 || words[0] != "some" || words[1] != "\"stuff\"" || words[2] != "\"quoted multiple words\"" || words[3] != "#some" || words[4] != "\"more stuff\"" { 71 t.Fatalf("Incorrect output splitWords: %v", words) 72 } 73 } 74 75 func TestParseSearchFlags(t *testing.T) { 76 if words, flags := parseSearchFlags(splitWords("")); len(words) != 0 { 77 t.Fatalf("got words from empty input") 78 } else if len(flags) != 0 { 79 t.Fatalf("got flags from empty input") 80 } 81 82 if words, flags := parseSearchFlags(splitWords("word")); len(words) != 1 || words[0] != "word" { 83 t.Fatalf("got incorrect words %v", words) 84 } else if len(flags) != 0 { 85 t.Fatalf("got incorrect flags %v", flags) 86 } 87 88 if words, flags := parseSearchFlags(splitWords("apple banana cherry")); len(words) != 3 || words[0] != "apple" || words[1] != "banana" || words[2] != "cherry" { 89 t.Fatalf("got incorrect words %v", words) 90 } else if len(flags) != 0 { 91 t.Fatalf("got incorrect flags %v", flags) 92 } 93 94 if words, flags := parseSearchFlags(splitWords("apple banana from:chan")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" { 95 t.Fatalf("got incorrect words %v", words) 96 } else if len(flags) != 1 || flags[0][0] != "from" || flags[0][1] != "chan" { 97 t.Fatalf("got incorrect flags %v", flags) 98 } 99 100 if words, flags := parseSearchFlags(splitWords("#apple #banana from:chan")); len(words) != 2 || words[0] != "#apple" || words[1] != "#banana" { 101 t.Fatalf("got incorrect words %v", words) 102 } else if len(flags) != 1 || flags[0][0] != "from" || flags[0][1] != "chan" { 103 t.Fatalf("got incorrect flags %v", flags) 104 } 105 106 if words, flags := parseSearchFlags(splitWords("apple banana from: chan")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" { 107 t.Fatalf("got incorrect words %v", words) 108 } else if len(flags) != 1 || flags[0][0] != "from" || flags[0][1] != "chan" { 109 t.Fatalf("got incorrect flags %v", flags) 110 } 111 112 if words, flags := parseSearchFlags(splitWords("apple banana in: chan")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" { 113 t.Fatalf("got incorrect words %v", words) 114 } else if len(flags) != 1 || flags[0][0] != "in" || flags[0][1] != "chan" { 115 t.Fatalf("got incorrect flags %v", flags) 116 } 117 118 if words, flags := parseSearchFlags(splitWords("apple banana channel:chan")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" { 119 t.Fatalf("got incorrect words %v", words) 120 } else if len(flags) != 1 || flags[0][0] != "channel" || flags[0][1] != "chan" { 121 t.Fatalf("got incorrect flags %v", flags) 122 } 123 124 if words, flags := parseSearchFlags(splitWords("fruit: cherry")); len(words) != 2 || words[0] != "fruit" || words[1] != "cherry" { 125 t.Fatalf("got incorrect words %v", words) 126 } else if len(flags) != 0 { 127 t.Fatalf("got incorrect flags %v", flags) 128 } 129 130 if words, flags := parseSearchFlags(splitWords("channel:")); len(words) != 1 || words[0] != "channel" { 131 t.Fatalf("got incorrect words %v", words) 132 } else if len(flags) != 0 { 133 t.Fatalf("got incorrect flags %v", flags) 134 } 135 136 if words, flags := parseSearchFlags(splitWords("channel: first in: second from:")); len(words) != 1 || words[0] != "from" { 137 t.Fatalf("got incorrect words %v", words) 138 } else if len(flags) != 2 || flags[0][0] != "channel" || flags[0][1] != "first" || flags[1][0] != "in" || flags[1][1] != "second" { 139 t.Fatalf("got incorrect flags %v", flags) 140 } 141 142 if words, flags := parseSearchFlags(splitWords("channel: first channel: second from: third from: fourth")); len(words) != 0 { 143 t.Fatalf("got incorrect words %v", words) 144 } else if len(flags) != 4 || flags[0][0] != "channel" || flags[0][1] != "first" || flags[1][0] != "channel" || flags[1][1] != "second" || 145 flags[2][0] != "from" || flags[2][1] != "third" || flags[3][0] != "from" || flags[3][1] != "fourth" { 146 t.Fatalf("got incorrect flags %v", flags) 147 } 148 149 if words, flags := parseSearchFlags(splitWords("\"quoted\"")); len(words) != 1 || words[0] != "\"quoted\"" { 150 t.Fatalf("got incorrect words %v", words) 151 } else if len(flags) != 0 { 152 t.Fatalf("got incorrect flags %v", flags) 153 } 154 155 if words, flags := parseSearchFlags(splitWords("\"quoted multiple words\"")); len(words) != 1 || words[0] != "\"quoted multiple words\"" { 156 t.Fatalf("got incorrect words %v", words) 157 } else if len(flags) != 0 { 158 t.Fatalf("got incorrect flags %v", flags) 159 } 160 161 if words, flags := parseSearchFlags(splitWords("some \"stuff\" \"quoted multiple words\" some \"more stuff\"")); len(words) != 5 || words[0] != "some" || words[1] != "\"stuff\"" || words[2] != "\"quoted multiple words\"" || words[3] != "some" || words[4] != "\"more stuff\"" { 162 t.Fatalf("Incorrect output splitWords: %v", words) 163 } else if len(flags) != 0 { 164 t.Fatalf("got incorrect flags %v", flags) 165 } 166 167 if words, flags := parseSearchFlags(splitWords("some in:here \"stuff\" \"quoted multiple words\" from:someone \"more stuff\"")); len(words) != 4 || words[0] != "some" || words[1] != "\"stuff\"" || words[2] != "\"quoted multiple words\"" || words[3] != "\"more stuff\"" { 168 t.Fatalf("Incorrect output splitWords: %v", words) 169 } else if len(flags) != 2 || flags[0][0] != "in" || flags[0][1] != "here" || flags[1][0] != "from" || flags[1][1] != "someone" { 170 t.Fatalf("got incorrect flags %v", flags) 171 } 172 173 if words, flags := parseSearchFlags(splitWords("after:2018-1-1")); len(words) != 0 { 174 t.Fatalf("got incorrect words %v", words) 175 } else if len(flags) != 1 || flags[0][0] != "after" || flags[0][1] != "2018-1-1" { 176 t.Fatalf("got incorrect flags %v", flags) 177 } 178 179 if words, flags := parseSearchFlags(splitWords("apple banana after:2018-1-1")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" { 180 t.Fatalf("got incorrect words %v", words) 181 } else if len(flags) != 1 || flags[0][0] != "after" || flags[0][1] != "2018-1-1" { 182 t.Fatalf("got incorrect flags %v", flags) 183 } 184 185 if words, flags := parseSearchFlags(splitWords("apple banana before:2018-1-1")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" { 186 t.Fatalf("got incorrect words %v", words) 187 } else if len(flags) != 1 || flags[0][0] != "before" || flags[0][1] != "2018-1-1" { 188 t.Fatalf("got incorrect flags %v", flags) 189 } 190 191 if words, flags := parseSearchFlags(splitWords("apple banana after:2018-1-1 before:2018-1-10")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" { 192 t.Fatalf("got incorrect words %v", words) 193 } else if len(flags) != 2 || flags[0][0] != "after" || flags[0][1] != "2018-1-1" || flags[1][0] != "before" || flags[1][1] != "2018-1-10" { 194 t.Fatalf("got incorrect flags %v", flags) 195 } 196 197 if words, flags := parseSearchFlags(splitWords("#apple #banana after:2018-1-1")); len(words) != 2 || words[0] != "#apple" || words[1] != "#banana" { 198 t.Fatalf("got incorrect words %v", words) 199 } else if len(flags) != 1 || flags[0][0] != "after" || flags[0][1] != "2018-1-1" { 200 t.Fatalf("got incorrect flags %v", flags) 201 } 202 203 if words, flags := parseSearchFlags(splitWords("#apple #banana before:2018-1-1")); len(words) != 2 || words[0] != "#apple" || words[1] != "#banana" { 204 t.Fatalf("got incorrect words %v", words) 205 } else if len(flags) != 1 || flags[0][0] != "before" || flags[0][1] != "2018-1-1" { 206 t.Fatalf("got incorrect flags %v", flags) 207 } 208 209 if words, flags := parseSearchFlags(splitWords("#apple #banana after:2018-1-1 before:2018-1-10")); len(words) != 2 || words[0] != "#apple" || words[1] != "#banana" { 210 t.Fatalf("got incorrect words %v", words) 211 } else if len(flags) != 2 || flags[0][0] != "after" || flags[0][1] != "2018-1-1" || flags[1][0] != "before" || flags[1][1] != "2018-1-10" { 212 t.Fatalf("got incorrect flags %v", flags) 213 } 214 215 if words, flags := parseSearchFlags(splitWords("apple banana after: 2018-1-1")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" { 216 t.Fatalf("got incorrect words %v", words) 217 } else if len(flags) != 1 || flags[0][0] != "after" || flags[0][1] != "2018-1-1" { 218 t.Fatalf("got incorrect flags %v", flags) 219 } 220 221 if words, flags := parseSearchFlags(splitWords("apple banana before: 2018-1-1")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" { 222 t.Fatalf("got incorrect words %v", words) 223 } else if len(flags) != 1 || flags[0][0] != "before" || flags[0][1] != "2018-1-1" { 224 t.Fatalf("got incorrect flags %v", flags) 225 } 226 227 if words, flags := parseSearchFlags(splitWords("apple banana after: 2018-1-1 before: 2018-1-10")); len(words) != 2 || words[0] != "apple" || words[1] != "banana" { 228 t.Fatalf("got incorrect words %v", words) 229 } else if len(flags) != 2 || flags[0][0] != "after" || flags[0][1] != "2018-1-1" || flags[1][0] != "before" || flags[1][1] != "2018-1-10" { 230 t.Fatalf("got incorrect flags %v", flags) 231 } 232 233 if words, flags := parseSearchFlags(splitWords("apple banana after: 2018-1-1 before: 2018-1-10 #fruit")); len(words) != 3 || words[0] != "apple" || words[1] != "banana" || words[2] != "#fruit" { 234 t.Fatalf("got incorrect words %v", words) 235 } else if len(flags) != 2 || flags[0][0] != "after" || flags[0][1] != "2018-1-1" || flags[1][0] != "before" || flags[1][1] != "2018-1-10" { 236 t.Fatalf("got incorrect flags %v", flags) 237 } 238 239 if words, flags := parseSearchFlags(splitWords("test after:2018-7-1")); len(words) != 1 || words[0] != "test" { 240 t.Fatalf("got incorrect words %v", words) 241 } else if len(flags) != 1 || flags[0][0] != "after" || flags[0][1] != "2018-7-1" { 242 t.Fatalf("got incorrect flags %v", flags) 243 } 244 } 245 246 func TestParseSearchParams(t *testing.T) { 247 if sp := ParseSearchParams("", 0); len(sp) != 0 { 248 t.Fatalf("Incorrect output from parse search params: %v", sp) 249 } 250 251 if sp := ParseSearchParams(" ", 0); len(sp) != 0 { 252 t.Fatalf("Incorrect output from parse search params: %v", sp) 253 } 254 255 if sp := ParseSearchParams("words words", 0); len(sp) != 1 || sp[0].Terms != "words words" || sp[0].IsHashtag || len(sp[0].InChannels) != 0 || len(sp[0].FromUsers) != 0 { 256 t.Fatalf("Incorrect output from parse search params: %v", sp) 257 } 258 259 if sp := ParseSearchParams("\"my stuff\"", 0); len(sp) != 1 || sp[0].Terms != "\"my stuff\"" || sp[0].IsHashtag || len(sp[0].InChannels) != 0 || len(sp[0].FromUsers) != 0 { 260 t.Fatalf("Incorrect output from parse search params: %v", sp) 261 } 262 263 if sp := ParseSearchParams("#words #words", 0); len(sp) != 1 || sp[0].Terms != "#words #words" || !sp[0].IsHashtag || len(sp[0].InChannels) != 0 || len(sp[0].FromUsers) != 0 { 264 t.Fatalf("Incorrect output from parse search params: %v", sp) 265 } 266 267 if sp := ParseSearchParams("#words words", 0); len(sp) != 2 || sp[1].Terms != "#words" || !sp[1].IsHashtag || len(sp[1].InChannels) != 0 || len(sp[1].FromUsers) != 0 || sp[0].Terms != "words" || sp[0].IsHashtag || len(sp[0].InChannels) != 0 { 268 t.Fatalf("Incorrect output from parse search params: %v", sp) 269 } 270 271 if sp := ParseSearchParams("in:channel", 0); len(sp) != 1 || sp[0].Terms != "" || len(sp[0].InChannels) != 1 || sp[0].InChannels[0] != "channel" || len(sp[0].FromUsers) != 0 { 272 t.Fatalf("Incorrect output from parse search params: %v", sp) 273 } 274 275 if sp := ParseSearchParams("testing in:channel", 0); len(sp) != 1 || sp[0].Terms != "testing" || len(sp[0].InChannels) != 1 || sp[0].InChannels[0] != "channel" || len(sp[0].FromUsers) != 0 { 276 t.Fatalf("Incorrect output from parse search params: %v", sp) 277 } 278 279 if sp := ParseSearchParams("in:channel testing", 0); len(sp) != 1 || sp[0].Terms != "testing" || len(sp[0].InChannels) != 1 || sp[0].InChannels[0] != "channel" || len(sp[0].FromUsers) != 0 { 280 t.Fatalf("Incorrect output from parse search params: %v", sp) 281 } 282 283 if sp := ParseSearchParams("in:channel in:otherchannel", 0); len(sp) != 1 || sp[0].Terms != "" || len(sp[0].InChannels) != 2 || sp[0].InChannels[0] != "channel" || sp[0].InChannels[1] != "otherchannel" || len(sp[0].FromUsers) != 0 { 284 t.Fatalf("Incorrect output from parse search params: %v", sp) 285 } 286 287 if sp := ParseSearchParams("testing in:channel from:someone", 0); len(sp) != 1 || sp[0].Terms != "testing" || len(sp[0].InChannels) != 1 || sp[0].InChannels[0] != "channel" || len(sp[0].FromUsers) != 1 || sp[0].FromUsers[0] != "someone" { 288 t.Fatalf("Incorrect output from parse search params: %v", sp[0]) 289 } 290 291 if sp := ParseSearchParams("##hashtag +#plus+", 0); len(sp) != 1 || sp[0].Terms != "#hashtag #plus" || !sp[0].IsHashtag || len(sp[0].InChannels) != 0 || len(sp[0].FromUsers) != 0 { 292 t.Fatalf("Incorrect output from parse search params: %v", sp[0]) 293 } 294 295 if sp := ParseSearchParams("wildcar*", 0); len(sp) != 1 || sp[0].Terms != "wildcar*" || sp[0].IsHashtag || len(sp[0].InChannels) != 0 || len(sp[0].FromUsers) != 0 { 296 t.Fatalf("Incorrect output from parse search params: %v", sp[0]) 297 } 298 299 if sp := ParseSearchParams("after:2018-8-1 testing", 0); len(sp) != 1 || sp[0].Terms != "testing" || len(sp[0].AfterDate) == 0 || sp[0].AfterDate != "2018-8-1" { 300 t.Fatalf("Incorrect output from parse search params: %v", sp) 301 } 302 303 if sp := ParseSearchParams("after:2018-8-1", 0); len(sp) != 1 || sp[0].Terms != "" || len(sp[0].AfterDate) == 0 || sp[0].AfterDate != "2018-8-1" { 304 t.Fatalf("Incorrect output from parse search params: %v", sp) 305 } 306 } 307 308 func TestGetOnDateMillis(t *testing.T) { 309 for _, testCase := range []struct { 310 Name string 311 Input string 312 StartOnDate int64 313 EndOnDate int64 314 }{ 315 { 316 Name: "Valid date", 317 Input: "2018-08-01", 318 StartOnDate: 1533081600000, 319 EndOnDate: 1533167999999, 320 }, 321 { 322 Name: "Valid date but requires padding of zero", 323 Input: "2018-8-1", 324 StartOnDate: 1533081600000, 325 EndOnDate: 1533167999999, 326 }, 327 { 328 Name: "Invalid date, date not exist", 329 Input: "2018-02-29", 330 StartOnDate: 0, 331 EndOnDate: 0, 332 }, 333 { 334 Name: "Invalid date, not date format", 335 Input: "holiday", 336 StartOnDate: 0, 337 EndOnDate: 0, 338 }, 339 } { 340 t.Run(testCase.Name, func(t *testing.T) { 341 sp := &SearchParams{OnDate: testCase.Input, TimeZoneOffset: 0} 342 startOnDate, endOnDate := sp.GetOnDateMillis() 343 assert.Equal(t, testCase.StartOnDate, startOnDate) 344 assert.Equal(t, testCase.EndOnDate, endOnDate) 345 }) 346 } 347 } 348 349 func TestGetBeforeDateMillis(t *testing.T) { 350 for _, testCase := range []struct { 351 Name string 352 Input string 353 BeforeDate int64 354 }{ 355 { 356 Name: "Valid date", 357 Input: "2018-08-01", 358 BeforeDate: 1533081599999, 359 }, 360 { 361 Name: "Valid date but requires padding of zero", 362 Input: "2018-8-1", 363 BeforeDate: 1533081599999, 364 }, 365 { 366 Name: "Invalid date, date not exist", 367 Input: "2018-02-29", 368 BeforeDate: 0, 369 }, 370 { 371 Name: "Invalid date, not date format", 372 Input: "holiday", 373 BeforeDate: 0, 374 }, 375 } { 376 t.Run(testCase.Name, func(t *testing.T) { 377 sp := &SearchParams{BeforeDate: testCase.Input, TimeZoneOffset: 0} 378 beforeDate := sp.GetBeforeDateMillis() 379 assert.Equal(t, testCase.BeforeDate, beforeDate) 380 }) 381 } 382 } 383 384 func TestGetAfterDateMillis(t *testing.T) { 385 for _, testCase := range []struct { 386 Name string 387 Input string 388 AfterDate int64 389 }{ 390 { 391 Name: "Valid date", 392 Input: "2018-08-01", 393 AfterDate: 1533168000000, 394 }, 395 { 396 Name: "Valid date but requires padding of zero", 397 Input: "2018-8-1", 398 AfterDate: 1533168000000, 399 }, 400 { 401 Name: "Invalid date, date not exist", 402 Input: "2018-02-29", 403 AfterDate: GetStartOfDayMillis(time.Now().Add(time.Hour*24), 0), 404 }, 405 { 406 Name: "Invalid date, not date format", 407 Input: "holiday", 408 AfterDate: GetStartOfDayMillis(time.Now().Add(time.Hour*24), 0), 409 }, 410 } { 411 t.Run(testCase.Name, func(t *testing.T) { 412 sp := &SearchParams{AfterDate: testCase.Input, TimeZoneOffset: 0} 413 afterDate := sp.GetAfterDateMillis() 414 assert.Equal(t, testCase.AfterDate, afterDate) 415 }) 416 } 417 }