Compare commits

..

2 commits

Author SHA1 Message Date
8fa1b803b0 Add hurl test and logseq plugin 2022-06-23 11:12:00 +02:00
db9a907d06 Rename Test->Quiz 2022-06-23 11:10:41 +02:00
10 changed files with 53 additions and 45 deletions

27
client/client.go Normal file
View file

@ -0,0 +1,27 @@
package client
import "git.andreafazzi.eu/andrea/testhub/models"
type Response struct {
Status string `json:"status"`
Content interface{} `json:"content"`
}
type QuizReadAllResponse struct {
Status string `json:"status"`
Content []*models.Quiz `json:"content"`
}
type CreateQuestionRequest struct {
Text string `json:"text"`
}
type CreateAnswerRequest struct {
Text string
Correct bool
}
type CreateQuizRequest struct {
Question *CreateQuestionRequest `json:"question"`
Answers []*CreateAnswerRequest `json:"answers"`
}

1
misc/logseq Submodule

@ -0,0 +1 @@
Subproject commit c4d136df36ed12f61766d587a749117b8d7b8093

View file

@ -5,6 +5,7 @@ import (
"io/ioutil"
"net/http"
"git.andreafazzi.eu/andrea/testhub/client"
"git.andreafazzi.eu/andrea/testhub/logger"
"git.andreafazzi.eu/andrea/testhub/models"
"git.andreafazzi.eu/andrea/testhub/store"
@ -17,18 +18,13 @@ type QuizHubCollectorServer struct {
http.Handler
}
type Response struct {
Status string `json:"success"`
Content interface{} `json:"content"`
}
func NewQuizHubCollectorServer(store store.QuizHubCollectorStore) *QuizHubCollectorServer {
ps := new(QuizHubCollectorServer)
ps.store = store
router := http.NewServeMux()
router.Handle("/tests", logger.WithLogging(http.HandlerFunc(ps.testHandler)))
router.Handle("/quizzes", logger.WithLogging(http.HandlerFunc(ps.testHandler)))
ps.Handler = router
@ -39,7 +35,7 @@ func (ps *QuizHubCollectorServer) testHandler(w http.ResponseWriter, r *http.Req
switch r.Method {
case http.MethodGet:
w.Header().Set("content-type", jsonContentType)
json.NewEncoder(w).Encode(ps.readAllQuizs(w, r))
json.NewEncoder(w).Encode(ps.readAllQuizzes(w, r))
case http.MethodPost:
w.WriteHeader(http.StatusAccepted)
@ -47,12 +43,12 @@ func (ps *QuizHubCollectorServer) testHandler(w http.ResponseWriter, r *http.Req
}
}
func (ps *QuizHubCollectorServer) readAllQuizs(w http.ResponseWriter, r *http.Request) *Response {
func (ps *QuizHubCollectorServer) readAllQuizzes(w http.ResponseWriter, r *http.Request) *client.Response {
tests, err := ps.store.ReadAllQuizzes()
if err != nil {
return &Response{"error", err.Error()}
return &client.Response{Status: "error", Content: err.Error()}
}
return &Response{"success", tests}
return &client.Response{Status: "success", Content: tests}
}
func (ps *QuizHubCollectorServer) createQuiz(w http.ResponseWriter, r *http.Request) *models.Quiz {
@ -60,7 +56,7 @@ func (ps *QuizHubCollectorServer) createQuiz(w http.ResponseWriter, r *http.Requ
if err != nil {
panic(err)
}
createQuizReq := new(store.CreateQuizRequest)
createQuizReq := new(client.CreateQuizRequest)
err = json.Unmarshal(body, &createQuizReq)
if err != nil {
panic(err)

View file

@ -6,6 +6,7 @@ import (
"net/http/httptest"
"strings"
"git.andreafazzi.eu/andrea/testhub/client"
"git.andreafazzi.eu/andrea/testhub/models"
"git.andreafazzi.eu/andrea/testhub/store"
"github.com/remogatto/prettytest"
@ -15,7 +16,7 @@ type integrationTestSuite struct {
prettytest.Suite
}
func (t *integrationTestSuite) TestCreateAndReadAllQuizzes() {
func (t *integrationTestSuite) TestQuizCreateAndReadAll() {
server := NewQuizHubCollectorServer(store.NewMemoryQuizHubCollectorStore())
// POST a new question using a JSON payload
@ -31,7 +32,7 @@ func (t *integrationTestSuite) TestCreateAndReadAllQuizzes() {
]
}
`
request, _ := http.NewRequest(http.MethodPost, "/tests", strings.NewReader(payload))
request, _ := http.NewRequest(http.MethodPost, "/quizzes", strings.NewReader(payload))
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
@ -51,12 +52,12 @@ func (t *integrationTestSuite) TestCreateAndReadAllQuizzes() {
t.True(returnedTest.Question.ID != "", "Question ID should not be empty")
t.True(returnedTest.Answers[0].ID != "", "Answer ID should not be empty")
request, _ = http.NewRequest(http.MethodGet, "/tests", nil)
request, _ = http.NewRequest(http.MethodGet, "/quizzes", nil)
response = httptest.NewRecorder()
server.ServeHTTP(response, request)
decodedResponse := new(TestResponse)
decodedResponse := new(client.QuizReadAllResponse)
err = json.Unmarshal(response.Body.Bytes(), &decodedResponse)
if err != nil {

View file

@ -9,9 +9,9 @@ import (
"reflect"
"testing"
"git.andreafazzi.eu/andrea/testhub/client"
"git.andreafazzi.eu/andrea/testhub/logger"
"git.andreafazzi.eu/andrea/testhub/models"
"git.andreafazzi.eu/andrea/testhub/store"
"github.com/remogatto/prettytest"
)
@ -19,16 +19,11 @@ type testSuite struct {
prettytest.Suite
}
type TestResponse struct {
Status string `json:"success"`
Content []*models.Quiz `json:"content"`
}
type StubTestHubCollectorStore struct {
tests []*models.Quiz
}
func (store *StubTestHubCollectorStore) CreateQuiz(test *store.CreateQuizRequest) *models.Quiz {
func (store *StubTestHubCollectorStore) CreateQuiz(test *client.CreateQuizRequest) *models.Quiz {
return nil
}
@ -49,7 +44,7 @@ func (t *testSuite) BeforeAll() {
}
func (t *testSuite) TestGETQuestions() {
expectedResult := &TestResponse{
expectedResult := &client.QuizReadAllResponse{
Status: "success",
Content: []*models.Quiz{
{
@ -68,7 +63,7 @@ func (t *testSuite) TestGETQuestions() {
server := NewQuizHubCollectorServer(store)
request, _ := http.NewRequest(http.MethodGet, "/tests", nil)
request, _ := http.NewRequest(http.MethodGet, "/quizzes", nil)
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
@ -81,7 +76,7 @@ func (t *testSuite) TestGETQuestions() {
t.Equal(http.StatusOK, response.Code)
}
func getResponse(body io.Reader) (response *TestResponse) {
func getResponse(body io.Reader) (response *client.QuizReadAllResponse) {
err := json.NewDecoder(body).Decode(&response)
if err != nil {
panic(fmt.Errorf("Unable to parse response from server %q into slice of Test, '%v'", body, err))
@ -90,6 +85,6 @@ func getResponse(body io.Reader) (response *TestResponse) {
return
}
func testsAreEqual(got, want *TestResponse) bool {
func testsAreEqual(got, want *client.QuizReadAllResponse) bool {
return reflect.DeepEqual(got, want)
}

View file

@ -5,6 +5,7 @@ import (
"fmt"
"sync"
"git.andreafazzi.eu/andrea/testhub/client"
"git.andreafazzi.eu/andrea/testhub/models"
)
@ -39,7 +40,7 @@ func (s *MemoryQuizHubCollectorStore) ReadAllQuizzes() ([]*models.Quiz, error) {
return result, nil
}
func (s *MemoryQuizHubCollectorStore) CreateQuiz(r *CreateQuizRequest) *models.Quiz {
func (s *MemoryQuizHubCollectorStore) CreateQuiz(r *client.CreateQuizRequest) *models.Quiz {
questionID := hash(r.Question.Text)
test := new(models.Quiz)
q, ok := s.questions[questionID]

View file

@ -1,24 +1,11 @@
package store
import (
"git.andreafazzi.eu/andrea/testhub/client"
"git.andreafazzi.eu/andrea/testhub/models"
)
type CreateQuestionRequest struct {
Text string `json:"text"`
}
type CreateAnswerRequest struct {
Text string
Correct bool
}
type CreateQuizRequest struct {
Question *CreateQuestionRequest `json:"question"`
Answers []*CreateAnswerRequest `json:"answers"`
}
type QuizHubCollectorStore interface {
ReadAllQuizzes() ([]*models.Quiz, error)
CreateQuiz(r *CreateQuizRequest) *models.Quiz
CreateQuiz(r *client.CreateQuizRequest) *models.Quiz
}

View file

@ -1,6 +1,6 @@
POST http://localhost:3000/tests
POST http://localhost:3000/quizzes
{
"question": {"text": "Test of Question 1"},
"question": {"text": "Text of Question 1"},
"answers": [
{"text": "Text of the answer 1", "correct": true},
{"text": "Text of the answer 2"},

View file

@ -1 +0,0 @@
GET http://localhost:3000/tests

View file

@ -0,0 +1 @@
GET http://localhost:3000/quizzes