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/go.mod b/go.mod index 7db6e78..cca3345 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/spf13/afero v1.2.2 // indirect github.com/spf13/cobra v1.1.3 github.com/spf13/viper v1.7.1 - github.com/stretchr/testify v1.6.1 // indirect + github.com/stretchr/testify v1.6.1 github.com/superoo7/go-gecko v1.0.0 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c golang.org/x/sys v0.0.0-20210503173754-0981d6026fa6 // indirect 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/display/utilitywidgets/currency.go b/pkg/display/utilitywidgets/currency.go index 79dd964..01ea01d 100644 --- a/pkg/display/utilitywidgets/currency.go +++ b/pkg/display/utilitywidgets/currency.go @@ -48,7 +48,7 @@ type CurrencyTable struct { IDMap *CurrencyIDMap } -// Currency holds information of a single currency, it used to populate currencyIDMaps +// CurrencyValue holds information of a single currency, it used to populate currencyIDMaps type CurrencyValue struct { Symbol string RateUSD float64 diff --git a/pkg/display/utilitywidgets/utility.go b/pkg/display/utilitywidgets/utility.go index f4c2c55..bcb73aa 100644 --- a/pkg/display/utilitywidgets/utility.go +++ b/pkg/display/utilitywidgets/utility.go @@ -16,13 +16,20 @@ limitations under the License. package utilitywidgets +// Utility represents a utilty displayed in the UI type Utility int const ( + // None is when no utility is being displayed and the page is visible None Utility = iota + // Help is when the help table is displayed Help + // Portfolio is when the mini portfolio table is displayed Portfolio + // Change is when the change interval table is displayed Change + // Duration is when the graph duration interval table is displayed Duration + // Currency is when the currency selection table is displayed Currency ) diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go new file mode 100644 index 0000000..ba80ee5 --- /dev/null +++ b/pkg/utils/utils_test.go @@ -0,0 +1,126 @@ +/* +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 utils_test + +import ( + "testing" + + "github.com/Gituser143/cryptgo/pkg/utils" + "github.com/stretchr/testify/assert" +) + +func TestMinFloat64(t *testing.T) { + tests := []struct { + inputVal []float64 + expectedVal float64 + }{ + { + inputVal: []float64{10, 20, 30, 40}, + expectedVal: 10, + }, + { + inputVal: []float64{}, + expectedVal: 0, + }, + { + inputVal: []float64{-10, 20, -30, 40}, + expectedVal: -30, + }, + } + + for _, test := range tests { + val := utils.MinFloat64(test.inputVal...) + assert.Equal(t, test.expectedVal, val) + } +} + +func TestMaxFloat64(t *testing.T) { + tests := []struct { + inputVal []float64 + expectedVal float64 + }{ + { + inputVal: []float64{10, 20, 30, 40}, + expectedVal: 40, + }, + { + inputVal: []float64{}, + expectedVal: 0, + }, + { + inputVal: []float64{-10, -20, -30, -40}, + expectedVal: -10, + }, + } + + for _, test := range tests { + val := utils.MaxFloat64(test.inputVal...) + 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) + } +}