Skip to content

Commit

Permalink
Perf unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slhmy committed Jul 26, 2024
1 parent bd8a63b commit c06b52f
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 78 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ignore:
- "cmd"
- "proto"
17 changes: 8 additions & 9 deletions tests/models/judge_test.go → models/judge/judge_test.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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)
}
Expand Down
21 changes: 10 additions & 11 deletions tests/models/problem_test.go → models/problem/problem_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
13 changes: 6 additions & 7 deletions tests/models/user_test.go → models/user/user_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
22 changes: 10 additions & 12 deletions tests/core/casbin_test.go → modules/agent/casbin/casbin_test.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package core_test
package clickhouse_agent

import (
"context"
"testing"
"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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core_test
package minio_agent

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core_test
package redis_agent

import (
"context"
Expand All @@ -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"},

Expand Down
26 changes: 26 additions & 0 deletions modules/auth/jwt_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 1 addition & 3 deletions tests/core/init_test.go → modules/log/init_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package core_test
package log_module

import (
"testing"

"github.com/spf13/viper"
)

const logLevelProp = "log.level"

func TestInit(T *testing.T) {
logLevel := viper.GetString(logLevelProp)
if logLevel != "debug" {
Expand Down
28 changes: 0 additions & 28 deletions tests/core/jwt_test.go

This file was deleted.

0 comments on commit c06b52f

Please sign in to comment.