86 lines
1.7 KiB
Go
86 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"git.andreafazzi.eu/andrea/testhub/logger"
|
|
"git.andreafazzi.eu/andrea/testhub/models"
|
|
"git.andreafazzi.eu/andrea/testhub/store"
|
|
"github.com/remogatto/prettytest"
|
|
)
|
|
|
|
type testSuite struct {
|
|
prettytest.Suite
|
|
}
|
|
|
|
type StubTestHubCollectorStore struct {
|
|
tests []*models.Test
|
|
}
|
|
|
|
func (store *StubTestHubCollectorStore) CreateTest(test *store.CreateTestRequest) *models.Test {
|
|
return nil
|
|
}
|
|
|
|
func (store *StubTestHubCollectorStore) GetTests() []*models.Test {
|
|
return store.tests
|
|
}
|
|
|
|
func TestRunner(t *testing.T) {
|
|
prettytest.Run(
|
|
t,
|
|
new(testSuite),
|
|
new(integrationTestSuite),
|
|
)
|
|
}
|
|
|
|
func (t *testSuite) BeforeAll() {
|
|
logger.SetLevel(logger.Disabled)
|
|
}
|
|
|
|
func (t *testSuite) TestGETQuestions() {
|
|
expectedResult := []*models.Test{
|
|
{
|
|
Question: &models.Question{ID: "1", Text: "Question 1"},
|
|
Answers: []*models.Answer{{}, {}, {}},
|
|
},
|
|
}
|
|
|
|
store := &StubTestHubCollectorStore{[]*models.Test{
|
|
{
|
|
Question: &models.Question{ID: "1", Text: "Question 1"},
|
|
Answers: []*models.Answer{{}, {}, {}},
|
|
},
|
|
}}
|
|
|
|
server := NewTestHubCollectorServer(store)
|
|
|
|
request, _ := http.NewRequest(http.MethodGet, "/tests", nil)
|
|
response := httptest.NewRecorder()
|
|
|
|
server.ServeHTTP(response, request)
|
|
|
|
result := getTestsFromResponse(response.Body)
|
|
|
|
t.True(testsAreEqual(expectedResult, result))
|
|
t.Equal(http.StatusOK, response.Code)
|
|
}
|
|
|
|
func getTestsFromResponse(body io.Reader) (tests []*models.Test) {
|
|
err := json.NewDecoder(body).Decode(&tests)
|
|
|
|
if err != nil {
|
|
panic(fmt.Errorf("Unable to parse response from server %q into slice of Test, '%v'", body, err))
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func testsAreEqual(got, want []*models.Test) bool {
|
|
return reflect.DeepEqual(got, want)
|
|
}
|