From 69e10651c97c7d7b30aa69a0155c8d3293e2b9bd Mon Sep 17 00:00:00 2001 From: Saturnino Abril Date: Fri, 5 Oct 2018 22:25:34 +0800 Subject: [MM-12484] Fix return search posts on date filters (#9568) * fix return search posts on date filters * add name to test cases --- model/search_params_test.go | 114 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) (limited to 'model/search_params_test.go') diff --git a/model/search_params_test.go b/model/search_params_test.go index 49480352e..297ca2a45 100644 --- a/model/search_params_test.go +++ b/model/search_params_test.go @@ -5,6 +5,9 @@ package model import ( "testing" + "time" + + "github.com/stretchr/testify/assert" ) func TestSplitWords(t *testing.T) { @@ -301,3 +304,114 @@ func TestParseSearchParams(t *testing.T) { t.Fatalf("Incorrect output from parse search params: %v", sp) } } + +func TestGetOnDateMillis(t *testing.T) { + for _, testCase := range []struct { + Name string + Input string + StartOnDate int64 + EndOnDate int64 + }{ + { + Name: "Valid date", + Input: "2018-08-01", + StartOnDate: 1533081600000, + EndOnDate: 1533167999999, + }, + { + Name: "Valid date but requires padding of zero", + Input: "2018-8-1", + StartOnDate: 1533081600000, + EndOnDate: 1533167999999, + }, + { + Name: "Invalid date, date not exist", + Input: "2018-02-29", + StartOnDate: 0, + EndOnDate: 0, + }, + { + Name: "Invalid date, not date format", + Input: "holiday", + StartOnDate: 0, + EndOnDate: 0, + }, + } { + t.Run(testCase.Name, func(t *testing.T) { + sp := &SearchParams{OnDate: testCase.Input, TimeZoneOffset: 0} + startOnDate, endOnDate := sp.GetOnDateMillis() + assert.Equal(t, testCase.StartOnDate, startOnDate) + assert.Equal(t, testCase.EndOnDate, endOnDate) + }) + } +} + +func TestGetBeforeDateMillis(t *testing.T) { + for _, testCase := range []struct { + Name string + Input string + BeforeDate int64 + }{ + { + Name: "Valid date", + Input: "2018-08-01", + BeforeDate: 1533081599999, + }, + { + Name: "Valid date but requires padding of zero", + Input: "2018-8-1", + BeforeDate: 1533081599999, + }, + { + Name: "Invalid date, date not exist", + Input: "2018-02-29", + BeforeDate: 0, + }, + { + Name: "Invalid date, not date format", + Input: "holiday", + BeforeDate: 0, + }, + } { + t.Run(testCase.Name, func(t *testing.T) { + sp := &SearchParams{BeforeDate: testCase.Input, TimeZoneOffset: 0} + beforeDate := sp.GetBeforeDateMillis() + assert.Equal(t, testCase.BeforeDate, beforeDate) + }) + } +} + +func TestGetAfterDateMillis(t *testing.T) { + for _, testCase := range []struct { + Name string + Input string + AfterDate int64 + }{ + { + Name: "Valid date", + Input: "2018-08-01", + AfterDate: 1533168000000, + }, + { + Name: "Valid date but requires padding of zero", + Input: "2018-8-1", + AfterDate: 1533168000000, + }, + { + Name: "Invalid date, date not exist", + Input: "2018-02-29", + AfterDate: GetStartOfDayMillis(time.Now().Add(time.Hour*24), 0), + }, + { + Name: "Invalid date, not date format", + Input: "holiday", + AfterDate: GetStartOfDayMillis(time.Now().Add(time.Hour*24), 0), + }, + } { + t.Run(testCase.Name, func(t *testing.T) { + sp := &SearchParams{AfterDate: testCase.Input, TimeZoneOffset: 0} + afterDate := sp.GetAfterDateMillis() + assert.Equal(t, testCase.AfterDate, afterDate) + }) + } +} -- cgit v1.2.3-1-g7c22