Compare commits
2 Commits
0dcd5fec51
...
c907c4812b
| Author | SHA1 | Date | |
|---|---|---|---|
| c907c4812b | |||
| c7f30070c0 |
@@ -69,6 +69,16 @@ func seedDatabase(userRepo repositories.UserRepository, postRepo repositories.Po
|
||||
return err
|
||||
}
|
||||
|
||||
if *numUsers < 0 {
|
||||
return fmt.Errorf("invalid value for --users: %d (must be >= 0)", *numUsers)
|
||||
}
|
||||
if *numPosts <= 0 {
|
||||
return fmt.Errorf("invalid value for --posts: %d (must be > 0)", *numPosts)
|
||||
}
|
||||
if *votesPerPost < 0 {
|
||||
return fmt.Errorf("invalid value for --votes-per-post: %d (must be >= 0)", *votesPerPost)
|
||||
}
|
||||
|
||||
if !IsJSONOutput() {
|
||||
fmt.Println("Starting database seeding...")
|
||||
}
|
||||
@@ -93,7 +103,7 @@ func seedDatabase(userRepo repositories.UserRepository, postRepo repositories.Po
|
||||
processor := NewParallelProcessor()
|
||||
|
||||
var progress *ProgressIndicator
|
||||
if !IsJSONOutput() {
|
||||
if !IsJSONOutput() && *numUsers > 0 {
|
||||
progress = NewProgressIndicator(*numUsers, "Creating users (parallel)")
|
||||
}
|
||||
users, err := processor.CreateUsersInParallel(userRepo, *numUsers, progress)
|
||||
@@ -106,7 +116,7 @@ func seedDatabase(userRepo repositories.UserRepository, postRepo repositories.Po
|
||||
|
||||
allUsers := append([]database.User{*seedUser}, users...)
|
||||
|
||||
if !IsJSONOutput() {
|
||||
if !IsJSONOutput() && *numPosts > 0 {
|
||||
progress = NewProgressIndicator(*numPosts, "Creating posts (parallel)")
|
||||
}
|
||||
posts, err := processor.CreatePostsInParallel(postRepo, seedUser.ID, *numPosts, progress)
|
||||
@@ -117,7 +127,7 @@ func seedDatabase(userRepo repositories.UserRepository, postRepo repositories.Po
|
||||
progress.Complete()
|
||||
}
|
||||
|
||||
if !IsJSONOutput() {
|
||||
if !IsJSONOutput() && len(posts) > 0 {
|
||||
progress = NewProgressIndicator(len(posts), "Creating votes (parallel)")
|
||||
}
|
||||
votes, err := processor.CreateVotesInParallel(voteRepo, allUsers, posts, *votesPerPost, progress)
|
||||
@@ -128,7 +138,7 @@ func seedDatabase(userRepo repositories.UserRepository, postRepo repositories.Po
|
||||
progress.Complete()
|
||||
}
|
||||
|
||||
if !IsJSONOutput() {
|
||||
if !IsJSONOutput() && len(posts) > 0 {
|
||||
progress = NewProgressIndicator(len(posts), "Updating scores (parallel)")
|
||||
}
|
||||
err = processor.UpdatePostScoresInParallel(postRepo, voteRepo, posts, progress)
|
||||
|
||||
@@ -178,4 +178,64 @@ func TestSeedDatabaseFlagParsing(t *testing.T) {
|
||||
t.Error("expected error for missing votes-per-post value")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("negative users value", func(t *testing.T) {
|
||||
err := seedDatabase(userRepo, postRepo, voteRepo, []string{"--users", "-1"})
|
||||
|
||||
if err == nil {
|
||||
t.Error("expected error for negative users value")
|
||||
}
|
||||
if err != nil && err.Error() != "invalid value for --users: -1 (must be >= 0)" {
|
||||
t.Errorf("expected specific error message, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("negative posts value", func(t *testing.T) {
|
||||
err := seedDatabase(userRepo, postRepo, voteRepo, []string{"--posts", "-5"})
|
||||
|
||||
if err == nil {
|
||||
t.Error("expected error for negative posts value")
|
||||
}
|
||||
if err != nil && err.Error() != "invalid value for --posts: -5 (must be > 0)" {
|
||||
t.Errorf("expected specific error message, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("zero posts value", func(t *testing.T) {
|
||||
err := seedDatabase(userRepo, postRepo, voteRepo, []string{"--posts", "0"})
|
||||
|
||||
if err == nil {
|
||||
t.Error("expected error for zero posts value")
|
||||
}
|
||||
if err != nil && err.Error() != "invalid value for --posts: 0 (must be > 0)" {
|
||||
t.Errorf("expected specific error message, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("negative votes-per-post value", func(t *testing.T) {
|
||||
err := seedDatabase(userRepo, postRepo, voteRepo, []string{"--votes-per-post", "-10"})
|
||||
|
||||
if err == nil {
|
||||
t.Error("expected error for negative votes-per-post value")
|
||||
}
|
||||
if err != nil && err.Error() != "invalid value for --votes-per-post: -10 (must be >= 0)" {
|
||||
t.Errorf("expected specific error message, got: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("zero users value is valid", func(t *testing.T) {
|
||||
err := seedDatabase(userRepo, postRepo, voteRepo, []string{"--users", "0", "--posts", "1"})
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("zero users should be valid, got error: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("zero votes-per-post value is valid", func(t *testing.T) {
|
||||
err := seedDatabase(userRepo, postRepo, voteRepo, []string{"--votes-per-post", "0", "--posts", "1"})
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("zero votes-per-post should be valid, got error: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user