Skip to content

Adding new RandomInt utility + updating tests #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions internal/events/types/hype_train/hype_train_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
var event []byte
var err error
lastUser := util.RandomUserID()
lastTotal := util.RandomViewerCount()
lastTotal := util.RandomInt(10 * 100)
lastType := util.RandomType()

//Local variables which will be used for the trigger params below
localTotal := util.RandomViewerCount()
localGoal := util.RandomViewerCount()
localTotal := util.RandomInt(10 * 100)
localGoal := util.RandomInt(10*100*100) + localTotal
localProgress := (localTotal / localGoal)

switch params.Transport {
Expand All @@ -64,12 +65,12 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
BroadcasterUserID: params.ToUserID,
BroadcasterUserLogin: params.ToUserName,
BroadcasterUserName: params.ToUserName,
Total: util.RandomViewerCount(),
Total: localTotal,
Progress: localProgress,
Goal: localGoal,
TopContributions: []models.ContributionData{
{
TotalContribution: util.RandomViewerCount(),
TotalContribution: util.RandomInt(10 * 100),
TypeOfContribution: util.RandomType(),
UserWhoMadeContribution: util.RandomUserID(),
UserNameWhoMadeContribution: "cli_user1",
Expand All @@ -95,7 +96,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
},
}
if triggerMapping[params.Transport][params.Trigger] == "hype-train-end " {
body.Event.CooldownEndsAtTimestamp = util.GetTimestamp().Format(time.RFC3339Nano)
body.Event.CooldownEndsAtTimestamp = util.GetTimestamp().Add(1 * time.Hour).Format(time.RFC3339Nano)
}
event, err = json.Marshal(body)
if err != nil {
Expand All @@ -120,7 +121,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
TypeOfContribution: lastType,
WebSubUser: lastUser,
},
Level: util.RandomViewerCount() % 4,
Level: util.RandomInt(4) + 1,
StartedAtTimestamp: util.GetTimestamp().Format(time.RFC3339),
TopContributions: []models.ContributionData{
{
Expand All @@ -129,7 +130,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
WebSubUser: lastUser,
},
{
TotalContribution: util.RandomViewerCount(),
TotalContribution: util.RandomInt(10 * 100),
TypeOfContribution: util.RandomType(),
WebSubUser: util.RandomUserID(),
},
Expand Down
2 changes: 1 addition & 1 deletion internal/events/types/streamup/streamup.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
GameID: "509658",
Type: "live",
Title: params.StreamTitle,
ViewerCount: 1337,
ViewerCount: util.RandomViewerCount(),
StartedAt: util.GetTimestamp().Format(time.RFC3339),
Language: "en",
ThumbnailURL: "https://static-cdn.jtvnw.net/ttv-static/404_preview-440x248.jpg",
Expand Down
21 changes: 15 additions & 6 deletions internal/util/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,34 @@ func RandomClientID() string {
return fmt.Sprintf("%x", b)[:30]
}

// RandomViewerCount generates a fake viewercount between 0->10,000,000
// RandomViewerCount generates a fake viewercount between 0->100,000
func RandomViewerCount() int64 {
viewer, err := rand.Int(rand.Reader, big.NewInt(1*10*100*100*100))
viewer, err := rand.Int(rand.Reader, big.NewInt(10*100*100))
if err != nil {
log.Fatal(err.Error())
}
return viewer.Int64()
}

//RandomInt generates a random integer between 0->max
func RandomInt(max int64) int64 {
someInt, err := rand.Int(rand.Reader, big.NewInt(max))
if err != nil {
log.Fatal(err.Error())
}

return someInt.Int64()
}

// RandomType generates a fake type; Either bits or subscription, in roughly even distribution
func RandomType() string {
someInt, err := rand.Int(rand.Reader, big.NewInt(1*10*100*100*100))
if err != nil {
log.Fatal(err.Error())
}
if (someInt.Int64()%2) == 0{
if (someInt.Int64() % 2) == 0 {
return "bits"
}else
{
} else {
return "subscription"
}
}
}
19 changes: 19 additions & 0 deletions internal/util/random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,22 @@ func TestRandomViewerCount(t *testing.T) {

a.NotEmpty(viewers)
}

func TestRandomType(t *testing.T) {
a := assert.New(t)

// run the test 20 times to make sure to get at least one of each random type
for i := 0; i < 20; i++ {
randomType := RandomType()

a.NotEmpty(randomType)
}
}

func TestRandomInt(t *testing.T) {
a := assert.New(t)

randomInt := RandomInt(10)

a.Equal(true, randomInt >= 0)
}