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 d089a80 commit a135011
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
Feature: Msg/UpdateProjectEnrollment

Background:
Given class "C01" with issuer "I01"
And an application for project "P001" to class "C01" with status "UNSPECIFIED" and metadata "abc"

Rule: an issuer can accept a project application from status unspecified or changes requested
Given project "P001"
* class "C01"
* class issuer "I01" for "C01"
* 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
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 "<status>" and metadata "<metadata>"
Then expect error contains <err>
And expect enrollment for "P001" to "C01" to be "<new_status>" with metadata "<new_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>"
Examples:
| cur_status | cur_metadata | issuer | new_status | new_metadata | err | expected_status | expected_metadata |
| UNSPECIFIED | abc | I01 | ACCEPTED | foo123 | | ACCEPTED | foo123 |
Expand All @@ -19,56 +24,30 @@ Feature: Msg/UpdateProjectEnrollment
| ACCEPTED | foo123 | I01 | ACCEPTED | bar357 | | ACCEPTED | bar357 |
| 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
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 "<status>" and metadata "<metadata>"
Then expect error contains <err>
And expect enrollment for "P001" to "C01" to be <exists>

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>"
Examples:
| cur_status | cur_metadata | issuer | new_status | new_metadata | err | exists |
| UNSPECIFIED | abc | I01 | REJECTED | baz789 | | false
| UNSPECIFIED | abc | Bob | REJECTED | baz789 | unauthorized | true |
| CHANGES_REQUESTED | bar456 | I01 | REJECTED | baz789 | | false |
| CHANGES_REQUESTED | bar456 | Bob | REJECTED | baz789 | unauthorized | true |
| ACCEPTED | foo123 | I01 | REJECTED | baz789 | invalid | true |
| ACCEPTED | foo123 | Bob | REJECTED | baz789 | unauthorized | true |
| ACCEPTED | foo123 | I01 | TERMINATED | baz789 | | false |
| ACCEPTED | foo123 | Bob | TERMINATED | baz789 | unauthorized | true |

Scenario: an issuer accepts a project application
When "I01" updates enrollment for "P001" to "C01" with status "ACCEPTED" and metadata "foo123"
Then expect no error
And expect enrollment for "P001" to "C01" to be "ACCEPTED" with metadata "foo123"

Scenario: non issuer attempts to accept a project application
When "Bob" updates enrollment for "P001" to "C01" with status "ACCEPTED" and metadata "foo123"
Then expect error contains "unauthorized"
And expect enrollment for "P001" to "C01" to be "UNSPECIFIED" with metadata "abc"

Rule: an issuer can request changes to a project application
Scenario: an issuer requests changes to a project application
When "I01" updates enrollment for "P001" to "C01" with status "CHANGES_REQUESTED" and metadata "bar456"
Then expect no error
And expect enrollment for "P001" to "C01" to be "CHANGES_REQUESTED" with metadata "bar456"

Scenario: non issuer attempts to request changes to a project application
When "Bob" updates enrollment for "P001" to "C01" with status "CHANGES_REQUESTED" and metadata "bar456"
Then expect error contains "unauthorized"
And expect enrollment for "P001" to "C01" to be "UNSPECIFIED" with metadata "abc"

Rule: an issuer can reject a project application and it is removed from state
Scenario: an issuer rejects a project application
When "I01" updates enrollment for "P001" to "C01" with status "REJECTED" and metadata "baz789"
Then expect no error

Scenario: non issuer attempts to reject a project application
When "Bob" updates enrollment for "P001" to "C01" with status "REJECTED" and metadata "baz789"
Then expect error contains "unauthorized"
And expect enrollment for "P001" to "C01" to be "UNSPECIFIED" with metadata "abc"

Rule: an issuer can terminate a project enrollment and it is removed from state

Rule: any issuer can change the status even if a different issuer changed it before

Rule: events get emitted
| cur_status | cur_metadata | issuer | new_status | new_metadata | err | exists |
| UNSPECIFIED | abc | I01 | REJECTED | baz789 | | false |
| UNSPECIFIED | abc | Bob | REJECTED | baz789 | unauthorized | true |
| CHANGES_REQUESTED | bar456 | I01 | REJECTED | baz789 | | false |
| CHANGES_REQUESTED | bar456 | Bob | REJECTED | baz789 | unauthorized | true |
| ACCEPTED | foo123 | I01 | REJECTED | baz789 | invalid | true |
| ACCEPTED | foo123 | Bob | REJECTED | baz789 | unauthorized | true |
| UNSPECIFIED | abc | I01 | TERMINATED | baz789 | invalid | true |
| UNSPECIFIED | abc | Bob | TERMINATED | baz789 | unauthorized | true |
| CHANGES_REQUESTED | bar456 | I01 | TERMINATED | baz789 | invalid | true |
| CHANGES_REQUESTED | bar456 | Bob | TERMINATED | baz789 | unauthorized | true |
| ACCEPTED | foo123 | I01 | TERMINATED | baz789 | | false |
| ACCEPTED | foo123 | Bob | TERMINATED | baz789 | unauthorized | true |

Rule: events get emitted

