Repository: ovrclk/akash
Pull request #662 (2020-06-12 19:25:01)

tests: x/deployment/handler - test emit events


Patch:
diff --git a/sdkutil/event.go b/sdkutil/event.go
index 71db7d6e..96656a8a 100644
--- a/sdkutil/event.go
+++ b/sdkutil/event.go
@@ -25,6 +25,10 @@ var (
 	ErrUnknownAction = errors.New("Unknown action")
 )
 
+type ModuleEvent interface {
+	ToSDKEvent() sdk.Event
+}
+
 // Event stores type, module, action and attributes list of sdk
 type Event struct {
 	Type       string
diff --git a/x/deployment/types/event.go b/x/deployment/types/event.go
index 4c8be706..d4f1eb8c 100644
--- a/x/deployment/types/event.go
+++ b/x/deployment/types/event.go
@@ -114,7 +114,7 @@ func ParseEVGroupID(attrs []sdk.Attribute) (GroupID, error) {
 
 // ParseEvent parses event and returns details of event and error if occurred
 // TODO: Enable returning actual events.
-func ParseEvent(ev sdkutil.Event) (interface{}, error) {
+func ParseEvent(ev sdkutil.Event) (sdkutil.ModuleEvent, error) {
 	if ev.Type != sdkutil.EventTypeMessage {
 		return nil, sdkutil.ErrUnknownType
 	}


Test patch:
diff --git a/events/publish_test.go b/events/publish_test.go
index 742d6883..39e09190 100644
--- a/events/publish_test.go
+++ b/events/publish_test.go
@@ -4,6 +4,7 @@ import (
 	"testing"
 
 	sdk "github.com/cosmos/cosmos-sdk/types"
+	"github.com/ovrclk/akash/sdkutil"
 	"github.com/ovrclk/akash/testutil"
 	dtypes "github.com/ovrclk/akash/x/deployment/types"
 	mtypes "github.com/ovrclk/akash/x/market/types"
@@ -11,9 +12,7 @@ import (
 )
 
 func Test_processEvent(t *testing.T) {
-	tests := []interface {
-		ToSDKEvent() sdk.Event
-	}{
+	tests := []sdkutil.ModuleEvent{
 		// x/deployment events
 		dtypes.EventDeploymentCreate{ID: testutil.DeploymentID(t)},
 		dtypes.EventDeploymentUpdate{ID: testutil.DeploymentID(t)},
diff --git a/testutil/event.go b/testutil/event.go
new file mode 100644
index 00000000..0ca7704a
--- /dev/null
+++ b/testutil/event.go
@@ -0,0 +1,34 @@
+package testutil
+
+import (
+	"testing"
+
+	sdk "github.com/cosmos/cosmos-sdk/types"
+	"github.com/ovrclk/akash/sdkutil"
+	dtypes "github.com/ovrclk/akash/x/deployment/types"
+	"github.com/stretchr/testify/require"
+)
+
+func ParseEvent(t testing.TB, events sdk.Events) sdkutil.Event {
+	t.Helper()
+
+	require.Equal(t, 1, len(events))
+
+	sev := sdk.StringifyEvent(events.ToABCIEvents()[0])
+	ev, err := sdkutil.ParseEvent(sev)
+
+	require.NoError(t, err)
+
+	return ev
+}
+
+func ParseDeploymentEvent(t testing.TB, events sdk.Events) sdkutil.ModuleEvent {
+	t.Helper()
+
+	uev := ParseEvent(t, events)
+
+	iev, err := dtypes.ParseEvent(uev)
+	require.NoError(t, err)
+
+	return iev
+}
diff --git a/testutil/sdk.go b/testutil/sdk.go
index 3204e1c8..65553934 100644
--- a/testutil/sdk.go
+++ b/testutil/sdk.go
@@ -8,5 +8,5 @@ import (
 )
 
 func Coin(_ testing.TB) sdk.Coin {
-	return sdk.NewCoin("testcoin", sdk.NewInt(rand.Int63()))
+	return sdk.NewCoin("testcoin", sdk.NewInt(int64(rand.Intn(999)+1)))
 }
diff --git a/testutil/types.go b/testutil/types.go
index e3cb1c56..7070a151 100644
--- a/testutil/types.go
+++ b/testutil/types.go
@@ -9,8 +9,8 @@ import (
 
 func Unit(_ testing.TB) types.Unit {
 	return types.Unit{
-		CPU:     rand.Uint32(),
-		Memory:  rand.Uint64(),
-		Storage: rand.Uint64(),
+		CPU:     uint32(rand.Intn(999) + 1),
+		Memory:  uint64(rand.Intn(999) + 1),
+		Storage: uint64(rand.Intn(999) + 1),
 	}
 }
diff --git a/x/deployment/handler/handler_test.go b/x/deployment/handler/handler_test.go
index ef144b85..bb98550c 100644
--- a/x/deployment/handler/handler_test.go
+++ b/x/deployment/handler/handler_test.go
@@ -9,12 +9,10 @@ import (
 	"github.com/pkg/errors"
 	"github.com/stretchr/testify/require"
 	abci "github.com/tendermint/tendermint/abci/types"
-	"github.com/tendermint/tendermint/libs/rand"
 	dbm "github.com/tendermint/tm-db"
 
 	"github.com/ovrclk/akash/app"
 	"github.com/ovrclk/akash/testutil"
-	atypes "github.com/ovrclk/akash/types"
 	"github.com/ovrclk/akash/x/deployment/handler"
 	"github.com/ovrclk/akash/x/deployment/keeper"
 	"github.com/ovrclk/akash/x/deployment/types"
@@ -84,6 +82,15 @@ func TestCreateDeployment(t *testing.T) {
 	require.NoError(t, err)
 	require.NotNil(t, res)
 
+	t.Run("ensure event created", func(t *testing.T) {
+		iev := testutil.ParseDeploymentEvent(t, res.Events)
+		require.IsType(t, types.EventDeploymentCreate{}, iev)
+
+		dev := iev.(types.EventDeploymentCreate)
+
+		require.Equal(t, msg.ID, dev.ID)
+	})
+
 	_, exists := suite.dkeeper.GetDeployment(suite.ctx, deployment.ID())
 	require.True(t, exists)
 
@@ -145,6 +152,15 @@ func TestUpdateDeploymentExisting(t *testing.T) {
 	res, err = suite.handler(suite.ctx, msgUpdate)
 	require.NotNil(t, res)
 	require.NoError(t, err)
+
+	t.Run("ensure event created", func(t *testing.T) {
+		iev := testutil.ParseDeploymentEvent(t, res.Events[1:])
+		require.IsType(t, types.EventDeploymentUpdate{}, iev)
+
+		dev := iev.(types.EventDeploymentUpdate)
+
+		require.Equal(t, msg.ID, dev.ID)
+	})
 }
 
 func TestCloseDeploymentNonExisting(t *testing.T) {
@@ -187,6 +203,17 @@ func TestCloseDeploymentExisting(t *testing.T) {
 	require.NotNil(t, res)
 	require.NoError(t, err)
 
+	t.Run("ensure event created", func(t *testing.T) {
+		// TODO: this should emit a DeploymentClose
+
+		iev := testutil.ParseDeploymentEvent(t, res.Events[1:])
+		require.IsType(t, types.EventDeploymentUpdate{}, iev)
+
+		dev := iev.(types.EventDeploymentUpdate)
+
+		require.Equal(t, msg.ID, dev.ID)
+	})
+
 	res, err = suite.handler(suite.ctx, msgClose)
 	require.Nil(t, res)
 	require.EqualError(t, err, types.ErrDeploymentClosed.Error())
@@ -199,13 +226,9 @@ func (st *testSuite) createDeployment() (types.Deployment, []types.Group) {
 	group := testutil.DeploymentGroup(st.t, deployment.ID(), 0)
 	group.Resources = []types.Resource{
 		{
-			Unit: atypes.Unit{
-				CPU:     100,
-				Memory:  100,
-				Storage: 10,
-			},
+			Unit:  testutil.Unit(st.t),
 			Count: 1,
-			Price: sdk.NewCoin("thepricedcoin", sdk.NewInt(int64(rand.Intn(10000)))),
+			Price: testutil.Coin(st.t),
 		},
 	}
 	groups := []types.Group{