autobrr/internal/database/action_test.go
kenstir 4009554d10
feat(filters): skip duplicates (#1711)
* feat(filters): skip duplicates

* fix: add interface instead of any

* fix(filters): tonullint

* feat(filters): skip dupes check month day

* chore: cleanup

* feat(db): set autoincrement id

* feat(filters): add repack and proper to dupe profile

* feat(filters): add default dupe profiles

* feat(duplicates): check audio and website

* feat(duplicates): update tests

* feat(duplicates): add toggles on addform

* feat(duplicates): fix sqlite upgrade path and initialize duplicate profiles

* feat(duplicates): simplify sqlite upgrade

avoiding temp table and unwieldy select.  Besides, FK constraints
are turned off anyway in #229.

* feat(duplicates): change CheckIsDuplicateRelease treatment of PROPER and REPACK

"Proper" and "Repack" are not parallel to the other conditions like "Title",
so they do not belong as dedup conditions.  "PROPER" means there was an issue in
the previous release, and so a PROPER is never a duplicate, even if it replaces
another PROPER.  Similarly, "REPACK" means there was an issue in the previous
release by that group, and so it is a duplicate only if we previously took a
release from a DIFFERENT group.

I have not removed Proper and Repack from the UI or the schema yet.

* feat(duplicates): update postgres schema to match sqlite

* feat(duplicates): fix web build errors

* feat(duplicates): fix postgres errors

* feat(filters): do leftjoin for duplicate profile

* fix(filters): partial update dupe profile

* go fmt `internal/domain/filter.go`

* feat(duplicates): restore straightforward logic for proper/repack

* feat(duplicates): remove mostly duplicate TV duplicate profiles

Having one profile seems the cleanest.  If somebody wants multiple
resolutions then they can add Resolution to the duplicate profile.
Tested this profile with both weekly episodic releases and daily
show releases.

* feat(release): add db indexes and sub_title

* feat(release): add IsDuplicate tests

* feat(release): update action handler

* feat(release): add more tests for skip duplicates

* feat(duplicates): check audio

* feat(duplicates): add more tests

* feat(duplicates): match edition cut and more

* fix(duplicates): tests

* fix(duplicates): missing imports

* fix(duplicates): tests

* feat(duplicates): handle sub_title edition and language in ui

* fix(duplicates): tests

* feat(duplicates): check name against normalized hash

* fix(duplicates): tests

* chore: update .gitignore to ignore .pnpm-store

* fix: tests

* fix(filters): tests

* fix: bad conflict merge

* fix: update release type in test

* fix: use vendored hot-toast

* fix: release_test.go

* fix: rss_test.go

* feat(duplicates): improve title hashing for unique check

* feat(duplicates): further improve title hashing for unique check with lang

* feat(duplicates): fix tests

* feat(duplicates): add macros IsDuplicate and DuplicateProfile ID and name

* feat(duplicates): add normalized hash match option

* fix: headlessui-state prop warning

* fix(duplicates): add missing year in daily ep normalize

* fix(duplicates): check rejections len

---------

Co-authored-by: ze0s <ze0s@riseup.net>
2024-12-25 22:33:46 +01:00

535 lines
18 KiB
Go

// Copyright (c) 2021 - 2024, Ludvig Lundgren and the autobrr contributors.
// SPDX-License-Identifier: GPL-2.0-or-later
//go:build integration
package database
import (
"context"
"fmt"
"testing"
"time"
"github.com/autobrr/autobrr/internal/domain"
"github.com/stretchr/testify/assert"
)
func getMockAction() *domain.Action {
return &domain.Action{
Name: "randomAction",
Type: domain.ActionTypeTest,
Enabled: true,
ExecCmd: "/home/user/Downloads/test.sh",
ExecArgs: "WGET_URL",
WatchFolder: "/home/user/Downloads",
Category: "HD, 720p",
Tags: "P2P, x264",
Label: "testLabel",
SavePath: "/home/user/Downloads",
Paused: false,
IgnoreRules: false,
SkipHashCheck: false,
FirstLastPiecePrio: false,
ContentLayout: domain.ActionContentLayoutOriginal,
LimitUploadSpeed: 0,
LimitDownloadSpeed: 0,
LimitRatio: 0,
LimitSeedTime: 0,
ReAnnounceSkip: false,
ReAnnounceDelete: false,
ReAnnounceInterval: 0,
ReAnnounceMaxAttempts: 0,
WebhookHost: "http://localhost:8080",
WebhookType: "test",
WebhookMethod: "POST",
WebhookData: "testData",
WebhookHeaders: []string{"testHeader"},
ExternalDownloadClientID: 21,
FilterID: 1,
ClientID: 1,
}
}
func TestActionRepo_Store(t *testing.T) {
for dbType, db := range testDBs {
log := setupLoggerForTest()
downloadClientRepo := NewDownloadClientRepo(log, db)
filterRepo := NewFilterRepo(log, db)
repo := NewActionRepo(log, db, downloadClientRepo)
mockData := getMockAction()
t.Run(fmt.Sprintf("Store_Succeeds [%s]", dbType), func(t *testing.T) {
// Setup
mock := getMockDownloadClient()
err := downloadClientRepo.Store(context.Background(), &mock)
assert.NoError(t, err)
assert.NotNil(t, mock)
err = filterRepo.Store(context.Background(), getMockFilter())
assert.NoError(t, err)
createdFilters, err := filterRepo.ListFilters(context.Background())
assert.NoError(t, err)
assert.NotNil(t, createdFilters)
mockData.ClientID = mock.ID
mockData.FilterID = createdFilters[0].ID
// Actual test for Store
err = repo.Store(context.Background(), mockData)
assert.NoError(t, err)
assert.NotNil(t, mockData)
// Cleanup
_ = repo.Delete(context.Background(), &domain.DeleteActionRequest{ActionId: mockData.ID})
_ = filterRepo.Delete(context.Background(), createdFilters[0].ID)
_ = downloadClientRepo.Delete(context.Background(), mock.ID)
})
t.Run(fmt.Sprintf("Store_Succeeds_With_Missing_or_empty_fields [%s]", dbType), func(t *testing.T) {
mockData := &domain.Action{}
err := repo.Store(context.Background(), mockData)
assert.NoError(t, err)
// Cleanup
_ = repo.Delete(context.Background(), &domain.DeleteActionRequest{ActionId: mockData.ID})
})
t.Run(fmt.Sprintf("Store_Fails_With_Invalid_ClientID [%s]", dbType), func(t *testing.T) {
mockData := getMockAction()
mockData.ClientID = 9999
err := repo.Store(context.Background(), mockData)
assert.Error(t, err)
})
t.Run(fmt.Sprintf("Store_Fails_With_Context_Timeout [%s]", dbType), func(t *testing.T) {
mockData := getMockAction()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond)
defer cancel()
err := repo.Store(ctx, mockData)
assert.Error(t, err)
})
}
}
func TestActionRepo_StoreFilterActions(t *testing.T) {
for dbType, db := range testDBs {
log := setupLoggerForTest()
downloadClientRepo := NewDownloadClientRepo(log, db)
filterRepo := NewFilterRepo(log, db)
repo := NewActionRepo(log, db, downloadClientRepo)
mockData := getMockAction()
t.Run(fmt.Sprintf("StoreFilterActions_Succeeds [%s]", dbType), func(t *testing.T) {
// Setup
mock := getMockDownloadClient()
err := downloadClientRepo.Store(context.Background(), &mock)
assert.NoError(t, err)
assert.NotNil(t, mock)
err = filterRepo.Store(context.Background(), getMockFilter())
assert.NoError(t, err)
createdFilters, err := filterRepo.ListFilters(context.Background())
assert.NoError(t, err)
assert.NotNil(t, createdFilters)
mockData.ClientID = mock.ID
mockData.FilterID = createdFilters[0].ID
// Actual test for StoreFilterActions
createdActions, err := repo.StoreFilterActions(context.Background(), int64(createdFilters[0].ID), []*domain.Action{mockData})
assert.NoError(t, err)
assert.NotNil(t, createdActions)
// Cleanup
_ = repo.Delete(context.Background(), &domain.DeleteActionRequest{ActionId: createdActions[0].ID})
_ = filterRepo.Delete(context.Background(), createdFilters[0].ID)
_ = downloadClientRepo.Delete(context.Background(), mock.ID)
})
t.Run(fmt.Sprintf("StoreFilterActions_Fails_Invalid_FilterID [%s]", dbType), func(t *testing.T) {
_, err := repo.StoreFilterActions(context.Background(), 9999, []*domain.Action{mockData})
assert.NoError(t, err)
})
t.Run(fmt.Sprintf("StoreFilterActions_Fails_Empty_Actions_Array [%s]", dbType), func(t *testing.T) {
// Setup
err := filterRepo.Store(context.Background(), getMockFilter())
assert.NoError(t, err)
createdFilters, err := filterRepo.ListFilters(context.Background())
assert.NoError(t, err)
assert.NotNil(t, createdFilters)
_, err = repo.StoreFilterActions(context.Background(), int64(createdFilters[0].ID), []*domain.Action{})
assert.NoError(t, err)
// Cleanup
_ = filterRepo.Delete(context.Background(), createdFilters[0].ID)
_ = filterRepo.Delete(context.Background(), createdFilters[0].ID)
})
t.Run(fmt.Sprintf("StoreFilterActions_Fails_Context_Timeout [%s]", dbType), func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond)
defer cancel()
err := filterRepo.Store(context.Background(), getMockFilter())
assert.NoError(t, err)
createdFilters, err := filterRepo.ListFilters(context.Background())
assert.NoError(t, err)
assert.NotNil(t, createdFilters)
_, err = repo.StoreFilterActions(ctx, int64(createdFilters[0].ID), []*domain.Action{mockData})
assert.Error(t, err)
// Cleanup
_ = filterRepo.Delete(context.Background(), createdFilters[0].ID)
})
}
}
func TestActionRepo_FindByFilterID(t *testing.T) {
for dbType, db := range testDBs {
log := setupLoggerForTest()
downloadClientRepo := NewDownloadClientRepo(log, db)
filterRepo := NewFilterRepo(log, db)
repo := NewActionRepo(log, db, downloadClientRepo)
mockData := getMockAction()
t.Run(fmt.Sprintf("FindByFilterID_Succeeds [%s]", dbType), func(t *testing.T) {
// Setup
mock := getMockDownloadClient()
err := downloadClientRepo.Store(context.Background(), &mock)
assert.NoError(t, err)
assert.NotNil(t, mock)
err = filterRepo.Store(context.Background(), getMockFilter())
assert.NoError(t, err)
createdFilters, err := filterRepo.ListFilters(context.Background())
assert.NoError(t, err)
assert.NotNil(t, createdFilters)
mockData.ClientID = mock.ID
mockData.FilterID = createdFilters[0].ID
createdActions, err := repo.StoreFilterActions(context.Background(), int64(createdFilters[0].ID), []*domain.Action{mockData})
assert.NoError(t, err)
// Actual test for FindByFilterID
actions, err := repo.FindByFilterID(context.Background(), createdFilters[0].ID, nil, false)
assert.NoError(t, err)
assert.NotNil(t, actions)
assert.Equal(t, 1, len(actions))
// Cleanup
_ = repo.Delete(context.Background(), &domain.DeleteActionRequest{ActionId: createdActions[0].ID})
_ = filterRepo.Delete(context.Background(), createdFilters[0].ID)
_ = downloadClientRepo.Delete(context.Background(), mock.ID)
})
t.Run(fmt.Sprintf("FindByFilterID_Fails_No_Actions [%s]", dbType), func(t *testing.T) {
// Setup
err := filterRepo.Store(context.Background(), getMockFilter())
assert.NoError(t, err)
createdFilters, err := filterRepo.ListFilters(context.Background())
assert.NoError(t, err)
assert.NotNil(t, createdFilters)
// Actual test for FindByFilterID
actions, err := repo.FindByFilterID(context.Background(), createdFilters[0].ID, nil, false)
assert.NoError(t, err)
assert.Equal(t, 0, len(actions))
// Cleanup
_ = filterRepo.Delete(context.Background(), createdFilters[0].ID)
})
t.Run(fmt.Sprintf("FindByFilterID_Succeeds_With_Invalid_FilterID [%s]", dbType), func(t *testing.T) {
actions, err := repo.FindByFilterID(context.Background(), 9999, nil, false) // 9999 is an invalid filter ID
assert.NoError(t, err)
assert.NotNil(t, actions)
assert.Equal(t, 0, len(actions))
})
t.Run(fmt.Sprintf("FindByFilterID_Fails_Context_Timeout [%s]", dbType), func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond)
defer cancel()
actions, err := repo.FindByFilterID(ctx, 1, nil, false)
assert.Error(t, err)
assert.Nil(t, actions)
})
}
}
func TestActionRepo_List(t *testing.T) {
for dbType, db := range testDBs {
log := setupLoggerForTest()
downloadClientRepo := NewDownloadClientRepo(log, db)
filterRepo := NewFilterRepo(log, db)
repo := NewActionRepo(log, db, downloadClientRepo)
mockData := getMockAction()
t.Run(fmt.Sprintf("List_Succeeds [%s]", dbType), func(t *testing.T) {
// Setup
mock := getMockDownloadClient()
err := downloadClientRepo.Store(context.Background(), &mock)
assert.NoError(t, err)
assert.NotNil(t, mock)
err = filterRepo.Store(context.Background(), getMockFilter())
assert.NoError(t, err)
createdFilters, err := filterRepo.ListFilters(context.Background())
assert.NoError(t, err)
assert.NotNil(t, createdFilters)
mockData.ClientID = mock.ID
mockData.FilterID = createdFilters[0].ID
createdActions, err := repo.StoreFilterActions(context.Background(), int64(createdFilters[0].ID), []*domain.Action{mockData})
assert.NoError(t, err)
// Actual test for List
actions, err := repo.List(context.Background())
assert.NoError(t, err)
assert.NotNil(t, actions)
assert.GreaterOrEqual(t, len(actions), 1)
// Cleanup
_ = repo.Delete(context.Background(), &domain.DeleteActionRequest{ActionId: createdActions[0].ID})
_ = filterRepo.Delete(context.Background(), createdFilters[0].ID)
_ = downloadClientRepo.Delete(context.Background(), mock.ID)
})
t.Run(fmt.Sprintf("List_Fails_Context_Timeout [%s]", dbType), func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond)
defer cancel()
actions, err := repo.List(ctx)
assert.Error(t, err)
assert.Nil(t, actions)
})
}
}
func TestActionRepo_Get(t *testing.T) {
for dbType, db := range testDBs {
log := setupLoggerForTest()
downloadClientRepo := NewDownloadClientRepo(log, db)
filterRepo := NewFilterRepo(log, db)
repo := NewActionRepo(log, db, downloadClientRepo)
mockData := getMockAction()
t.Run(fmt.Sprintf("Get_Succeeds [%s]", dbType), func(t *testing.T) {
// Setup
mock := getMockDownloadClient()
err := downloadClientRepo.Store(context.Background(), &mock)
assert.NoError(t, err)
assert.NotNil(t, mock)
err = filterRepo.Store(context.Background(), getMockFilter())
assert.NoError(t, err)
createdFilters, err := filterRepo.ListFilters(context.Background())
assert.NoError(t, err)
assert.NotNil(t, createdFilters)
mockData.ClientID = mock.ID
mockData.FilterID = createdFilters[0].ID
createdActions, err := repo.StoreFilterActions(context.Background(), int64(createdFilters[0].ID), []*domain.Action{mockData})
assert.NoError(t, err)
// Actual test for Get
action, err := repo.Get(context.Background(), &domain.GetActionRequest{Id: createdActions[0].ID})
assert.NoError(t, err)
assert.NotNil(t, action)
assert.Equal(t, createdActions[0].ID, action.ID)
// Cleanup
_ = repo.Delete(context.Background(), &domain.DeleteActionRequest{ActionId: createdActions[0].ID})
_ = filterRepo.Delete(context.Background(), createdFilters[0].ID)
_ = downloadClientRepo.Delete(context.Background(), mock.ID)
})
t.Run(fmt.Sprintf("Get_Fails_No_Record [%s]", dbType), func(t *testing.T) {
action, err := repo.Get(context.Background(), &domain.GetActionRequest{Id: 9999})
assert.Error(t, err)
assert.Equal(t, domain.ErrRecordNotFound, err)
assert.Nil(t, action)
})
t.Run(fmt.Sprintf("Get_Fails_Context_Timeout [%s]", dbType), func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond)
defer cancel()
action, err := repo.Get(ctx, &domain.GetActionRequest{Id: 1})
assert.Error(t, err)
assert.Nil(t, action)
})
}
}
func TestActionRepo_Delete(t *testing.T) {
for dbType, db := range testDBs {
log := setupLoggerForTest()
downloadClientRepo := NewDownloadClientRepo(log, db)
filterRepo := NewFilterRepo(log, db)
repo := NewActionRepo(log, db, downloadClientRepo)
mockData := getMockAction()
t.Run(fmt.Sprintf("Delete_Succeeds [%s]", dbType), func(t *testing.T) {
// Setup
mock := getMockDownloadClient()
err := downloadClientRepo.Store(context.Background(), &mock)
assert.NoError(t, err)
assert.NotNil(t, mock)
err = filterRepo.Store(context.Background(), getMockFilter())
assert.NoError(t, err)
createdFilters, err := filterRepo.ListFilters(context.Background())
assert.NoError(t, err)
assert.NotNil(t, createdFilters)
mockData.ClientID = mock.ID
mockData.FilterID = createdFilters[0].ID
createdActions, err := repo.StoreFilterActions(context.Background(), int64(createdFilters[0].ID), []*domain.Action{mockData})
assert.NoError(t, err)
// Actual test for Delete
err = repo.Delete(context.Background(), &domain.DeleteActionRequest{ActionId: createdActions[0].ID})
assert.NoError(t, err)
// Verify that the record was actually deleted
action, err := repo.Get(context.Background(), &domain.GetActionRequest{Id: createdActions[0].ID})
assert.Error(t, err)
assert.Equal(t, domain.ErrRecordNotFound, err)
assert.Nil(t, action)
// Cleanup
_ = repo.Delete(context.Background(), &domain.DeleteActionRequest{ActionId: createdActions[0].ID})
_ = filterRepo.Delete(context.Background(), createdFilters[0].ID)
_ = downloadClientRepo.Delete(context.Background(), mock.ID)
})
t.Run(fmt.Sprintf("Delete_Fails_Context_Timeout [%s]", dbType), func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond)
defer cancel()
err := repo.Delete(ctx, &domain.DeleteActionRequest{ActionId: 1})
assert.Error(t, err)
})
}
}
func TestActionRepo_DeleteByFilterID(t *testing.T) {
for dbType, db := range testDBs {
log := setupLoggerForTest()
downloadClientRepo := NewDownloadClientRepo(log, db)
filterRepo := NewFilterRepo(log, db)
repo := NewActionRepo(log, db, downloadClientRepo)
mockData := getMockAction()
t.Run(fmt.Sprintf("DeleteByFilterID_Succeeds [%s]", dbType), func(t *testing.T) {
// Setup
mock := getMockDownloadClient()
err := downloadClientRepo.Store(context.Background(), &mock)
assert.NoError(t, err)
assert.NotNil(t, mock)
err = filterRepo.Store(context.Background(), getMockFilter())
assert.NoError(t, err)
createdFilters, err := filterRepo.ListFilters(context.Background())
assert.NoError(t, err)
assert.NotNil(t, createdFilters)
mockData.ClientID = mock.ID
mockData.FilterID = createdFilters[0].ID
createdActions, err := repo.StoreFilterActions(context.Background(), int64(createdFilters[0].ID), []*domain.Action{mockData})
assert.NoError(t, err)
err = repo.DeleteByFilterID(context.Background(), mockData.FilterID)
assert.NoError(t, err)
// Verify that actions with the given filterID are actually deleted
action, err := repo.Get(context.Background(), &domain.GetActionRequest{Id: createdActions[0].ID})
assert.Error(t, err)
assert.Equal(t, domain.ErrRecordNotFound, err)
assert.Nil(t, action)
// Cleanup
_ = repo.Delete(context.Background(), &domain.DeleteActionRequest{ActionId: createdActions[0].ID})
_ = filterRepo.Delete(context.Background(), createdFilters[0].ID)
_ = downloadClientRepo.Delete(context.Background(), mock.ID)
})
t.Run(fmt.Sprintf("DeleteByFilterID_Fails_Context_Timeout [%s]", dbType), func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond)
defer cancel()
err := repo.DeleteByFilterID(ctx, mockData.FilterID)
assert.Error(t, err)
})
}
}
func TestActionRepo_ToggleEnabled(t *testing.T) {
for dbType, db := range testDBs {
log := setupLoggerForTest()
downloadClientRepo := NewDownloadClientRepo(log, db)
filterRepo := NewFilterRepo(log, db)
repo := NewActionRepo(log, db, downloadClientRepo)
mockData := getMockAction()
t.Run(fmt.Sprintf("ToggleEnabled_Succeeds [%s]", dbType), func(t *testing.T) {
// Setup
mock := getMockDownloadClient()
err := downloadClientRepo.Store(context.Background(), &mock)
assert.NoError(t, err)
assert.NotNil(t, mock)
err = filterRepo.Store(context.Background(), getMockFilter())
assert.NoError(t, err)
createdFilters, err := filterRepo.ListFilters(context.Background())
assert.NoError(t, err)
assert.NotNil(t, createdFilters)
mockData.ClientID = mock.ID
mockData.FilterID = createdFilters[0].ID
mockData.Enabled = false
createdActions, err := repo.StoreFilterActions(context.Background(), int64(createdFilters[0].ID), []*domain.Action{mockData})
assert.NoError(t, err)
// Actual test for ToggleEnabled
err = repo.ToggleEnabled(createdActions[0].ID)
assert.NoError(t, err)
// Verify that the record was actually updated
action, err := repo.Get(context.Background(), &domain.GetActionRequest{Id: createdActions[0].ID})
assert.NoError(t, err)
assert.Equal(t, true, action.Enabled)
// Cleanup
_ = repo.Delete(context.Background(), &domain.DeleteActionRequest{ActionId: createdActions[0].ID})
_ = filterRepo.Delete(context.Background(), createdFilters[0].ID)
_ = downloadClientRepo.Delete(context.Background(), mock.ID)
})
t.Run(fmt.Sprintf("ToggleEnabled_Fails_No_Record [%s]", dbType), func(t *testing.T) {
err := repo.ToggleEnabled(9999)
assert.Error(t, err)
})
}
}