Skip to content
This repository has been archived by the owner on Sep 15, 2020. It is now read-only.

Commit

Permalink
fixes #16, refactoring of tests (#18)
Browse files Browse the repository at this point in the history
* use http.server

* responsechannel

* less boilerplate

* test-context

* move tests in clients

* rename

* reorganize mocks
  • Loading branch information
Elmer Bulthuis committed Aug 24, 2018
1 parent db7c104 commit b47fa36
Show file tree
Hide file tree
Showing 51 changed files with 887 additions and 947 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
steps:
- checkout
- run: go get golang.org/x/lint/golint
- run: ~/go/bin/golint -set_exit_status ./clients ./models ./selectors ./test ./utils
- run: ~/go/bin/golint -set_exit_status ./clients ./models ./selectors ./utils

spec:
docker: *docker_golang
Expand All @@ -36,7 +36,7 @@ jobs:
at: .
- run: go get github.com/jstemmer/go-junit-report
- run: mkdir -p /tmp/test-results
- run: go test ./clients ./models ./selectors ./test ./utils -v | ~/go/bin/go-junit-report > /tmp/test-results/spec.xml
- run: go test ./clients ./models ./selectors ./utils -v | ~/go/bin/go-junit-report > /tmp/test-results/spec.xml
- store_test_results: &store_test_results
path: /tmp/test-results

Expand Down
37 changes: 29 additions & 8 deletions test/api.go → clients/api.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
package test
package clients

import (
"fmt"
"net/http"
)

var port = 10000

/*
createAPITestServer creates a test server
*/
func createAPITestServer(
responseChannel chan string,
) (
server *http.Server,
) {
port++

mux := createAPITestServerMux(responseChannel)
server = &http.Server{
Handler: mux,
Addr: ":" + fmt.Sprint(port),
}

return
}

/*
CreateAPITestServerMux creates the ServeMux for a api test server
createAPITestServerMux creates the ServeMux for a api test server
*/
func CreateAPITestServerMux(
state string,
patchChannel chan string,
func createAPITestServerMux(
responseChannel chan string,
) (
mux *http.ServeMux,
) {
Expand All @@ -27,7 +47,8 @@ func CreateAPITestServerMux(
accept := r.Header.Get("Accept")
switch accept {
case "application/json":
_, err = fmt.Fprintln(w, state)
response := responseChannel
_, err = fmt.Fprintln(w, <-response)
if err != nil {
panic(err)
}
Expand All @@ -45,8 +66,8 @@ func CreateAPITestServerMux(
case <-closeChannel:
return

case patch := <-patchChannel:
_, err = fmt.Fprintln(w, patch)
case response := <-responseChannel:
_, err = fmt.Fprintln(w, response)
if err != nil {
panic(err)
}
Expand Down
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions clients/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package clients

import "testing"

func TestGameyeClient_command(t *testing.T) {
runInTestContext(t, func(ctx *testContext) (err error) {
err = ctx.Client.command("noop", struct{}{})
if err != nil {
return
}
return
})
}
File renamed without changes.
48 changes: 48 additions & 0 deletions clients/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package clients

import (
"context"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

/*
testContext provides a handy context for testing
*/
type testContext struct {
Server *http.Server
Client GameyeClient
Response chan string
}

/*
runInTestContext runs a test in a context
*/
func runInTestContext(t *testing.T, job func(*testContext) error) {
var err error
defer func() {
assert.NoError(t, err)
}()

responseChannel := make(chan string, 1)
defer close(responseChannel)

server := createAPITestServer(responseChannel)
go server.ListenAndServe()
defer server.Shutdown(context.Background())

client := NewGameyeClient(GameyeClientConfig{
Token: "",
Endpoint: "http://localhost" + server.Addr + "",
})

testContext := &testContext{
Client: client,
Server: server,
Response: responseChannel,
}

err = job(testContext)
}
20 changes: 20 additions & 0 deletions clients/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package clients

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRunInContext(t *testing.T) {
runInTestContext(t, func(ctx *testContext) (err error) {
var res *http.Response
res, err = http.Get("http://localhost" + ctx.Server.Addr + "/noop")
if err != nil {
return
}
assert.Equal(t, 204, res.StatusCode)
return
})
}
File renamed without changes.
File renamed without changes.
51 changes: 51 additions & 0 deletions clients/game_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package clients

import (
"testing"

"github.com/Gameye/gameye-sdk-go/models"
"github.com/stretchr/testify/assert"
)

func TestGameyeClient_SubscribeGame(t *testing.T) {
runInTestContext(t, func(ctx *testContext) (err error) {
var sub *GameQuerySubscription
sub, err = ctx.Client.SubscribeGame()
if err != nil {
return
}
defer sub.Cancel()

{
ctx.Response <- `[{"path":[],"value":{"game":{},"location":{}}}]`
var state *models.GameQueryState
state, err = sub.NextState()
if err != nil {
return
}

assert.NotNil(t, state)
// assert.Equal(t, &GameStateMock, state)
}

return
})
}

func TestGameyeClient_QueryGame(t *testing.T) {
runInTestContext(t, func(ctx *testContext) (err error) {
ctx.Response <- `{"game":{},"location":{}}`

var state *models.GameQueryState
state, err = ctx.Client.QueryGame()
if err != nil {
return
}

assert.NotNil(t, state)
// assert.Equal(t, &GameStateMock, state)

return
})

}
39 changes: 0 additions & 39 deletions clients/gameye-command_test.go

This file was deleted.

88 changes: 0 additions & 88 deletions clients/gameye-game_test.go

This file was deleted.

Loading

0 comments on commit b47fa36

Please sign in to comment.