@@ -0,0 +1,121 @@
|
||||
package mediacontentser
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"91porn-server/models/v/mediacontentmod"
|
||||
"91porn-server/models/v/vidmod"
|
||||
)
|
||||
|
||||
func TestValidateDramaEpisodeState(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
episodeNumber int
|
||||
listenPermission int
|
||||
price int64
|
||||
videoURL string
|
||||
active bool
|
||||
status int
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "free", episodeNumber: 1, listenPermission: 2, videoURL: "1.m3u8", active: true, status: 1},
|
||||
{name: "coin", episodeNumber: 2, listenPermission: 1, price: 30, videoURL: "2.m3u8", active: true, status: 1},
|
||||
{name: "free with price", episodeNumber: 1, listenPermission: 2, price: 30, wantErr: true},
|
||||
{name: "coin without price", episodeNumber: 2, listenPermission: 1, wantErr: true},
|
||||
{name: "active while transcoding", episodeNumber: 1, listenPermission: 2, videoURL: "1.m3u8", active: true, status: 0, wantErr: true},
|
||||
{name: "active without resource", episodeNumber: 1, listenPermission: 2, active: true, status: 1, wantErr: true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := validateDramaEpisodeState(tt.episodeNumber, tt.listenPermission, tt.price, "第1集", tt.videoURL, "", tt.active, tt.status)
|
||||
if gotErr := err != nil; gotErr != tt.wantErr {
|
||||
t.Fatalf("validateDramaEpisodeState() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDramaEpisodeStateAllowsNonContinuousFreeEpisodes(t *testing.T) {
|
||||
episodes := []struct {
|
||||
episodeNumber int
|
||||
listenPermission int
|
||||
price int64
|
||||
}{
|
||||
{episodeNumber: 2, listenPermission: 1, price: 30},
|
||||
{episodeNumber: 3, listenPermission: 2, price: 0},
|
||||
}
|
||||
for _, episode := range episodes {
|
||||
if err := validateDramaEpisodeState(
|
||||
episode.episodeNumber,
|
||||
episode.listenPermission,
|
||||
episode.price,
|
||||
"剧集",
|
||||
"episode.m3u8",
|
||||
"",
|
||||
true,
|
||||
1,
|
||||
); err != nil {
|
||||
t.Fatalf("episode %d validation failed: %v", episode.episodeNumber, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileStatusToEpisodeStatus(t *testing.T) {
|
||||
tests := map[string]int{
|
||||
vidmod.Converting: 0,
|
||||
vidmod.ConvertCompleted: 1,
|
||||
vidmod.Completed: 1,
|
||||
vidmod.ConvertError: 2,
|
||||
vidmod.MergeError: 2,
|
||||
}
|
||||
for input, want := range tests {
|
||||
if got := fileStatusToEpisodeStatus(input); got != want {
|
||||
t.Fatalf("fileStatusToEpisodeStatus(%q) = %d, want %d", input, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebBatchUpdateReqValidatedIDs(t *testing.T) {
|
||||
id := "507f1f77bcf86cd799439011"
|
||||
price := int64(30)
|
||||
permission := 1
|
||||
freeEpisode := 1
|
||||
req := WebBatchUpdateReq{
|
||||
IDS: []string{id, id},
|
||||
Price: &price,
|
||||
Permission: &permission,
|
||||
FreeEpisode: &freeEpisode,
|
||||
}
|
||||
ids, err := req.ValidatedIDs()
|
||||
if err != nil {
|
||||
t.Fatalf("ValidatedIDs() error: %v", err)
|
||||
}
|
||||
if len(ids) != 1 {
|
||||
t.Fatalf("ValidatedIDs() length = %d, want 1", len(ids))
|
||||
}
|
||||
if _, err = (&WebBatchUpdateReq{IDS: []string{id}}).ValidatedIDs(); err == nil {
|
||||
t.Fatal("request without update fields unexpectedly accepted")
|
||||
}
|
||||
req.IDS = []string{"bad-id"}
|
||||
if _, err = req.ValidatedIDs(); err == nil {
|
||||
t.Fatal("invalid ObjectID unexpectedly accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebBatchUpdateReqAppliesPricingWithoutIsActive(t *testing.T) {
|
||||
price := int64(30)
|
||||
permission := 1
|
||||
req := WebBatchUpdateReq{Price: &price, Permission: &permission}
|
||||
content := mediacontentmod.MediaContent{IsActive: true, Price: 10, ListenPermission: 2}
|
||||
|
||||
next := req.applyContentFields(content)
|
||||
if !next.IsActive {
|
||||
t.Fatal("isActive changed when omitted")
|
||||
}
|
||||
if next.Price != price {
|
||||
t.Fatalf("price = %d, want %d", next.Price, price)
|
||||
}
|
||||
if next.ListenPermission != permission {
|
||||
t.Fatalf("listenPermission = %d, want %d", next.ListenPermission, permission)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user