Skip to content

Commit

Permalink
Supplementary unit testing (#43)
Browse files Browse the repository at this point in the history
* add: supplementary-unit-testing

add config_test.go
add validate_test.go
补充单元测试

* edit: 修改不存在的配置文件的名称

不存在的配置文件的名称修改为missing_config.yml
  • Loading branch information
Zherphy authored Dec 16, 2024
1 parent 7c9fa01 commit 6c7319c
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 16 deletions.
15 changes: 15 additions & 0 deletions config.example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
LFS_BUCKET: "***********"
CDN_DOMAIN: "***********"
OBS_REGION: "***********"
OBS_ACCESS_KEY_ID: "***********"
OBS_SECRET_ACCESS_KEY: "***********"
CLIENT_ID: "***********"
CLIENT_SECRET: "***********"
PATH_PREFIX: "***********"
DEFAULT_TOKEN: "***********"
VALIDATE_REGEXP:
OWNER_REGEXP: "^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$"
REPONAME_REGEXP: "^[a-zA-Z0-9_.-]{1,189}[a-zA-Z0-9]$"
USERNAME_REGEXP: "^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$"
PASSWORD_REGEXP: "^[a-zA-Z0-9!@_#$%^&*()\\-=+,?.,]*$"

16 changes: 0 additions & 16 deletions config.yml.example

This file was deleted.

51 changes: 51 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package config

import "testing"

func TestLoadConfig(t *testing.T) {
type args struct {
path string
cfg *Config
remove bool
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "TestLoadConfig success",
args: args{
path: "../config.example.yml",
cfg: &Config{},
remove: false,
},
wantErr: false,
},
{
name: "TestLoadConfig fail",
args: args{
path: "../missing_config.yml",
cfg: &Config{},
remove: false,
},
wantErr: true,
},
{
name: "TestLoadConfig fail and remove LocalFile",
args: args{
path: "../missing_config.yml",
cfg: &Config{},
remove: true,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := LoadConfig(tt.args.path, tt.args.cfg, tt.args.remove); (err != nil) != tt.wantErr {
t.Errorf("LoadConfig() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
66 changes: 66 additions & 0 deletions server/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package server

import (
"github.com/metalogical/BigFiles/config"
"testing"
)

func TestInit(t *testing.T) {
tests := []struct {
name string
args config.ValidateConfig
wantErr bool
}{
{
name: "compile owner regexp failed",
args: config.ValidateConfig{
OwnerRegexp: `^[\\-?]$`,
},
wantErr: true,
},
{
name: "compile repo regexp failed",
args: config.ValidateConfig{
OwnerRegexp: `^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$`,
RepoNameRegexp: `^[\\-?]$`,
},
wantErr: true,
},
{
name: "compile username regexp failed",
args: config.ValidateConfig{
OwnerRegexp: `^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$`,
RepoNameRegexp: `^[a-zA-Z0-9_.-]{1,189}[a-zA-Z0-9]$`,
UsernameRegexp: `^[\\-?]$`,
},
wantErr: true,
},
{
name: "compile password regexp failed",
args: config.ValidateConfig{
OwnerRegexp: `^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$`,
RepoNameRegexp: `^[a-zA-Z0-9_.-]{1,189}[a-zA-Z0-9]$`,
UsernameRegexp: `^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$`,
PasswordRegexp: `^[\\-?]$`,
},
wantErr: true,
},
{
name: "compile regexp success",
args: config.ValidateConfig{
OwnerRegexp: `^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$`,
RepoNameRegexp: `^[a-zA-Z0-9_.-]{1,189}[a-zA-Z0-9]$`,
UsernameRegexp: `^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$`,
PasswordRegexp: `^[a-zA-Z0-9!@_#$%^&*()-=+,?.,]*$`,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Init(tt.args); (err != nil) != tt.wantErr {
t.Errorf("Init() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
57 changes: 57 additions & 0 deletions utils/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package utils

import (
"testing"
)

func TestLoadFromYaml(t *testing.T) {
type ValidateConfig struct {
OwnerRegexp string `json:"OWNER_REGEXP" required:"true"`
RepoNameRegexp string `json:"REPONAME_REGEXP" required:"true"`
UsernameRegexp string `json:"USERNAME_REGEXP" required:"true"`
PasswordRegexp string `json:"PASSWORD_REGEXP" required:"true"`
}
type Config struct {
Prefix string `json:"PATH_PREFIX"`
LfsBucket string `json:"LFS_BUCKET"`
ClientId string `json:"CLIENT_ID"`
ClientSecret string `json:"CLIENT_SECRET"`
CdnDomain string `json:"CDN_DOMAIN"`
ObsRegion string `json:"OBS_REGION"`
ObsAccessKeyId string `json:"OBS_ACCESS_KEY_ID"`
ObsSecretAccessKey string `json:"OBS_SECRET_ACCESS_KEY"`
ValidateConfig ValidateConfig `json:"VALIDATE_REGEXP"`
DefaultToken string `json:"DEFAULT_TOKEN"`
}
type args struct {
path string
cfg Config
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "TestLoadFromYaml success",
args: args{
path: "../config.example.yml",
},
wantErr: false,
},
{
name: "TestLoadFromYaml fail",
args: args{
path: "../missing_config.yml",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := LoadFromYaml(tt.args.path, tt.args.cfg); (err != nil) != tt.wantErr {
t.Errorf("LoadFromYaml() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit 6c7319c

Please sign in to comment.