mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 04:14:39 +03:00
Fix marker tag filtering (#3846)
This commit is contained in:
@@ -52,7 +52,7 @@ func TestMarkerCountByTagID(t *testing.T) {
|
||||
t.Errorf("error calling CountByTagID: %s", err.Error())
|
||||
}
|
||||
|
||||
assert.Equal(t, 4, markerCount)
|
||||
assert.Equal(t, 6, markerCount)
|
||||
|
||||
markerCount, err = mqb.CountByTagID(ctx, tagIDs[tagIdxWithMarkers])
|
||||
|
||||
@@ -60,7 +60,7 @@ func TestMarkerCountByTagID(t *testing.T) {
|
||||
t.Errorf("error calling CountByTagID: %s", err.Error())
|
||||
}
|
||||
|
||||
assert.Equal(t, 1, markerCount)
|
||||
assert.Equal(t, 2, markerCount)
|
||||
|
||||
markerCount, err = mqb.CountByTagID(ctx, 0)
|
||||
|
||||
@@ -89,6 +89,40 @@ func TestMarkerQuerySortBySceneUpdated(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func verifyIDs(t *testing.T, modifier models.CriterionModifier, values []int, results []int) {
|
||||
t.Helper()
|
||||
switch modifier {
|
||||
case models.CriterionModifierIsNull:
|
||||
assert.Len(t, results, 0)
|
||||
case models.CriterionModifierNotNull:
|
||||
assert.NotEqual(t, 0, len(results))
|
||||
case models.CriterionModifierIncludes:
|
||||
for _, v := range values {
|
||||
assert.Contains(t, results, v)
|
||||
}
|
||||
case models.CriterionModifierExcludes:
|
||||
for _, v := range values {
|
||||
assert.NotContains(t, results, v)
|
||||
}
|
||||
case models.CriterionModifierEquals:
|
||||
for _, v := range values {
|
||||
assert.Contains(t, results, v)
|
||||
}
|
||||
assert.Len(t, results, len(values))
|
||||
case models.CriterionModifierNotEquals:
|
||||
foundAll := true
|
||||
for _, v := range values {
|
||||
if !intslice.IntInclude(results, v) {
|
||||
foundAll = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if foundAll && len(results) == len(values) {
|
||||
t.Errorf("expected ids not equal to %v - found %v", values, results)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkerQueryTags(t *testing.T) {
|
||||
type test struct {
|
||||
name string
|
||||
@@ -97,17 +131,19 @@ func TestMarkerQueryTags(t *testing.T) {
|
||||
}
|
||||
|
||||
withTxn(func(ctx context.Context) error {
|
||||
testTags := func(m *models.SceneMarker, markerFilter *models.SceneMarkerFilterType) {
|
||||
testTags := func(t *testing.T, m *models.SceneMarker, markerFilter *models.SceneMarkerFilterType) {
|
||||
tagIDs, err := db.SceneMarker.GetTagIDs(ctx, m.ID)
|
||||
if err != nil {
|
||||
t.Errorf("error getting marker tag ids: %v", err)
|
||||
}
|
||||
if markerFilter.Tags.Modifier == models.CriterionModifierIsNull && len(tagIDs) > 0 {
|
||||
t.Errorf("expected marker %d to have no tags - found %d", m.ID, len(tagIDs))
|
||||
}
|
||||
if markerFilter.Tags.Modifier == models.CriterionModifierNotNull && len(tagIDs) == 0 {
|
||||
t.Errorf("expected marker %d to have tags - found 0", m.ID)
|
||||
|
||||
// HACK - if modifier isn't null/not null, then add the primary tag id
|
||||
if markerFilter.Tags.Modifier != models.CriterionModifierIsNull && markerFilter.Tags.Modifier != models.CriterionModifierNotNull {
|
||||
tagIDs = append(tagIDs, m.PrimaryTagID)
|
||||
}
|
||||
|
||||
values, _ := stringslice.StringSliceToIntSlice(markerFilter.Tags.Value)
|
||||
verifyIDs(t, markerFilter.Tags.Modifier, values, tagIDs)
|
||||
}
|
||||
|
||||
cases := []test{
|
||||
@@ -129,6 +165,71 @@ func TestMarkerQueryTags(t *testing.T) {
|
||||
},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"includes",
|
||||
&models.SceneMarkerFilterType{
|
||||
Tags: &models.HierarchicalMultiCriterionInput{
|
||||
Modifier: models.CriterionModifierIncludes,
|
||||
Value: []string{
|
||||
strconv.Itoa(tagIDs[tagIdxWithMarkers]),
|
||||
},
|
||||
},
|
||||
},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"includes all",
|
||||
&models.SceneMarkerFilterType{
|
||||
Tags: &models.HierarchicalMultiCriterionInput{
|
||||
Modifier: models.CriterionModifierIncludesAll,
|
||||
Value: []string{
|
||||
strconv.Itoa(tagIDs[tagIdxWithMarkers]),
|
||||
strconv.Itoa(tagIDs[tagIdx2WithMarkers]),
|
||||
},
|
||||
},
|
||||
},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"equals",
|
||||
&models.SceneMarkerFilterType{
|
||||
Tags: &models.HierarchicalMultiCriterionInput{
|
||||
Modifier: models.CriterionModifierEquals,
|
||||
Value: []string{
|
||||
strconv.Itoa(tagIDs[tagIdxWithPrimaryMarkers]),
|
||||
strconv.Itoa(tagIDs[tagIdxWithMarkers]),
|
||||
strconv.Itoa(tagIDs[tagIdx2WithMarkers]),
|
||||
},
|
||||
},
|
||||
},
|
||||
nil,
|
||||
},
|
||||
// not equals not supported
|
||||
// {
|
||||
// "not equals",
|
||||
// &models.SceneMarkerFilterType{
|
||||
// Tags: &models.HierarchicalMultiCriterionInput{
|
||||
// Modifier: models.CriterionModifierNotEquals,
|
||||
// Value: []string{
|
||||
// strconv.Itoa(tagIDs[tagIdx2WithScene]),
|
||||
// strconv.Itoa(tagIDs[tagIdx3WithScene]),
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// nil,
|
||||
// },
|
||||
{
|
||||
"excludes",
|
||||
&models.SceneMarkerFilterType{
|
||||
Tags: &models.HierarchicalMultiCriterionInput{
|
||||
Modifier: models.CriterionModifierIncludes,
|
||||
Value: []string{
|
||||
strconv.Itoa(tagIDs[tagIdx2WithMarkers]),
|
||||
},
|
||||
},
|
||||
},
|
||||
nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
@@ -136,7 +237,7 @@ func TestMarkerQueryTags(t *testing.T) {
|
||||
markers := queryMarkers(ctx, t, db.SceneMarker, tc.markerFilter, tc.findFilter)
|
||||
assert.Greater(t, len(markers), 0)
|
||||
for _, m := range markers {
|
||||
testTags(m, tc.markerFilter)
|
||||
testTags(t, m, tc.markerFilter)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -167,40 +268,7 @@ func TestMarkerQuerySceneTags(t *testing.T) {
|
||||
|
||||
tagIDs := s.TagIDs.List()
|
||||
values, _ := stringslice.StringSliceToIntSlice(markerFilter.SceneTags.Value)
|
||||
switch markerFilter.SceneTags.Modifier {
|
||||
case models.CriterionModifierIsNull:
|
||||
if len(tagIDs) > 0 {
|
||||
t.Errorf("expected marker %d to have no scene tags - found %d", m.ID, len(tagIDs))
|
||||
}
|
||||
case models.CriterionModifierNotNull:
|
||||
if len(tagIDs) == 0 {
|
||||
t.Errorf("expected marker %d to have scene tags - found 0", m.ID)
|
||||
}
|
||||
case models.CriterionModifierIncludes:
|
||||
for _, v := range values {
|
||||
assert.Contains(t, tagIDs, v)
|
||||
}
|
||||
case models.CriterionModifierExcludes:
|
||||
for _, v := range values {
|
||||
assert.NotContains(t, tagIDs, v)
|
||||
}
|
||||
case models.CriterionModifierEquals:
|
||||
for _, v := range values {
|
||||
assert.Contains(t, tagIDs, v)
|
||||
}
|
||||
assert.Len(t, tagIDs, len(values))
|
||||
case models.CriterionModifierNotEquals:
|
||||
foundAll := true
|
||||
for _, v := range values {
|
||||
if !intslice.IntInclude(tagIDs, v) {
|
||||
foundAll = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if foundAll && len(tagIDs) == len(values) {
|
||||
t.Errorf("expected marker %d to have scene tags not equal to %v - found %v", m.ID, values, tagIDs)
|
||||
}
|
||||
}
|
||||
verifyIDs(t, markerFilter.SceneTags.Modifier, values, tagIDs)
|
||||
}
|
||||
|
||||
cases := []test{
|
||||
|
||||
Reference in New Issue
Block a user