77 lines
2.1 KiB
Go
77 lines
2.1 KiB
Go
package mediaService
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"91jd-server/models/v/mediacontentmod"
|
|
"91jd-server/models/v/mediamod"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
func TestFindExistingMediaContentDramaFallsBackToEpisodeNumber(t *testing.T) {
|
|
mediaID := primitive.NewObjectID()
|
|
existing := mediacontentmod.MediaContent{
|
|
ID: primitive.NewObjectID(),
|
|
MediaID: mediaID,
|
|
EpisodeNumber: 3,
|
|
HashId: "old-hash",
|
|
}
|
|
episodeLookupCalled := false
|
|
|
|
got, hashChanged, err := findExistingMediaContent(
|
|
mediaID,
|
|
mediamod.MediaTypeDrama,
|
|
"new-hash",
|
|
3,
|
|
func(primitive.ObjectID, string) (mediacontentmod.MediaContent, error) {
|
|
return mediacontentmod.MediaContent{}, mediacontentmod.MediaContentNotFound
|
|
},
|
|
func(gotMediaID primitive.ObjectID, gotEpisodeNumber int) (mediacontentmod.MediaContent, error) {
|
|
episodeLookupCalled = true
|
|
if gotMediaID != mediaID || gotEpisodeNumber != 3 {
|
|
t.Fatalf("episode lookup = (%s, %d), want (%s, 3)", gotMediaID, gotEpisodeNumber, mediaID)
|
|
}
|
|
return existing, nil
|
|
},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("findExistingMediaContent returned error: %v", err)
|
|
}
|
|
if !episodeLookupCalled {
|
|
t.Fatal("episode fallback was not called")
|
|
}
|
|
if !hashChanged {
|
|
t.Fatal("hashChanged = false, want true")
|
|
}
|
|
if got.ID != existing.ID {
|
|
t.Fatalf("content ID = %s, want %s", got.ID, existing.ID)
|
|
}
|
|
}
|
|
|
|
func TestFindExistingMediaContentNonDramaDoesNotFallback(t *testing.T) {
|
|
episodeLookupCalled := false
|
|
_, hashChanged, err := findExistingMediaContent(
|
|
primitive.NewObjectID(),
|
|
"video",
|
|
"new-hash",
|
|
3,
|
|
func(primitive.ObjectID, string) (mediacontentmod.MediaContent, error) {
|
|
return mediacontentmod.MediaContent{}, mediacontentmod.MediaContentNotFound
|
|
},
|
|
func(primitive.ObjectID, int) (mediacontentmod.MediaContent, error) {
|
|
episodeLookupCalled = true
|
|
return mediacontentmod.MediaContent{}, nil
|
|
},
|
|
)
|
|
if err != mediacontentmod.MediaContentNotFound {
|
|
t.Fatalf("error = %v, want MediaContentNotFound", err)
|
|
}
|
|
if hashChanged {
|
|
t.Fatal("hashChanged = true, want false")
|
|
}
|
|
if episodeLookupCalled {
|
|
t.Fatal("non-drama content used episode fallback")
|
|
}
|
|
}
|