-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add add-to-basket cli command (#750)
- Loading branch information
Showing
4 changed files
with
163 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package basketclient | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
|
||
"github.com/regen-network/regen-ledger/x/ecocredit/basket" | ||
) | ||
|
||
func parseBasketCredits(creditsFile string) ([]*basket.BasketCredit, error) { | ||
credits := []*basket.BasketCredit{} | ||
|
||
if creditsFile == "" { | ||
return nil, fmt.Errorf("credits file path is empty") | ||
} | ||
|
||
bz, err := ioutil.ReadFile(creditsFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err = json.Unmarshal(bz, &credits); err != nil { | ||
return nil, err | ||
} | ||
|
||
return credits, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package basketclient | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/testutil" | ||
|
||
"github.com/regen-network/regen-ledger/x/ecocredit/basket" | ||
) | ||
|
||
func TestParseCredits(t *testing.T) { | ||
invalidContent := testutil.WriteToNewTempFile(t, `{}`).Name() | ||
validCredits := testutil.WriteToNewTempFile(t, `[ | ||
{ | ||
"batch_denom": "C01-20210101-20220101-001", | ||
"amount": "10" | ||
}, | ||
{ | ||
"batch_denom": "C01-20210101-20220101-001", | ||
"amount": "10.555" | ||
} | ||
]`).Name() | ||
|
||
testCases := []struct { | ||
name string | ||
filePath string | ||
expErr bool | ||
result []*basket.BasketCredit | ||
errMsg string | ||
}{ | ||
{ | ||
"empty file path", | ||
"", | ||
true, | ||
nil, | ||
"file path is empty", | ||
}, | ||
{ | ||
"invalid file content", | ||
invalidContent, | ||
true, | ||
nil, | ||
"cannot unmarshal object", | ||
}, | ||
{ | ||
"valid test", | ||
validCredits, | ||
false, | ||
[]*basket.BasketCredit{ | ||
{ | ||
BatchDenom: "C01-20210101-20220101-001", | ||
Amount: "10", | ||
}, | ||
{ | ||
BatchDenom: "C01-20210101-20220101-001", | ||
Amount: "10.555", | ||
}, | ||
}, | ||
"", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
res, err := parseBasketCredits(tc.filePath) | ||
if tc.expErr { | ||
require.Error(t, err, err.Error()) | ||
require.Contains(t, err.Error(), tc.errMsg) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, res, tc.result) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters