Skip to content

Commit

Permalink
Add delete and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
solomonng2001 committed Nov 13, 2024
1 parent b3e1d61 commit 8a31a6f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
7 changes: 7 additions & 0 deletions apps/execution-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,14 @@ curl -X PUT http://localhost:8083/tests/{questionDocRefId} \
}'
```

`DELETE /tests/{questionDocRefId}`

To delete an existing test case from an existing question, run the following command:

```bash
curl -X DELETE http://localhost:8083/tests/{questionDocRefId} \
-H "Content-Type: application/json"
```

`POST /tests/{questionDocRefId}/execute`

Expand Down
14 changes: 14 additions & 0 deletions apps/execution-service/handlers/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func (s *Service) CreateTest(w http.ResponseWriter, r *http.Request) {
return
}

// Normalise test cases
test.VisibleTestCases = utils.NormaliseTestCaseFormat(test.VisibleTestCases)
test.HiddenTestCases = utils.NormaliseTestCaseFormat(test.HiddenTestCases)

// Automatically populate validation for input and output in test case
test.InputValidation = utils.GetDefaultValidation()
test.OutputValidation = utils.GetDefaultValidation()
Expand Down Expand Up @@ -54,6 +58,7 @@ func (s *Service) CreateTest(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Test already exists for the question", http.StatusConflict)
return
}
defer iter.Stop()

// Save test to Firestore
docRef, _, err := s.Client.Collection("tests").Add(ctx, map[string]interface{}{
Expand Down Expand Up @@ -103,3 +108,12 @@ func (s *Service) CreateTest(w http.ResponseWriter, r *http.Request) {
//"visibleTestCases": "2\nhello\nolleh\nHannah\nhannaH",
//"hiddenTestCases": "2\nHannah\nhannaH\nabcdefg\ngfedcba"
//}'

//curl -X POST http://localhost:8083/tests \
//-H "Content-Type: application/json" \
//-d "{
//\"questionDocRefId\": \"sampleDocRefId12345\",
//\"questionTitle\": \"Sample Question Title\",
//\"visibleTestCases\": \"2\\nhello\\nolleh\\nHannah\\nhannaH\",
//\"hiddenTestCases\": \"2\\nHannah\\nhannaH\\nabcdefg\\ngfedcba\"
//}"
40 changes: 40 additions & 0 deletions apps/execution-service/handlers/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package handlers

import (
"github.com/go-chi/chi/v5"
"google.golang.org/api/iterator"
"net/http"
)

func (s *Service) DeleteTest(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

// Parse request
docRefID := chi.URLParam(r, "questionDocRefId")

docRef := s.Client.Collection("tests").Where("questionDocRefId", "==", docRefID).Limit(1).Documents(ctx)
doc, err := docRef.Next()
if err != nil {
if err == iterator.Done {
http.Error(w, "Test not found", http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer docRef.Stop()

_, err = doc.Ref.Delete(ctx)
if err != nil {
http.Error(w, "Error deleting test", http.StatusInternalServerError)
return
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
}

// Manual test cases

//curl -X DELETE http://localhost:8083/tests/sampleDocRefId123 \
//-H "Content-Type: application/json"
4 changes: 4 additions & 0 deletions apps/execution-service/handlers/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func (s *Service) UpdateTest(w http.ResponseWriter, r *http.Request) {
return
}

// Normalise test cases
test.VisibleTestCases = utils.NormaliseTestCaseFormat(test.VisibleTestCases)
test.HiddenTestCases = utils.NormaliseTestCaseFormat(test.HiddenTestCases)

// Only test cases will be updated
// Validate test case format with default validation
if _, err := utils.ValidateTestCaseFormat(test.VisibleTestCases, utils.GetDefaultValidation(),
Expand Down
2 changes: 1 addition & 1 deletion apps/execution-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func registerRoutes(r *chi.Mux, service *handlers.Service) {
// Current: Unused, since testcases are executed within service and not exposed
// Future extension: can be read by admin to view testcases
r.Put("/", service.UpdateTest)
//r.Delete("/", service.DeleteTest)
r.Delete("/", service.DeleteTest)
r.Get("/", service.ReadVisibleTests)
r.Post("/execute", service.ExecuteVisibleAndCustomTests)
r.Post("/submit", service.ExecuteVisibleAndHiddenTestsAndSubmit)
Expand Down
4 changes: 4 additions & 0 deletions apps/execution-service/utils/validateTestCaseFormat.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ package main
result := validateFunc.Interface().(func(string) bool)(inputOrOutput)
return result, nil
}

func NormaliseTestCaseFormat(testCase string) string {
return strings.ReplaceAll(testCase, "\\n", "\n")
}

0 comments on commit 8a31a6f

Please sign in to comment.