Skip to content

Commit

Permalink
rewrite tests to testify
Browse files Browse the repository at this point in the history
  • Loading branch information
krabiworld committed Aug 28, 2024
1 parent b53839c commit fc9e474
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 118 deletions.
24 changes: 5 additions & 19 deletions env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,19 @@ package env

import (
"cmp"
"github.com/stretchr/testify/assert"
"reflect"
"slices"
"testing"
)

func test[X comparable](t *testing.T, wantType string, got X, want X) {
gotType := reflect.TypeOf(got).String()
if gotType != wantType {
t.Errorf("got type %s, want %s", gotType, wantType)
return
}

if got != want {
t.Errorf("got %v, wanted %v", got, want)
}
assert.Equal(t, wantType, reflect.TypeOf(got).String())
assert.Equal(t, want, got)
}

func testSlice[X ~[]E, E cmp.Ordered](t *testing.T, wantType string, got X, want X) {
gotType := reflect.TypeOf(got).String()
if gotType != wantType {
t.Errorf("got type %s, want %s", gotType, wantType)
return
}

if slices.Compare(got, want) != 0 {
t.Errorf("got %v, wanted %v", got, want)
}
assert.Equal(t, wantType, reflect.TypeOf(got).String())
assert.Equal(t, want, got)
}

// String
Expand Down
55 changes: 15 additions & 40 deletions http/http_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package http

import "testing"
import (
"github.com/stretchr/testify/assert"
"testing"
)

type User struct {
ID int `json:"id"`
Expand Down Expand Up @@ -35,20 +38,13 @@ func TestGet(t *testing.T) {
todo := Todo{}

err := Get("https://dummyjson.com/todos/1", &todo)
if err != nil {
t.Error(err)
}

if todo.ID != 1 {
t.Error("todo id not equal 1")
}
assert.Nil(t, err)
assert.Equal(t, todo.ID, 1)
}

func TestNilVGet(t *testing.T) {
err := Get("https://dummyjson.com/todos/1", nil)
if err != nil {
t.Error(err)
}
assert.Nil(t, err)
}

func TestGetWithToken(t *testing.T) {
Expand All @@ -59,25 +55,15 @@ func TestGetWithToken(t *testing.T) {
}

err := Post("https://dummyjson.com/auth/login", &body, &user)
if err != nil {
t.Error(err)
}

if user.ID != 15 {
t.Error("user id not equal 15")
}
assert.Nil(t, err)
assert.Equal(t, user.ID, 15)

token := user.Token
user = User{}

err = GetWithToken("https://dummyjson.com/auth/me", token, &user)
if err != nil {
t.Error(err)
}

if user.ID != 15 {
t.Error("user id not equal 15")
}
assert.Nil(t, err)
assert.Equal(t, user.ID, 15)
}

func TestPost(t *testing.T) {
Expand All @@ -89,19 +75,8 @@ func TestPost(t *testing.T) {
}

err := Post("https://dummyjson.com/todos/add", &body, &todo)
if err != nil {
t.Error(err)
}

if todo.Todo != "Test" {
t.Error("todo name not equal Test")
}

if !todo.Completed {
t.Error("todo completed not equal true")
}

if todo.UserID != 5 {
t.Error("todo user id not equal 5")
}
assert.Nil(t, err)
assert.Equal(t, todo.Todo, "Test")
assert.True(t, todo.Completed)
assert.Equal(t, todo.UserID, 5)
}
31 changes: 7 additions & 24 deletions output/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttputil"
"testing"
Expand Down Expand Up @@ -45,43 +46,31 @@ func TestCORSOptions(t *testing.T) {
CORSOptions(ctx)
})

contentType := string(resp.Header.Peek("Content-Type"))
if contentType != `text/html` {
t.Errorf("content-type: %s", contentType)
}
assert.Equal(t, string(resp.Header.Peek("Content-Type")), "text/html")
}

func TestOutputJson(t *testing.T) {
resp := makeRequest(func(ctx *fasthttp.RequestCtx) {
OutputJson(ctx, 200, out{Code: 123, Message: "test"})
})

body := string(resp.Body())
if body != `{"code":123,"message":"test"}` {
t.Errorf("body: %s", body)
}
assert.Equal(t, string(resp.Body()), `{"code":123,"message":"test"}`)
}

