Parent studios (#595)

* Refactor getMultiCriterionClause
Co-authored-by: Anon247 <61889302+Anon247@users.noreply.github.com>
This commit is contained in:
WithoutPants
2020-06-15 21:34:39 +10:00
committed by GitHub
parent a77fea5724
commit 96e6e16507
37 changed files with 818 additions and 146 deletions

View File

@@ -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