testhub/server_test.go

96 lines
2 KiB
Go
Raw Normal View History

2022-05-25 12:04:53 +02:00
package main
import (
2022-05-25 18:09:54 +02:00
"encoding/json"
"fmt"
"io"
2022-05-25 12:04:53 +02:00
"net/http"
"net/http/httptest"
2022-05-25 18:09:54 +02:00
"reflect"
2022-05-25 12:04:53 +02:00
"testing"
2022-05-26 12:30:53 +02:00
"git.andreafazzi.eu/andrea/testhub/logger"
2022-05-25 19:24:12 +02:00
"git.andreafazzi.eu/andrea/testhub/models"
2022-06-16 17:21:04 +02:00
"git.andreafazzi.eu/andrea/testhub/store"
2022-05-25 12:04:53 +02:00
"github.com/remogatto/prettytest"
)
type testSuite struct {
prettytest.Suite
}
2022-06-17 16:02:58 +02:00
type TestResponse struct {
Status string `json:"success"`
Content []*models.Quiz `json:"content"`
}
2022-05-26 13:58:17 +02:00
type StubTestHubCollectorStore struct {
2022-06-17 16:02:58 +02:00
tests []*models.Quiz
2022-05-26 13:58:17 +02:00
}
2022-05-25 12:04:53 +02:00
2022-06-17 16:02:58 +02:00
func (store *StubTestHubCollectorStore) CreateQuiz(test *store.CreateQuizRequest) *models.Quiz {
2022-05-26 09:53:08 +02:00
return nil
2022-05-25 12:04:53 +02:00
}
2022-06-17 16:02:58 +02:00
func (store *StubTestHubCollectorStore) ReadAllQuizzes() ([]*models.Quiz, error) {
return store.tests, nil
2022-05-25 18:09:54 +02:00
}
2022-05-25 12:04:53 +02:00
func TestRunner(t *testing.T) {
prettytest.Run(
t,
new(testSuite),
2022-05-26 13:58:17 +02:00
new(integrationTestSuite),
2022-05-25 12:04:53 +02:00
)
}
2022-05-26 12:30:53 +02:00
func (t *testSuite) BeforeAll() {
logger.SetLevel(logger.Disabled)
}
2022-05-26 07:59:55 +02:00
func (t *testSuite) TestGETQuestions() {
2022-06-17 16:02:58 +02:00
expectedResult := &TestResponse{
Status: "success",
Content: []*models.Quiz{
{
Question: &models.Question{ID: "1", Text: "Question 1"},
Answers: []*models.Answer{{}, {}, {}},
},
2022-05-26 12:30:53 +02:00
},
}
2022-05-25 18:09:54 +02:00
2022-06-17 16:02:58 +02:00
store := &StubTestHubCollectorStore{[]*models.Quiz{
2022-05-26 13:58:17 +02:00
{
2022-06-16 17:21:04 +02:00
Question: &models.Question{ID: "1", Text: "Question 1"},
Answers: []*models.Answer{{}, {}, {}},
2022-05-26 13:58:17 +02:00
},
}}
2022-05-25 18:09:54 +02:00
2022-06-17 16:02:58 +02:00
server := NewQuizHubCollectorServer(store)
2022-05-25 18:09:54 +02:00
2022-06-16 17:21:04 +02:00
request, _ := http.NewRequest(http.MethodGet, "/tests", nil)
2022-05-25 18:09:54 +02:00
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
2022-06-17 16:02:58 +02:00
result := getResponse(response.Body)
t.True(result.Status == expectedResult.Status)
t.True(testsAreEqual(result, expectedResult))
2022-05-25 18:09:54 +02:00
t.Equal(http.StatusOK, response.Code)
}
2022-06-17 16:02:58 +02:00
func getResponse(body io.Reader) (response *TestResponse) {
err := json.NewDecoder(body).Decode(&response)
2022-05-25 18:09:54 +02:00
if err != nil {
2022-06-16 17:21:04 +02:00
panic(fmt.Errorf("Unable to parse response from server %q into slice of Test, '%v'", body, err))
2022-05-25 18:09:54 +02:00
}
return
}
2022-06-17 16:02:58 +02:00
func testsAreEqual(got, want *TestResponse) bool {
2022-05-25 18:09:54 +02:00
return reflect.DeepEqual(got, want)
}