Skip to content

Commit

Permalink
chore: add utils test and CI action (#44)
Browse files Browse the repository at this point in the history
* chore: add utils test and CI action

* chore: fixed linting
  • Loading branch information
freak12techno authored Nov 3, 2024
1 parent e572d45 commit 77d2c30
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,19 @@ jobs:
uses: golangci/golangci-lint-action@v2
with:
args: --timeout 300s
test:
name: test
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v5
with:
go-version: '1.23.0'
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Run tests
run: go test -coverprofile coverage.txt -coverpkg ./... -v ./...
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4.3.0
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: freak12techno/grafana-interacter
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ main
dist/
grafana-interacter
**/.DS_Store
cover.out
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ linters:
- goimports
- mnd
- perfsprint
- testpackage
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ install:

lint:
golangci-lint run --fix ./...

test:
go test -coverprofile cover.out -coverpkg ./... -v ./...

coverage:
go tool cover -html=cover.out
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ require (
github.com/creasty/defaults v1.8.0
github.com/rs/zerolog v1.33.0
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.8.0
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c
golang.org/x/sync v0.8.0
gopkg.in/telebot.v3 v3.3.8
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.12.0 // indirect
)
75 changes: 75 additions & 0 deletions pkg/utils/generic/generic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package generic

import (
"testing"

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

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 TestFind(t *testing.T) {
t.Parallel()

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

_, found := Find(array, func(value int64) bool {
return value == 2
})
require.True(t, found)

value, found2 := Find(array, func(value int64) bool {
return value == 4
})
require.Nil(t, value)
require.False(t, found2)
}

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

map1 := map[string]string{"a": "1"}
map2 := map[string]string{"b": "2"}

resultMap := MergeMaps(map1, map2)
require.Equal(t, map[string]string{"a": "1", "b": "2"}, resultMap)
}

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

array := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}

chunks1 := SplitArrayIntoChunks(array, 3)
require.Equal(t, [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, chunks1)

chunks2 := SplitArrayIntoChunks(array, 5)
require.Equal(t, [][]int{{1, 2, 3, 4, 5}, {6, 7, 8, 9}}, chunks2)
}
12 changes: 12 additions & 0 deletions pkg/utils/normalize/normalize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package normalize

import (
"testing"

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

func TestNormalizeString(t *testing.T) {
t.Parallel()
require.Equal(t, "abc", NormalizeString("abcабв"))
}

0 comments on commit 77d2c30

Please sign in to comment.