Skip to content

Commit 8a31a6f

Browse files
committed
Add delete and update tests
1 parent b3e1d61 commit 8a31a6f

File tree

6 files changed

+70
-1
lines changed

6 files changed

+70
-1
lines changed

apps/execution-service/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,14 @@ curl -X PUT http://localhost:8083/tests/{questionDocRefId} \
134134
}'
135135
```
136136

137+
`DELETE /tests/{questionDocRefId}`
138+
139+
To delete an existing test case from an existing question, run the following command:
140+
137141
```bash
142+
curl -X DELETE http://localhost:8083/tests/{questionDocRefId} \
143+
-H "Content-Type: application/json"
144+
```
138145

139146
`POST /tests/{questionDocRefId}/execute`
140147

apps/execution-service/handlers/create.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ func (s *Service) CreateTest(w http.ResponseWriter, r *http.Request) {
2424
return
2525
}
2626

27+
// Normalise test cases
28+
test.VisibleTestCases = utils.NormaliseTestCaseFormat(test.VisibleTestCases)
29+
test.HiddenTestCases = utils.NormaliseTestCaseFormat(test.HiddenTestCases)
30+
2731
// Automatically populate validation for input and output in test case
2832
test.InputValidation = utils.GetDefaultValidation()
2933
test.OutputValidation = utils.GetDefaultValidation()
@@ -54,6 +58,7 @@ func (s *Service) CreateTest(w http.ResponseWriter, r *http.Request) {
5458
http.Error(w, "Test already exists for the question", http.StatusConflict)
5559
return
5660
}
61+
defer iter.Stop()
5762

5863
// Save test to Firestore
5964
docRef, _, err := s.Client.Collection("tests").Add(ctx, map[string]interface{}{
@@ -103,3 +108,12 @@ func (s *Service) CreateTest(w http.ResponseWriter, r *http.Request) {
103108
//"visibleTestCases": "2\nhello\nolleh\nHannah\nhannaH",
104109
//"hiddenTestCases": "2\nHannah\nhannaH\nabcdefg\ngfedcba"
105110
//}'
111+
112+
//curl -X POST http://localhost:8083/tests \
113+
//-H "Content-Type: application/json" \
114+
//-d "{
115+
//\"questionDocRefId\": \"sampleDocRefId12345\",
116+
//\"questionTitle\": \"Sample Question Title\",
117+
//\"visibleTestCases\": \"2\\nhello\\nolleh\\nHannah\\nhannaH\",
118+
//\"hiddenTestCases\": \"2\\nHannah\\nhannaH\\nabcdefg\\ngfedcba\"
119+
//}"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package handlers
2+
3+
import (
4+
"github.com/go-chi/chi/v5"
5+
"google.golang.org/api/iterator"
6+
"net/http"
7+
)
8+
9+
func (s *Service) DeleteTest(w http.ResponseWriter, r *http.Request) {
10+
ctx := r.Context()
11+
12+
// Parse request
13+
docRefID := chi.URLParam(r, "questionDocRefId")
14+
15+
docRef := s.Client.Collection("tests").Where("questionDocRefId", "==", docRefID).Limit(1).Documents(ctx)
16+
doc, err := docRef.Next()
17+
if err != nil {
18+
if err == iterator.Done {
19+
http.Error(w, "Test not found", http.StatusNotFound)
20+
return
21+
}
22+
http.Error(w, err.Error(), http.StatusInternalServerError)
23+
return
24+
}
25+
defer docRef.Stop()
26+
27+
_, err = doc.Ref.Delete(ctx)
28+
if err != nil {
29+
http.Error(w, "Error deleting test", http.StatusInternalServerError)
30+
return
31+
}
32+
33+
w.Header().Set("Content-Type", "application/json")
34+
w.WriteHeader(http.StatusOK)
35+
}
36+
37+
// Manual test cases
38+
39+
//curl -X DELETE http://localhost:8083/tests/sampleDocRefId123 \
40+
//-H "Content-Type: application/json"

apps/execution-service/handlers/update.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func (s *Service) UpdateTest(w http.ResponseWriter, r *http.Request) {
2222
return
2323
}
2424

25+
// Normalise test cases
26+
test.VisibleTestCases = utils.NormaliseTestCaseFormat(test.VisibleTestCases)
27+
test.HiddenTestCases = utils.NormaliseTestCaseFormat(test.HiddenTestCases)
28+
2529
// Only test cases will be updated
2630
// Validate test case format with default validation
2731
if _, err := utils.ValidateTestCaseFormat(test.VisibleTestCases, utils.GetDefaultValidation(),

apps/execution-service/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func registerRoutes(r *chi.Mux, service *handlers.Service) {
9191
// Current: Unused, since testcases are executed within service and not exposed
9292
// Future extension: can be read by admin to view testcases
9393
r.Put("/", service.UpdateTest)
94-
//r.Delete("/", service.DeleteTest)
94+
r.Delete("/", service.DeleteTest)
9595
r.Get("/", service.ReadVisibleTests)
9696
r.Post("/execute", service.ExecuteVisibleAndCustomTests)
9797
r.Post("/submit", service.ExecuteVisibleAndHiddenTestsAndSubmit)

apps/execution-service/utils/validateTestCaseFormat.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,7 @@ package main
7777
result := validateFunc.Interface().(func(string) bool)(inputOrOutput)
7878
return result, nil
7979
}
80+
81+
func NormaliseTestCaseFormat(testCase string) string {
82+
return strings.ReplaceAll(testCase, "\\n", "\n")
83+
}

0 commit comments

Comments
 (0)