Skip to content

Commit 15bc201

Browse files
committed
Merge branch 'feature/handle-vectr-bugs'
2 parents dddca48 + 4b824bb commit 15bc201

File tree

1 file changed

+85
-13
lines changed

1 file changed

+85
-13
lines changed

restore.go

Lines changed: 85 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,76 @@ var outcomeStatusMap map[string]dao.TestCaseStatus = map[string]dao.TestCaseStat
5353
"Not Performed": dao.TestCaseStatusNotperformed,
5454
}
5555

56+
func NewGroupedCreateTestCaseWithLibraryIdInput(td dao.CreateTestCaseMatchByLibraryIdInput) *GroupedCreateTestCaseWithLibraryIdInput {
57+
return &GroupedCreateTestCaseWithLibraryIdInput{
58+
Base: dao.CreateTestCaseMatchByLibraryIdInput{
59+
Db: td.Db,
60+
CampaignId: td.CampaignId,
61+
},
62+
TestCases: make(map[LibraryTestCaseIdIndex][]dao.CreateTestCaseDataWithLibraryIdInput),
63+
}
64+
}
65+
66+
type LibraryTestCaseIdIndex string
67+
68+
// This is an object that will handle creating objects for campaigns, instead of the default
69+
// This allows us to split out the requests so we don't have one requests with multiple library
70+
// test case ids
71+
type GroupedCreateTestCaseWithLibraryIdInput struct {
72+
Base dao.CreateTestCaseMatchByLibraryIdInput
73+
TestCases map[LibraryTestCaseIdIndex][]dao.CreateTestCaseDataWithLibraryIdInput
74+
}
75+
76+
func (g *GroupedCreateTestCaseWithLibraryIdInput) Add(tcd dao.CreateTestCaseDataWithLibraryIdInput) {
77+
if _, ok := g.TestCases[LibraryTestCaseIdIndex(tcd.LibraryTestCaseId)]; !ok {
78+
g.TestCases[LibraryTestCaseIdIndex(tcd.LibraryTestCaseId)] = make([]dao.CreateTestCaseDataWithLibraryIdInput, 0, 5)
79+
}
80+
81+
g.TestCases[LibraryTestCaseIdIndex(tcd.LibraryTestCaseId)] = append(g.TestCases[LibraryTestCaseIdIndex(tcd.LibraryTestCaseId)], tcd)
82+
}
83+
84+
func (g *GroupedCreateTestCaseWithLibraryIdInput) Len() int {
85+
size := 0
86+
87+
for _, tcs := range g.TestCases {
88+
size += len(tcs)
89+
}
90+
91+
return size
92+
}
93+
94+
func (g *GroupedCreateTestCaseWithLibraryIdInput) GenerateInsertsData() []dao.CreateTestCaseMatchByLibraryIdInput {
95+
maxSize := 0
96+
97+
for _, testcases := range g.TestCases {
98+
if len(testcases) > maxSize {
99+
maxSize = len(testcases)
100+
}
101+
}
102+
103+
if maxSize == 0 {
104+
return nil
105+
}
106+
107+
results := make([]dao.CreateTestCaseMatchByLibraryIdInput, 0, maxSize)
108+
109+
for i := 0; i < maxSize; i++ {
110+
var obj dao.CreateTestCaseMatchByLibraryIdInput
111+
obj.Db = g.Base.Db
112+
obj.CampaignId = g.Base.CampaignId
113+
obj.CreateTestCaseInputs = []dao.CreateTestCaseDataWithLibraryIdInput{}
114+
for _, testcases := range g.TestCases {
115+
if len(testcases) > (i) {
116+
obj.CreateTestCaseInputs = append(obj.CreateTestCaseInputs, testcases[i])
117+
} else {
118+
continue
119+
}
120+
}
121+
results = append(results, obj)
122+
}
123+
return results
124+
}
125+
56126
// RestoreAssessment restores an assessment to a VECTR instance by deserializing
57127
// and importing serialized assessment data. It ensures that all required
58128
// organizations, tools, and templates exist in the target instance before
@@ -432,11 +502,11 @@ func RestoreAssessment(ctx context.Context, client graphql.Client, db string, ad
432502
testCaseCount := 0
433503
for _, c := range ad.Assessment.Campaigns {
434504
// there could be a mix of test case types in a campaign, so add both types in
435-
tc_with_library := dao.CreateTestCaseMatchByLibraryIdInput{
505+
tc_with_library := NewGroupedCreateTestCaseWithLibraryIdInput(dao.CreateTestCaseMatchByLibraryIdInput{
436506
Db: db,
437507
CampaignId: campaign_map[c.Name],
438508
CreateTestCaseInputs: []dao.CreateTestCaseDataWithLibraryIdInput{},
439-
}
509+
})
440510

441511
tc_no_template := dao.CreateTestCaseWithoutTemplateInput{
442512
Db: db,
@@ -540,24 +610,26 @@ func RestoreAssessment(ctx context.Context, client graphql.Client, db string, ad
540610
CreateNewIfNotExists: false,
541611
TestCaseData: testCaseData,
542612
}
543-
tc_with_library.CreateTestCaseInputs = append(tc_with_library.CreateTestCaseInputs, tcd)
544-
613+
tc_with_library.Add(tcd)
545614
}
546615
}
547616
slog.DebugContext(ctx, "Creating test cases",
548617
"campaign_name", c.Name,
549-
"test_case_count", len(tc_with_library.CreateTestCaseInputs),
618+
"test_case_count", tc_with_library.Len(),
550619
"test-case-count-no-template", len(tc_no_template.TestCaseData),
551620
"assessment_name", ad.Assessment.Name)
552-
if len(tc_with_library.CreateTestCaseInputs) > 0 {
553-
_, err := dao.CreateTestCasesByLibraryId(ctx, client, tc_with_library)
554-
if err != nil {
555-
if gqlObject, ok := gqlErrParse(err); ok {
556-
slog.ErrorContext(ctx, "detailed error", "error", gqlObject)
621+
if tc_with_library.Len() > 0 {
622+
inserts := tc_with_library.GenerateInsertsData()
623+
for _, insertdata := range inserts {
624+
_, err := dao.CreateTestCasesByLibraryId(ctx, client, insertdata)
625+
if err != nil {
626+
if gqlObject, ok := gqlErrParse(err); ok {
627+
slog.ErrorContext(ctx, "detailed error", "error", gqlObject)
628+
}
629+
return fmt.Errorf("could not write test cases for %s, campaign: %s; check vectr version: %w", ad.Assessment.Name, c.Name, err)
557630
}
558-
return fmt.Errorf("could not write test cases for %s, campaign: %s; check vectr version: %w", ad.Assessment.Name, c.Name, err)
631+
testCaseCount += len(insertdata.CreateTestCaseInputs)
559632
}
560-
testCaseCount += len(tc_with_library.CreateTestCaseInputs)
561633
}
562634
if len(tc_no_template.TestCaseData) > 0 {
563635
_, err := dao.CreateTestCasesNoTemplate(ctx, client, tc_no_template)
@@ -567,7 +639,7 @@ func RestoreAssessment(ctx context.Context, client graphql.Client, db string, ad
567639
}
568640
return fmt.Errorf("could not write test cases for %s: %w", ad.Assessment.Name, err)
569641
}
570-
testCaseCount += len(tc_with_library.CreateTestCaseInputs)
642+
testCaseCount += len(tc_no_template.TestCaseData)
571643
}
572644
}
573645
slog.InfoContext(ctx, "Test cases created", "assessment-name", ad.Assessment.Name, "test-case-count", testCaseCount)

0 commit comments

Comments
 (0)