From c06b52f095ad2b009fd8e2f1866a3dd8ef2afd59 Mon Sep 17 00:00:00 2001
From: slhmy <slhmy.zzy@gmail.com>
Date: Fri, 26 Jul 2024 22:35:05 +0800
Subject: [PATCH] Perf unit tests

---
 Makefile                                      |  3 +-
 codecov.yml                                   |  1 +
 {tests/models => models/judge}/judge_test.go  | 17 ++++++-----
 .../models => models/problem}/problem_test.go | 21 +++++++-------
 {tests/models => models/user}/user_test.go    | 13 ++++-----
 .../agent/casbin}/casbin_test.go              | 22 +++++++--------
 .../agent/clickhouse}/clickhouse_test.go      |  6 ++--
 .../agent/minio}/minio_test.go                |  2 +-
 .../agent/redis}/redis_test.go                |  5 ++--
 modules/auth/jwt_test.go                      | 26 +++++++++++++++++
 {tests/core => modules/log}/init_test.go      |  4 +--
 tests/core/jwt_test.go                        | 28 -------------------
 12 files changed, 70 insertions(+), 78 deletions(-)
 rename {tests/models => models/judge}/judge_test.go (63%)
 rename {tests/models => models/problem}/problem_test.go (60%)
 rename {tests/models => models/user}/user_test.go (69%)
 rename {tests/core => modules/agent/casbin}/casbin_test.go (70%)
 rename {tests/core => modules/agent/clickhouse}/clickhouse_test.go (90%)
 rename {tests/core => modules/agent/minio}/minio_test.go (74%)
 rename {tests/core => modules/agent/redis}/redis_test.go (98%)
 create mode 100644 modules/auth/jwt_test.go
 rename {tests/core => modules/log}/init_test.go (80%)
 delete mode 100644 tests/core/jwt_test.go

diff --git a/Makefile b/Makefile
index 0eb9ed9..effd737 100644
--- a/Makefile
+++ b/Makefile
@@ -92,7 +92,8 @@ check: gen-proto install-cilint
 
 .PHONY: test
 test: build gen-swagger setup-dependencies
-	go test -race -covermode=atomic -coverprofile=coverage.out -cover -v -count=1 ./...
+	go test -race -covermode=atomic -coverprofile=coverage.out -cover -v -count=1 \
+		./models/... ./modules/... ./services/...
 
 # Dependent targets
 
diff --git a/codecov.yml b/codecov.yml
index b6e2eba..7820421 100644
--- a/codecov.yml
+++ b/codecov.yml
@@ -1,2 +1,3 @@
 ignore:
   - "cmd"
+  - "proto"
diff --git a/tests/models/judge_test.go b/models/judge/judge_test.go
similarity index 63%
rename from tests/models/judge_test.go
rename to models/judge/judge_test.go
index 7cd83ea..5688b25 100644
--- a/tests/models/judge_test.go
+++ b/models/judge/judge_test.go
@@ -1,10 +1,9 @@
-package models_test
+package judge_model
 
 import (
 	"encoding/json"
 	"testing"
 
-	judge_model "github.com/oj-lab/oj-lab-platform/models/judge"
 	problem_model "github.com/oj-lab/oj-lab-platform/models/problem"
 	gorm_agent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm"
 )
