Added tests to decimal.go, completed map_test.go

This commit is contained in:
andronache
2020-11-04 16:50:45 +02:00
committed by Dan Christian Bogos
parent dbdfee42a7
commit 1bd762ef77
3 changed files with 200 additions and 21 deletions

86
utils/decimal_test.go Normal file
View File

@@ -0,0 +1,86 @@
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package utils
import (
"reflect"
"testing"
"github.com/ericlagergren/decimal"
)
func TestNewDecimalFromFloat64(t *testing.T) {
expected := &Decimal{new(decimal.Big).SetFloat64(1.25)}
received := NewDecimalFromFloat64(1.25)
if !reflect.DeepEqual(expected, received) {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
}
func TestNewDecimal(t *testing.T) {
expected := &Decimal{new(decimal.Big)}
received := NewDecimal()
if !reflect.DeepEqual(expected, received) {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
}
func TestDecimalFloat64(t *testing.T) {
expected := 3.2795784983858396
received := NewDecimalFromFloat64(3.2795784983858396).Float64()
if expected != received {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
}
func TestDecimalFloat64Negative(t *testing.T) {
expected := -3.2795784983858396
received := NewDecimalFromFloat64(-3.2795784983858396).Float64()
if expected != received {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
}
/*
func TestDecimalMarshalJSON(t *testing.T) {
expected := []byte("3.27")
a := NewDecimal()
received, err := NewDecimalFromFloat64(3.27).MarshalJSON()
if err != nil {
t.Errorf("Expecting: nil, received: %+v", received)
}
fmt.Println(string(expected))
fmt.Println(string(received))
if !reflect.DeepEqual(expected, received) {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
a.UnmarshalJSON(received)
fmt.Println(a.Float64())
}
func TestDecimalUnmarshalJSON(t *testing.T) {
expected :=
received :=
if !reflect.DeepEqual(expected, received) {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
}
*/

View File

@@ -168,14 +168,6 @@ func (sm StringMap) GetOne() string {
return EmptyString
}
func (sm StringMap) Join(mps ...StringMap) {
for _, mp := range mps {
for k, v := range mp {
sm[k] = v
}
}
}
func (sm StringMap) HasKey(key string) (has bool) {
_, has = sm[key]
return
@@ -196,18 +188,6 @@ func MergeMapsStringIface(mps ...map[string]interface{}) (outMp map[string]inter
return
}
// FieldMultiplyFactor defines multiply factors for different field values
// original defined for CDRE component
type FieldMultiplyFactor map[string]float64
func (fmp FieldMultiplyFactor) Clone() (cln FieldMultiplyFactor) {
cln = make(FieldMultiplyFactor, len(fmp))
for k, v := range fmp {
cln[k] = v
}
return
}
func MapStringToInt64(in map[string]string) (out map[string]int64, err error) {
mapout := make(map[string]int64, len(in))
for key, val := range in {

View File

@@ -102,7 +102,7 @@ func TestMapKeys(t *testing.T) {
}
}
func MapKeysStringMapParse(t *testing.T) {
func TestMapKeysStringMapParse(t *testing.T) {
if sm := ParseStringMap(EmptyString); len(sm) != 0 {
t.Errorf("Expecting %+v, received %+v", 0, len(sm))
}
@@ -346,3 +346,116 @@ func TestFlagsToSlice2(t *testing.T) {
t.Errorf("Expecting: %+v, received: %+v", sls, flgSls)
}
}
func TestNewStringMap(t *testing.T) {
expected := StringMap{"item1": true, "item2": true, "negitem1": false}
received := NewStringMap("item1", "item2", "!negitem1")
if !reflect.DeepEqual(expected, received) {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
}
func TestNewStringMap2(t *testing.T) {
expected := StringMap{"test": true, "test!": true, "t!est": true}
received := NewStringMap("test", "test!", "t!est")
if !reflect.DeepEqual(expected, received) {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
}
func TestStringMapEqual1(t *testing.T) {
expected := false
received := new(StringMap).Equal(StringMap{"test": true})
if expected != received {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
}
func TestStringMapEqual2(t *testing.T) {
expected := false
received := StringMap{"test": true, "test2": true}.Equal(StringMap{"test": true})
if expected != received {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
}
func TestStringMapEqual3(t *testing.T) {
expected := true
received := StringMap{"test": true, "test2": true}.Equal(StringMap{"test": true, "test2": true})
if expected != received {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
}
func TestStringMapIncludes1(t *testing.T) {
expected := false
received := StringMap{"test": true}.Includes(StringMap{"test": true, "test2": true})
if expected != received {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
}
func TestStringMapIncludes2(t *testing.T) {
expected := false
received := StringMap{"test": true, "test2": true}.Includes(StringMap{"test3": true})
if expected != received {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
}
func TestStringMapIncludes3(t *testing.T) {
expected := true
received := StringMap{"test": true, "test2": true}.Includes(StringMap{"test": true, "test2": true})
if expected != received {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
}
func TestStringMapSlice(t *testing.T) {
expected := []string{"test", "test2"}
received := StringMap{"test": true, "test2": true}.Slice()
if !reflect.DeepEqual(expected, received) {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
}
func TestStringMapClone(t *testing.T) {
expected := StringMap{"test": true, "test2": true}
received := StringMap{"test": true, "test2": true}.Clone()
if !reflect.DeepEqual(expected, received) {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
}
func TestStringMapString(t *testing.T) {
expected := "test;test2"
received := StringMap{"test": true, "test2": true}.String()
if !reflect.DeepEqual(expected, received) {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
}
func TestStringMapGetOneEmpty(t *testing.T) {
expected := EmptyString
received := StringMap{}.GetOne()
if !reflect.DeepEqual(expected, received) {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
}
func TestStringMapGetOneNotEmpty(t *testing.T) {
expected := "test"
received := StringMap{"test": true, "test2": true}.GetOne()
if !reflect.DeepEqual(expected, received) {
t.Errorf("Expecting: %+v, received: %+v", expected, received)
}
}
func TestMapStringToInt64Err(t *testing.T) {
t2 := map[string]string{"test": "a"}
_, err := MapStringToInt64(t2)
if err == nil {
t.Error("Got Error: ", err)
}
}