mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 20:34:37 +03:00
Parent studios (#595)
* Refactor getMultiCriterionClause Co-authored-by: Anon247 <61889302+Anon247@users.noreply.github.com>
This commit is contained in:
@@ -3,9 +3,13 @@
|
||||
package models_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stashapp/stash/pkg/database"
|
||||
"github.com/stashapp/stash/pkg/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -39,6 +43,173 @@ func TestStudioFindByName(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestStudioQueryParent(t *testing.T) {
|
||||
sqb := models.NewStudioQueryBuilder()
|
||||
studioCriterion := models.MultiCriterionInput{
|
||||
Value: []string{
|
||||
strconv.Itoa(studioIDs[studioIdxWithChildStudio]),
|
||||
},
|
||||
Modifier: models.CriterionModifierIncludes,
|
||||
}
|
||||
|
||||
studioFilter := models.StudioFilterType{
|
||||
Parents: &studioCriterion,
|
||||
}
|
||||
|
||||
studios, _ := sqb.Query(&studioFilter, nil)
|
||||
|
||||
assert.Len(t, studios, 1)
|
||||
|
||||
// ensure id is correct
|
||||
assert.Equal(t, sceneIDs[studioIdxWithParentStudio], studios[0].ID)
|
||||
|
||||
studioCriterion = models.MultiCriterionInput{
|
||||
Value: []string{
|
||||
strconv.Itoa(studioIDs[studioIdxWithChildStudio]),
|
||||
},
|
||||
Modifier: models.CriterionModifierExcludes,
|
||||
}
|
||||
|
||||
q := getStudioStringValue(studioIdxWithParentStudio, titleField)
|
||||
findFilter := models.FindFilterType{
|
||||
Q: &q,
|
||||
}
|
||||
|
||||
studios, _ = sqb.Query(&studioFilter, &findFilter)
|
||||
assert.Len(t, studios, 0)
|
||||
}
|
||||
|
||||
func TestStudioDestroyParent(t *testing.T) {
|
||||
const parentName = "parent"
|
||||
const childName = "child"
|
||||
|
||||
// create parent and child studios
|
||||
ctx := context.TODO()
|
||||
tx := database.DB.MustBeginTx(ctx, nil)
|
||||
|
||||
createdParent, err := createStudio(tx, parentName, nil)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
t.Fatalf("Error creating parent studio: %s", err.Error())
|
||||
}
|
||||
|
||||
parentID := int64(createdParent.ID)
|
||||
createdChild, err := createStudio(tx, childName, &parentID)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
t.Fatalf("Error creating child studio: %s", err.Error())
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
tx.Rollback()
|
||||
t.Fatalf("Error committing: %s", err.Error())
|
||||
}
|
||||
|
||||
sqb := models.NewStudioQueryBuilder()
|
||||
|
||||
// destroy the parent
|
||||
tx = database.DB.MustBeginTx(ctx, nil)
|
||||
|
||||
err = sqb.Destroy(strconv.Itoa(createdParent.ID), tx)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
t.Fatalf("Error destroying parent studio: %s", err.Error())
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
tx.Rollback()
|
||||
t.Fatalf("Error committing: %s", err.Error())
|
||||
}
|
||||
|
||||
// destroy the child
|
||||
tx = database.DB.MustBeginTx(ctx, nil)
|
||||
|
||||
err = sqb.Destroy(strconv.Itoa(createdChild.ID), tx)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
t.Fatalf("Error destroying child studio: %s", err.Error())
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
tx.Rollback()
|
||||
t.Fatalf("Error committing: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestStudioFindChildren(t *testing.T) {
|
||||
sqb := models.NewStudioQueryBuilder()
|
||||
|
||||
studios, err := sqb.FindChildren(studioIDs[studioIdxWithChildStudio], nil)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("error calling FindChildren: %s", err.Error())
|
||||
}
|
||||
|
||||
assert.Len(t, studios, 1)
|
||||
assert.Equal(t, studioIDs[studioIdxWithParentStudio], studios[0].ID)
|
||||
|
||||
studios, err = sqb.FindChildren(0, nil)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("error calling FindChildren: %s", err.Error())
|
||||
}
|
||||
|
||||
assert.Len(t, studios, 0)
|
||||
}
|
||||
|
||||
func TestStudioUpdateClearParent(t *testing.T) {
|
||||
const parentName = "clearParent_parent"
|
||||
const childName = "clearParent_child"
|
||||
|
||||
// create parent and child studios
|
||||
ctx := context.TODO()
|
||||
tx := database.DB.MustBeginTx(ctx, nil)
|
||||
|
||||
createdParent, err := createStudio(tx, parentName, nil)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
t.Fatalf("Error creating parent studio: %s", err.Error())
|
||||
}
|
||||
|
||||
parentID := int64(createdParent.ID)
|
||||
createdChild, err := createStudio(tx, childName, &parentID)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
t.Fatalf("Error creating child studio: %s", err.Error())
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
tx.Rollback()
|
||||
t.Fatalf("Error committing: %s", err.Error())
|
||||
}
|
||||
|
||||
sqb := models.NewStudioQueryBuilder()
|
||||
|
||||
// clear the parent id from the child
|
||||
tx = database.DB.MustBeginTx(ctx, nil)
|
||||
|
||||
updatePartial := models.StudioPartial{
|
||||
ID: createdChild.ID,
|
||||
ParentID: &sql.NullInt64{Valid: false},
|
||||
}
|
||||
|
||||
updatedStudio, err := sqb.Update(updatePartial, tx)
|
||||
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
t.Fatalf("Error updated studio: %s", err.Error())
|
||||
}
|
||||
|
||||
if updatedStudio.ParentID.Valid {
|
||||
t.Error("updated studio has parent ID set")
|
||||
}
|
||||
|
||||
if err := tx.Commit(); err != nil {
|
||||
tx.Rollback()
|
||||
t.Fatalf("Error committing: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Create
|
||||
// TODO Update
|
||||
// TODO Destroy
|
||||
|
||||
Reference in New Issue
Block a user