Skip to content

Commit

Permalink
try add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
bouroo committed Jul 25, 2021
1 parent 791c3b5 commit 4c6f352
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 4 deletions.
16 changes: 12 additions & 4 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package helpers
import (
"fmt"
"net/http"
"path"
"strconv"
"strings"
"time"
Expand All @@ -11,13 +12,20 @@ import (
)

func FileNameWithoutExtension(fileName string) string {
if pos := strings.LastIndexByte(fileName, '.'); pos != -1 {
_, fileName = path.Split(fileName)
fileExtension := path.Ext(fileName)
if pos := strings.LastIndex(fileName, fileExtension); pos != -1 {
return fileName[:pos]
}
return fileName
}

func DateStrTotime(dateStr string) (result time.Time, err error) {
time.Local, err = time.LoadLocation("Asia/Bangkok")
if err != nil {
err = fiber.NewError(http.StatusInternalServerError, err.Error())
return
}
dateStr = strings.TrimSpace(dateStr)
var (
yearStr string
Expand Down Expand Up @@ -49,7 +57,7 @@ func DateStrTotime(dateStr string) (result time.Time, err error) {
if dayStr == "00" {
dayStr = "01"
}
result, err = time.Parse("2006-01-02", fmt.Sprintf("%04s-%02s-%02s", yearStr, monthStr, dayStr))
result, err = time.ParseInLocation("2006-01-02", fmt.Sprintf("%04s-%02s-%02s", yearStr, monthStr, dayStr), time.Local)
if err == nil && !result.IsZero() {
// convert from BE
if result.Year() > (currentTime.Year() + 272) {
Expand All @@ -71,7 +79,7 @@ func DateStrTotime(dateStr string) (result time.Time, err error) {
if dayStr == "00" {
dayStr = "01"
}
result, err = time.Parse("2006-01-02", fmt.Sprintf("%04s-%02s-%02s", yearStr, monthStr, dayStr))
result, err = time.ParseInLocation("2006-01-02", fmt.Sprintf("%04s-%02s-%02s", yearStr, monthStr, dayStr), time.Local)
if err == nil && !result.IsZero() {
// convert from BE
if result.Year() > (currentTime.Year() + 272) {
Expand All @@ -83,7 +91,7 @@ func DateStrTotime(dateStr string) (result time.Time, err error) {
yearStr = dateStr[4:8]
monthStr = dateStr[2:4]
dayStr = dateStr[:2]
result, err = time.Parse("2006-01-02", fmt.Sprintf("%04s-%02s-%02s", yearStr, monthStr, dayStr))
result, err = time.ParseInLocation("2006-01-02", fmt.Sprintf("%04s-%02s-%02s", yearStr, monthStr, dayStr), time.Local)
if err == nil && !result.IsZero() {
// convert from BE
if result.Year() > (currentTime.Year() + 272) {
Expand Down
112 changes: 112 additions & 0 deletions data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package helpers

import (
"fmt"
"net/http"
"testing"
"time"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
)

func TestFileNameWithoutExtension(t *testing.T) {
t.Parallel()
var (
// err error
subject string
expect string
)

subject = "/dir0/dir1/test.ext"
expect = "test"
result := FileNameWithoutExtension(subject)

utils.AssertEqual(t, expect, result)
}

func TestDateStrTotime(t *testing.T) {
t.Parallel()
var (
err error
subject string
expect time.Time
)

subject = "1990-12-09"
expect = time.Date(1990, 12, 9, 0, 0, 0, 0, time.Local)
result, err := DateStrTotime(subject)
if err != nil {
utils.AssertEqual(t, nil, err)
}
utils.AssertEqual(t, expect.Unix(), result.Unix())

subject = "09-12-1990"
result, err = DateStrTotime(subject)
if err != nil {
utils.AssertEqual(t, nil, err)
}
utils.AssertEqual(t, expect.Unix(), result.Unix())

subject = "19901209"
result, err = DateStrTotime(subject)
if err != nil {
utils.AssertEqual(t, nil, err)
}
utils.AssertEqual(t, expect.Unix(), result.Unix())

subject = "09121990"
result, err = DateStrTotime(subject)
if err != nil {
utils.AssertEqual(t, nil, err)
}
utils.AssertEqual(t, expect.Unix(), result.Unix())
}

func TestTimeToDGADate(t *testing.T) {
t.Parallel()
var (
// err error
subject time.Time
expect string
)

subject = time.Date(1990, 12, 9, 0, 0, 0, 0, time.Local)
expect = "25331209"
result := TimeToDGADate(subject)

utils.AssertEqual(t, expect, result)
}

func TestValidCID(t *testing.T) {
t.Parallel()
var (
err error
subject string
expect bool
)

subject = "1111111111119"
expect = true
result, err := ValidCID(subject)
if err != nil {
utils.AssertEqual(t, nil, err)
}
utils.AssertEqual(t, expect, result)

subject = "1111111111110"
expect = true
result, err = ValidCID(subject)
if err != nil {
utils.AssertEqual(t, fiber.NewError(http.StatusBadRequest, fmt.Sprintf("invalid cid: %+v", subject)), err)
}
utils.AssertEqual(t, false, result)

subject = "111111111111"
expect = true
result, err = ValidCID(subject)
if err != nil {
utils.AssertEqual(t, fiber.NewError(http.StatusBadRequest, fmt.Sprintf("cid must be 13 digits: %+v", subject)), err)
}
utils.AssertEqual(t, false, result)
}
23 changes: 23 additions & 0 deletions forms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package helpers

type ResponseForm struct {
Success bool `json:"success"`
Result interface{} `json:"result"`
Messages []string `json:"messages"`
Errors []*ResposeError `json:"errors"`
ResultInfo *ResultInfo `json:"result_info,omitempty"`
}

type ResposeError struct {
Code int `json:"code"`
Source interface{} `json:"source,omitempty"`
Title string `json:"title,omitempty"`
Message string `json:"message"`
}

type ResultInfo struct {
Page int `json:"page"`
PerPage int `json:"per_page"`
Count int `json:"count"`
TotalCont int `json:"total_count"`
}
68 changes: 68 additions & 0 deletions oauth_resp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package helpers

// OauthRequest oauth request by IETF
type OauthRequest struct {
APIKey string `json:"client_id" form:"client_id" query:"client_id"`
APISecret string `json:"client_secret" form:"client_secret" query:"client_secret"`
ResponseType string `json:"response_type" form:"response_type" query:"response_type"`
RedirectURI string `json:"redirect_uri" form:"redirect_uri" query:"redirect_uri"`
Scope string `json:"scope" form:"scope" query:"scope"`
State string `json:"state" form:"state" query:"state"`
Code string `json:"code" form:"code" query:"code"`
GrantType string `json:"grant_type" form:"grant_type" query:"grant_type"`
UserName string `json:"username" form:"username" query:"username"`
Password string `json:"password" form:"password" query:"password"`
RefreshToken string `json:"refresh_token" form:"refresh_token" query:"refresh_token"`
Token string `json:"token" form:"token" query:"token"`
}

// OauthResponse oauth request by IETF
type OauthResponse struct {
Scope string `json:"scope,omitempty"`
State string `json:"state,omitempty"`
Code string `json:"code,omitempty"`
Error OauthErr `json:"error,omitempty"`
ErrorDesc string `json:"error_description,omitempty"`
ErrorURI string `json:"error_uri,omitempty"`
AccessToken string `json:"access_token,omitempty"`
IDToken string `json:"id_token,omitempty"`
TokenType string `json:"token_type,omitempty"`
ExpiresIn int `json:"expires_in,omitempty"`
RefreshToken string `json:"refresh_token,omitempty"`
Data interface{} `json:"data,omitempty"`
}

// OauthErr Oauth error response
type OauthErr string

// Oauth Error Response
const (
InvalidRequest OauthErr = "invalid_request"
UnauthorizedClient OauthErr = "unauthorized_client"
AccessDenied OauthErr = "access_denied"
UnsupportType OauthErr = "unsupported_response_type"
InvalidScope OauthErr = "invalid_scope"
ServerError OauthErr = "server_error"
Unavailable OauthErr = "temporarily_unavailable"
)

// GrantType Oauth grant type
type GrantType string

// GrantType constant
const (
GrantTypeCode GrantType = "authorization_code"
GrantTypeClient GrantType = "client_credentials"
GrantTypeRefresh GrantType = "refresh_token"
GrantTypeaccess GrantType = "access_token"
GrantTypePassword GrantType = "password"
)

// ResponseType Oauth response type
type ResponseType string

// Responsetype constant
const (
ResponseTypeCode ResponseType = "code"
ResponseTypeToken ResponseType = "token"
)

0 comments on commit 4c6f352

Please sign in to comment.