mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-08 06:39:28 +02:00
171 lines
6 KiB
Go
171 lines
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/mbtypes"
|
|
"go.uploadedlobster.com/scotty/internal/models"
|
|
)
|
|
|
|
func TestTrackArtistName(t *testing.T) {
|
|
track := models.Track{
|
|
ArtistNames: []string{
|
|
"Foo",
|
|
"Bar",
|
|
"Baz",
|
|
},
|
|
}
|
|
assert.Equal(t, "Foo, Bar, Baz", track.ArtistName())
|
|
}
|
|
|
|
func TestTrackFillAdditionalInfo(t *testing.T) {
|
|
track := models.Track{
|
|
RecordingMBID: mbtypes.MBID("c0a1fc94-5f04-4a5f-bc09-e5de0c49cd12"),
|
|
ReleaseGroupMBID: mbtypes.MBID("80aca1ee-aa51-41be-9f75-024710d92ff4"),
|
|
ReleaseMBID: mbtypes.MBID("aa1ea1ac-7ec4-4542-a494-105afbfe547d"),
|
|
ArtistMBIDs: []mbtypes.MBID{"24412926-c7bd-48e8-afad-8a285b42e131"},
|
|
WorkMBIDs: []mbtypes.MBID{"c0a1fc94-5f04-4a5f-bc09-e5de0c49cd12"},
|
|
TrackNumber: 5,
|
|
DiscNumber: 1,
|
|
Duration: time.Duration(413787 * time.Millisecond),
|
|
ISRC: "DES561620801",
|
|
Tags: []string{"rock", "psychedelic rock"},
|
|
}
|
|
track.FillAdditionalInfo()
|
|
i := track.AdditionalInfo
|
|
assert := assert.New(t)
|
|
assert.Equal(track.RecordingMBID, i["recording_mbid"])
|
|
assert.Equal(track.ReleaseGroupMBID, i["release_group_mbid"])
|
|
assert.Equal(track.ReleaseMBID, i["release_mbid"])
|
|
assert.Equal(track.ArtistMBIDs, i["artist_mbids"])
|
|
assert.Equal(track.WorkMBIDs, i["work_mbids"])
|
|
assert.Equal(track.TrackNumber, i["tracknumber"])
|
|
assert.Equal(track.DiscNumber, i["discnumber"])
|
|
assert.Equal(track.Duration.Milliseconds(), i["duration_ms"])
|
|
assert.Equal(track.ISRC, i["isrc"])
|
|
assert.Equal(track.Tags, i["tags"])
|
|
}
|
|
|
|
func TestTrackFillAdditionalInfoRoundSecond(t *testing.T) {
|
|
ts := models.Track{Duration: time.Duration(123000 * time.Millisecond)}
|
|
ts.FillAdditionalInfo()
|
|
assert.Equal(t, int64(123), ts.AdditionalInfo["duration"])
|
|
assert.Equal(t, nil, ts.AdditionalInfo["duration_ms"])
|
|
tms := models.Track{Duration: time.Duration(123001 * time.Millisecond)}
|
|
tms.FillAdditionalInfo()
|
|
assert.Equal(t, nil, tms.AdditionalInfo["duration"])
|
|
assert.Equal(t, int64(123001), tms.AdditionalInfo["duration_ms"])
|
|
}
|
|
|
|
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) {
|
|
logEntry1 := models.LogEntry{
|
|
Type: models.Warning,
|
|
Message: "foo",
|
|
}
|
|
logEntry2 := models.LogEntry{
|
|
Type: models.Error,
|
|
Message: "bar",
|
|
}
|
|
result := models.ImportResult{
|
|
TotalCount: 100,
|
|
ImportCount: 20,
|
|
LastTimestamp: time.Now(),
|
|
ImportLog: []models.LogEntry{logEntry1},
|
|
}
|
|
newResult := models.ImportResult{
|
|
TotalCount: 120,
|
|
ImportCount: 50,
|
|
LastTimestamp: time.Now().Add(1 * time.Hour),
|
|
ImportLog: []models.LogEntry{logEntry2},
|
|
}
|
|
result.Update(newResult)
|
|
assert.Equal(t, 120, result.TotalCount)
|
|
assert.Equal(t, 50, result.ImportCount)
|
|
assert.Equal(t, newResult.LastTimestamp, result.LastTimestamp)
|
|
assert.Equal(t, []models.LogEntry{logEntry1, logEntry2}, result.ImportLog)
|
|
}
|
|
|
|
func TestImportResultLog(t *testing.T) {
|
|
result := models.ImportResult{}
|
|
result.Log(models.Warning, "foo")
|
|
result.Log(models.Error, "bar")
|
|
expected := []models.LogEntry{{
|
|
Type: models.Warning,
|
|
Message: "foo",
|
|
}, {
|
|
Type: models.Error,
|
|
Message: "bar",
|
|
}}
|
|
assert.Equal(t, expected, result.ImportLog)
|
|
}
|
|
|
|
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")
|
|
}
|