-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #160 from aalug/develop
add util functions to load sample data (#159)
- Loading branch information
Showing
9 changed files
with
192 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"github.com/aalug/go-gin-job-search/pkg/utils" | ||
"github.com/bxcodec/faker/v3" | ||
"log" | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
// LoadTestData loads the test data into the database | ||
func (store *SQLStore) LoadTestData(ctx context.Context) { | ||
var wg sync.WaitGroup | ||
nOfJobsCreated := int32(0) | ||
jobTitles := append(utils.GenerateEngineerJobs(), utils.GenerateDeveloperJobs()...) | ||
|
||
// create fake companies | ||
for i := 0; i < 5; i++ { | ||
for _, industry := range utils.Industries { | ||
// Increment the WaitGroup counter for each goroutine | ||
wg.Add(1) | ||
|
||
go func(industry string) { | ||
// Decrement the WaitGroup counter when the goroutine is done | ||
defer wg.Done() | ||
|
||
idx := utils.RandomInt(0, int32(len(utils.Locations)-1)) | ||
location := utils.Locations[idx] | ||
companyParams := CreateCompanyParams{ | ||
Name: faker.DomainName(), | ||
Industry: industry, | ||
Location: location, | ||
} | ||
company, err := store.CreateCompany(ctx, companyParams) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
var jobsCreated int32 | ||
var jobWg sync.WaitGroup | ||
|
||
// create jobs | ||
for j := 0; j < 3; j++ { | ||
// Increment the job WaitGroup counter | ||
jobWg.Add(1) | ||
|
||
go func() { | ||
// Decrement the job WaitGroup counter when the job is done | ||
defer jobWg.Done() | ||
|
||
idx := utils.RandomInt(0, int32(len(jobTitles)-1)) | ||
jobTitle := jobTitles[idx] | ||
jobParams := CreateJobParams{ | ||
Title: jobTitle, | ||
Industry: industry, | ||
CompanyID: company.ID, | ||
Description: jobTitle + " " + faker.Paragraph(), | ||
Location: location, | ||
SalaryMin: utils.RandomInt(0, 2000), | ||
SalaryMax: utils.RandomInt(2001, 5000), | ||
Requirements: jobTitle + " " + faker.Paragraph(), | ||
} | ||
_, err := store.CreateJob(ctx, jobParams) | ||
if err != nil { | ||
log.Println(err) | ||
} else { | ||
atomic.AddInt32(&jobsCreated, 1) | ||
} | ||
}() | ||
} | ||
|
||
jobWg.Wait() // Wait for all jobs to finish | ||
atomic.AddInt32(&nOfJobsCreated, jobsCreated) | ||
}(industry) | ||
} | ||
} | ||
|
||
wg.Wait() // Wait for all companies to finish | ||
log.Printf("Created %d jobs", nOfJobsCreated) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package utils | ||
|
||
import "fmt" | ||
|
||
var Industries = []string{ | ||
"IT", | ||
"Game Development", | ||
"Financial Technology", | ||
"E-commerce", | ||
} | ||
|
||
var Locations = []string{ | ||
"London", | ||
"New York", | ||
"Paris", | ||
"Berlin", | ||
"Madrid", | ||
"Milan", | ||
"Rome", | ||
"Amsterdam", | ||
"Barcelona", | ||
"Vienna", | ||
"Hamburg", | ||
"Dublin", | ||
"Sydney", | ||
"Singapore", | ||
"Hong Kong", | ||
"Tokyo", | ||
"Seoul", | ||
"Warsaw", | ||
} | ||
|
||
// qualifiers | ||
var qualifiers = []string{"Junior", "Senior", "Lead"} | ||
|
||
// languages for Developers and Engineers | ||
var languages = []string{"Java", "Python", "JavaScript", "Go", "C#", "C++", "Ruby", "C"} | ||
|
||
// GenerateDeveloperJobs Function to combine different parts for Developers | ||
func GenerateDeveloperJobs() []string { | ||
var combined []string | ||
for _, qualifier := range qualifiers { | ||
for _, language := range languages { | ||
combined = append(combined, fmt.Sprintf("%s %s Developer", qualifier, language)) | ||
} | ||
} | ||
return combined | ||
} | ||
|
||
// GenerateEngineerJobs Function to combine different parts for Engineers | ||
func GenerateEngineerJobs() []string { | ||
var combined []string | ||
for _, qualifier := range qualifiers { | ||
for _, language := range languages { | ||
combined = append(combined, fmt.Sprintf("%s %s Engineer", qualifier, language)) | ||
} | ||
} | ||
return combined | ||
} |