From 6c7319c6749dc37a58e40de4a06ae0d27262fe14 Mon Sep 17 00:00:00 2001 From: Zherphy <1123678689@qq.com> Date: Mon, 16 Dec 2024 19:03:53 +0800 Subject: [PATCH] Supplementary unit testing (#43) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add: supplementary-unit-testing add config_test.go add validate_test.go 补充单元测试 * edit: 修改不存在的配置文件的名称 不存在的配置文件的名称修改为missing_config.yml --- config.example.yml | 15 ++++++++++ config.yml.example | 16 ---------- config/config_test.go | 51 +++++++++++++++++++++++++++++++ server/validate_test.go | 66 +++++++++++++++++++++++++++++++++++++++++ utils/util_test.go | 57 +++++++++++++++++++++++++++++++++++ 5 files changed, 189 insertions(+), 16 deletions(-) create mode 100644 config.example.yml delete mode 100644 config.yml.example create mode 100644 config/config_test.go create mode 100644 server/validate_test.go create mode 100644 utils/util_test.go diff --git a/config.example.yml b/config.example.yml new file mode 100644 index 0000000..d21e024 --- /dev/null +++ b/config.example.yml @@ -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!@_#$%^&*()\\-=+,?.,]*$" + diff --git a/config.yml.example b/config.yml.example deleted file mode 100644 index 80c0f4d..0000000 --- a/config.yml.example +++ /dev/null @@ -1,16 +0,0 @@ -{ - "LFS_BUCKET": *********** - "CDN_DOMAIN": *********** - "OBS_REGION": *********** - "OBS_ACCESS_KEY_ID": *********** - "OBS_SECRET_ACCESS_KEY": *********** - "CLIENT_ID": *********** - "CLIENT_SECRET": *********** - "PATH_PREFIX": *********** - "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!@_#$%^&*()\\-=+,?.,]*$" - } -} diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 0000000..6edeff0 --- /dev/null +++ b/config/config_test.go @@ -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) + } + }) + } +} diff --git a/server/validate_test.go b/server/validate_test.go new file mode 100644 index 0000000..6f696f4 --- /dev/null +++ b/server/validate_test.go @@ -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) + } + }) + } +} diff --git a/utils/util_test.go b/utils/util_test.go new file mode 100644 index 0000000..316709b --- /dev/null +++ b/utils/util_test.go @@ -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) + } + }) + } +}