Skip to content

Commit

Permalink
[MM-12484] Fix return search posts on date filters (#9568)
Browse files Browse the repository at this point in the history
* fix return search posts on date filters

* add name to test cases
  • Loading branch information
saturninoabril committed Oct 5, 2018
1 parent fc03edd commit 0c2bf28
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 36 deletions.
18 changes: 15 additions & 3 deletions model/search_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ type SearchParams struct {

// Returns the epoch timestamp of the start of the day specified by SearchParams.AfterDate
func (p *SearchParams) GetAfterDateMillis() int64 {
date := ParseDateFilterToTime(p.AfterDate)
date, err := time.Parse("2006-01-02", PadDateStringZeros(p.AfterDate))
if err != nil {
date = time.Now()
}

// travel forward 1 day
oneDay := time.Hour * 24
afterDate := date.Add(oneDay)
Expand All @@ -36,7 +40,11 @@ func (p *SearchParams) GetAfterDateMillis() int64 {

// Returns the epoch timestamp of the end of the day specified by SearchParams.BeforeDate
func (p *SearchParams) GetBeforeDateMillis() int64 {
date := ParseDateFilterToTime(p.BeforeDate)
date, err := time.Parse("2006-01-02", PadDateStringZeros(p.BeforeDate))
if err != nil {
return 0
}

// travel back 1 day
oneDay := time.Hour * -24
beforeDate := date.Add(oneDay)
Expand All @@ -45,7 +53,11 @@ func (p *SearchParams) GetBeforeDateMillis() int64 {

// Returns the epoch timestamps of the start and end of the day specified by SearchParams.OnDate
func (p *SearchParams) GetOnDateMillis() (int64, int64) {
date := ParseDateFilterToTime(p.OnDate)
date, err := time.Parse("2006-01-02", PadDateStringZeros(p.OnDate))
if err != nil {
return 0, 0
}

return GetStartOfDayMillis(date, p.TimeZoneOffset), GetEndOfDayMillis(date, p.TimeZoneOffset)
}

Expand Down
114 changes: 114 additions & 0 deletions model/search_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ package model

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestSplitWords(t *testing.T) {
Expand Down Expand Up @@ -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)
})
}
}
19 changes: 5 additions & 14 deletions model/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,26 +141,17 @@ func NewRandomString(length int) string {
return b.String()
}

// GetMillis is a convience method to get milliseconds since epoch.
// GetMillis is a convenience method to get milliseconds since epoch.
func GetMillis() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}

// GetMillisForTime is a convience method to get milliseconds since epoch for provided Time.
// GetMillisForTime is a convenience method to get milliseconds since epoch for provided Time.
func GetMillisForTime(thisTime time.Time) int64 {
return thisTime.UnixNano() / int64(time.Millisecond)
}

// ParseDateFilterToTime is a convience method to get Time from string
func ParseDateFilterToTime(filterString string) time.Time {
resultTime, err := time.Parse("2006-01-02", PadDateStringZeros(filterString))
if err != nil {
return time.Now()
}
return resultTime
}

// PadDateStringZeros is a convience method to pad 2 digit date parts with zeros to meet ISO 8601 format
// PadDateStringZeros is a convenience method to pad 2 digit date parts with zeros to meet ISO 8601 format
func PadDateStringZeros(dateString string) string {
parts := strings.Split(dateString, "-")
for index, part := range parts {
Expand All @@ -172,14 +163,14 @@ func PadDateStringZeros(dateString string) string {
return dateString
}

// GetStartOfDayMillis is a convience method to get milliseconds since epoch for provided date's start of day
// GetStartOfDayMillis is a convenience method to get milliseconds since epoch for provided date's start of day
func GetStartOfDayMillis(thisTime time.Time, timeZoneOffset int) int64 {
localSearchTimeZone := time.FixedZone("Local Search Time Zone", timeZoneOffset)
resultTime := time.Date(thisTime.Year(), thisTime.Month(), thisTime.Day(), 0, 0, 0, 0, localSearchTimeZone)
return GetMillisForTime(resultTime)
}

// GetEndOfDayMillis is a convience method to get milliseconds since epoch for provided date's end of day
// GetEndOfDayMillis is a convenience method to get milliseconds since epoch for provided date's end of day
func GetEndOfDayMillis(thisTime time.Time, timeZoneOffset int) int64 {
localSearchTimeZone := time.FixedZone("Local Search Time Zone", timeZoneOffset)
resultTime := time.Date(thisTime.Year(), thisTime.Month(), thisTime.Day(), 23, 59, 59, 999999999, localSearchTimeZone)
Expand Down
39 changes: 20 additions & 19 deletions model/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,26 @@ func TestGetMillisForTime(t *testing.T) {
}
}

func TestParseDateFilterToTimeISO8601(t *testing.T) {
testString := "2016-08-01"
compareTime := time.Date(2016, time.August, 1, 0, 0, 0, 0, time.UTC)

result := ParseDateFilterToTime(testString)

if result != compareTime {
t.Fatalf(fmt.Sprintf("parsed date doesn't match the expected result: parsed result %v and expected time %v", result, compareTime))
}
}

func TestParseDateFilterToTimeNeedZeroPadding(t *testing.T) {
testString := "2016-8-1"
compareTime := time.Date(2016, time.August, 1, 0, 0, 0, 0, time.UTC)

result := ParseDateFilterToTime(testString)

if result != compareTime {
t.Fatalf(fmt.Sprintf("parsed date doesn't match the expected result: parsed result %v and expected time %v", result, compareTime))
func TestPadDateStringZeros(t *testing.T) {
for _, testCase := range []struct {
Name string
Input string
Expected string
}{
{
Name: "Valid date",
Input: "2016-08-01",
Expected: "2016-08-01",
},
{
Name: "Valid date but requires padding of zero",
Input: "2016-8-1",
Expected: "2016-08-01",
},
} {
t.Run(testCase.Name, func(t *testing.T) {
assert.Equal(t, testCase.Expected, PadDateStringZeros(testCase.Input))
})
}
}

Expand Down

0 comments on commit 0c2bf28

Please sign in to comment.