-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement crowdfund:create command
- Loading branch information
Showing
27 changed files
with
387 additions
and
60 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
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 |
---|---|---|
@@ -1,14 +1,43 @@ | ||
package crowdfund | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/pagu-project/pagu/internal/engine/command" | ||
"github.com/pagu-project/pagu/internal/entity" | ||
) | ||
|
||
func (*Crowdfund) createHandler( | ||
func (c *CrowdfundCmd) createHandler( | ||
_ *entity.User, | ||
cmd *command.Command, | ||
_ map[string]string, | ||
args map[string]string, | ||
) command.CommandResult { | ||
return cmd.SuccessfulResult("TODO") | ||
title := args["title"] | ||
desc := args["desc"] | ||
packagesJSON := args["packages"] | ||
|
||
packages := []entity.Package{} | ||
err := json.Unmarshal([]byte(packagesJSON), &packages) | ||
if err != nil { | ||
return cmd.FailedResult(err.Error()) | ||
} | ||
|
||
if title == "" { | ||
return cmd.FailedResult("The title of the crowdfunding campaign cannot be empty") | ||
} | ||
|
||
if len(packages) < 2 { | ||
return cmd.FailedResult("At least 3 packages are required for the crowdfunding campaign") | ||
} | ||
|
||
campaign := &entity.CrowdfundCampaign{ | ||
Title: title, | ||
Desc: desc, | ||
Packages: packages, | ||
} | ||
c.db.AddCrowdfundCampaign(campaign) | ||
|
||
return cmd.SuccessfulResultF( | ||
"Crowdfund campaign '%s' created successfully with %d packages", | ||
title, len(packages)) | ||
} |
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,65 @@ | ||
package crowdfund | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/pagu-project/pagu/internal/engine/command" | ||
"github.com/pagu-project/pagu/internal/entity" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCreate(t *testing.T) { | ||
td := setup(t) | ||
|
||
caller := &entity.User{DBModel: entity.DBModel{ID: 1}} | ||
cmd := &command.Command{} | ||
|
||
t.Run("Invalid Packages", func(t *testing.T) { | ||
args := map[string]string{ | ||
"title": "crowdfund-title", | ||
"desc": "crowdfund-desc", | ||
"packages": "INVALID-JSON", | ||
} | ||
result := td.crowdfundCmd.createHandler(caller, cmd, args) | ||
assert.False(t, result.Successful) | ||
assert.Equal(t, result.Message, "invalid character 'I' looking for beginning of value") | ||
}) | ||
|
||
t.Run("Empty title", func(t *testing.T) { | ||
args := map[string]string{ | ||
"title": "", | ||
"desc": "", | ||
"packages": "[]", | ||
} | ||
result := td.crowdfundCmd.createHandler(caller, cmd, args) | ||
assert.False(t, result.Successful) | ||
assert.Equal(t, result.Message, "The title of the crowdfunding campaign cannot be empty") | ||
}) | ||
|
||
t.Run("Empty Packages", func(t *testing.T) { | ||
args := map[string]string{ | ||
"title": "crowdfund-title", | ||
"desc": "crowdfund-desc", | ||
"packages": "[]", | ||
} | ||
result := td.crowdfundCmd.createHandler(caller, cmd, args) | ||
assert.False(t, result.Successful) | ||
assert.Equal(t, result.Message, "At least 3 packages are required for the crowdfunding campaign") | ||
}) | ||
|
||
t.Run("Ok", func(t *testing.T) { | ||
args := map[string]string{ | ||
"title": "crowdfund-title", | ||
"desc": "crowdfund-desc", | ||
"packages": ` | ||
[ | ||
{"name": "package-1", "usd_amount": 100, "pac_amount": 100}, | ||
{"name": "package-2", "usd_amount": 200, "pac_amount": 200}, | ||
{"name": "package-3", "usd_amount": 300, "pac_amount": 300} | ||
]`, | ||
} | ||
result := td.crowdfundCmd.createHandler(caller, cmd, args) | ||
assert.True(t, result.Successful) | ||
assert.Equal(t, result.Message, "Crowdfund campaign 'crowdfund-title' created successfully with 3 packages") | ||
}) | ||
} |
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,95 @@ | ||
package crowdfund | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/pagu-project/pagu/internal/entity" | ||
"github.com/pagu-project/pagu/internal/repository" | ||
"github.com/pagu-project/pagu/internal/testsuite" | ||
"github.com/pagu-project/pagu/pkg/nowpayments" | ||
"github.com/pagu-project/pagu/pkg/wallet" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
type testData struct { | ||
*testsuite.TestSuite | ||
|
||
crowdfundCmd *CrowdfundCmd | ||
database *repository.Database | ||
nowpayments *nowpayments.MockINowpayments | ||
wallet *wallet.MockIWallet | ||
} | ||
|
||
func setup(t *testing.T) *testData { | ||
t.Helper() | ||
|
||
ts := testsuite.NewTestSuite(t) | ||
ctrl := gomock.NewController(t) | ||
|
||
testDB := ts.MakeTestDB() | ||
mockNowpayments := nowpayments.NewMockINowpayments(ctrl) | ||
mockWallet := wallet.NewMockIWallet(ctrl) | ||
|
||
crowdfundCmd := NewCrowdfundCmd(context.Background(), | ||
testDB, mockWallet, mockNowpayments) | ||
|
||
return &testData{ | ||
TestSuite: ts, | ||
crowdfundCmd: crowdfundCmd, | ||
database: testDB, | ||
nowpayments: mockNowpayments, | ||
wallet: mockWallet, | ||
} | ||
} | ||
|
||
type CampaignOption func(*entity.CrowdfundCampaign) | ||
|
||
func WithTitle(title string) CampaignOption { | ||
return func(c *entity.CrowdfundCampaign) { | ||
c.Title = title | ||
} | ||
} | ||
|
||
func WithPackages(packages []entity.Package) CampaignOption { | ||
return func(c *entity.CrowdfundCampaign) { | ||
c.Packages = packages | ||
} | ||
} | ||
|
||
func (td *testData) createTestCampaign(t *testing.T, opts ...CampaignOption) *entity.CrowdfundCampaign { | ||
t.Helper() | ||
|
||
campaign := &entity.CrowdfundCampaign{ | ||
Title: td.RandString(16), | ||
Desc: td.RandString(128), | ||
Packages: []entity.Package{ | ||
entity.Package{ | ||
Name: td.RandString(16), | ||
USDAmount: td.RandInt(1000), | ||
PACAmount: td.RandInt(1000), | ||
}, | ||
entity.Package{ | ||
Name: td.RandString(16), | ||
USDAmount: td.RandInt(1000), | ||
PACAmount: td.RandInt(1000), | ||
}, | ||
entity.Package{ | ||
Name: td.RandString(16), | ||
USDAmount: td.RandInt(1000), | ||
PACAmount: td.RandInt(1000), | ||
}, | ||
}, | ||
} | ||
|
||
// Apply options | ||
for _, opt := range opts { | ||
opt(campaign) | ||
} | ||
|
||
err := td.database.AddCrowdfundCampaign(campaign) | ||
require.NoError(t, err) | ||
|
||
return campaign | ||
} |
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
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
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
Oops, something went wrong.