Rename Test->Quiz

This commit is contained in:
Andrea Fazzi 2022-06-23 11:10:41 +02:00
parent 6dea137d7a
commit db9a907d06
7 changed files with 22 additions and 53 deletions

View file

@ -5,6 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"git.andreafazzi.eu/andrea/testhub/client"
"git.andreafazzi.eu/andrea/testhub/logger" "git.andreafazzi.eu/andrea/testhub/logger"
"git.andreafazzi.eu/andrea/testhub/models" "git.andreafazzi.eu/andrea/testhub/models"
"git.andreafazzi.eu/andrea/testhub/store" "git.andreafazzi.eu/andrea/testhub/store"
@ -17,18 +18,13 @@ type QuizHubCollectorServer struct {
http.Handler http.Handler
} }
type Response struct {
Status string `json:"success"`
Content interface{} `json:"content"`
}
func NewQuizHubCollectorServer(store store.QuizHubCollectorStore) *QuizHubCollectorServer { func NewQuizHubCollectorServer(store store.QuizHubCollectorStore) *QuizHubCollectorServer {
ps := new(QuizHubCollectorServer) ps := new(QuizHubCollectorServer)
ps.store = store ps.store = store
router := http.NewServeMux() router := http.NewServeMux()
router.Handle("/tests", logger.WithLogging(http.HandlerFunc(ps.testHandler))) router.Handle("/quizzes", logger.WithLogging(http.HandlerFunc(ps.testHandler)))
ps.Handler = router ps.Handler = router
@ -39,7 +35,7 @@ func (ps *QuizHubCollectorServer) testHandler(w http.ResponseWriter, r *http.Req
switch r.Method { switch r.Method {
case http.MethodGet: case http.MethodGet:
w.Header().Set("content-type", jsonContentType) 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: case http.MethodPost:
w.WriteHeader(http.StatusAccepted) 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() tests, err := ps.store.ReadAllQuizzes()
if err != nil { 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 { 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 { if err != nil {
panic(err) panic(err)
} }
createQuizReq := new(store.CreateQuizRequest) createQuizReq := new(client.CreateQuizRequest)
err = json.Unmarshal(body, &createQuizReq) err = json.Unmarshal(body, &createQuizReq)
if err != nil { if err != nil {
panic(err) panic(err)

View file

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

View file

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

View file

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

View file

@ -1,24 +1,11 @@
package store package store
import ( import (
"git.andreafazzi.eu/andrea/testhub/client"
"git.andreafazzi.eu/andrea/testhub/models" "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 { type QuizHubCollectorStore interface {
ReadAllQuizzes() ([]*models.Quiz, error) ReadAllQuizzes() ([]*models.Quiz, error)
CreateQuiz(r *CreateQuizRequest) *models.Quiz CreateQuiz(r *client.CreateQuizRequest) *models.Quiz
} }

View file

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

View file

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