2021-09-08 05:30:15 +00:00
|
|
|
//go:build integration
|
2021-06-21 05:48:28 +00:00
|
|
|
// +build integration
|
|
|
|
|
2021-01-18 01:23:20 +00:00
|
|
|
package sqlite_test
|
|
|
|
|
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
2021-01-18 01:23:20 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stashIDReaderWriter interface {
|
2022-08-12 02:21:46 +00:00
|
|
|
GetStashIDs(ctx context.Context, performerID int) ([]models.StashID, error)
|
|
|
|
UpdateStashIDs(ctx context.Context, performerID int, stashIDs []models.StashID) error
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func testStashIDReaderWriter(ctx context.Context, t *testing.T, r stashIDReaderWriter, id int) {
|
2021-01-18 01:23:20 +00:00
|
|
|
// ensure no stash IDs to begin with
|
2022-05-19 07:49:32 +00:00
|
|
|
testNoStashIDs(ctx, t, r, id)
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
// ensure GetStashIDs with non-existing also returns none
|
2022-05-19 07:49:32 +00:00
|
|
|
testNoStashIDs(ctx, t, r, -1)
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
// add stash ids
|
|
|
|
const stashIDStr = "stashID"
|
|
|
|
const endpoint = "endpoint"
|
2022-08-12 02:21:46 +00:00
|
|
|
stashID := models.StashID{
|
2021-01-18 01:23:20 +00:00
|
|
|
StashID: stashIDStr,
|
|
|
|
Endpoint: endpoint,
|
|
|
|
}
|
|
|
|
|
|
|
|
// update stash ids and ensure was updated
|
2022-08-12 02:21:46 +00:00
|
|
|
if err := r.UpdateStashIDs(ctx, id, []models.StashID{stashID}); err != nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
t.Error(err.Error())
|
|
|
|
}
|
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
testStashIDs(ctx, t, r, id, []models.StashID{stashID})
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
// update non-existing id - should return error
|
2022-08-12 02:21:46 +00:00
|
|
|
if err := r.UpdateStashIDs(ctx, -1, []models.StashID{stashID}); err == nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
t.Error("expected error when updating non-existing id")
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove stash ids and ensure was updated
|
2022-08-12 02:21:46 +00:00
|
|
|
if err := r.UpdateStashIDs(ctx, id, []models.StashID{}); err != nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
t.Error(err.Error())
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
testNoStashIDs(ctx, t, r, id)
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func testNoStashIDs(ctx context.Context, t *testing.T, r stashIDReaderWriter, id int) {
|
2021-01-18 01:23:20 +00:00
|
|
|
t.Helper()
|
2022-05-19 07:49:32 +00:00
|
|
|
stashIDs, err := r.GetStashIDs(ctx, id)
|
2021-01-18 01:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Len(t, stashIDs, 0)
|
|
|
|
}
|
|
|
|
|
2022-08-12 02:21:46 +00:00
|
|
|
func testStashIDs(ctx context.Context, t *testing.T, r stashIDReaderWriter, id int, expected []models.StashID) {
|
2021-01-18 01:23:20 +00:00
|
|
|
t.Helper()
|
2022-05-19 07:49:32 +00:00
|
|
|
stashIDs, err := r.GetStashIDs(ctx, id)
|
2021-01-18 01:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, stashIDs, expected)
|
|
|
|
}
|