diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 639d4f5..2f55b49 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -22,3 +22,6 @@ jobs: - name: Build run: go build -v ./... + + - name: Test + run: go test -v ./... diff --git a/pkg/display/allcoin/allCoin_test.go b/pkg/display/allcoin/allCoin_test.go new file mode 100644 index 0000000..46ac09a --- /dev/null +++ b/pkg/display/allcoin/allCoin_test.go @@ -0,0 +1,134 @@ +/* +Copyright © 2021 Bhargav SNV bhargavsnv100@gmail.com + +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) + } +} diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index ff68b5f..ba80ee5 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -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) + } +}