mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-16 01:59:29 +02:00
114 lines
4.5 KiB
Go
114 lines
4.5 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 backends_test
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/spf13/viper"
|
|
"github.com/stretchr/testify/assert"
|
|
"go.uploadedlobster.com/scotty/backends"
|
|
"go.uploadedlobster.com/scotty/backends/dump"
|
|
"go.uploadedlobster.com/scotty/backends/funkwhale"
|
|
"go.uploadedlobster.com/scotty/backends/jspf"
|
|
"go.uploadedlobster.com/scotty/backends/listenbrainz"
|
|
"go.uploadedlobster.com/scotty/backends/maloja"
|
|
"go.uploadedlobster.com/scotty/backends/scrobblerlog"
|
|
"go.uploadedlobster.com/scotty/backends/subsonic"
|
|
"go.uploadedlobster.com/scotty/models"
|
|
)
|
|
|
|
func TestResolveBackend(t *testing.T) {
|
|
config := viper.New()
|
|
config.Set("backend", "dump")
|
|
backend, err := backends.ResolveBackend[models.ListensImport](config)
|
|
assert.NoError(t, err)
|
|
assert.IsType(t, &dump.DumpBackend{}, backend)
|
|
}
|
|
|
|
func TestResolveBackendUnknown(t *testing.T) {
|
|
config := viper.New()
|
|
config.Set("backend", "foo")
|
|
_, err := backends.ResolveBackend[models.ListensImport](config)
|
|
assert.EqualError(t, err, "Unknown backend foo")
|
|
}
|
|
|
|
func TestResolveBackendInvalidInterface(t *testing.T) {
|
|
config := viper.New()
|
|
config.Set("backend", "dump")
|
|
_, err := backends.ResolveBackend[models.ListensExport](config)
|
|
assert.EqualError(t, err, "Backend dump does not implement ListensExport")
|
|
}
|
|
|
|
func TestGetBackends(t *testing.T) {
|
|
backends := backends.GetBackends()
|
|
for _, info := range backends {
|
|
if info.Name == "dump" {
|
|
assert.Containsf(t, info.ImportCapabilities, "listens",
|
|
"ImportCapabilities for \"dump\" expected to contain \"listens\"")
|
|
assert.Emptyf(t, info.ExportCapabilities,
|
|
"ExportCapabilities for \"dump\" expected to be empty")
|
|
return // Finish the test
|
|
}
|
|
}
|
|
|
|
// If we got here the "dump" backend was not included
|
|
t.Errorf("GetBackends() did not return expected bacend \"dump\"")
|
|
}
|
|
|
|
func TestImplementsInterfaces(t *testing.T) {
|
|
expectInterface[models.ListensImport](t, &dump.DumpBackend{})
|
|
expectInterface[models.LovesImport](t, &dump.DumpBackend{})
|
|
|
|
expectInterface[models.ListensExport](t, &funkwhale.FunkwhaleApiBackend{})
|
|
// expectInterface[models.ListensImport](t, &funkwhale.FunkwhaleApiBackend{})
|
|
expectInterface[models.LovesExport](t, &funkwhale.FunkwhaleApiBackend{})
|
|
// expectInterface[models.LovesImport](t, &funkwhale.FunkwhaleApiBackend{})
|
|
|
|
// expectInterface[models.ListensExport](t, &jspf.JspfBackend{})
|
|
// expectInterface[models.ListensImport](t, &jspf.JspfBackend{})
|
|
// expectInterface[models.LovesExport](t, &jspf.JspfBackend{})
|
|
expectInterface[models.LovesImport](t, &jspf.JspfBackend{})
|
|
|
|
expectInterface[models.ListensExport](t, &listenbrainz.ListenBrainzApiBackend{})
|
|
// expectInterface[models.ListensImport](t, &listenbrainz.ListenBrainzApiBackend{})
|
|
expectInterface[models.LovesExport](t, &listenbrainz.ListenBrainzApiBackend{})
|
|
expectInterface[models.LovesImport](t, &listenbrainz.ListenBrainzApiBackend{})
|
|
|
|
expectInterface[models.ListensExport](t, &maloja.MalojaApiBackend{})
|
|
expectInterface[models.ListensImport](t, &maloja.MalojaApiBackend{})
|
|
|
|
expectInterface[models.ListensExport](t, &scrobblerlog.ScrobblerLogBackend{})
|
|
expectInterface[models.ListensImport](t, &scrobblerlog.ScrobblerLogBackend{})
|
|
|
|
expectInterface[models.LovesExport](t, &subsonic.SubsonicApiBackend{})
|
|
// expectInterface[models.LovesImport](t, &subsonic.SubsonicApiBackend{})
|
|
}
|
|
|
|
func expectInterface[T interface{}](t *testing.T, backend models.Backend) {
|
|
ok, name := backends.ImplementsInterface[T](&backend)
|
|
if !ok {
|
|
t.Errorf("%v expected to implement %v", reflect.TypeOf(backend).Name(), name)
|
|
}
|
|
}
|