scotty/backends/jspf/models_test.go
Philipp Wolfer 117014a977
Change project license to GPLv3
Individual files, mainly the models and the HTTP clients stay under MIT
2023-11-22 08:05:23 +01:00

106 lines
3.4 KiB
Go

/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
This file is part of Scotty.
Scotty is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
Scotty. If not, see <https://www.gnu.org/licenses/>.
*/
package jspf_test
import (
"encoding/json"
"fmt"
"io"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uploadedlobster.com/scotty/backends/jspf"
)
func TestUnmarshalSimple(t *testing.T) {
data, err := readSampleJson("testdata/simple.jspf")
require.NoError(t, err)
assert := assert.New(t)
playlist := data.Playlist
assert.Equal("Two Songs From Thriller", playlist.Title)
assert.Equal("MJ Fan", playlist.Creator)
require.Len(t, playlist.Tracks, 2)
track1 := playlist.Tracks[0]
require.Len(t, track1.Location, 1)
assert.Equal("http://example.com/billiejean.mp3", track1.Location[0])
assert.Equal("Billie Jean", track1.Title)
assert.Equal("Michael Jackson", track1.Creator)
assert.Equal("Thriller", track1.Album)
}
func TestUnmarshalComprehensive(t *testing.T) {
data, err := readSampleJson("testdata/comprehensive.jspf")
require.NoError(t, err)
assert := assert.New(t)
playlist := data.Playlist
assert.Equal("http://example.com/", playlist.License)
require.Len(t, playlist.Attribution, 2)
assert.Equal("http://example.com/", playlist.Attribution[0]["identifier"])
assert.Equal("http://example.com/", playlist.Attribution[1]["location"])
require.Len(t, playlist.Meta, 2)
assert.Equal("345", playlist.Meta[1]["http://example.com/rel/2/"])
require.Len(t, playlist.Links, 2)
assert.Equal("http://example.com/body/1/", playlist.Links[0]["http://example.com/rel/1/"])
}
func TestUnmarshalListenBrainzPlaylist(t *testing.T) {
data, err := readSampleJson("testdata/lb-playlist.jspf")
require.NoError(t, err)
assert := assert.New(t)
playlist := data.Playlist
assert.Equal(
"https://listenbrainz.org/playlist/96485e27-967a-492a-9d04-c5a819baa2f3",
playlist.Identifier)
expectedPlaylistDate, err := time.Parse(time.RFC3339, "2023-07-04T21:03:52.317148+00:00")
require.NoError(t, err)
assert.Equal(expectedPlaylistDate, playlist.Date)
assert.NotNil(playlist.Extension["https://musicbrainz.org/doc/jspf#playlist"])
require.Len(t, playlist.Tracks, 2)
track1 := playlist.Tracks[0]
assert.Equal(
"https://musicbrainz.org/recording/3f2bdbbd-063e-478c-a394-6da0cb303302",
track1.Identifier[0])
fmt.Printf("Ext: %v\n", track1.Extension["https://musicbrainz.org/doc/jspf#track"])
extension := track1.Extension["https://musicbrainz.org/doc/jspf#track"].(map[string]any)
assert.NotNil(extension)
assert.Equal("outsidecontext", extension["added_by"])
}
func readSampleJson(path string) (jspf.Jspf, error) {
var result jspf.Jspf
jsonFile, err := os.Open(path)
if err != nil {
return result, err
}
defer jsonFile.Close()
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
return result, err
}
err = json.Unmarshal(byteValue, &result)
return result, err
}