Merge branch 'master' into EMS-559-use-decimal-type-for-prices

This commit is contained in:
Patryk Kowalewski 2023-10-31 07:54:52 +01:00
commit 488943cead
3 changed files with 93 additions and 1 deletions

View file

@ -15,6 +15,7 @@ import (
"testing"
"testing/quick"
"time"
"gopkg.in/mgo.v2/bson"
)
type testEnt struct {
@ -3339,3 +3340,77 @@ func ExampleNewFromFloat() {
//0.123123123123123
//-10000000000000
}
func TestBSON(t *testing.T) {
// Capture positive and negative cases of floating numbers and whole numbers in various bit ranges; also copied scientific notation
// test cases from above
tests := []string{
"3.14159",
"42",
"42949672960",
"18446744073709551616000",
"-3.14159",
"-42",
"-42949672960",
"-18446744073709551616000",
"0",
"1e9",
"2.41E-3",
"24.2E-4",
"243E-5",
"1e-5",
"245E3",
"1.2345E-1",
"0e5",
"0e-5",
"123.456e0",
"123.456e2",
"123.456e10",
}
type decStruct struct {
Dec Decimal
}
// For each test the idea is that the String output of the original parsed decimal should match the String output of the
// decimal after it has been marshalled and unmarshalled into BSON
for i := range tests {
d, errD := NewFromString(tests[i])
if errD != nil {
t.Errorf("TestBSON failed decimal parsing for case %v because of parse error - %v", tests[i], errD)
}
exp := d.String()
// Test structure marshalling first
s1 := decStruct{Dec: d}
data, err := bson.Marshal(s1)
if err != nil {
t.Errorf("TestBSON failed structure marshalling for case %v because of marshal error - %v", tests[i], err)
}
s2 := decStruct{}
err = bson.Unmarshal(data, &s2)
if err != nil {
t.Errorf("TestBSON failed structure marshalling for case %v because of unmarshal error - %v", tests[i], err)
}
res := s2.Dec.String()
if exp != res {
t.Errorf("TestBSON failed structure marshalling for case %v got %v expected %v", tests[i], res, exp)
}
// Next test map marshalling
m := bson.M{"dec": d}
data2, err2 := bson.Marshal(m)
if err2 != nil {
t.Errorf("TestBSON failed map marshalling for case %v because of marshal error - %v", tests[i], err2)
}
m2 := make(bson.M)
err2 = bson.Unmarshal(data2, m2)
if err2 != nil {
t.Errorf("TestBSON failed map marshalling for case %v because of unmarshal error - %v", tests[i], err2)
}
d2, errD2 := NewFromString(m2["dec"].(bson.Decimal128).String())
if errD2 != nil {
t.Errorf("TestBSON failed map marshalling for case %v because of parse error - %v", tests[i], errD2)
}
res2 := d2.String()
if exp != res2 {
t.Errorf("TestBSON failed map marshalling for case %v got %v expected %v", tests[i], res2, exp)
}
}
}

7
go.mod
View file

@ -2,4 +2,9 @@ module github.com/husarlabs/decimal
go 1.15
require go.mongodb.org/mongo-driver v1.12.1
require (
go.mongodb.org/mongo-driver v1.12.1
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
gopkg.in/yaml.v2 v2.4.0 // indirect
)

12
go.sum
View file

@ -4,6 +4,11 @@ github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
@ -42,3 +47,10 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw=
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=