@@ -19,25 +18,25 @@ func TestJudgeDB(t *testing.T) {
 	if err != nil {
 		t.Error(err)
 	}
-	judge := &judge_model.Judge{
-		Language:    judge_model.ProgrammingLanguageCpp,
+	judge := &Judge{
+		Language:    ProgrammingLanguageCpp,
 		ProblemSlug: problem.Slug,
 	}
-	judge, err = judge_model.CreateJudge(db, *judge)
+	judge, err = CreateJudge(db, *judge)
 	if err != nil {
 		t.Error(err)
 	}
 
-	judgeResult := &judge_model.JudgeResult{
+	judgeResult := &JudgeResult{
 		JudgeUID: judge.UID,
-		Verdict:  judge_model.JudgeVerdictAccepted,
+		Verdict:  JudgeVerdictAccepted,
 	}
-	_, err = judge_model.CreateJudgeResult(db, *judgeResult)
+	_, err = CreateJudgeResult(db, *judgeResult)
 	if err != nil {
 		t.Error(err)
 	}
 
-	judge, err = judge_model.GetJudge(db, judge.UID)
+	judge, err = GetJudge(db, judge.UID)
 	if err != nil {
 		t.Error(err)
 	}
diff --git a/tests/models/problem_test.go b/models/problem/problem_test.go
similarity index 60%
rename from tests/models/problem_test.go
rename to models/problem/problem_test.go
index 555d9ba..c2ee8e7 100644
--- a/tests/models/problem_test.go
+++ b/models/problem/problem_test.go
@@ -1,30 +1,29 @@
-package models_test
+package problem_model
 
 import (
 	"encoding/json"
 	"fmt"
 	"testing"
 
-	problem_model "github.com/oj-lab/oj-lab-platform/models/problem"
 	gorm_agent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm"
 )
 
 func TestProblemDB(t *testing.T) {
 	db := gorm_agent.GetDefaultDB()
 	description := "Given two integer A and B, please output the answer of A+B."
-	problem := problem_model.Problem{
+	problem := Problem{
 		Slug:        "a-plus-b-problem",
 		Title:       "A+B Problem",
 		Description: &description,
-		Tags:        []*problem_model.AlgorithmTag{{Name: "tag1"}, {Name: "tag2"}},
+		Tags:        []*AlgorithmTag{{Name: "tag1"}, {Name: "tag2"}},
 	}
 
-	err := problem_model.CreateProblem(db, problem)
+	err := CreateProblem(db, problem)
 	if err != nil {
 		t.Error(err)
 	}
 
-	dbProblem, err := problem_model.GetProblem(db, problem.Slug)
+	dbProblem, err := GetProblem(db, problem.Slug)
 	if err != nil {
 		t.Error(err)
 	}
@@ -35,13 +34,13 @@ func TestProblemDB(t *testing.T) {
 	}
 	fmt.Printf("%+v\n", string(problemJson))
 
-	problemOption := problem_model.GetProblemOptions{
-		Selection: problem_model.ProblemInfoSelection,
-		Tags:      []*problem_model.AlgorithmTag{{Name: "tag1"}},
+	problemOption := GetProblemOptions{
+		Selection: ProblemInfoSelection,
+		Tags:      []*AlgorithmTag{{Name: "tag1"}},
 		Slug:      &problem.Slug,
 	}
 
-	problemList, problemCount, err := problem_model.GetProblemInfoListByOptions(db, problemOption)
+	problemList, problemCount, err := GetProblemInfoListByOptions(db, problemOption)
 	if err != nil {
 		t.Error(err)
 	}
@@ -56,7 +55,7 @@ func TestProblemDB(t *testing.T) {
 	}
 	fmt.Printf("%+v\n", string(problemListJson))
 
-	err = problem_model.DeleteProblem(db, problem.Slug)
+	err = DeleteProblem(db, problem.Slug)
 	if err != nil {
 		t.Error(err)
 	}
diff --git a/tests/models/user_test.go b/models/user/user_test.go
similarity index 69%
rename from tests/models/user_test.go
rename to models/user/user_test.go
index 5f1e35b..93dd09a 100644
--- a/tests/models/user_test.go
+++ b/models/user/user_test.go
@@ -1,26 +1,25 @@
-package models_test
+package user_model
 
 import (
 	"encoding/json"
 	"fmt"
 	"testing"
 
-	user_model "github.com/oj-lab/oj-lab-platform/models/user"
 	gorm_agent "github.com/oj-lab/oj-lab-platform/modules/agent/gorm"
 )
 
 func TestUserDB(t *testing.T) {
 	db := gorm_agent.GetDefaultDB()
-	user := user_model.User{
+	user := User{
 		Account:  "test",
 		Password: func() *string { s := "test"; return &s }(),
 	}
-	err := user_model.CreateUser(db, user)
+	err := CreateUser(db, user)
 	if err != nil {
 		t.Error(err)
 	}
 
-	dbUser, err := user_model.GetUser(db, user.Account)
+	dbUser, err := GetUser(db, user.Account)
 	if err != nil {
 		t.Error(err)
 	}
@@ -30,7 +29,7 @@ func TestUserDB(t *testing.T) {
 	}
 	fmt.Printf("%+v\n", string(userJson))
 
-	dbPublicUser, err := user_model.GetPublicUser(db, user.Account)
+	dbPublicUser, err := GetPublicUser(db, user.Account)
 	if err != nil {
 		t.Error(err)
 	}
@@ -40,7 +39,7 @@ func TestUserDB(t *testing.T) {
 	}
 	fmt.Printf("%+v\n", string(publicUserJson))
 
-	err = user_model.DeleteUser(db, user)
+	err = DeleteUser(db, user)
 	if err != nil {
 		t.Error(err)
 	}
diff --git a/tests/core/casbin_test.go b/modules/agent/casbin/casbin_test.go
similarity index 70%
rename from tests/core/casbin_test.go
rename to modules/agent/casbin/casbin_test.go
index 9600073..934b211 100644
--- a/tests/core/casbin_test.go
+++ b/modules/agent/casbin/casbin_test.go
@@ -1,48 +1,46 @@
-package core_test
+package casbin_agent
 
 import (
 	"net/http"
 	"testing"
-
-	casbin_agent "github.com/oj-lab/oj-lab-platform/modules/agent/casbin"
 )
 
 func TestKeyMatchGin(t *testing.T) {
 	key2 := "/api/v1/user/:id"
 	key1 := "/api/v1/user/1"
-	if !casbin_agent.KeyMatchGin(key1, key2) {
+	if !KeyMatchGin(key1, key2) {
 		t.Error("Expected to match")
 	}
 	key1 = "/api/v1/user/"
-	if casbin_agent.KeyMatchGin(key1, key2) {
+	if KeyMatchGin(key1, key2) {
 		t.Error("Expected not to match")
 	}
 
 	key2 = "/api/v1/:resource/*any"
 	key1 = "/api/v1/user"
-	if !casbin_agent.KeyMatchGin(key1, key2) {
+	if !KeyMatchGin(key1, key2) {
 		t.Error("Expected to match")
 	}
 	key1 = "/api/v1/user/1"
-	if !casbin_agent.KeyMatchGin(key1, key2) {
+	if !KeyMatchGin(key1, key2) {
 		t.Error("Expected to match")
 	}
 	key1 = "/api/v1/user/"
-	if !casbin_agent.KeyMatchGin(key1, key2) {
+	if !KeyMatchGin(key1, key2) {
 		t.Error("Expected to match")
 	}
 	key1 = "/api/v1/user/1/send"
-	if !casbin_agent.KeyMatchGin(key1, key2) {
+	if !KeyMatchGin(key1, key2) {
 		t.Error("Expected to match")
 	}
 	key1 = "/api/v1//"
-	if casbin_agent.KeyMatchGin(key1, key2) {
+	if KeyMatchGin(key1, key2) {
 		t.Error("Expected not to match")
 	}
 }
 
 func TestCasbin(t *testing.T) {
-	enforcer := casbin_agent.GetDefaultCasbinEnforcer()
+	enforcer := GetDefaultCasbinEnforcer()
 	_, err := enforcer.AddPolicy(
 		`user_test`, `r.ext.IsVIP == true`, `system`, `testData`, http.MethodGet, "allow")
 	if err != nil {
@@ -63,7 +61,7 @@ func TestCasbin(t *testing.T) {
 	}
 	t.Logf("Policies: %v", policies)
 
-	allow, err := enforcer.Enforce("user_test", casbin_agent.ExtraInfo{
+	allow, err := enforcer.Enforce("user_test", ExtraInfo{
 		IsVIP: true,
 	}, `system`, `testData`, http.MethodGet)
 	if err != nil {
diff --git a/tests/core/clickhouse_test.go b/modules/agent/clickhouse/clickhouse_test.go
similarity index 90%
rename from tests/core/clickhouse_test.go
rename to modules/agent/clickhouse/clickhouse_test.go
index 8de70b5..225d01e 100644
--- a/tests/core/clickhouse_test.go
+++ b/modules/agent/clickhouse/clickhouse_test.go
@@ -1,4 +1,4 @@
-package core_test
+package clickhouse_agent
 
 import (
 	"context"
@@ -6,12 +6,12 @@ import (
 	"time"
 
 	"github.com/google/uuid"
-	clickhouse_agent "github.com/oj-lab/oj-lab-platform/modules/agent/clickhouse"
 )
 
 func TestClickhouse(t *testing.T) {
+	var ctx = context.Background()
 	t.Log("TestClickhouse")
-	conn, err := clickhouse_agent.Connect()
+	conn, err := Connect()
 	if err != nil {
 		t.Error(err)
 	}
diff --git a/tests/core/minio_test.go b/modules/agent/minio/minio_test.go
similarity index 74%
rename from tests/core/minio_test.go
rename to modules/agent/minio/minio_test.go
index 6088f2f..7092d3c 100644
--- a/tests/core/minio_test.go
+++ b/modules/agent/minio/minio_test.go
@@ -1,4 +1,4 @@
-package core_test
+package minio_agent
 
 import (
 	"testing"
diff --git a/tests/core/redis_test.go b/modules/agent/redis/redis_test.go
similarity index 98%
rename from tests/core/redis_test.go
rename to modules/agent/redis/redis_test.go
index ff955fe..bd42a06 100644
--- a/tests/core/redis_test.go
+++ b/modules/agent/redis/redis_test.go
@@ -1,4 +1,4 @@
-package core_test
+package redis_agent
 
 import (
 	"context"
@@ -11,9 +11,8 @@ import (
 	"google.golang.org/protobuf/proto"
 )
 
-var ctx = context.Background()
-
 func TestRedis(t *testing.T) {
+	var ctx = context.Background()
 	// rdb := redis.NewClusterClient(&redis.ClusterOptions{
 	// 	Addrs: []string{":7000", ":7001", ":7002", ":7003", ":7004", ":7005"},
 
diff --git a/modules/auth/jwt_test.go b/modules/auth/jwt_test.go
new file mode 100644
index 0000000..4a7b87d
--- /dev/null
+++ b/modules/auth/jwt_test.go
@@ -0,0 +1,26 @@
+package auth_module
+
+import (
+	"log"
+	"testing"
+)
+
+func TestGenerateTokenString(t *testing.T) {
+	tokenString, err := GenerateAuthTokenString("account", []string{"admin"}...)
+	if err != nil {
+		panic(err)
+	}
+	log.Print(tokenString)
+}
+
+func TestParseTokenString(t *testing.T) {
+	tokenString, err := GenerateAuthTokenString("account", []string{"admin"}...)
+	if err != nil {
+		panic(err)
+	}
+	account, role, err := ParseAuthTokenString(tokenString)
+	if err != nil {
+		panic(err)
+	}
+	log.Println(account, role)
+}
diff --git a/tests/core/init_test.go b/modules/log/init_test.go
similarity index 80%
rename from tests/core/init_test.go
rename to modules/log/init_test.go
index 34ac83e..4f42017 100644
--- a/tests/core/init_test.go
+++ b/modules/log/init_test.go
@@ -1,4 +1,4 @@
-package core_test
+package log_module
 
 import (
 	"testing"
@@ -6,8 +6,6 @@ import (
 	"github.com/spf13/viper"
 )
 
-const logLevelProp = "log.level"
-
 func TestInit(T *testing.T) {
 	logLevel := viper.GetString(logLevelProp)
 	if logLevel != "debug" {
diff --git a/tests/core/jwt_test.go b/tests/core/jwt_test.go
deleted file mode 100644
index 0f4ade2..0000000
--- a/tests/core/jwt_test.go
+++ /dev/null
@@ -1,28 +0,0 @@
-package core_test
-
-import (
-	"log"
-	"testing"
-
-	auth_module "github.com/oj-lab/oj-lab-platform/modules/auth"
-)
-
-func TestGenerateTokenString(t *testing.T) {
-	tokenString, err := auth_module.GenerateAuthTokenString("account", []string{"admin"}...)
-	if err != nil {
-		panic(err)
-	}
-	log.Print(tokenString)
-}
-
-func TestParseTokenString(t *testing.T) {
-	tokenString, err := auth_module.GenerateAuthTokenString("account", []string{"admin"}...)
-	if err != nil {
-		panic(err)
-	}
-	account, role, err := auth_module.ParseAuthTokenString(tokenString)
-	if err != nil {
-		panic(err)
-	}
-	log.Println(account, role)
-}