Move mock datastore to tests package

This commit is contained in:
Deluan 2020-10-27 11:01:40 -04:00
parent 313a088f86
commit d0bf37a8a9
19 changed files with 41 additions and 79 deletions

View file

@ -49,7 +49,7 @@ func CreateSubsonicAPIRouter() *subsonic.Router {
transcodingCache := core.NewTranscodingCache() transcodingCache := core.NewTranscodingCache()
mediaStreamer := core.NewMediaStreamer(dataStore, transcoderTranscoder, transcodingCache) mediaStreamer := core.NewMediaStreamer(dataStore, transcoderTranscoder, transcodingCache)
archiver := core.NewArchiver(dataStore) archiver := core.NewArchiver(dataStore)
players := engine.NewPlayers(dataStore) players := core.NewPlayers(dataStore)
client := core.LastFMNewClient() client := core.LastFMNewClient()
spotifyClient := core.SpotifyNewClient() spotifyClient := core.SpotifyNewClient()
externalInfo := core.NewExternalInfo(dataStore, client, spotifyClient) externalInfo := core.NewExternalInfo(dataStore, client, spotifyClient)

View file

@ -10,7 +10,7 @@ import (
"github.com/deluan/navidrome/conf" "github.com/deluan/navidrome/conf"
"github.com/deluan/navidrome/log" "github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model" "github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/persistence" "github.com/deluan/navidrome/tests"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
@ -21,13 +21,13 @@ var _ = Describe("Artwork", func() {
ctx := log.NewContext(context.TODO()) ctx := log.NewContext(context.TODO())
BeforeEach(func() { BeforeEach(func() {
ds = &persistence.MockDataStore{MockedTranscoding: &mockTranscodingRepository{}} ds = &tests.MockDataStore{MockedTranscoding: &tests.MockTranscodingRepository{}}
ds.Album(ctx).(*persistence.MockAlbum).SetData(model.Albums{ ds.Album(ctx).(*tests.MockAlbum).SetData(model.Albums{
{ID: "222", CoverArtId: "123", CoverArtPath: "tests/fixtures/test.mp3"}, {ID: "222", CoverArtId: "123", CoverArtPath: "tests/fixtures/test.mp3"},
{ID: "333", CoverArtId: ""}, {ID: "333", CoverArtId: ""},
{ID: "444", CoverArtId: "444", CoverArtPath: "tests/fixtures/cover.jpg"}, {ID: "444", CoverArtId: "444", CoverArtPath: "tests/fixtures/cover.jpg"},
}) })
ds.MediaFile(ctx).(*persistence.MockMediaFile).SetData(model.MediaFiles{ ds.MediaFile(ctx).(*tests.MockMediaFile).SetData(model.MediaFiles{
{ID: "123", AlbumID: "222", Path: "tests/fixtures/test.mp3", HasCoverArt: true}, {ID: "123", AlbumID: "222", Path: "tests/fixtures/test.mp3", HasCoverArt: true},
{ID: "456", AlbumID: "222", Path: "tests/fixtures/test.ogg", HasCoverArt: false}, {ID: "456", AlbumID: "222", Path: "tests/fixtures/test.ogg", HasCoverArt: false},
}) })
@ -132,14 +132,14 @@ var _ = Describe("Artwork", func() {
Context("Errors", func() { Context("Errors", func() {
It("returns err if gets error from album table", func() { It("returns err if gets error from album table", func() {
ds.Album(ctx).(*persistence.MockAlbum).SetError(true) ds.Album(ctx).(*tests.MockAlbum).SetError(true)
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
Expect(artwork.Get(ctx, "al-222", 0, buf)).To(MatchError("Error!")) Expect(artwork.Get(ctx, "al-222", 0, buf)).To(MatchError("Error!"))
}) })
It("returns err if gets error from media_file table", func() { It("returns err if gets error from media_file table", func() {
ds.MediaFile(ctx).(*persistence.MockMediaFile).SetError(true) ds.MediaFile(ctx).(*tests.MockMediaFile).SetError(true)
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
Expect(artwork.Get(ctx, "123", 0, buf)).To(MatchError("Error!")) Expect(artwork.Get(ctx, "123", 0, buf)).To(MatchError("Error!"))

View file

@ -11,7 +11,7 @@ import (
"github.com/deluan/navidrome/log" "github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model" "github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/model/request" "github.com/deluan/navidrome/model/request"
"github.com/deluan/navidrome/persistence" "github.com/deluan/navidrome/tests"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
@ -25,8 +25,8 @@ var _ = Describe("MediaStreamer", func() {
BeforeEach(func() { BeforeEach(func() {
conf.Server.DataFolder, _ = ioutil.TempDir("", "file_caches") conf.Server.DataFolder, _ = ioutil.TempDir("", "file_caches")
conf.Server.TranscodingCacheSize = "100MB" conf.Server.TranscodingCacheSize = "100MB"
ds = &persistence.MockDataStore{MockedTranscoding: &mockTranscodingRepository{}} ds = &tests.MockDataStore{MockedTranscoding: &tests.MockTranscodingRepository{}}
ds.MediaFile(ctx).(*persistence.MockMediaFile).SetData(model.MediaFiles{ ds.MediaFile(ctx).(*tests.MockMediaFile).SetData(model.MediaFiles{
{ID: "123", Path: "tests/fixtures/test.mp3", Suffix: "mp3", BitRate: 128, Duration: 257.0}, {ID: "123", Path: "tests/fixtures/test.mp3", Suffix: "mp3", BitRate: 128, Duration: 257.0},
}) })
testCache := NewTranscodingCache() testCache := NewTranscodingCache()

View file

@ -1,4 +1,4 @@
package engine package core
import ( import (
"context" "context"

View file

@ -1,4 +1,4 @@
package engine package core
import ( import (
"context" "context"
@ -7,7 +7,7 @@ import (
"github.com/deluan/navidrome/log" "github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model" "github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/model/request" "github.com/deluan/navidrome/model/request"
"github.com/deluan/navidrome/persistence" "github.com/deluan/navidrome/tests"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
@ -22,7 +22,7 @@ var _ = Describe("Players", func() {
BeforeEach(func() { BeforeEach(func() {
repo = &mockPlayerRepository{} repo = &mockPlayerRepository{}
ds := &persistence.MockDataStore{MockedPlayer: repo, MockedTranscoding: &mockTranscodingRepository{}} ds := &tests.MockDataStore{MockedPlayer: repo, MockedTranscoding: &tests.MockTranscodingRepository{}}
players = NewPlayers(ds) players = NewPlayers(ds)
beforeRegister = time.Now() beforeRegister = time.Now()
}) })

View file

@ -4,7 +4,7 @@ import (
"context" "context"
"github.com/deluan/navidrome/model" "github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/persistence" "github.com/deluan/navidrome/tests"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
@ -15,7 +15,7 @@ var _ = Describe("playlistSync", func() {
var ps *playlistSync var ps *playlistSync
ctx := context.TODO() ctx := context.TODO()
BeforeEach(func() { BeforeEach(func() {
ds = &persistence.MockDataStore{ ds = &tests.MockDataStore{
MockedMediaFile: &mockedMediaFile{}, MockedMediaFile: &mockedMediaFile{},
} }
ps = newPlaylistSync(ds) ps = newPlaylistSync(ds)

View file

@ -11,7 +11,7 @@ import (
"github.com/deluan/navidrome/conf" "github.com/deluan/navidrome/conf"
"github.com/deluan/navidrome/consts" "github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/model" "github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/persistence" "github.com/deluan/navidrome/tests"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
@ -22,7 +22,7 @@ var _ = Describe("serveIndex", func() {
fs := http.Dir("tests/fixtures") fs := http.Dir("tests/fixtures")
BeforeEach(func() { BeforeEach(func() {
ds = &persistence.MockDataStore{MockedUser: mockUser} ds = &tests.MockDataStore{MockedUser: mockUser}
conf.Server.UILoginBackgroundURL = "" conf.Server.UILoginBackgroundURL = ""
}) })

View file

@ -6,7 +6,7 @@ import (
"github.com/deluan/navidrome/log" "github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model" "github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/persistence" "github.com/deluan/navidrome/tests"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
@ -14,13 +14,13 @@ import (
var _ = Describe("AlbumListController", func() { var _ = Describe("AlbumListController", func() {
var controller *AlbumListController var controller *AlbumListController
var ds model.DataStore var ds model.DataStore
var mockRepo *persistence.MockAlbum var mockRepo *tests.MockAlbum
var w *httptest.ResponseRecorder var w *httptest.ResponseRecorder
ctx := log.NewContext(context.TODO()) ctx := log.NewContext(context.TODO())
BeforeEach(func() { BeforeEach(func() {
ds = &persistence.MockDataStore{} ds = &tests.MockDataStore{}
mockRepo = ds.Album(ctx).(*persistence.MockAlbum) mockRepo = ds.Album(ctx).(*tests.MockAlbum)
controller = NewAlbumListController(ds, nil) controller = NewAlbumListController(ds, nil)
w = httptest.NewRecorder() w = httptest.NewRecorder()
}) })

View file

@ -27,7 +27,7 @@ type Router struct {
Playlists engine.Playlists Playlists engine.Playlists
Streamer core.MediaStreamer Streamer core.MediaStreamer
Archiver core.Archiver Archiver core.Archiver
Players engine.Players Players core.Players
ExternalInfo core.ExternalInfo ExternalInfo core.ExternalInfo
DataStore model.DataStore DataStore model.DataStore
@ -36,7 +36,7 @@ type Router struct {
func New(artwork core.Artwork, func New(artwork core.Artwork,
playlists engine.Playlists, streamer core.MediaStreamer, playlists engine.Playlists, streamer core.MediaStreamer,
archiver core.Archiver, players engine.Players, externalInfo core.ExternalInfo, ds model.DataStore) *Router { archiver core.Archiver, players core.Players, externalInfo core.ExternalInfo, ds model.DataStore) *Router {
r := &Router{Artwork: artwork, Playlists: playlists, r := &Router{Artwork: artwork, Playlists: playlists,
Streamer: streamer, Archiver: archiver, Players: players, ExternalInfo: externalInfo, DataStore: ds} Streamer: streamer, Archiver: archiver, Players: players, ExternalInfo: externalInfo, DataStore: ds}
r.mux = r.routes() r.mux = r.routes()

View file

@ -1,17 +0,0 @@
package engine
import (
"testing"
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/tests"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestEngine(t *testing.T) {
tests.Init(t, false)
log.SetLevel(log.LevelCritical)
RegisterFailHandler(Fail)
RunSpecs(t, "Engine Suite")
}

View file

@ -1,22 +0,0 @@
package engine
import "github.com/deluan/navidrome/model"
type mockTranscodingRepository struct {
model.TranscodingRepository
}
func (m *mockTranscodingRepository) Get(id string) (*model.Transcoding, error) {
return &model.Transcoding{ID: id, TargetFormat: "mp3", DefaultBitRate: 160}, nil
}
func (m *mockTranscodingRepository) FindByFormat(format string) (*model.Transcoding, error) {
switch format {
case "mp3":
return &model.Transcoding{ID: "mp31", TargetFormat: "mp3", DefaultBitRate: 160}, nil
case "oga":
return &model.Transcoding{ID: "oga1", TargetFormat: "oga", DefaultBitRate: 128}, nil
default:
return nil, model.ErrNotFound
}
}

View file

@ -1,10 +1,11 @@
package engine package engine
import ( import (
"github.com/deluan/navidrome/core"
"github.com/google/wire" "github.com/google/wire"
) )
var Set = wire.NewSet( var Set = wire.NewSet(
NewPlaylists, NewPlaylists,
NewPlayers, core.NewPlayers,
) )

View file

@ -10,11 +10,11 @@ import (
"net/url" "net/url"
"strings" "strings"
"github.com/deluan/navidrome/core"
"github.com/deluan/navidrome/core/auth" "github.com/deluan/navidrome/core/auth"
"github.com/deluan/navidrome/log" "github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model" "github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/model/request" "github.com/deluan/navidrome/model/request"
"github.com/deluan/navidrome/server/subsonic/engine"
"github.com/deluan/navidrome/server/subsonic/responses" "github.com/deluan/navidrome/server/subsonic/responses"
"github.com/deluan/navidrome/utils" "github.com/deluan/navidrome/utils"
) )
@ -139,7 +139,7 @@ func validateUser(ctx context.Context, ds model.DataStore, username, pass, token
return user, nil return user, nil
} }
func getPlayer(players engine.Players) func(next http.Handler) http.Handler { func getPlayer(players core.Players) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()

View file

@ -7,12 +7,12 @@ import (
"net/http/httptest" "net/http/httptest"
"strings" "strings"
"github.com/deluan/navidrome/core"
"github.com/deluan/navidrome/core/auth" "github.com/deluan/navidrome/core/auth"
"github.com/deluan/navidrome/log" "github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model" "github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/model/request" "github.com/deluan/navidrome/model/request"
"github.com/deluan/navidrome/persistence" "github.com/deluan/navidrome/tests"
"github.com/deluan/navidrome/server/subsonic/engine"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
@ -117,7 +117,7 @@ var _ = Describe("Middlewares", func() {
Describe("Authenticate", func() { Describe("Authenticate", func() {
var ds model.DataStore var ds model.DataStore
BeforeEach(func() { BeforeEach(func() {
ds = &persistence.MockDataStore{} ds = &tests.MockDataStore{}
}) })
It("passes authentication with correct credentials", func() { It("passes authentication with correct credentials", func() {
@ -222,7 +222,7 @@ var _ = Describe("Middlewares", func() {
Describe("validateUser", func() { Describe("validateUser", func() {
var ds model.DataStore var ds model.DataStore
BeforeEach(func() { BeforeEach(func() {
ds = &persistence.MockDataStore{} ds = &tests.MockDataStore{}
}) })
Context("Plaintext password", func() { Context("Plaintext password", func() {
@ -302,7 +302,7 @@ func (mh *mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
type mockPlayers struct { type mockPlayers struct {
engine.Players core.Players
transcoding *model.Transcoding transcoding *model.Transcoding
} }

View file

@ -1,4 +1,4 @@
package persistence package tests
import ( import (
"errors" "errors"

View file

@ -1,4 +1,4 @@
package persistence package tests
import ( import (
"errors" "errors"

View file

@ -1,4 +1,4 @@
package persistence package tests
import ( import (
"errors" "errors"

View file

@ -1,4 +1,4 @@
package persistence package tests
import ( import (
"context" "context"

View file

@ -1,16 +1,16 @@
package core package tests
import "github.com/deluan/navidrome/model" import "github.com/deluan/navidrome/model"
type mockTranscodingRepository struct { type MockTranscodingRepository struct {
model.TranscodingRepository model.TranscodingRepository
} }
func (m *mockTranscodingRepository) Get(id string) (*model.Transcoding, error) { func (m *MockTranscodingRepository) Get(id string) (*model.Transcoding, error) {
return &model.Transcoding{ID: id, TargetFormat: "mp3", DefaultBitRate: 160}, nil return &model.Transcoding{ID: id, TargetFormat: "mp3", DefaultBitRate: 160}, nil
} }
func (m *mockTranscodingRepository) FindByFormat(format string) (*model.Transcoding, error) { func (m *MockTranscodingRepository) FindByFormat(format string) (*model.Transcoding, error) {
switch format { switch format {
case "mp3": case "mp3":
return &model.Transcoding{ID: "mp31", TargetFormat: "mp3", DefaultBitRate: 160}, nil return &model.Transcoding{ID: "mp31", TargetFormat: "mp3", DefaultBitRate: 160}, nil