Rule: any issuer can transition states
2 changes: 2 additions & 0 deletions x/ecocredit/base/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type baseSuite struct {
ctrl *gomock.Controller
addr sdk.AccAddress
addr2 sdk.AccAddress
addr3 sdk.AccAddress
bankKeeper *mocks.MockBankKeeper
storeKey *storetypes.KVStoreKey
sdkCtx sdk.Context
Expand Down Expand Up @@ -86,6 +87,7 @@ func setupBase(t gocuke.TestingT) *baseSuite {
s.k = NewKeeper(s.stateStore, s.bankKeeper, moduleAddress, basketStore, marketStore, s.authority)
_, _, s.addr = testdata.KeyTestPubAddr()
_, _, s.addr2 = testdata.KeyTestPubAddr()
_, _, s.addr3 = testdata.KeyTestPubAddr()

return s
}
Expand Down
3 changes: 3 additions & 0 deletions x/ecocredit/base/keeper/msg_update_project_enrollment.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (k Keeper) UpdateProjectEnrollment(ctx context.Context, msg *types.MsgUpdat
}
case ecocreditv1.ProjectEnrollmentStatus_PROJECT_ENROLLMENT_STATUS_ACCEPTED:
switch newStatus {
case ecocreditv1.ProjectEnrollmentStatus_PROJECT_ENROLLMENT_STATUS_ACCEPTED:
// Valid case for just updating metadata of an accepted project
break
case ecocreditv1.ProjectEnrollmentStatus_PROJECT_ENROLLMENT_STATUS_TERMINATED:
delete = true
break
Expand Down
45 changes: 30 additions & 15 deletions x/ecocredit/base/keeper/msg_update_project_enrollment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,42 @@ func (s *updateProjectEnrollmentSuite) Before(t gocuke.TestingT) {
s.baseSuite = setupBase(t)
s.addrs = map[string]sdk.AccAddress{
"I01": s.addr,
"Bob": s.addr2,
"I02": s.addr2,
"Bob": s.addr3,
}
}

func (s *updateProjectEnrollmentSuite) ClassWithIssuer(clsId, issuer string) {
clsKey, err := s.stateStore.ClassTable().InsertReturningID(s.ctx, &api.Class{
Id: clsId,
Admin: s.addrs[issuer],
func (s *updateProjectEnrollmentSuite) Project(projId string) {

Check warning on line 38 in x/ecocredit/base/keeper/msg_update_project_enrollment_test.go

View workflow job for this annotation

GitHub Actions / golangci (x/ecocredit)

var-naming: method parameter projId should be projID (revive)
err := s.stateStore.ProjectTable().Insert(s.ctx, &api.Project{
Id: projId,
})
require.NoError(s.t, err)
}

func (s *updateProjectEnrollmentSuite) Class(clsId string) {
err := s.stateStore.ClassTable().Insert(s.ctx, &api.Class{
Id: clsId,
})
require.NoError(s.t, err)
}

func (s *updateProjectEnrollmentSuite) ClassIssuerFor(issuer, clsId string) {
cls, err := s.stateStore.ClassTable().GetById(s.ctx, clsId)
require.NoError(s.t, err)
err = s.stateStore.ClassIssuerTable().Insert(s.ctx, &api.ClassIssuer{
ClassKey: clsKey,
ClassKey: cls.Key,
Issuer: s.addrs[issuer],
})
require.NoError(s.t, err)
}

func (s *updateProjectEnrollmentSuite) AnApplicationForProjectToClassWithStatusAndMetadata(projId, clsId, status, metadata string) {
projKey, err := s.stateStore.ProjectTable().InsertReturningID(s.ctx, &api.Project{
Id: projId,
})
func (s *updateProjectEnrollmentSuite) EnrollmentForToIsWithMetadata(projId, clsId, status, metadata string) {
proj, err := s.stateStore.ProjectTable().GetById(s.ctx, projId)
require.NoError(s.t, err)
cls, err := s.stateStore.ClassTable().GetById(s.ctx, clsId)
require.NoError(s.t, err)
err = s.stateStore.ProjectEnrollmentTable().Insert(s.ctx, &api.ProjectEnrollment{
ProjectKey: projKey,
ProjectKey: proj.Key,
ClassKey: cls.Key,
Status: statusFromString(status),
EnrollmentMetadata: metadata,
Expand Down Expand Up @@ -85,16 +94,22 @@ func (s *updateProjectEnrollmentSuite) ExpectEnrollmentForToToBeWithMetadata(pro
require.Equal(s.t, metadata, enrollment.EnrollmentMetadata)
}

func (s *updateProjectEnrollmentSuite) ExpectNoEnrollmentForTo(projId, clsId string) {
func (s *updateProjectEnrollmentSuite) ExpectEnrollmentExistsForToToBe(projId, clsId, exists string) {
enrollment, err := s.getEnrollment(projId, clsId)
if !ormerrors.IsNotFound(err) {
s.t.Fatalf("expected project enrollment not found, got %v", enrollment)
if exists == "true" {

Check failure on line 99 in x/ecocredit/base/keeper/msg_update_project_enrollment_test.go

View workflow job for this annotation

GitHub Actions / golangci (x/ecocredit)

ifElseChain: rewrite if-else to switch statement (gocritic)
require.NoError(s.t, err)
} else if exists == "false" {
if !ormerrors.IsNotFound(err) {
s.t.Fatalf("expected project enrollment not found, got %v", enrollment)
}
} else {
s.t.Fatalf("invalid exists value: %s", exists)
}
}

func (s *updateProjectEnrollmentSuite) ExpectErrorContains(a string) {
if a == "" {
require.Error(s.t, s.err)
require.NoError(s.t, s.err)
} else {
require.ErrorContains(s.t, s.err, a)
}
Expand Down

0 comments on commit a135011

Please sign in to comment.