MCPcopy
hub / github.com/keploy/keploy / ProcessTestCasesV2

Method ProcessTestCasesV2

pkg/service/tools/templatize.go:78–148  ·  view source on GitHub ↗

--- V2 Optimized Templatization Logic ---

(ctx context.Context, tcs []*models.TestCase, testSetID string)

Source from the content-addressed store, hash-verified

76
77// --- V2 Optimized Templatization Logic ---
78func (t *Tools) ProcessTestCasesV2(ctx context.Context, tcs []*models.TestCase, testSetID string) error {
79 for _, tc := range tcs {
80 tc.HTTPReq.Body = addQuotesInTemplates(tc.HTTPReq.Body)
81 tc.HTTPResp.Body = addQuotesInTemplates(tc.HTTPResp.Body)
82 }
83
84 reqBodies := make([]interface{}, len(tcs))
85 respBodies := make([]interface{}, len(tcs))
86 for i, tc := range tcs {
87 decoderReq := json.NewDecoder(strings.NewReader(tc.HTTPReq.Body))
88 decoderReq.UseNumber()
89 _ = decoderReq.Decode(&reqBodies[i])
90 decoderResp := json.NewDecoder(strings.NewReader(tc.HTTPResp.Body))
91 decoderResp.UseNumber()
92 _ = decoderResp.Decode(&respBodies[i])
93 }
94
95 valueIndex := t.buildValueIndexV2(ctx, tcs, reqBodies, respBodies)
96 chains := t.applyTemplatesFromIndexV2(ctx, valueIndex, utils.TemplatizedValues)
97
98 for i, tc := range tcs {
99 if reqBodies[i] != nil {
100 newBody, _ := json.Marshal(reqBodies[i])
101 tc.HTTPReq.Body = string(newBody)
102 }
103 if respBodies[i] != nil {
104 newBody, _ := json.Marshal(respBodies[i])
105 tc.HTTPResp.Body = string(newBody)
106 }
107 tc.HTTPReq.Body = removeQuotesInTemplates(tc.HTTPReq.Body)
108 tc.HTTPResp.Body = removeQuotesInTemplates(tc.HTTPResp.Body)
109 if err := t.testDB.UpdateTestCase(ctx, tc, testSetID, false); err != nil {
110 utils.LogError(t.logger, err, "failed to update test case")
111 return err
112 }
113 }
114
115 utils.RemoveDoubleQuotes(utils.TemplatizedValues)
116
117 var existingMetadata map[string]interface{}
118 existingTestSet, err := t.testSetConf.Read(ctx, testSetID)
119 if err == nil && existingTestSet != nil && existingTestSet.Metadata != nil {
120 existingMetadata = existingTestSet.Metadata
121 }
122
123 err = t.testSetConf.Write(ctx, testSetID, &models.TestSet{
124 PreScript: "",
125 PostScript: "",
126 Template: utils.TemplatizedValues,
127 Metadata: existingMetadata,
128 })
129 if err != nil {
130 utils.LogError(t.logger, err, "failed to write test set")
131 return err
132 }
133
134 if len(utils.SecretValues) > 0 {
135 err = utils.AddToGitIgnore(t.logger, t.config.Path, "/*/secret.yaml")

Callers 1

TemplatizeMethod · 0.95

Calls 15

buildValueIndexV2Method · 0.95
logAPIChainsMethod · 0.95
AssertChainsMethod · 0.95
LogErrorFunction · 0.92
RemoveDoubleQuotesFunction · 0.92
AddToGitIgnoreFunction · 0.92
addQuotesInTemplatesFunction · 0.85
removeQuotesInTemplatesFunction · 0.85
MarshalMethod · 0.80
UpdateTestCaseMethod · 0.65
ReadMethod · 0.65

Tested by

no test coverage detected