probo/server_test.go

91 lines
1.9 KiB
Go
Raw Normal View History

2022-06-23 11:25:35 +02:00
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
2022-06-24 18:56:06 +02:00
"git.andreafazzi.eu/andrea/probo/client"
"git.andreafazzi.eu/andrea/probo/logger"
"git.andreafazzi.eu/andrea/probo/models"
2022-06-23 11:25:35 +02:00
"github.com/remogatto/prettytest"
)
type testSuite struct {
prettytest.Suite
}
type StubTestHubCollectorStore struct {
tests []*models.Quiz
}
2022-06-24 18:56:06 +02:00
func (store *StubTestHubCollectorStore) CreateQuiz(test *client.CreateQuizRequest) (*models.Quiz, error) {
return nil, nil
2022-06-23 11:25:35 +02:00
}
func (store *StubTestHubCollectorStore) ReadAllQuizzes() ([]*models.Quiz, error) {
return store.tests, nil
}
func TestRunner(t *testing.T) {
prettytest.Run(
t,
new(testSuite),
new(integrationTestSuite),
)
}
func (t *testSuite) BeforeAll() {
logger.SetLevel(logger.Disabled)
}
func (t *testSuite) TestGETQuestions() {
2022-06-24 18:56:06 +02:00
expectedResult := &client.ReadAllQuizResponse{
2022-06-23 11:25:35 +02:00
Status: "success",
Content: []*models.Quiz{
{
Question: &models.Question{ID: "1", Text: "Question 1"},
Answers: []*models.Answer{{}, {}, {}},
},
},
}
store := &StubTestHubCollectorStore{[]*models.Quiz{
{
Question: &models.Question{ID: "1", Text: "Question 1"},
Answers: []*models.Answer{{}, {}, {}},
},
}}
server := NewQuizHubCollectorServer(store)
request, _ := http.NewRequest(http.MethodGet, "/quizzes", nil)
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
result := getResponse(response.Body)
t.True(result.Status == expectedResult.Status)
t.True(testsAreEqual(result, expectedResult))
t.Equal(http.StatusOK, response.Code)
}
2022-06-24 18:56:06 +02:00
func getResponse(body io.Reader) (response *client.ReadAllQuizResponse) {
2022-06-23 11:25:35 +02:00
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))
}
return
}
2022-06-24 18:56:06 +02:00
func testsAreEqual(got, want *client.ReadAllQuizResponse) bool {
2022-06-23 11:25:35 +02:00
return reflect.DeepEqual(got, want)
}