@@ -0,0 +1,108 @@
|
||||
package vipcardexperimentser
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"91porn-server/models/v/prdcthsomod"
|
||||
"91porn-server/models/v/rchgordmod"
|
||||
"91porn-server/models/v/vipcardexperimentmod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
func TestValidatePublicationWindow(t *testing.T) {
|
||||
now := time.Now()
|
||||
future := now.Add(2 * time.Minute)
|
||||
if err := validatePublicationWindow(
|
||||
&vipcardexperimentmod.Experiment{StartAt: &future},
|
||||
now,
|
||||
); err == nil {
|
||||
t.Fatal("expected future startAt to fail")
|
||||
}
|
||||
|
||||
past := now.Add(-time.Minute)
|
||||
if err := validatePublicationWindow(
|
||||
&vipcardexperimentmod.Experiment{EndAt: &past},
|
||||
now,
|
||||
); err == nil {
|
||||
t.Fatal("expected expired endAt to fail")
|
||||
}
|
||||
|
||||
end := now.Add(time.Hour)
|
||||
if err := validatePublicationWindow(
|
||||
&vipcardexperimentmod.Experiment{EndAt: &end},
|
||||
now,
|
||||
); err != nil {
|
||||
t.Fatalf("valid publication window failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeOrderStatisticsAddsGoldPurchases(t *testing.T) {
|
||||
sharedProductID := primitive.NewObjectID()
|
||||
goldOnlyProductID := primitive.NewObjectID()
|
||||
result := mergeOrderStatistics(
|
||||
[]rchgordmod.VIPExperimentOrderStat{{
|
||||
Variant: vipcardexperimentmod.VariantA,
|
||||
ProductID: sharedProductID,
|
||||
CreatedOrders: 2,
|
||||
PaidOrders: 1,
|
||||
PaidAmount: 1000,
|
||||
}},
|
||||
[]prdcthsomod.VIPExperimentGoldOrderStat{
|
||||
{
|
||||
Variant: vipcardexperimentmod.VariantA,
|
||||
ProductID: sharedProductID,
|
||||
GoldPaidOrders: 3,
|
||||
GoldPaidAmount: 600,
|
||||
},
|
||||
{
|
||||
Variant: vipcardexperimentmod.VariantB,
|
||||
ProductID: goldOnlyProductID,
|
||||
GoldPaidOrders: 1,
|
||||
GoldPaidAmount: 200,
|
||||
},
|
||||
},
|
||||
)
|
||||
if len(result) != 2 {
|
||||
t.Fatalf("merged order count = %d, want 2", len(result))
|
||||
}
|
||||
if result[0].PaidOrders != 1 || result[0].PaidAmount != 1000 ||
|
||||
result[0].GoldPaidOrders != 3 || result[0].GoldPaidAmount != 600 {
|
||||
t.Fatalf("merged shared product stats = %#v", result[0])
|
||||
}
|
||||
if result[1].Variant != vipcardexperimentmod.VariantB ||
|
||||
result[1].ProductID != goldOnlyProductID ||
|
||||
result[1].GoldPaidOrders != 1 || result[1].GoldPaidAmount != 200 {
|
||||
t.Fatalf("gold-only product stats = %#v", result[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublishRejectsInvalidUIConfigBeforeDatabaseAccess(t *testing.T) {
|
||||
productID := primitive.NewObjectID()
|
||||
experiment := &vipcardexperimentmod.Experiment{
|
||||
ExperimentID: "invalid-ui-config",
|
||||
TrafficA: 50,
|
||||
TrafficB: 50,
|
||||
VariantA: vipcardexperimentmod.VariantConfig{
|
||||
DefaultProductID: productID,
|
||||
ProductIDs: []primitive.ObjectID{productID},
|
||||
UIConfig: &vipcardexperimentmod.UIConfig{
|
||||
BadgeStyles: []vipcardexperimentmod.BadgeStyle{{
|
||||
BadgeType: vipcardexperimentmod.BadgeMostPopular,
|
||||
BackgroundColor: "#FFF",
|
||||
TextColor: "#FFFFFF",
|
||||
}},
|
||||
},
|
||||
},
|
||||
VariantB: vipcardexperimentmod.VariantConfig{
|
||||
DefaultProductID: productID,
|
||||
ProductIDs: []primitive.ObjectID{productID},
|
||||
},
|
||||
}
|
||||
err := Publish(experiment, "tester")
|
||||
if err == nil || !strings.Contains(err.Error(), "variantA.uiConfig") {
|
||||
t.Fatalf("Publish() error = %v, want variantA.uiConfig error", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user