Skip to content

Commit

Permalink
update project enrollment tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc committed Mar 28, 2024
1 parent a135011 commit 7279a19
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ Feature: Msg/UpdateProjectEnrollment
* class issuer "I02" for "C01"

Rule: valid state transitions performed by issuers which don't remove enrollment entries are:
UNSPECIFIED -> CHANGES_REQUESTED or ACCEPTED
CHANGES_REQUESTED -> ACCEPTED
ACCEPTED -> ACCEPTED with new metadata
UNSPECIFIED -> CHANGES_REQUESTED or ACCEPTED
CHANGES_REQUESTED -> ACCEPTED
ACCEPTED -> ACCEPTED with new metadata
Scenario Outline:
Given enrollment for "P001" to "C01" is "<cur_status>" with metadata "<cur_metadata>"
When "<issuer>" updates enrollment for "P001" to "C01" with status "<new_status>" and metadata "<new_metadata>"
Then expect error contains "<err>"
And expect enrollment for "P001" to "C01" to be "<expected_status>" with metadata "<expected_metadata>"
And if no error expect EventUpdateProjectEnrollment with properties
"""
{
"issuer": "<issuer>", "project_id": "P001", "class_id": "C01",
"old_status": "PROJECT_ENROLLMENT_STATUS_<cur_status>",
"new_status": "PROJECT_ENROLLMENT_STATUS_<expected_status>",
"new_enrollment_metadata": "<expected_metadata>"
}
"""
Examples:
| cur_status | cur_metadata | issuer | new_status | new_metadata | err | expected_status | expected_metadata |
| UNSPECIFIED | abc | I01 | ACCEPTED | foo123 | | ACCEPTED | foo123 |
Expand All @@ -25,14 +34,23 @@ Feature: Msg/UpdateProjectEnrollment
| ACCEPTED | foo123 | Bob | ACCEPTED | bar357 | unauthorized | ACCEPTED | foo123 |

Rule: valid state transitions performed by issuers which remove enrollment entries are:
UNSPECIFIED -> REJECTED
CHANGES_REQUESTED -> REJECTED
ACCEPTED -> TERMINATED
UNSPECIFIED -> REJECTED
CHANGES_REQUESTED -> REJECTED
ACCEPTED -> TERMINATED
Scenario Outline:
Given enrollment for "P001" to "C01" is "<cur_status>" with metadata "<cur_metadata>"
When "<issuer>" updates enrollment for "P001" to "C01" with status "<new_status>" and metadata "<new_metadata>"
Then expect error contains "<err>"
And expect enrollment exists for "P001" to "C01" to be "<exists>"
And if no error expect EventUpdateProjectEnrollment with properties
"""
{
"issuer": "<issuer>", "project_id": "P001", "class_id": "C01",
"old_status": "PROJECT_ENROLLMENT_STATUS_<cur_status>",
"new_status": "PROJECT_ENROLLMENT_STATUS_<new_status>",
"new_enrollment_metadata": "<new_metadata>"
}
"""
Examples:
| cur_status | cur_metadata | issuer | new_status | new_metadata | err | exists |
| UNSPECIFIED | abc | I01 | REJECTED | baz789 | | false |
Expand All @@ -48,6 +66,30 @@ Feature: Msg/UpdateProjectEnrollment
| ACCEPTED | foo123 | I01 | TERMINATED | baz789 | | false |
| ACCEPTED | foo123 | Bob | TERMINATED | baz789 | unauthorized | true |

Rule: events get emitted

Rule: any issuer can transition states
Rule: any issuer can transition states
Scenario: Issuer 1 requests changes, issuer 2 accepts
Given enrollment for "P001" to "C01" is "UNSPECIFIED" with metadata "abc"
When "I01" updates enrollment for "P001" to "C01" with status "CHANGES_REQUESTED" and metadata "def"
Then expect no error
And expect enrollment for "P001" to "C01" to be "CHANGES_REQUESTED" with metadata "def"
And expect EventUpdateProjectEnrollment with properties
"""
{
"issuer": "I01", "project_id": "P001", "class_id": "C01",
"old_status": "PROJECT_ENROLLMENT_STATUS_UNSPECIFIED",
"new_status": "PROJECT_ENROLLMENT_STATUS_CHANGES_REQUESTED",
"new_enrollment_metadata": "def"
}
"""
When "I02" updates enrollment for "P001" to "C01" with status "ACCEPTED" and metadata "ghi"
Then expect no error
And expect enrollment for "P001" to "C01" to be "ACCEPTED" with metadata "ghi"
And expect EventUpdateProjectEnrollment with properties
"""
{
"issuer": "I02", "project_id": "P001", "class_id": "C01",
"old_status": "PROJECT_ENROLLMENT_STATUS_CHANGES_REQUESTED",
"new_status": "PROJECT_ENROLLMENT_STATUS_ACCEPTED",
"new_enrollment_metadata": "ghi"
}
"""
2 changes: 2 additions & 0 deletions x/ecocredit/base/keeper/msg_update_project_enrollment.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ func (k Keeper) UpdateProjectEnrollment(ctx context.Context, msg *types.MsgUpdat
}

if err := sdk.UnwrapSDKContext(ctx).EventManager().EmitTypedEvent(&types.EventUpdateProjectEnrollment{
Issuer: msg.Issuer,
ProjectId: proj.Id,
ClassId: class.Id,
OldStatus: types.ProjectEnrollmentStatus(existingStatus),
NewStatus: types.ProjectEnrollmentStatus(newStatus),
NewEnrollmentMetadata: msg.Metadata,
}); err != nil {
Expand Down
25 changes: 25 additions & 0 deletions x/ecocredit/base/keeper/msg_update_project_enrollment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (

"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/jsonpb"
"github.com/regen-network/gocuke"
"github.com/stretchr/testify/require"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"

api "github.com/regen-network/regen-ledger/api/v2/regen/ecocredit/v1"
"github.com/regen-network/regen-ledger/types/v2/testutil"
v1 "github.com/regen-network/regen-ledger/x/ecocredit/v3/base/types/v1"
)

Expand Down Expand Up @@ -115,6 +117,29 @@ func (s *updateProjectEnrollmentSuite) ExpectErrorContains(a string) {
}
}

func (s *updateProjectEnrollmentSuite) IfNoErrorExpectEventupdateprojectenrollmentWithProperties(a gocuke.DocString) {
if s.err != nil {
return
}

s.ExpectEventupdateprojectenrollmentWithProperties(a)
}

func (s *updateProjectEnrollmentSuite) ExpectEventupdateprojectenrollmentWithProperties(a gocuke.DocString) {
var evtExpected v1.EventUpdateProjectEnrollment
err := jsonpb.UnmarshalString(a.Content, &evtExpected)
require.NoError(s.t, err)

// update issuer to actual address
evtExpected.Issuer = s.addrs[evtExpected.Issuer].String()

evtActual, found := testutil.GetEvent(&evtExpected, s.sdkCtx.EventManager().Events())
require.True(s.t, found)

err = testutil.MatchEvent(&evtExpected, evtActual)
require.NoError(s.t, err)
}

func (s *updateProjectEnrollmentSuite) getEnrollment(projId, clsId string) (*api.ProjectEnrollment, error) {
proj, err := s.stateStore.ProjectTable().GetById(s.ctx, projId)
require.NoError(s.t, err)
Expand Down

0 comments on commit 7279a19

Please sign in to comment.