-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'pagu-project:main' into fix/stake-time
- Loading branch information
Showing
6 changed files
with
203 additions
and
12 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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package zealy | ||
|
||
import ( | ||
"encoding/csv" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/pagu-project/Pagu/database" | ||
"github.com/pagu-project/Pagu/engine/command" | ||
) | ||
|
||
/* | ||
importWinnersHandler gives a csv file with below format and push the data into ZealyUser table. | ||
Position| Discord User | Prize . | ||
1st | user_id_1 | amount . | ||
2nd | user_id_2 | amount . | ||
*/ | ||
func (z *Zealy) importWinnersHandler(cmd command.Command, _ command.AppID, _ string, args ...string) command.CommandResult { | ||
if len(args) == 0 { | ||
return cmd.FailedResult("please specify a file path to import") | ||
} | ||
|
||
path := args[0] | ||
records, err := readCSV(path) | ||
if err != nil { | ||
return cmd.ErrorResult(fmt.Errorf("csv file is not valid")) | ||
} | ||
|
||
totalInserted := 0 | ||
|
||
for _, record := range records[1:] { | ||
discordID := record[1] | ||
if _, err := z.db.GetZealyUser(discordID); err == nil { | ||
return cmd.ErrorResult(fmt.Errorf("duplicate zealy user with discord ID: %s", discordID)) | ||
} | ||
|
||
prizeStr := record[2] | ||
prize, _ := strconv.Atoi(prizeStr) | ||
if err := z.db.AddZealyUser(&database.ZealyUser{ | ||
Amount: int64(prize), | ||
DiscordID: discordID, | ||
TxHash: "", | ||
}); err != nil { | ||
return cmd.ErrorResult(fmt.Errorf("adding zealy user into db with discord ID: %s", discordID)) | ||
} | ||
|
||
totalInserted++ | ||
} | ||
|
||
return cmd.SuccessfulResult("Imported successfully\nTotal inserted: %d", totalInserted) | ||
} | ||
|
||
func readCSV(path string) ([][]string, error) { | ||
csvFile, err := os.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
reader := csv.NewReader(csvFile) | ||
records, err := reader.ReadAll() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return records, 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,105 @@ | ||
package zealy | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/pagu-project/Pagu/database" | ||
"github.com/pagu-project/Pagu/engine/command" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestImportWinnersHandler(t *testing.T) { | ||
t.Run("import list of winners successfully", func(t *testing.T) { | ||
zealy := setup(t) | ||
tempPath := "temp-csv" | ||
csvData := "Position,Discord ID,Prize\n1,id1,1\n2,id2,2\n3,id3,3" | ||
tempFile := createTempFile(t, tempPath, csvData) | ||
|
||
cmd := zealy.GetCommand() | ||
expectedRes := zealy.importWinnersHandler(cmd, command.AppIdCLI, "", tempFile.Name()) | ||
|
||
assert.Equal(t, true, expectedRes.Successful) | ||
assert.Equal(t, "Imported successfully\nTotal inserted: 3", expectedRes.Message) | ||
|
||
users, err := zealy.db.GetAllZealyUser() | ||
assert.Equal(t, nil, err) | ||
for i, u := range users { | ||
assert.Equal(t, "", u.TxHash) | ||
assert.Equal(t, fmt.Sprintf("id%d", i+1), u.DiscordID) | ||
assert.Equal(t, int64(i+1), u.Amount) | ||
} | ||
}) | ||
|
||
t.Run("import list with duplicate items", func(t *testing.T) { | ||
zealy := setup(t) | ||
tempPath := "temp-csv" | ||
csvData := "Position,Discord ID,Prize\n1,id1,1\n2,id2,2\n3,id3,3\n4,id3,4" | ||
tempFile := createTempFile(t, tempPath, csvData) | ||
|
||
cmd := zealy.GetCommand() | ||
expectedRes := zealy.importWinnersHandler(cmd, command.AppIdCLI, "", tempFile.Name()) | ||
|
||
assert.Equal(t, false, expectedRes.Successful) | ||
assert.Equal(t, "An error occurred: duplicate zealy user with discord ID: id3", expectedRes.Message) | ||
|
||
users, err := zealy.db.GetAllZealyUser() | ||
assert.Equal(t, nil, err) | ||
for i, u := range users { | ||
assert.Equal(t, "", u.TxHash) | ||
assert.Equal(t, fmt.Sprintf("id%d", i+1), u.DiscordID) | ||
assert.Equal(t, int64(i+1), u.Amount) | ||
} | ||
}) | ||
|
||
t.Run("no csv file passed", func(t *testing.T) { | ||
zealy := setup(t) | ||
|
||
cmd := zealy.GetCommand() | ||
expectedRes := zealy.importWinnersHandler(cmd, command.AppIdCLI, "") | ||
|
||
assert.Equal(t, false, expectedRes.Successful) | ||
assert.Equal(t, "please specify a file path to import", expectedRes.Message) | ||
}) | ||
|
||
t.Run("wrong csv file passed", func(t *testing.T) { | ||
zealy := setup(t) | ||
|
||
cmd := zealy.GetCommand() | ||
expectedRes := zealy.importWinnersHandler(cmd, command.AppIdCLI, "", "fake_csv_file_address") | ||
|
||
assert.Equal(t, false, expectedRes.Successful) | ||
assert.Equal(t, "An error occurred: csv file is not valid", expectedRes.Message) | ||
}) | ||
} | ||
|
||
func setup(t *testing.T) *Zealy { | ||
t.Helper() | ||
|
||
dbFile, err := os.CreateTemp("", "temp-db") | ||
require.NoError(t, err) | ||
db, err := database.NewDB(dbFile.Name()) | ||
require.NoError(t, err) | ||
|
||
zealy := NewZealy(db, nil) | ||
return &zealy | ||
} | ||
|
||
func createTempFile(t *testing.T, path, data string) *os.File { | ||
t.Helper() | ||
|
||
tempFile, err := os.CreateTemp("", path) | ||
if err != nil { | ||
t.Fatalf("createTempFile() error = %v", err) | ||
} | ||
|
||
defer tempFile.Close() | ||
|
||
if _, err := tempFile.Write([]byte(data)); err != nil { | ||
t.Fatalf("createTempFile() error writing data to temp file: %v", err) | ||
} | ||
|
||
return tempFile | ||
} |
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