Skip to content

Commit

Permalink
refactor test
Browse files Browse the repository at this point in the history
  • Loading branch information
smith committed Apr 7, 2024
1 parent 341f5fa commit add5d66
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
run: docker compose -f opc_ua/docker-compose-local.yml up -d

- name: run server
run: env git_test=1 go run main.go
run: docker compose -f docker-compose-testenv.yml up -d

- name: Test
run: env git_test=1 go test -v ./...
18 changes: 18 additions & 0 deletions docker-compose-testenv.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '3.8'


services:
opc-plugin-server:
build:
context: .
dockerfile: Dockerfile
environment:
- git_test=1
ports:
- "8080:8080"
healthcheck:
test: ["CMD","/healthcheck" ]
interval: 5s
timeout: 3s
retries: 3
start_period: 8s
18 changes: 9 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
version: '3.8'

networks:
opcnetwork:
name: mynetwork # 已经存在的network
external: true
# networks:
# opcnetwork:
# name: mynetwork # 已经存在的network
# external: true


services:
opc-plugin-server:
build:
context: .
dockerfile: Dockerfile
env_file:
# Ensure that the variables in .env match the same variables in devcontainer.json
- .devcontainer/.env
# env_file:
# # Ensure that the variables in .env match the same variables in devcontainer.json
# - .devcontainer/.env
ports:
- "8080:8080"
networks:
- opcnetwork
# networks:
# - opcnetwork
healthcheck:
test: ["CMD","/healthcheck" ]
interval: 5s
Expand Down
10 changes: 5 additions & 5 deletions test/routers/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ import (
gentool "github.com/Brandon-lz/myopcua/db/gen"
"github.com/Brandon-lz/myopcua/db/gen/query"
globaldata "github.com/Brandon-lz/myopcua/global"

// httpservice "github.com/Brandon-lz/myopcua/http_service"
"github.com/Brandon-lz/myopcua/log"
// opcservice "github.com/Brandon-lz/myopcua/opc_service"
"github.com/stretchr/testify/assert"
)


func TestMain(t *testing.T) {
t.Skip()
assert := assert.New(t)
assert.NoError(os.Remove("./systemvars.obj"))

sysdb.InitDB()
cleanDb(assert)
migrateModel()
Expand All @@ -43,12 +44,11 @@ func TestMain(t *testing.T) {
time.Sleep(1 * time.Second)

// Run router tests ---------------------------------
t.Run("Test_AddWebhookConfig", testAddWebhookConfig)
t.Run("Test_getWebhookConfig", testGetWebhookConfigById)
t.Run("Test_AddWebhookConfig", TestAddWebhookConfig)
t.Run("Test_getWebhookConfig", TestGetWebhookConfigById)

// cancel()


}

func cleanDb(assert *assert.Assertions) {
Expand Down
30 changes: 15 additions & 15 deletions test/routers/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"github.com/stretchr/testify/require"
)

func testAddWebhookConfig(t *testing.T) {
require := require.New(t)
url := "http://localhost:8080/api/v1/webhook"
func TestAddWebhookConfig(t *testing.T) {
require := require.New(t)
url := "http://localhost:8080/api/v1/webhook"

body := `
body := `
{
"active": true,
"name": "webhook1",
Expand Down Expand Up @@ -43,20 +43,20 @@ func testAddWebhookConfig(t *testing.T) {
"need_node_list": ["MyVariable"]
}
`
res, err := utils.PostRequest(url, body)
require.NoError(err)
res, err := utils.PostRequest(url, body)
require.NoError(err)

require.Equal(http.StatusOK, res.StatusCode) // 断言状态码
require.Equal(http.StatusOK, res.StatusCode) // 断言状态码

resData, err := io.ReadAll(res.Body)
require.NoError(err)
resData, err := io.ReadAll(res.Body)
require.NoError(err)

t.Log("resoponseData:" + string(resData))
t.Log("resoponseData:" + string(resData))
}

func testGetWebhookConfigById(t *testing.T) {
require := require.New(t)
res, err := utils.GetRequest("http://localhost:8080/api/v1/webhook/1")
require.NoError(err)
require.Equal(http.StatusOK, res.StatusCode) // 断言状态码
func TestGetWebhookConfigById(t *testing.T) {
require := require.New(t)
res, err := utils.GetRequest("http://localhost:8080/api/v1/webhook/1")
require.NoError(err)
require.Equal(http.StatusOK, res.StatusCode) // 断言状态码
}

0 comments on commit add5d66

Please sign in to comment.