Skip to content

Commit

Permalink
Merge branch 'main' of github.com:CAS735-F23/macrun-archless-team int…
Browse files Browse the repository at this point in the history
…o main
  • Loading branch information
leonliu288 committed Dec 5, 2023
2 parents d7f7f24 + afb6b24 commit 5e6751b
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ docker-compose -f docker-compose.dep.yml up -d
### With Simulator Script

The script will simulate all actions like player registration, login, start heart rate monitor, start game, etc. It will
randomly generate heart rate, move random distance and react to game attacks.
also randomly generate heart rate, move random distance and react to game attacks.

- Please make sure you have **Python3.8+** installed.
- Run the following commands in your shell:
Expand Down Expand Up @@ -92,7 +92,7 @@ python3 ./simulator.py 127.0.0.1:8080

### Auto Test

We automate the testing of our services using GitHub Action (CI/CD). You can view the test results at:
We automate the testing of our services using GitHub Actions (CI/CD). You can view the test results at:

- <https://github.com/CAS735-F23/macrun-archless-team/actions/workflows/test.yml>.

Expand All @@ -111,6 +111,6 @@ mvn test
- Golang Services, e.g. game-service:

```shell
cd ./player-service
cd ./game-service
go test ./...
```
41 changes: 41 additions & 0 deletions game-service/api/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package api

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"

"game-service/config"
"game-service/consts"
"game-service/game"
)

func TestNewServer(t *testing.T) {
app, _ := game.New(&config.Config{})
r := New(app)

{
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)

resp := responseMessage{}
_ = json.Unmarshal(w.Body.Bytes(), &resp)
assert.Equal(t, consts.GameServiceName, (resp.Data).(map[string]any)["service"])
}

{
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/not-found", nil)
r.ServeHTTP(w, req)
assert.Equal(t, http.StatusNotFound, w.Code)

resp := responseMessage{}
_ = json.Unmarshal(w.Body.Bytes(), &resp)
assert.Equal(t, "NOT FOUND", resp.Status)
}
}
12 changes: 12 additions & 0 deletions game-service/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package config

import (
"testing"

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

func TestLoadConfigFromFile(t *testing.T) {
_, err := LoadConfigFromFile("not-exists.yml")
assert.Error(t, err)
}
20 changes: 20 additions & 0 deletions game-service/dto/dto_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dto

import (
"testing"

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

func TestActionResponseDTO(t *testing.T) {
art := &ActionResponseDTO{
message: "hello",
}
assert.Equal(t, "hello", art.GetMessage())

art.SetMessage("world")
assert.Equal(t, "world", art.GetMessage())

art.ResetMessage()
assert.Equal(t, "", art.GetMessage())
}
49 changes: 49 additions & 0 deletions game-service/game/context/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package context

import (
"testing"

"github.com/stretchr/testify/assert"

"game-service/dto"
)

func TestContext(t *testing.T) {
ctx := &Context{}

{
ctx.UpdateHeartRate(100)
assert.Equal(t, 100, ctx.GetHeartRate())
}

{
ctx.UpdateLocation(dto.PointDTO{X: 12, Y: 21})
assert.Equal(t, float64(12), ctx.GetLocation().X)
assert.Equal(t, float64(21), ctx.GetLocation().Y)
}

{
shelters := []dto.PointDTO{{12, 21}, {13, 31}}
ctx.UpdateShelters(shelters)
for i, s := range ctx.GetShelters() {
assert.Equal(t, shelters[i].X, s.X)
assert.Equal(t, shelters[i].Y, s.Y)
}
}

{
ctx.UpdateAttackMode("TEST")
assert.Equal(t, "TEST", ctx.GetAttackMode())
ctx.UpdateAttackMode() // Reset
assert.Equal(t, "", ctx.GetAttackMode())
}

{
defer func() {
r := recover()
assert.Nil(t, r)
}()

ctx.Close()
}
}
3 changes: 3 additions & 0 deletions game-service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/nacos-group/nacos-sdk-go/v2 v2.2.3
github.com/rabbitmq/amqp091-go v1.9.0
github.com/redis/go-redis/v9 v9.3.0
github.com/stretchr/testify v1.8.4
go.uber.org/atomic v1.11.0
gopkg.in/yaml.v2 v2.4.0
)
Expand All @@ -20,6 +21,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
Expand All @@ -39,6 +41,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.2 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
Expand Down

0 comments on commit 5e6751b

Please sign in to comment.