func TestJsonNoIndent(t *testing.T) {
resp := makeRequest(func(ctx *fasthttp.RequestCtx) {
JsonNoIndent(ctx, 200, out{Code: 123, Message: "test"})
})

body := string(resp.Body())
if body != `{"code":123,"message":"test"}` {
t.Errorf("body: %s", body)
}
assert.Equal(t, string(resp.Body()), `{"code":123,"message":"test"}`)
}

func TestJsonMessageResult(t *testing.T) {
resp := makeRequest(func(ctx *fasthttp.RequestCtx) {
JsonMessageResult(ctx, 200, "test")
})

body := string(resp.Body())
if body != `{"code":200,"message":"test"}` {
t.Errorf("body: %s", body)
}
assert.Equal(t, string(resp.Body()), `{"code":200,"message":"test"}`)
}

func getResult() []byte {
Expand All @@ -101,10 +90,7 @@ func TestFprint(t *testing.T) {
}
})

body := string(resp.Body())
if body != `{"code":200,"message":"test"}` {
t.Errorf("body: %s", body)
}
assert.Equal(t, string(resp.Body()), `{"code":200,"message":"test"}`)
}

func TestWrite(t *testing.T) {
Expand All @@ -116,10 +102,7 @@ func TestWrite(t *testing.T) {
}
})

body := string(resp.Body())
if body != `{"code":200,"message":"test"}` {
t.Errorf("body: %s", body)
}
assert.Equal(t, string(resp.Body()), `{"code":200,"message":"test"}`)
}

func BenchmarkFprint(b *testing.B) {
Expand Down
50 changes: 15 additions & 35 deletions validator/validator_test.go
Original file line number Diff line number Diff line change
@@ -1,68 +1,48 @@
package validator

import "testing"
import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestIs(t *testing.T) {
if !Is("[email protected]", "required,email") {
t.Error("Expected true, got false")
}
assert.True(t, Is("[email protected]", "required,email"))
}

func TestInSlice(t *testing.T) {
if !InSlice("ABC", []string{"ABC", "DEF"}) {
t.Error("Expected true, got false")
}

if InSlice("NOO", []string{"ABC", "DEF"}) {
t.Error("Expected false, got true")
}
assert.True(t, InSlice("ABC", []string{"ABC", "DEF"}))
assert.False(t, InSlice("NOO", []string{"ABC", "DEF"}))
}

func TestIsExchange(t *testing.T) {
if !IsExchange("binance") {
t.Error("Expected true, got false")
}
assert.True(t, IsExchange("binance"))
}

func TestIsExchangeWith(t *testing.T) {
if !IsExchangeWith("myexchange", []string{"myexchange"}) {
t.Error("Expected true, got false")
}
assert.True(t, IsExchangeWith("myexchange", []string{"myexchange"}))
}

func TestIsExchangeWithInit(t *testing.T) {
InitExchangeList([]string{"ownexchange"})
if !IsExchange("ownexchange") {
t.Error("Expected true, got false")
}
assert.True(t, IsExchange("ownexchange"))
}

func TestIsSymbol(t *testing.T) {
if !IsSymbol("BTC/USDT") {
t.Error("Expected true, got false")
}
assert.True(t, IsSymbol("BTC/USDT"))
}

func TestIsSymbolWith(t *testing.T) {
if !IsSymbolWith("BTC-USDT", "-") {
t.Error("Expected true, got false")
}
assert.True(t, IsSymbolWith("BTC-USDT", "-"))
}

func TestIsBuyerMakerStr(t *testing.T) {
if !IsBuyerMaker("buy") {
t.Error("Expected true, got false")
}
assert.True(t, IsBuyerMaker("buy"))
}

func TestIsBuyerMakerOC(t *testing.T) {
if !IsBuyerMakerOC(2.0, 3.0) {
t.Error("Expected true, got false")
}
assert.True(t, IsBuyerMakerOC(2.0, 3.0))
}

func TestIsEmpty(t *testing.T) {
if !IsEmpty(" ") {
t.Error("Expected true, got false")
}
assert.True(t, IsEmpty(" "))
}

0 comments on commit fc9e474

Please sign in to comment.