Skip to content

Commit

Permalink
Merge pull request lavanet#619 from lavanet/CNS-503-cleanup-unit-tests
Browse files Browse the repository at this point in the history
CNS-503 cleanup unit tests (part 1)
  • Loading branch information
Yaroms authored Jul 19, 2023
2 parents 2a53588 + d72f0c5 commit 16cbd7f
Show file tree
Hide file tree
Showing 12 changed files with 1,307 additions and 1,279 deletions.
2 changes: 1 addition & 1 deletion common/types/constants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package types

const (
STALE_ENTRY_TIME int64 = 1440 // 1440 blocks (equivalent to 24 hours when block_time = 1min)
STALE_ENTRY_TIME = 1440 // 1440 blocks (equivalent to 24 hours when block_time = 1min)
MODULE_NAME string = "common"
)
53 changes: 21 additions & 32 deletions common/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,53 @@ import (
"golang.org/x/exp/constraints"
)

func FindMin[T constraints.Ordered](s []T) T {
if len(s) == 0 {
var zero T
return zero
}
m := s[0]
for _, v := range s {
if m > v {
m = v
func FindMin[T constraints.Ordered](s []T) (m T) {
if len(s) > 0 {
m = s[0]
for _, v := range s[1:] {
if m > v {
m = v
}
}
}
return m
}

func FindMax[T constraints.Ordered](s []T) T {
if len(s) == 0 {
var zero T
return zero
}
m := s[0]
for _, v := range s {
if m < v {
m = v
func FindMax[T constraints.Ordered](s []T) (m T) {
if len(s) > 0 {
m = s[0]
for _, v := range s[1:] {
if m < v {
m = v
}
}
}
return m
}

func Intersection[T comparable](arrays ...[]T) []T {
// Create a map to store the elements and their occurrence count
elements := make(map[T]int)

// Iterate through each array
for _, arr := range arrays {
// Create a map to store the elements of the current array
arrElements := make(map[T]bool)

// Populate the map with elements from the current array
for _, elem := range arr {
arrElements[elem] = true
}

// Increment the occurrence count for each element in the map
for elem := range arrElements {
elements[elem]++
if _, ok := arrElements[elem]; !ok {
arrElements[elem] = true
elements[elem]++
}
}
}

var intersection []T
res := make([]T, 0)

// Check the occurrence count of each element
for elem, count := range elements {
if count == len(arrays) {
intersection = append(intersection, elem)
res = append(res, elem)
}
}

return intersection
return res
}

func Union[T comparable](arrays ...[]T) []T {
Expand Down
70 changes: 70 additions & 0 deletions common/types/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package types

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestFindMin(t *testing.T) {
for _, tt := range []struct {
name string
slice []int
min int
}{
{"empty", []int{}, 0},
{"one element", []int{1}, 1},
{"min is first", []int{1, 2, 3}, 1},
{"min is middle", []int{2, 1, 3}, 1},
{"min is last", []int{3, 2, 1}, 1},
{"min is zero", []int{3, 0, 1}, 0},
{"min < zero", []int{3, -2, 1}, -2},
{"min twice", []int{3, 1, 1}, 1},
} {
t.Run(tt.name, func(t *testing.T) {
min := FindMin(tt.slice)
require.Equal(t, tt.min, min)
})
}
}

func TestFindMax(t *testing.T) {
for _, tt := range []struct {
name string
slice []int
max int
}{
{"empty", []int{}, 0},
{"one element", []int{1}, 1},
{"max is first", []int{3, 2, 1}, 3},
{"max is middle", []int{2, 1, 3}, 3},
{"max is last", []int{1, 2, 3}, 3},
{"max is zero", []int{-3, 0, -1}, 0},
{"max < zero", []int{-3, -2, -5}, -2},
{"max twice", []int{1, 3, 3}, 3},
} {
t.Run(tt.name, func(t *testing.T) {
max := FindMax(tt.slice)
require.Equal(t, tt.max, max)
})
}
}

func TestIntersection(t *testing.T) {
for _, tt := range []struct {
name string
slices [][]int
result []int
}{
{"zero slices", [][]int{}, []int{}},
{"one slice", [][]int{{1, 2}}, []int{1, 2}},
{"two slices, one empty", [][]int{{1, 2}, {}}, []int{}},
{"two slices, non empty", [][]int{{1, 2, 3}, {1, 3}}, []int{1, 3}},
} {
t.Run(tt.name, func(t *testing.T) {
res := Intersection(tt.slices...)
require.Subset(t, tt.result, res)
require.Subset(t, res, tt.result)
})
}
}
42 changes: 2 additions & 40 deletions testutil/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
conflictconstruct "github.com/lavanet/lava/x/conflict/types/construct"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
"github.com/lavanet/lava/x/pairing/types"
plantypes "github.com/lavanet/lava/x/plans/types"
spectypes "github.com/lavanet/lava/x/spec/types"
subscriptiontypes "github.com/lavanet/lava/x/subscription/types"
"github.com/stretchr/testify/require"
Expand All @@ -23,47 +22,10 @@ type Account struct {
Addr sdk.AccAddress
}

func CreateMockSpec() spectypes.Spec {
specName := "mockSpec"
spec := spectypes.Spec{}
spec.Name = specName
spec.Index = specName
spec.Enabled = true
spec.ReliabilityThreshold = 4294967295
spec.BlockDistanceForFinalizedData = 0
spec.DataReliabilityEnabled = true
spec.MinStakeClient = sdk.NewCoin(epochstoragetypes.TokenDenom, sdk.NewInt(100))
spec.MinStakeProvider = sdk.NewCoin(epochstoragetypes.TokenDenom, sdk.NewInt(1000))
spec.ApiCollections = []*spectypes.ApiCollection{{Enabled: true, CollectionData: spectypes.CollectionData{ApiInterface: "stub", Type: "GET"}, Apis: []*spectypes.Api{{Name: specName + "API", ComputeUnits: 100, Enabled: true}}}}
spec.BlockDistanceForFinalizedData = 0
return spec
}

func CreateMockPlan() plantypes.Plan {
policy := plantypes.Policy{
TotalCuLimit: 100000,
EpochCuLimit: 10000,
MaxProvidersToPair: 3,
GeolocationProfile: 1,
}
plan := plantypes.Plan{
Index: "mockPlan",
Description: "plan for testing",
Type: "rpc",
Block: 100,
Price: sdk.NewCoin("ulava", sdk.NewInt(100)),
AllowOveruse: true,
OveruseRate: 10,
AnnualDiscountPercentage: 20,
PlanPolicy: policy,
}

return plan
}

func CreateNewAccount(ctx context.Context, keepers testkeeper.Keepers, balance int64) (acc Account) {
acc.SK, acc.Addr = sigs.GenerateFloatingKey()
keepers.BankKeeper.SetBalance(sdk.UnwrapSDKContext(ctx), acc.Addr, sdk.NewCoins(sdk.NewCoin(epochstoragetypes.TokenDenom, sdk.NewInt(balance))))
coins := sdk.NewCoins(sdk.NewCoin(epochstoragetypes.TokenDenom, sdk.NewInt(balance)))
keepers.BankKeeper.SetBalance(sdk.UnwrapSDKContext(ctx), acc.Addr, coins)
return
}

Expand Down
51 changes: 51 additions & 0 deletions testutil/common/mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package common

import (
sdk "github.com/cosmos/cosmos-sdk/types"
epochstoragetypes "github.com/lavanet/lava/x/epochstorage/types"
plantypes "github.com/lavanet/lava/x/plans/types"
spectypes "github.com/lavanet/lava/x/spec/types"
)

func CreateMockSpec() spectypes.Spec {
specName := "mock_spec"
spec := spectypes.Spec{}
spec.Name = specName
spec.Index = specName
spec.Enabled = true
spec.ReliabilityThreshold = 4294967295
spec.BlockDistanceForFinalizedData = 0
spec.DataReliabilityEnabled = true
spec.MinStakeClient = sdk.NewCoin(epochstoragetypes.TokenDenom, sdk.NewInt(100))
spec.MinStakeProvider = sdk.NewCoin(epochstoragetypes.TokenDenom, sdk.NewInt(1000))
spec.ApiCollections = []*spectypes.ApiCollection{{Enabled: true, CollectionData: spectypes.CollectionData{ApiInterface: "stub", Type: "GET"}, Apis: []*spectypes.Api{{Name: specName + "API", ComputeUnits: 100, Enabled: true}}}}
spec.BlockDistanceForFinalizedData = 0
return spec
}

func CreateMockPlan() plantypes.Plan {
plan := plantypes.Plan{
Index: "mock_plan",
Description: "plan for testing",
Type: "rpc",
Block: 100,
Price: sdk.NewCoin("ulava", sdk.NewInt(100)),
AllowOveruse: true,
OveruseRate: 10,
AnnualDiscountPercentage: 20,
PlanPolicy: CreateMockPolicy(),
}

return plan
}

func CreateMockPolicy() plantypes.Policy {
policy := plantypes.Policy{
TotalCuLimit: 100000,
EpochCuLimit: 10000,
MaxProvidersToPair: 3,
GeolocationProfile: 1,
}

return policy
}
Loading

0 comments on commit 16cbd7f

Please sign in to comment.