Skip to content

Commit

Permalink
chore: test types and utils (#79)
Browse files Browse the repository at this point in the history
* chore: test types and utils

* chore: fixed linting
  • Loading branch information
freak12techno authored Apr 29, 2024
1 parent ed6b4cd commit 43eb236
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pkg/types/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"errors"
"testing"

"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -36,3 +38,30 @@ func TestQueryErrorSerializeWithoutQueryError(t *testing.T) {
"Error mismatch!",
)
}

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

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

require.NoError(t, err)
assert.Equal(t, []byte("\"error\""), value)
}

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

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

require.NoError(t, err)
assert.Equal(t, "error", jsonErr.error)
}

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

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

import (
"main/pkg/constants"
"net/http"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/stretchr/testify/assert"
)

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)

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

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

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

require.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)

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

0 comments on commit 43eb236

Please sign in to comment.