Skip to content

Commit

Permalink
chore: test types and utils
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Apr 29, 2024
1 parent ed6b4cd commit 983159c
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
27 changes: 27 additions & 0 deletions pkg/types/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,30 @@ func TestQueryErrorSerializeWithoutQueryError(t *testing.T) {
"Error mismatch!",
)
}

func TestJsonErrorMarshalJson(t *testing.T) {
t.Parallel()

jsonErr := JSONError{error: "error"}
value, err := jsonErr.MarshalJSON()

assert.NoError(t, err)

Check failure on line 46 in pkg/types/error_test.go

View workflow job for this annotation

GitHub Actions / lint

require-error: for error assertions use require (testifylint)
assert.Equal(t, []byte("\"error\""), value)
}

func TestJsonErrorUnmarshalJson(t *testing.T) {
t.Parallel()

jsonErr := JSONError{}
err := jsonErr.UnmarshalJSON([]byte("error"))

assert.NoError(t, err)

Check failure on line 56 in pkg/types/error_test.go

View workflow job for this annotation

GitHub Actions / lint

require-error: for error assertions use require (testifylint)
assert.Equal(t, "error", jsonErr.error)
}

func TestJsonErrorToString(t *testing.T) {
t.Parallel()

jsonErr := JSONError{error: "error"}
assert.Equal(t, "error", jsonErr.Error())
}
89 changes: 89 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package utils

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

Check failure on line 4 in pkg/utils/utils_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
"main/pkg/constants"
"net/http"
"testing"
"time"
)

func TestFilter(t *testing.T) {
t.Parallel()

array := []int64{1, 2, 3}

filtered := Filter(array, func(value int64) bool {
return value == 2
})

assert.Len(t, filtered, 1)
assert.Equal(t, int64(2), filtered[0])
}

func TestMap(t *testing.T) {
t.Parallel()

array := []int64{1, 2, 3}

filtered := Map(array, func(value int64) int64 {
return value * 2
})

assert.Len(t, filtered, 3)
assert.Equal(t, int64(2), filtered[0])
assert.Equal(t, int64(4), filtered[1])
assert.Equal(t, int64(6), filtered[2])
}

func TestContains(t *testing.T) {
t.Parallel()

array := []int64{1, 2, 3}

assert.True(t, Contains(array, 3))
assert.False(t, Contains(array, 4))
}

func TestFormatDuration(t *testing.T) {
t.Parallel()

duration := time.Hour*24 + time.Hour*2 + time.Second*4
formatted := FormatDuration(duration)

assert.Equal(t, "1 day 2 hours 4 seconds", formatted)
}

func TestGetBlockFromHeaderNoValue(t *testing.T) {
t.Parallel()

header := http.Header{}
value, err := GetBlockHeightFromHeader(header)

assert.NoError(t, err)

Check failure on line 63 in pkg/utils/utils_test.go

View workflow job for this annotation

GitHub Actions / lint

require-error: for error assertions use require (testifylint)
assert.Equal(t, int64(0), value)
}

func TestGetBlockFromHeaderInvalidValue(t *testing.T) {
t.Parallel()

header := http.Header{
constants.HeaderBlockHeight: []string{"invalid"},
}
value, err := GetBlockHeightFromHeader(header)

assert.Error(t, err)
assert.Equal(t, int64(0), value)
}

func TestGetBlockFromHeaderValidValue(t *testing.T) {
t.Parallel()

header := http.Header{
constants.HeaderBlockHeight: []string{"123"},
}
value, err := GetBlockHeightFromHeader(header)

assert.NoError(t, err)
assert.Equal(t, int64(123), value)
}

0 comments on commit 983159c

Please sign in to comment.