From 76c1646593aafe6ac64ad6ee8b841258065a552f Mon Sep 17 00:00:00 2001 From: xjasonlyu Date: Mon, 4 Dec 2023 20:56:03 -0500 Subject: [PATCH 1/5] chore docs --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9596011..c69cac5 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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: - . From 14471ccf8e79d47eb28898571b15a63faf1ec3f5 Mon Sep 17 00:00:00 2001 From: xjasonlyu Date: Mon, 4 Dec 2023 21:04:09 -0500 Subject: [PATCH 2/5] feat: add init tests --- game-service/config/config_test.go | 12 ++++++++++++ game-service/go.mod | 3 +++ 2 files changed, 15 insertions(+) create mode 100644 game-service/config/config_test.go diff --git a/game-service/config/config_test.go b/game-service/config/config_test.go new file mode 100644 index 0000000..29c5d2b --- /dev/null +++ b/game-service/config/config_test.go @@ -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) +} diff --git a/game-service/go.mod b/game-service/go.mod index 60cac13..86a050a 100644 --- a/game-service/go.mod +++ b/game-service/go.mod @@ -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 ) @@ -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 @@ -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 From 68a37fa3bde261d62938aab7adca0affdaf9a916 Mon Sep 17 00:00:00 2001 From: xjasonlyu Date: Mon, 4 Dec 2023 21:04:18 -0500 Subject: [PATCH 3/5] docs: fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c69cac5..fd3637f 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,6 @@ mvn test - Golang Services, e.g. game-service: ```shell -cd ./player-service +cd ./game-service go test ./... ``` From 394d18c4d0f35a7c998130bb80fb5bf4f3eaf57d Mon Sep 17 00:00:00 2001 From: xjasonlyu Date: Mon, 4 Dec 2023 21:09:35 -0500 Subject: [PATCH 4/5] test: add dto tests --- game-service/dto/dto_test.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 game-service/dto/dto_test.go diff --git a/game-service/dto/dto_test.go b/game-service/dto/dto_test.go new file mode 100644 index 0000000..38f11a4 --- /dev/null +++ b/game-service/dto/dto_test.go @@ -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()) +} From 9c1f45a0333442b5be31cb9cdaf7581b2df08f81 Mon Sep 17 00:00:00 2001 From: xjasonlyu Date: Mon, 4 Dec 2023 23:18:36 -0500 Subject: [PATCH 5/5] test: add new tests --- game-service/api/server_test.go | 41 +++++++++++++++++++ game-service/game/context/context_test.go | 49 +++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 game-service/api/server_test.go create mode 100644 game-service/game/context/context_test.go diff --git a/game-service/api/server_test.go b/game-service/api/server_test.go new file mode 100644 index 0000000..fb5a105 --- /dev/null +++ b/game-service/api/server_test.go @@ -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) + } +} diff --git a/game-service/game/context/context_test.go b/game-service/game/context/context_test.go new file mode 100644 index 0000000..a43a04d --- /dev/null +++ b/game-service/game/context/context_test.go @@ -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() + } +}