mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-25 13:57:56 +02:00
Add ImportResult.UpdateTimestamp method
This commit is contained in:
parent
298697dcfc
commit
729a3d0ed0
6 changed files with 39 additions and 30 deletions
|
@ -60,20 +60,3 @@ type LovesImport interface {
|
|||
// Imports the given list of loves.
|
||||
ImportLoves(results chan LovesResult, oldestTimestamp time.Time) (ImportResult, error)
|
||||
}
|
||||
|
||||
type ListensResult struct {
|
||||
Error error
|
||||
Listens ListensList
|
||||
}
|
||||
|
||||
type LovesResult struct {
|
||||
Error error
|
||||
Loves LovesList
|
||||
}
|
||||
|
||||
type ImportResult struct {
|
||||
TotalCount int
|
||||
ImportCount int
|
||||
LastTimestamp time.Time
|
||||
ImportErrors []string
|
||||
}
|
||||
|
|
|
@ -92,3 +92,27 @@ func (l LovesList) Less(i, j int) bool {
|
|||
func (l LovesList) Swap(i, j int) {
|
||||
l[i], l[j] = l[j], l[i]
|
||||
}
|
||||
|
||||
type ListensResult struct {
|
||||
Error error
|
||||
Listens ListensList
|
||||
}
|
||||
|
||||
type LovesResult struct {
|
||||
Error error
|
||||
Loves LovesList
|
||||
}
|
||||
|
||||
type ImportResult struct {
|
||||
TotalCount int
|
||||
ImportCount int
|
||||
LastTimestamp time.Time
|
||||
ImportErrors []string
|
||||
}
|
||||
|
||||
// Sets LastTimestamp to newTime, if newTime is newer than LastTimestamp
|
||||
func (i *ImportResult) UpdateTimestamp(newTime time.Time) {
|
||||
if newTime.Unix() > i.LastTimestamp.Unix() {
|
||||
i.LastTimestamp = newTime
|
||||
}
|
||||
}
|
||||
|
|
|
@ -62,3 +62,14 @@ func TestLovesListSort(t *testing.T) {
|
|||
assert.Equal(t, love2, list[0])
|
||||
assert.Equal(t, love3, list[1])
|
||||
}
|
||||
|
||||
func TestImportResultUpdateTimestamp(t *testing.T) {
|
||||
timestamp := time.Now()
|
||||
i := models.ImportResult{LastTimestamp: timestamp}
|
||||
newTimestamp := time.Now().Add(-time.Duration(2 * time.Second))
|
||||
i.UpdateTimestamp(newTimestamp)
|
||||
assert.Equalf(t, timestamp, i.LastTimestamp, "Expected older timestamp is kept")
|
||||
newTimestamp = time.Now().Add(time.Duration(2 * time.Second))
|
||||
i.UpdateTimestamp(newTimestamp)
|
||||
assert.Equalf(t, newTimestamp, i.LastTimestamp, "Updated timestamp expected")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue