[MM-12484] Fix return search posts on date filters (#9568)
* fix return search posts on date filters * add name to test cases
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
cba33137d2
Коммит
69e10651c9
@@ -27,7 +27,11 @@ type SearchParams struct {
|
|||||||
|
|
||||||
// Returns the epoch timestamp of the start of the day specified by SearchParams.AfterDate
|
// Returns the epoch timestamp of the start of the day specified by SearchParams.AfterDate
|
||||||
func (p *SearchParams) GetAfterDateMillis() int64 {
|
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
|
// travel forward 1 day
|
||||||
oneDay := time.Hour * 24
|
oneDay := time.Hour * 24
|
||||||
afterDate := date.Add(oneDay)
|
afterDate := date.Add(oneDay)
|
||||||
@@ -36,7 +40,11 @@ func (p *SearchParams) GetAfterDateMillis() int64 {
|
|||||||
|
|
||||||
// Returns the epoch timestamp of the end of the day specified by SearchParams.BeforeDate
|
// Returns the epoch timestamp of the end of the day specified by SearchParams.BeforeDate
|
||||||
func (p *SearchParams) GetBeforeDateMillis() int64 {
|
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
|
// travel back 1 day
|
||||||
oneDay := time.Hour * -24
|
oneDay := time.Hour * -24
|
||||||
beforeDate := date.Add(oneDay)
|
beforeDate := date.Add(oneDay)
|
||||||
@@ -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
|
// Returns the epoch timestamps of the start and end of the day specified by SearchParams.OnDate
|
||||||
func (p *SearchParams) GetOnDateMillis() (int64, int64) {
|
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)
|
return GetStartOfDayMillis(date, p.TimeZoneOffset), GetEndOfDayMillis(date, p.TimeZoneOffset)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,9 @@ package model
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSplitWords(t *testing.T) {
|
func TestSplitWords(t *testing.T) {
|
||||||
@@ -301,3 +304,114 @@ func TestParseSearchParams(t *testing.T) {
|
|||||||
t.Fatalf("Incorrect output from parse search params: %v", sp)
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -141,26 +141,17 @@ func NewRandomString(length int) string {
|
|||||||
return b.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 {
|
func GetMillis() int64 {
|
||||||
return time.Now().UnixNano() / int64(time.Millisecond)
|
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 {
|
func GetMillisForTime(thisTime time.Time) int64 {
|
||||||
return thisTime.UnixNano() / int64(time.Millisecond)
|
return thisTime.UnixNano() / int64(time.Millisecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseDateFilterToTime is a convience method to get Time from string
|
// PadDateStringZeros is a convenience method to pad 2 digit date parts with zeros to meet ISO 8601 format
|
||||||
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
|
|
||||||
func PadDateStringZeros(dateString string) string {
|
func PadDateStringZeros(dateString string) string {
|
||||||
parts := strings.Split(dateString, "-")
|
parts := strings.Split(dateString, "-")
|
||||||
for index, part := range parts {
|
for index, part := range parts {
|
||||||
@@ -172,14 +163,14 @@ func PadDateStringZeros(dateString string) string {
|
|||||||
return dateString
|
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 {
|
func GetStartOfDayMillis(thisTime time.Time, timeZoneOffset int) int64 {
|
||||||
localSearchTimeZone := time.FixedZone("Local Search Time Zone", timeZoneOffset)
|
localSearchTimeZone := time.FixedZone("Local Search Time Zone", timeZoneOffset)
|
||||||
resultTime := time.Date(thisTime.Year(), thisTime.Month(), thisTime.Day(), 0, 0, 0, 0, localSearchTimeZone)
|
resultTime := time.Date(thisTime.Year(), thisTime.Month(), thisTime.Day(), 0, 0, 0, 0, localSearchTimeZone)
|
||||||
return GetMillisForTime(resultTime)
|
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 {
|
func GetEndOfDayMillis(thisTime time.Time, timeZoneOffset int) int64 {
|
||||||
localSearchTimeZone := time.FixedZone("Local Search Time Zone", timeZoneOffset)
|
localSearchTimeZone := time.FixedZone("Local Search Time Zone", timeZoneOffset)
|
||||||
resultTime := time.Date(thisTime.Year(), thisTime.Month(), thisTime.Day(), 23, 59, 59, 999999999, localSearchTimeZone)
|
resultTime := time.Date(thisTime.Year(), thisTime.Month(), thisTime.Day(), 23, 59, 59, 999999999, localSearchTimeZone)
|
||||||
|
|||||||
@@ -44,25 +44,26 @@ func TestGetMillisForTime(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseDateFilterToTimeISO8601(t *testing.T) {
|
func TestPadDateStringZeros(t *testing.T) {
|
||||||
testString := "2016-08-01"
|
for _, testCase := range []struct {
|
||||||
compareTime := time.Date(2016, time.August, 1, 0, 0, 0, 0, time.UTC)
|
Name string
|
||||||
|
Input string
|
||||||
result := ParseDateFilterToTime(testString)
|
Expected string
|
||||||
|
}{
|
||||||
if result != compareTime {
|
{
|
||||||
t.Fatalf(fmt.Sprintf("parsed date doesn't match the expected result: parsed result %v and expected time %v", result, compareTime))
|
Name: "Valid date",
|
||||||
}
|
Input: "2016-08-01",
|
||||||
}
|
Expected: "2016-08-01",
|
||||||
|
},
|
||||||
func TestParseDateFilterToTimeNeedZeroPadding(t *testing.T) {
|
{
|
||||||
testString := "2016-8-1"
|
Name: "Valid date but requires padding of zero",
|
||||||
compareTime := time.Date(2016, time.August, 1, 0, 0, 0, 0, time.UTC)
|
Input: "2016-8-1",
|
||||||
|
Expected: "2016-08-01",
|
||||||
result := ParseDateFilterToTime(testString)
|
},
|
||||||
|
} {
|
||||||
if result != compareTime {
|
t.Run(testCase.Name, func(t *testing.T) {
|
||||||
t.Fatalf(fmt.Sprintf("parsed date doesn't match the expected result: parsed result %v and expected time %v", result, compareTime))
|
assert.Equal(t, testCase.Expected, PadDateStringZeros(testCase.Input))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user