scotty/backends/backends_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

109 lines
4.1 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 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)
}
}