mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-16 18:19:28 +02:00
106 lines
3.6 KiB
Go
106 lines
3.6 KiB
Go
/*
|
|
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
package models_test
|
|
|
|
import (
|
|
"sort"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"go.uploadedlobster.com/scotty/models"
|
|
)
|
|
|
|
func TestTrackArtistName(t *testing.T) {
|
|
track := models.Track{
|
|
ArtistNames: []string{
|
|
"Foo",
|
|
"Bar",
|
|
"Baz",
|
|
},
|
|
}
|
|
assert.Equal(t, "Foo, Bar, Baz", track.ArtistName())
|
|
}
|
|
|
|
func TestListensListNewerThan(t *testing.T) {
|
|
listen1 := models.Listen{ListenedAt: time.Unix(3, 0)}
|
|
listen2 := models.Listen{ListenedAt: time.Unix(0, 0)}
|
|
listen3 := models.Listen{ListenedAt: time.Unix(2, 0)}
|
|
list := models.ListensList{listen1, listen2, listen3}
|
|
sort.Sort(list)
|
|
assert.Equal(t, listen1, list[2])
|
|
assert.Equal(t, listen2, list[0])
|
|
assert.Equal(t, listen3, list[1])
|
|
}
|
|
|
|
func TestListensListSort(t *testing.T) {
|
|
now := time.Now()
|
|
listen1 := models.Listen{UserName: "l1", ListenedAt: now.Add(-1 * time.Hour)}
|
|
listen2 := models.Listen{UserName: "l2", ListenedAt: now}
|
|
listen3 := models.Listen{UserName: "l3", ListenedAt: now.Add(1 * time.Hour)}
|
|
listen4 := models.Listen{UserName: "l4", ListenedAt: now.Add(2 * time.Hour)}
|
|
list := models.ListensList{listen1, listen2, listen3, listen4}
|
|
newList := list.NewerThan(now)
|
|
require.Len(t, newList, 2)
|
|
assert.Equal(t, listen3, newList[0])
|
|
assert.Equal(t, listen4, newList[1])
|
|
}
|
|
|
|
func TestLovesListSort(t *testing.T) {
|
|
love1 := models.Love{Created: time.Unix(3, 0)}
|
|
love2 := models.Love{Created: time.Unix(0, 0)}
|
|
love3 := models.Love{Created: time.Unix(2, 0)}
|
|
list := models.LovesList{love1, love2, love3}
|
|
sort.Sort(list)
|
|
assert.Equal(t, love1, list[2])
|
|
assert.Equal(t, love2, list[0])
|
|
assert.Equal(t, love3, list[1])
|
|
}
|
|
|
|
func TestImportResultUpdate(t *testing.T) {
|
|
result := models.ImportResult{
|
|
TotalCount: 100,
|
|
ImportCount: 20,
|
|
LastTimestamp: time.Now(),
|
|
}
|
|
newResult := models.ImportResult{
|
|
TotalCount: 120,
|
|
ImportCount: 50,
|
|
LastTimestamp: time.Now().Add(1 * time.Hour),
|
|
}
|
|
result.Update(newResult)
|
|
assert.Equal(t, 120, result.TotalCount)
|
|
assert.Equal(t, 70, result.ImportCount)
|
|
assert.Equal(t, newResult.LastTimestamp, result.LastTimestamp)
|
|
}
|
|
|
|
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")
|
|
}
|