feat: write unit tests for filtering logic
This commit is contained in:
122
internal/musicbrainz/filter_test.go
Normal file
122
internal/musicbrainz/filter_test.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package musicbrainz_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"naviwatcher/internal/database"
|
||||
"naviwatcher/internal/musicbrainz"
|
||||
)
|
||||
|
||||
func TestApplyTypeToggles(t *testing.T) {
|
||||
releases := []database.ExternalRelease{
|
||||
{RGID: "r1", Type: "Single", SecondaryTypes: []string{}},
|
||||
{RGID: "r2", Type: "Album", SecondaryTypes: []string{"Single"}},
|
||||
{RGID: "r3", Type: "Compilation", SecondaryTypes: []string{}},
|
||||
{RGID: "r4", Type: "Album", SecondaryTypes: []string{"Compilation"}},
|
||||
{RGID: "r5", Type: "EP", SecondaryTypes: []string{}},
|
||||
{RGID: "r6", Type: "Album", SecondaryTypes: []string{"EP"}},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
opts musicbrainz.FilterOptions
|
||||
expectedCounts int
|
||||
expectedRGIDs []string
|
||||
}{
|
||||
{
|
||||
name: "No filters",
|
||||
opts: musicbrainz.FilterOptions{IgnoreSingles: false, IgnoreCompilations: false},
|
||||
expectedCounts: 6,
|
||||
expectedRGIDs: []string{"r1", "r2", "r3", "r4", "r5", "r6"},
|
||||
},
|
||||
{
|
||||
name: "Ignore singles only",
|
||||
opts: musicbrainz.FilterOptions{IgnoreSingles: true, IgnoreCompilations: false},
|
||||
expectedCounts: 2, // r3, r4
|
||||
expectedRGIDs: []string{"r3", "r4"},
|
||||
},
|
||||
{
|
||||
name: "Ignore compilations only",
|
||||
opts: musicbrainz.FilterOptions{IgnoreSingles: false, IgnoreCompilations: true},
|
||||
expectedCounts: 4, // r1, r2, r5, r6
|
||||
expectedRGIDs: []string{"r1", "r2", "r5", "r6"},
|
||||
},
|
||||
{
|
||||
name: "Ignore both",
|
||||
opts: musicbrainz.FilterOptions{IgnoreSingles: true, IgnoreCompilations: true},
|
||||
expectedCounts: 0,
|
||||
expectedRGIDs: []string{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := musicbrainz.ApplyTypeToggles(releases, tt.opts)
|
||||
if len(got) != tt.expectedCounts {
|
||||
t.Errorf("expected %d releases, got %d", tt.expectedCounts, len(got))
|
||||
}
|
||||
for i, r := range got {
|
||||
if r.RGID != tt.expectedRGIDs[i] {
|
||||
t.Errorf("expected RGID %s at index %d, got %s", tt.expectedRGIDs[i], i, r.RGID)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyTypeTogglesToReleaseGroups(t *testing.T) {
|
||||
groups := []musicbrainz.ReleaseGroup{
|
||||
{ID: "g1", Type: "Single", SecondaryTypes: []string{}},
|
||||
{ID: "g2", Type: "Album", SecondaryTypes: []string{"Single"}},
|
||||
{ID: "g3", Type: "Compilation", SecondaryTypes: []string{}},
|
||||
{ID: "g4", Type: "Album", SecondaryTypes: []string{"Compilation"}},
|
||||
{ID: "g5", Type: "EP", SecondaryTypes: []string{}},
|
||||
{ID: "g6", Type: "Album", SecondaryTypes: []string{"EP"}},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
opts musicbrainz.FilterOptions
|
||||
expectedCounts int
|
||||
expectedIDs []string
|
||||
}{
|
||||
{
|
||||
name: "No filters",
|
||||
opts: musicbrainz.FilterOptions{IgnoreSingles: false, IgnoreCompilations: false},
|
||||
expectedCounts: 6,
|
||||
expectedIDs: []string{"g1", "g2", "g3", "g4", "g5", "g6"},
|
||||
},
|
||||
{
|
||||
name: "Ignore singles only",
|
||||
opts: musicbrainz.FilterOptions{IgnoreSingles: true, IgnoreCompilations: false},
|
||||
expectedCounts: 2, // g3, g4
|
||||
expectedIDs: []string{"g3", "g4"},
|
||||
},
|
||||
{
|
||||
name: "Ignore compilations only",
|
||||
opts: musicbrainz.FilterOptions{IgnoreSingles: false, IgnoreCompilations: true},
|
||||
expectedCounts: 4, // g1, g2, g5, g6
|
||||
expectedIDs: []string{"g1", "g2", "g5", "g6"},
|
||||
},
|
||||
{
|
||||
name: "Ignore both",
|
||||
opts: musicbrainz.FilterOptions{IgnoreSingles: true, IgnoreCompilations: true},
|
||||
expectedCounts: 0,
|
||||
expectedIDs: []string{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := musicbrainz.ApplyTypeTogglesToReleaseGroups(groups, tt.opts)
|
||||
if len(got) != tt.expectedCounts {
|
||||
t.Errorf("expected %d groups, got %d", tt.expectedCounts, len(got))
|
||||
}
|
||||
for i, g := range got {
|
||||
if g.ID != tt.expectedIDs[i] {
|
||||
t.Errorf("expected ID %s at index %d, got %s", tt.expectedIDs[i], i, g.ID)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user