Skip to content

Commit 93da167

Browse files
committed
add test, fix case of Id
1 parent 5003725 commit 93da167

File tree

4 files changed

+81
-45
lines changed

4 files changed

+81
-45
lines changed

backend/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki
267267
github.com/agext/levenshtein v1.2.2/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
268268
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
269269
github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
270+
github.com/alecthomas/kong v0.7.1 h1:azoTh0IOfwlAX3qN9sHWTxACE2oV8Bg2gAwBsMwDQY4=
271+
github.com/alecthomas/kong v0.7.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U=
270272
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
271273
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
272274
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=

backend/models/scheduler.go

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ type DiggerBatch struct {
3333

3434
type DiggerJob struct {
3535
gorm.Model
36-
DiggerJobId string `gorm:"size:50,index:idx_digger_job_id"`
36+
DiggerJobID string `gorm:"size:50,index:idx_digger_job_id"`
3737
Status orchestrator_scheduler.DiggerJobStatus
3838
Batch *DiggerBatch
3939
BatchID *string `gorm:"index:idx_digger_job_id"`
40-
DiggerJobSummary *DiggerJobSummary
41-
DiggerJobSummaryId *uint `gorm:""`
40+
DiggerJobSummary DiggerJobSummary
41+
DiggerJobSummaryID uint
4242
SerializedJob []byte
4343
StatusUpdatedAt time.Time
4444
}
@@ -68,26 +68,19 @@ type GithubDiggerJobLink struct {
6868
}
6969

7070
func (j *DiggerJob) MapToJsonStruct() interface{} {
71-
if j.DiggerJobSummary == nil {
72-
return orchestrator_scheduler.SerializedJob{
73-
DiggerJobId: j.DiggerJobId,
74-
Status: j.Status,
75-
}
76-
} else {
77-
var job orchestrator.JobJson
78-
err := json.Unmarshal(j.SerializedJob, &job)
79-
if err != nil {
80-
log.Printf("Failed to convert unmarshall Serialized job")
81-
}
82-
return orchestrator_scheduler.SerializedJob{
83-
DiggerJobId: j.DiggerJobId,
84-
Status: j.Status,
85-
JobString: j.SerializedJob,
86-
ProjectName: job.ProjectName,
87-
ResourcesCreated: j.DiggerJobSummary.ResourcesCreated,
88-
ResourcesUpdated: j.DiggerJobSummary.ResourcesUpdated,
89-
ResourcesDeleted: j.DiggerJobSummary.ResourcesDeleted,
90-
}
71+
var job orchestrator.JobJson
72+
err := json.Unmarshal(j.SerializedJob, &job)
73+
if err != nil {
74+
log.Printf("Failed to convert unmarshall Serialized job")
75+
}
76+
return orchestrator_scheduler.SerializedJob{
77+
DiggerJobId: j.DiggerJobID,
78+
Status: j.Status,
79+
JobString: j.SerializedJob,
80+
ProjectName: job.ProjectName,
81+
ResourcesCreated: j.DiggerJobSummary.ResourcesCreated,
82+
ResourcesUpdated: j.DiggerJobSummary.ResourcesUpdated,
83+
ResourcesDeleted: j.DiggerJobSummary.ResourcesDeleted,
9184
}
9285
}
9386
func (b *DiggerBatch) MapToJsonStruct() (interface{}, error) {

backend/models/storage.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -626,14 +626,21 @@ func (db *Database) CreateDiggerJob(batchId uuid.UUID, serializedJob []byte) (*D
626626
}
627627
jobId := uniuri.New()
628628
batchIdStr := batchId.String()
629-
job := &DiggerJob{DiggerJobId: jobId, Status: scheduler.DiggerJobCreated,
630-
BatchID: &batchIdStr, SerializedJob: serializedJob}
631-
result := db.GormDB.Save(job)
629+
630+
summary := &DiggerJobSummary{}
631+
result := db.GormDB.Save(summary)
632+
if result.Error != nil {
633+
return nil, result.Error
634+
}
635+
636+
job := &DiggerJob{DiggerJobID: jobId, Status: scheduler.DiggerJobCreated,
637+
BatchID: &batchIdStr, SerializedJob: serializedJob, DiggerJobSummary: *summary}
638+
result = db.GormDB.Save(job)
632639
if result.Error != nil {
633640
return nil, result.Error
634641
}
635642

636-
log.Printf("DiggerJob %v, (id: %v) has been created successfully\n", job.DiggerJobId, job.ID)
643+
log.Printf("DiggerJob %v, (id: %v) has been created successfully\n", job.DiggerJobID, job.ID)
637644
return job, nil
638645
}
639646

@@ -643,21 +650,12 @@ func (db *Database) UpdateDiggerJobSummary(diggerJobId string, resourcesCreated
643650
return nil, fmt.Errorf("Could not get digger job")
644651
}
645652
var jobSummary *DiggerJobSummary
646-
if diggerJob.DiggerJobSummary == nil {
647-
jobSummary = &DiggerJobSummary{
648-
ResourcesCreated: resourcesCreated,
649-
ResourcesUpdated: resourcesUpdated,
650-
ResourcesDeleted: resourcesDeleted,
651-
}
652-
diggerJob.DiggerJobSummary = jobSummary
653-
} else {
654-
jobSummary = diggerJob.DiggerJobSummary
655-
jobSummary.ResourcesCreated = resourcesCreated
656-
jobSummary.ResourcesUpdated = resourcesUpdated
657-
jobSummary.ResourcesDeleted = resourcesDeleted
658-
}
653+
jobSummary = &diggerJob.DiggerJobSummary
654+
jobSummary.ResourcesCreated = resourcesCreated
655+
jobSummary.ResourcesUpdated = resourcesUpdated
656+
jobSummary.ResourcesDeleted = resourcesDeleted
659657

660-
result := db.GormDB.Save(jobSummary)
658+
result := db.GormDB.Save(&jobSummary)
661659
if result.Error != nil {
662660
return nil, result.Error
663661
}
@@ -671,17 +669,17 @@ func (db *Database) UpdateDiggerJob(job *DiggerJob) error {
671669
if result.Error != nil {
672670
return result.Error
673671
}
674-
log.Printf("DiggerJob %v, (id: %v) has been updated successfully\n", job.DiggerJobId, job.ID)
672+
log.Printf("DiggerJob %v, (id: %v) has been updated successfully\n", job.DiggerJobID, job.ID)
675673
return nil
676674
}
677675

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

681679
var where *gorm.DB
682-
where = db.GormDB.Preload("Batch").Preload("DiggerJobSummary").Where("digger_jobs.batch_id = ?", batchId)
680+
where = db.GormDB.Where("digger_jobs.batch_id = ?", batchId)
683681

684-
result := where.Find(&jobs)
682+
result := where.Preload("Batch").Preload("DiggerJobSummary").Find(&jobs)
685683
if result.Error != nil {
686684
if !errors.Is(result.Error, gorm.ErrRecordNotFound) {
687685
return nil, result.Error

backend/models/storage_test.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package models
22

33
import (
4+
"github.com/diggerhq/digger/libs/orchestrator/scheduler"
45
"github.com/stretchr/testify/assert"
56
"gorm.io/driver/sqlite"
67
"gorm.io/gorm"
8+
"gorm.io/gorm/logger"
79
"log"
810
"os"
911
"strings"
@@ -25,7 +27,9 @@ func setupSuite(tb testing.TB) (func(tb testing.TB), *Database, *Organisation) {
2527
}
2628

2729
// open and create a new database
28-
gdb, err := gorm.Open(sqlite.Open(dbName), &gorm.Config{})
30+
gdb, err := gorm.Open(sqlite.Open(dbName), &gorm.Config{
31+
Logger: logger.Default.LogMode(logger.Info),
32+
})
2933
if err != nil {
3034
log.Fatal(err)
3135
}
@@ -128,3 +132,42 @@ func TestGithubRepoRemoved(t *testing.T) {
128132
assert.Equal(t, i.ID, i2.ID)
129133
assert.Equal(t, GithubAppInstallDeleted, i.Status)
130134
}
135+
136+
func TestGetDiggerJobsForBatchPreloadsSummary(t *testing.T) {
137+
teardownSuite, _, _ := setupSuite(t)
138+
defer teardownSuite(t)
139+
140+
prNumber := 123
141+
repoName := "test"
142+
repoOwner := "test"
143+
repoFullName := "test/test"
144+
diggerconfig := ""
145+
branchName := "main"
146+
batchType := scheduler.BatchTypePlan
147+
commentId := int64(123)
148+
149+
batch, err := DB.CreateDiggerBatch(123, repoOwner, repoName, repoFullName, prNumber, diggerconfig, branchName, batchType, &commentId)
150+
assert.NoError(t, err)
151+
152+
job, err := DB.CreateDiggerJob(batch.ID, []byte{97})
153+
assert.NoError(t, err)
154+
155+
job, err = DB.UpdateDiggerJobSummary(job.DiggerJobId, 1, 2, 3)
156+
assert.NoError(t, err)
157+
158+
//fetchedBatch, err := DB.GetDiggerBatch(&batch.ID)
159+
//assert.NoError(t, err)
160+
//jsons, err := fetchedBatch.MapToJsonStruct()
161+
//assert.NoError(t, err)
162+
163+
//jobs := job.MapToJsonStruct()
164+
//
165+
//println(job.DiggerJobSummary.ResourcesCreated, job.DiggerJobSummary.ResourcesUpdated, job.DiggerJobSummary.ResourcesDeleted)
166+
//spew.Dump(jsons)
167+
//spew.Dump(jobs)
168+
169+
jobssss, err := DB.GetDiggerJobsForBatch(batch.ID)
170+
println(job.DiggerJobSummary.ResourcesCreated, job.DiggerJobSummary.ResourcesUpdated, job.DiggerJobSummary.ResourcesDeleted)
171+
println(jobssss[0].DiggerJobSummary.ResourcesCreated, jobssss[0].DiggerJobSummary.ResourcesUpdated, jobssss[0].DiggerJobSummary.ResourcesDeleted)
172+
173+
}

0 commit comments

Comments
 (0)