Skip to content

Commit

Permalink
Unit tests (#28)
Browse files Browse the repository at this point in the history
* Add tests for RoundValues

* Add tests for filterRows

* Add test run to CI

* additional filter by name casesi + empty cases
  • Loading branch information
nolancon authored Oct 3, 2021
1 parent b916ac3 commit 932c593
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ jobs:

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
134 changes: 134 additions & 0 deletions pkg/display/allcoin/allCoin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
Copyright © 2021 Bhargav SNV [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package allcoin

import (
"sync"
"testing"

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

func TestFilterRows(t *testing.T) {
tests := []struct {
allRows [][]string
filter string
filteredRows [][]string
}{
{
allRows: [][]string{
{"", "BTC", "", "", "", "bitcoin"},
{"", "ETH", "", "", "", "ethereum"},
{"", "LTC", "", "", "", "litecoin"},
{"", "BTC", "", "", "", "bitcoin"},
{"", "BTC", "", "", "", "bitcoin"},
},
filter: "bitcoin",
filteredRows: [][]string{
{"", "BTC", "", "", "", "bitcoin"},
{"", "BTC", "", "", "", "bitcoin"},
{"", "BTC", "", "", "", "bitcoin"},
},
},
{
allRows: [][]string{
{"", "BTC", "", "", "", "bitcoin"},
{"", "ETH", "", "", "", "ethereum"},
{"", "LTC", "", "", "", "litecoin"},
{"", "BTC", "", "", "", "bitcoin"},
{"", "BTC", "", "", "", "bitcoin"},
},
filter: "LTC",
filteredRows: [][]string{
{"", "LTC", "", "", "", "litecoin"},
},
},
{
allRows: [][]string{
{"", "XRP", "", "", "", "ripple"},
{"", "ETH", "", "", "", "ethereum"},
{"", "LTC", "", "", "", "litecoin"},
{"", "BTC", "", "", "", "bitcoin"},
},
filter: "ABA",
filteredRows: [][]string{},
},
{
allRows: [][]string{
{"", "XRP", "", "", "", "ripple"},
{"", "ETH", "", "", "", "ethereum"},
{"", "LTC", "", "", "", "litecoin"},
{"", "BTC", "", "", "", "bitcoin"},
{"", "XRP", "", "", "", "ripple"},
},
filter: "ripple",
filteredRows: [][]string{
{"", "XRP", "", "", "", "ripple"},
{"", "XRP", "", "", "", "ripple"},
},
},
{
allRows: [][]string{
{"", "BNB", "", "", "", "binance"},
{"", "ETH", "", "", "", "ethereum"},
{"", "DOGE", "", "", "", "dogecoin"},
{"", "BTC", "", "", "", "bitcoin"},
{"", "XRP", "", "", "", "ripple"},
},
filter: "dogecoin",
filteredRows: [][]string{
{"", "DOGE", "", "", "", "dogecoin"},
},
},
{
allRows: [][]string{
{"", "BNB", "", "", "", "binance"},
{"", "ETH", "", "", "", "ethereum"},
{"", "BTC", "", "", "", "bitcoin"},
{"", "XRP", "", "", "", "ripple"},
},
filter: "dogecoin",
filteredRows: [][]string{},
},
{
allRows: [][]string{},
filter: "litecoin",
filteredRows: [][]string{},
},
{
allRows: [][]string{
{"", "BNB", "", "", "", "binance"},
{"", "ETH", "", "", "", "ethereum"},
{"", "BTC", "", "", "", "bitcoin"},
{"", "XRP", "", "", "", "ripple"},
},
filter: "",
filteredRows: [][]string{
{"", "BNB", "", "", "", "binance"},
{"", "ETH", "", "", "", "ethereum"},
{"", "BTC", "", "", "", "bitcoin"},
{"", "XRP", "", "", "", "ripple"},
},
},
}

for _, test := range tests {
var mutex = &sync.Mutex{}
filteredRows := filterRows(test.allRows, test.filter, mutex)
assert.Equal(t, test.filteredRows, filteredRows)
}
}
52 changes: 52 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,55 @@ func TestMaxFloat64(t *testing.T) {
assert.Equal(t, test.expectedVal, val)
}
}

func TestRoundValues(t *testing.T) {
tests := []struct {
name string
inputNum1 float64
inputNum2 float64
expRoundedVals []float64
expUnit string
}{
{
name: "both values smaller than kilo",
inputNum1: 250,
inputNum2: 500,
expRoundedVals: []float64{250, 500},
expUnit: "",
},
{
name: "both values kilo",
inputNum1: 80000,
inputNum2: 9000,
expRoundedVals: []float64{80, 9},
expUnit: "K",
},
{
name: "mega and less than mega",
inputNum1: 400000,
inputNum2: 7000000,
expRoundedVals: []float64{0.4, 7},
expUnit: "M",
},
{
name: "giga and less than giga",
inputNum1: 100000000,
inputNum2: 9000000000,
expRoundedVals: []float64{0.1, 9},
expUnit: "B",
},
{
name: "both values tera",
inputNum1: 5000000000000000,
inputNum2: 100000000000000,
expRoundedVals: []float64{5000, 100},
expUnit: "T",
},
}

for _, test := range tests {
roundedVals, unit := utils.RoundValues(test.inputNum1, test.inputNum2)
assert.Equal(t, test.expRoundedVals, roundedVals)
assert.Equal(t, test.expUnit, unit)
}
}

0 comments on commit 932c593

Please sign in to comment.