Skip to content

Commit

Permalink
add test, fix case of Id
Browse files Browse the repository at this point in the history
  • Loading branch information
motatoes committed Jan 25, 2024
1 parent 5003725 commit 93da167
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 45 deletions.
2 changes: 2 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki
github.com/agext/levenshtein v1.2.2/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/alecthomas/kong v0.7.1 h1:azoTh0IOfwlAX3qN9sHWTxACE2oV8Bg2gAwBsMwDQY4=
github.com/alecthomas/kong v0.7.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
Expand Down
39 changes: 16 additions & 23 deletions backend/models/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ type DiggerBatch struct {

type DiggerJob struct {
gorm.Model
DiggerJobId string `gorm:"size:50,index:idx_digger_job_id"`
DiggerJobID string `gorm:"size:50,index:idx_digger_job_id"`
Status orchestrator_scheduler.DiggerJobStatus
Batch *DiggerBatch
BatchID *string `gorm:"index:idx_digger_job_id"`
DiggerJobSummary *DiggerJobSummary
DiggerJobSummaryId *uint `gorm:""`
DiggerJobSummary DiggerJobSummary
DiggerJobSummaryID uint
SerializedJob []byte
StatusUpdatedAt time.Time
}
Expand Down Expand Up @@ -68,26 +68,19 @@ type GithubDiggerJobLink struct {
}

func (j *DiggerJob) MapToJsonStruct() interface{} {
if j.DiggerJobSummary == nil {
return orchestrator_scheduler.SerializedJob{
DiggerJobId: j.DiggerJobId,
Status: j.Status,
}
} else {
var job orchestrator.JobJson
err := json.Unmarshal(j.SerializedJob, &job)
if err != nil {
log.Printf("Failed to convert unmarshall Serialized job")
}
return orchestrator_scheduler.SerializedJob{
DiggerJobId: j.DiggerJobId,
Status: j.Status,
JobString: j.SerializedJob,
ProjectName: job.ProjectName,
ResourcesCreated: j.DiggerJobSummary.ResourcesCreated,
ResourcesUpdated: j.DiggerJobSummary.ResourcesUpdated,
ResourcesDeleted: j.DiggerJobSummary.ResourcesDeleted,
}
var job orchestrator.JobJson
err := json.Unmarshal(j.SerializedJob, &job)
if err != nil {
log.Printf("Failed to convert unmarshall Serialized job")
}
return orchestrator_scheduler.SerializedJob{
DiggerJobId: j.DiggerJobID,
Status: j.Status,
JobString: j.SerializedJob,
ProjectName: job.ProjectName,
ResourcesCreated: j.DiggerJobSummary.ResourcesCreated,
ResourcesUpdated: j.DiggerJobSummary.ResourcesUpdated,
ResourcesDeleted: j.DiggerJobSummary.ResourcesDeleted,
}
}
func (b *DiggerBatch) MapToJsonStruct() (interface{}, error) {
Expand Down
40 changes: 19 additions & 21 deletions backend/models/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,14 +626,21 @@ func (db *Database) CreateDiggerJob(batchId uuid.UUID, serializedJob []byte) (*D
}
jobId := uniuri.New()
batchIdStr := batchId.String()
job := &DiggerJob{DiggerJobId: jobId, Status: scheduler.DiggerJobCreated,
BatchID: &batchIdStr, SerializedJob: serializedJob}
result := db.GormDB.Save(job)

summary := &DiggerJobSummary{}
result := db.GormDB.Save(summary)
if result.Error != nil {
return nil, result.Error
}

job := &DiggerJob{DiggerJobID: jobId, Status: scheduler.DiggerJobCreated,
BatchID: &batchIdStr, SerializedJob: serializedJob, DiggerJobSummary: *summary}
result = db.GormDB.Save(job)
if result.Error != nil {
return nil, result.Error
}

log.Printf("DiggerJob %v, (id: %v) has been created successfully\n", job.DiggerJobId, job.ID)
log.Printf("DiggerJob %v, (id: %v) has been created successfully\n", job.DiggerJobID, job.ID)
return job, nil
}

Expand All @@ -643,21 +650,12 @@ func (db *Database) UpdateDiggerJobSummary(diggerJobId string, resourcesCreated
return nil, fmt.Errorf("Could not get digger job")
}
var jobSummary *DiggerJobSummary
if diggerJob.DiggerJobSummary == nil {
jobSummary = &DiggerJobSummary{
ResourcesCreated: resourcesCreated,
ResourcesUpdated: resourcesUpdated,
ResourcesDeleted: resourcesDeleted,
}
diggerJob.DiggerJobSummary = jobSummary
} else {
jobSummary = diggerJob.DiggerJobSummary
jobSummary.ResourcesCreated = resourcesCreated
jobSummary.ResourcesUpdated = resourcesUpdated
jobSummary.ResourcesDeleted = resourcesDeleted
}
jobSummary = &diggerJob.DiggerJobSummary
jobSummary.ResourcesCreated = resourcesCreated
jobSummary.ResourcesUpdated = resourcesUpdated
jobSummary.ResourcesDeleted = resourcesDeleted

result := db.GormDB.Save(jobSummary)
result := db.GormDB.Save(&jobSummary)
if result.Error != nil {
return nil, result.Error
}
Expand All @@ -671,17 +669,17 @@ func (db *Database) UpdateDiggerJob(job *DiggerJob) error {
if result.Error != nil {
return result.Error
}
log.Printf("DiggerJob %v, (id: %v) has been updated successfully\n", job.DiggerJobId, job.ID)
log.Printf("DiggerJob %v, (id: %v) has been updated successfully\n", job.DiggerJobID, job.ID)
return nil
}

func (db *Database) GetDiggerJobsForBatch(batchId uuid.UUID) ([]DiggerJob, error) {
jobs := make([]DiggerJob, 0)

var where *gorm.DB
where = db.GormDB.Preload("Batch").Preload("DiggerJobSummary").Where("digger_jobs.batch_id = ?", batchId)
where = db.GormDB.Where("digger_jobs.batch_id = ?", batchId)

result := where.Find(&jobs)
result := where.Preload("Batch").Preload("DiggerJobSummary").Find(&jobs)
if result.Error != nil {
if !errors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, result.Error
Expand Down
45 changes: 44 additions & 1 deletion backend/models/storage_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package models

import (
"github.com/diggerhq/digger/libs/orchestrator/scheduler"
"github.com/stretchr/testify/assert"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"log"
"os"
"strings"
Expand All @@ -25,7 +27,9 @@ func setupSuite(tb testing.TB) (func(tb testing.TB), *Database, *Organisation) {
}

// open and create a new database
gdb, err := gorm.Open(sqlite.Open(dbName), &gorm.Config{})
gdb, err := gorm.Open(sqlite.Open(dbName), &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
})
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -128,3 +132,42 @@ func TestGithubRepoRemoved(t *testing.T) {
assert.Equal(t, i.ID, i2.ID)
assert.Equal(t, GithubAppInstallDeleted, i.Status)
}

func TestGetDiggerJobsForBatchPreloadsSummary(t *testing.T) {
teardownSuite, _, _ := setupSuite(t)
defer teardownSuite(t)

prNumber := 123
repoName := "test"
repoOwner := "test"
repoFullName := "test/test"
diggerconfig := ""
branchName := "main"
batchType := scheduler.BatchTypePlan
commentId := int64(123)

batch, err := DB.CreateDiggerBatch(123, repoOwner, repoName, repoFullName, prNumber, diggerconfig, branchName, batchType, &commentId)
assert.NoError(t, err)

job, err := DB.CreateDiggerJob(batch.ID, []byte{97})
assert.NoError(t, err)

job, err = DB.UpdateDiggerJobSummary(job.DiggerJobId, 1, 2, 3)
assert.NoError(t, err)

//fetchedBatch, err := DB.GetDiggerBatch(&batch.ID)
//assert.NoError(t, err)
//jsons, err := fetchedBatch.MapToJsonStruct()
//assert.NoError(t, err)

//jobs := job.MapToJsonStruct()
//
//println(job.DiggerJobSummary.ResourcesCreated, job.DiggerJobSummary.ResourcesUpdated, job.DiggerJobSummary.ResourcesDeleted)
//spew.Dump(jsons)
//spew.Dump(jobs)

jobssss, err := DB.GetDiggerJobsForBatch(batch.ID)
println(job.DiggerJobSummary.ResourcesCreated, job.DiggerJobSummary.ResourcesUpdated, job.DiggerJobSummary.ResourcesDeleted)
println(jobssss[0].DiggerJobSummary.ResourcesCreated, jobssss[0].DiggerJobSummary.ResourcesUpdated, jobssss[0].DiggerJobSummary.ResourcesDeleted)

}

0 comments on commit 93da167

Please sign in to comment.