Skip to content

Commit

Permalink
Increase test coverage of accounting and improve some test packages (
Browse files Browse the repository at this point in the history
  • Loading branch information
carpawell authored Jul 5, 2024
2 parents 81aa461 + b069242 commit 6d3ce31
Show file tree
Hide file tree
Showing 53 changed files with 867 additions and 1,283 deletions.
91 changes: 50 additions & 41 deletions accounting/decimal_test.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,65 @@
package accounting_test

import (
"math/rand"
"testing"

v2accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting"
apiaccounting "github.com/nspcc-dev/neofs-api-go/v2/accounting"
"github.com/nspcc-dev/neofs-sdk-go/accounting"
accountingtest "github.com/nspcc-dev/neofs-sdk-go/accounting/test"
"github.com/stretchr/testify/require"
)

func TestDecimalData(t *testing.T) {
const v, p = 4, 2

func testDecimalField[T uint32 | int64](
t *testing.T,
get func(accounting.Decimal) T,
set func(*accounting.Decimal, T),
getAPI func(*apiaccounting.Decimal) T,
) {
var d accounting.Decimal

require.Zero(t, d.Value())
require.Zero(t, d.Precision())

d.SetValue(v)
d.SetPrecision(p)

require.EqualValues(t, v, d.Value())
require.EqualValues(t, p, d.Precision())
require.Zero(t, get(d))

val := T(rand.Uint64())
set(&d, val)
require.EqualValues(t, val, get(d))
valOther := val + 1
set(&d, valOther)
require.EqualValues(t, valOther, get(d))

t.Run("encoding", func(t *testing.T) {
t.Run("binary", func(t *testing.T) {
var src, dst accounting.Decimal

set(&dst, val)
require.NoError(t, dst.Unmarshal(src.Marshal()))
require.Zero(t, get(dst))

set(&src, val)
require.NoError(t, dst.Unmarshal(src.Marshal()))
require.EqualValues(t, val, get(dst))
})
t.Run("api", func(t *testing.T) {
var src, dst accounting.Decimal
var msg apiaccounting.Decimal

set(&dst, val)
src.WriteToV2(&msg)
require.Zero(t, getAPI(&msg))
require.NoError(t, dst.ReadFromV2(msg))
require.Zero(t, get(dst))

set(&src, val)
src.WriteToV2(&msg)
require.EqualValues(t, val, getAPI(&msg))
require.NoError(t, dst.ReadFromV2(msg))
require.EqualValues(t, val, get(dst))
})
})
}

func TestDecimalMessageV2(t *testing.T) {
var (
d accounting.Decimal
m v2accounting.Decimal
)

m.SetValue(7)
m.SetPrecision(8)

require.NoError(t, d.ReadFromV2(m))

require.EqualValues(t, m.GetValue(), d.Value())
require.EqualValues(t, m.GetPrecision(), d.Precision())

var m2 v2accounting.Decimal

d.WriteToV2(&m2)

require.EqualValues(t, d.Value(), m2.GetValue())
require.EqualValues(t, d.Precision(), m2.GetPrecision())
func TestDecimal_SetValue(t *testing.T) {
testDecimalField(t, accounting.Decimal.Value, (*accounting.Decimal).SetValue, (*apiaccounting.Decimal).GetValue)
}

func TestDecimal_Marshal(t *testing.T) {
d := accountingtest.Decimal()

var d2 accounting.Decimal
require.NoError(t, d2.Unmarshal(d.Marshal()))

require.Equal(t, d, d2)
func TestDecimal_SetPrecision(t *testing.T) {
testDecimalField(t, accounting.Decimal.Precision, (*accounting.Decimal).SetPrecision, (*apiaccounting.Decimal).GetPrecision)
}
26 changes: 0 additions & 26 deletions accounting/example_test.go

This file was deleted.

23 changes: 23 additions & 0 deletions accounting/test/decimal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package accountingtest_test

import (
"testing"

apiaccounting "github.com/nspcc-dev/neofs-api-go/v2/accounting"
"github.com/nspcc-dev/neofs-sdk-go/accounting"
accountingtest "github.com/nspcc-dev/neofs-sdk-go/accounting/test"
"github.com/stretchr/testify/require"
)

func TestDecimal(t *testing.T) {
d := accountingtest.Decimal()
require.NotEqual(t, d, accountingtest.Decimal())

var m apiaccounting.Decimal
d.WriteToV2(&m)
var d2 accounting.Decimal
require.NoError(t, d2.ReadFromV2(m))
require.Equal(t, d, d2)

require.NoError(t, new(accounting.Decimal).Unmarshal(d.Marshal()))
}
81 changes: 0 additions & 81 deletions audit/collect.go

This file was deleted.

6 changes: 0 additions & 6 deletions audit/doc.go

This file was deleted.

25 changes: 0 additions & 25 deletions audit/example_test.go

This file was deleted.

Loading

0 comments on commit 6d3ce31

Please sign in to comment.