Compare commits
No commits in common. "master" and "test_model" have entirely different histories.
master
...
test_model
10 changed files with 45 additions and 53 deletions
|
@ -1,27 +0,0 @@
|
||||||
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 +0,0 @@
|
||||||
Subproject commit c4d136df36ed12f61766d587a749117b8d7b8093
|
|
18
server.go
18
server.go
|
@ -5,7 +5,6 @@ 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"
|
||||||
|
@ -18,13 +17,18 @@ 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("/quizzes", logger.WithLogging(http.HandlerFunc(ps.testHandler)))
|
router.Handle("/tests", logger.WithLogging(http.HandlerFunc(ps.testHandler)))
|
||||||
|
|
||||||
ps.Handler = router
|
ps.Handler = router
|
||||||
|
|
||||||
|
@ -35,7 +39,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.readAllQuizzes(w, r))
|
json.NewEncoder(w).Encode(ps.readAllQuizs(w, r))
|
||||||
|
|
||||||
case http.MethodPost:
|
case http.MethodPost:
|
||||||
w.WriteHeader(http.StatusAccepted)
|
w.WriteHeader(http.StatusAccepted)
|
||||||
|
@ -43,12 +47,12 @@ func (ps *QuizHubCollectorServer) testHandler(w http.ResponseWriter, r *http.Req
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ps *QuizHubCollectorServer) readAllQuizzes(w http.ResponseWriter, r *http.Request) *client.Response {
|
func (ps *QuizHubCollectorServer) readAllQuizs(w http.ResponseWriter, r *http.Request) *Response {
|
||||||
tests, err := ps.store.ReadAllQuizzes()
|
tests, err := ps.store.ReadAllQuizzes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &client.Response{Status: "error", Content: err.Error()}
|
return &Response{"error", err.Error()}
|
||||||
}
|
}
|
||||||
return &client.Response{Status: "success", Content: tests}
|
return &Response{"success", 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 {
|
||||||
|
@ -56,7 +60,7 @@ func (ps *QuizHubCollectorServer) createQuiz(w http.ResponseWriter, r *http.Requ
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
createQuizReq := new(client.CreateQuizRequest)
|
createQuizReq := new(store.CreateQuizRequest)
|
||||||
err = json.Unmarshal(body, &createQuizReq)
|
err = json.Unmarshal(body, &createQuizReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
|
@ -6,7 +6,6 @@ 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"
|
||||||
|
@ -16,7 +15,7 @@ type integrationTestSuite struct {
|
||||||
prettytest.Suite
|
prettytest.Suite
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *integrationTestSuite) TestQuizCreateAndReadAll() {
|
func (t *integrationTestSuite) TestCreateAndReadAllQuizzes() {
|
||||||
server := NewQuizHubCollectorServer(store.NewMemoryQuizHubCollectorStore())
|
server := NewQuizHubCollectorServer(store.NewMemoryQuizHubCollectorStore())
|
||||||
|
|
||||||
// POST a new question using a JSON payload
|
// POST a new question using a JSON payload
|
||||||
|
@ -32,7 +31,7 @@ func (t *integrationTestSuite) TestQuizCreateAndReadAll() {
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
request, _ := http.NewRequest(http.MethodPost, "/quizzes", strings.NewReader(payload))
|
request, _ := http.NewRequest(http.MethodPost, "/tests", strings.NewReader(payload))
|
||||||
response := httptest.NewRecorder()
|
response := httptest.NewRecorder()
|
||||||
|
|
||||||
server.ServeHTTP(response, request)
|
server.ServeHTTP(response, request)
|
||||||
|
@ -52,12 +51,12 @@ func (t *integrationTestSuite) TestQuizCreateAndReadAll() {
|
||||||
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, "/quizzes", nil)
|
request, _ = http.NewRequest(http.MethodGet, "/tests", nil)
|
||||||
response = httptest.NewRecorder()
|
response = httptest.NewRecorder()
|
||||||
|
|
||||||
server.ServeHTTP(response, request)
|
server.ServeHTTP(response, request)
|
||||||
|
|
||||||
decodedResponse := new(client.QuizReadAllResponse)
|
decodedResponse := new(TestResponse)
|
||||||
|
|
||||||
err = json.Unmarshal(response.Body.Bytes(), &decodedResponse)
|
err = json.Unmarshal(response.Body.Bytes(), &decodedResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -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,11 +19,16 @@ 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 *client.CreateQuizRequest) *models.Quiz {
|
func (store *StubTestHubCollectorStore) CreateQuiz(test *store.CreateQuizRequest) *models.Quiz {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,7 +49,7 @@ func (t *testSuite) BeforeAll() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *testSuite) TestGETQuestions() {
|
func (t *testSuite) TestGETQuestions() {
|
||||||
expectedResult := &client.QuizReadAllResponse{
|
expectedResult := &TestResponse{
|
||||||
Status: "success",
|
Status: "success",
|
||||||
Content: []*models.Quiz{
|
Content: []*models.Quiz{
|
||||||
{
|
{
|
||||||
|
@ -63,7 +68,7 @@ func (t *testSuite) TestGETQuestions() {
|
||||||
|
|
||||||
server := NewQuizHubCollectorServer(store)
|
server := NewQuizHubCollectorServer(store)
|
||||||
|
|
||||||
request, _ := http.NewRequest(http.MethodGet, "/quizzes", nil)
|
request, _ := http.NewRequest(http.MethodGet, "/tests", nil)
|
||||||
response := httptest.NewRecorder()
|
response := httptest.NewRecorder()
|
||||||
|
|
||||||
server.ServeHTTP(response, request)
|
server.ServeHTTP(response, request)
|
||||||
|
@ -76,7 +81,7 @@ func (t *testSuite) TestGETQuestions() {
|
||||||
t.Equal(http.StatusOK, response.Code)
|
t.Equal(http.StatusOK, response.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getResponse(body io.Reader) (response *client.QuizReadAllResponse) {
|
func getResponse(body io.Reader) (response *TestResponse) {
|
||||||
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))
|
||||||
|
@ -85,6 +90,6 @@ func getResponse(body io.Reader) (response *client.QuizReadAllResponse) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func testsAreEqual(got, want *client.QuizReadAllResponse) bool {
|
func testsAreEqual(got, want *TestResponse) bool {
|
||||||
return reflect.DeepEqual(got, want)
|
return reflect.DeepEqual(got, want)
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"git.andreafazzi.eu/andrea/testhub/client"
|
|
||||||
"git.andreafazzi.eu/andrea/testhub/models"
|
"git.andreafazzi.eu/andrea/testhub/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -40,7 +39,7 @@ func (s *MemoryQuizHubCollectorStore) ReadAllQuizzes() ([]*models.Quiz, error) {
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *MemoryQuizHubCollectorStore) CreateQuiz(r *client.CreateQuizRequest) *models.Quiz {
|
func (s *MemoryQuizHubCollectorStore) CreateQuiz(r *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]
|
||||||
|
|
|
@ -1,11 +1,24 @@
|
||||||
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 *client.CreateQuizRequest) *models.Quiz
|
CreateQuiz(r *CreateQuizRequest) *models.Quiz
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
POST http://localhost:3000/quizzes
|
POST http://localhost:3000/tests
|
||||||
{
|
{
|
||||||
"question": {"text": "Text of Question 1"},
|
"question": {"text": "Test of Question 1"},
|
||||||
"answers": [
|
"answers": [
|
||||||
{"text": "Text of the answer 1", "correct": true},
|
{"text": "Text of the answer 1", "correct": true},
|
||||||
{"text": "Text of the answer 2"},
|
{"text": "Text of the answer 2"},
|
1
tests/hurl/get_all_tests.hurl
Normal file
1
tests/hurl/get_all_tests.hurl
Normal file
|
@ -0,0 +1 @@
|
||||||
|
GET http://localhost:3000/tests
|
|
@ -1 +0,0 @@
|
||||||
GET http://localhost:3000/quizzes
|
|
Loading…
Reference in a new issue