-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
593 additions
and
507 deletions.
There are no files selected for viewing
240 changes: 90 additions & 150 deletions
240
pkg/converters/alipay/alipay_transaction_csv_data_table.go
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -364,6 +364,56 @@ func TestAlipayCsvFileImporterParseImportedData_ParseCategory(t *testing.T) { | |
assert.Equal(t, "Test Category3", allNewSubTransferCategories[0].Name) | ||
} | ||
|
||
func TestAlipayCsvFileImporterParseImportedData_ParseRelatedAccount(t *testing.T) { | ||
converter := AlipayAppTransactionDataCsvFileImporter | ||
context := core.NewNullContext() | ||
|
||
user := &models.User{ | ||
Uid: 1234567890, | ||
DefaultCurrency: "CNY", | ||
} | ||
|
||
data1, err := simplifiedchinese.GB18030.NewEncoder().String("------------------------------------------------------------------------------------\n" + | ||
"导出信息:\n" + | ||
"姓名:xxx\n" + | ||
"支付宝账户:[email protected]\n" + | ||
"起始时间:[2024-01-01 00:00:00] 终止时间:[2024-09-01 23:59:59]\n" + | ||
"导出交易类型:[全部]\n" + | ||
"------------------------支付宝(中国)网络技术有限公司 电子客户回单------------------------\n" + | ||
"交易时间,商品说明,收/支,金额,收/付款方式,交易状态,\n" + | ||
"2024-09-01 03:45:07,余额宝-单次转入,不计收支,0.01,Test Account,交易成功,\n" + | ||
"2024-09-01 05:07:29,信用卡还款,不计收支,0.02,Test Account2,交易成功,\n") | ||
assert.Nil(t, err) | ||
|
||
allNewTransactions, allNewAccounts, _, _, _, _, err := converter.ParseImportedData(context, user, []byte(data1), 0, nil, nil, nil, nil, nil) | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, 2, len(allNewTransactions)) | ||
assert.Equal(t, 3, len(allNewAccounts)) | ||
|
||
assert.Equal(t, int64(1234567890), allNewTransactions[0].Uid) | ||
assert.Equal(t, int64(1), allNewTransactions[0].Amount) | ||
assert.Equal(t, "Test Account", allNewTransactions[0].OriginalSourceAccountName) | ||
assert.Equal(t, "", allNewTransactions[0].OriginalDestinationAccountName) | ||
|
||
assert.Equal(t, int64(1234567890), allNewTransactions[1].Uid) | ||
assert.Equal(t, int64(2), allNewTransactions[1].Amount) | ||
assert.Equal(t, "Test Account2", allNewTransactions[1].OriginalSourceAccountName) | ||
assert.Equal(t, "", allNewTransactions[1].OriginalDestinationAccountName) | ||
|
||
assert.Equal(t, int64(1234567890), allNewAccounts[0].Uid) | ||
assert.Equal(t, "Test Account", allNewAccounts[0].Name) | ||
assert.Equal(t, "CNY", allNewAccounts[0].Currency) | ||
|
||
assert.Equal(t, int64(1234567890), allNewAccounts[1].Uid) | ||
assert.Equal(t, "", allNewAccounts[1].Name) | ||
assert.Equal(t, "CNY", allNewAccounts[1].Currency) | ||
|
||
assert.Equal(t, int64(1234567890), allNewAccounts[2].Uid) | ||
assert.Equal(t, "Test Account2", allNewAccounts[2].Name) | ||
assert.Equal(t, "CNY", allNewAccounts[2].Currency) | ||
} | ||
|
||
func TestAlipayCsvFileImporterParseImportedData_ParseDescription(t *testing.T) { | ||
converter := AlipayWebTransactionDataCsvFileImporter | ||
context := core.NewNullContext() | ||
|
@@ -479,3 +529,23 @@ func TestAlipayCsvFileImporterParseImportedData_MissingRequiredColumn(t *testing | |
_, _, _, _, _, _, err = converter.ParseImportedData(context, user, []byte(data4), 0, nil, nil, nil, nil, nil) | ||
assert.EqualError(t, err, errs.ErrMissingRequiredFieldInHeaderRow.Message) | ||
} | ||
|
||
func TestAlipayCsvFileImporterParseImportedData_NoTransactionData(t *testing.T) { | ||
converter := AlipayWebTransactionDataCsvFileImporter | ||
context := core.NewNullContext() | ||
|
||
user := &models.User{ | ||
Uid: 1, | ||
DefaultCurrency: "CNY", | ||
} | ||
|
||
// Missing Time Column | ||
data1, err := simplifiedchinese.GB18030.NewEncoder().String("支付宝交易记录明细查询\n" + | ||
"账号:[[email protected]]\n" + | ||
"起始日期:[2024-01-01 00:00:00] 终止日期:[2024-09-01 23:59:59]\n" + | ||
"---------------------------------交易记录明细列表------------------------------------\n" + | ||
"交易创建时间 ,金额(元),收/支 ,交易状态 ,\n" + | ||
"------------------------------------------------------------------------------------\n") | ||
_, _, _, _, _, _, err = converter.ParseImportedData(context, user, []byte(data1), 0, nil, nil, nil, nil, nil) | ||
assert.EqualError(t, err, errs.ErrNotFoundTransactionDataInFile.Message) | ||
} |
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,40 @@ | ||
package datatable | ||
|
||
// CommonDataTable defines the structure of common data table | ||
type CommonDataTable interface { | ||
// HeaderColumnCount returns the total count of column in header row | ||
HeaderColumnCount() int | ||
|
||
// HasColumn returns whether the common data table has specified column name | ||
HasColumn(columnName string) bool | ||
|
||
// DataRowCount returns the total count of common data row | ||
DataRowCount() int | ||
|
||
// DataRowIterator returns the iterator of common data row | ||
DataRowIterator() CommonDataRowIterator | ||
} | ||
|
||
// CommonDataRow defines the structure of common data row | ||
type CommonDataRow interface { | ||
// ColumnCount returns the total count of column in this data row | ||
ColumnCount() int | ||
|
||
// HasData returns whether the common data row has specified column data | ||
HasData(columnName string) bool | ||
|
||
// GetData returns the data in the specified column name | ||
GetData(columnName string) string | ||
} | ||
|
||
// CommonDataRowIterator defines the structure of common data row iterator | ||
type CommonDataRowIterator interface { | ||
// HasNext returns whether the iterator does not reach the end | ||
HasNext() bool | ||
|
||
// CurrentRowId returns current row id | ||
CurrentRowId() string | ||
|
||
// Next returns the next common data row | ||
Next() CommonDataRow | ||
} |
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,107 @@ | ||
package datatable | ||
|
||
// ImportedCommonDataTable defines the structure of imported common data table | ||
type ImportedCommonDataTable struct { | ||
innerDataTable ImportedDataTable | ||
dataColumnIndexes map[string]int | ||
} | ||
|
||
// ImportedCommonDataRow defines the structure of imported common data row | ||
type ImportedCommonDataRow struct { | ||
rowData map[string]string | ||
} | ||
|
||
// ImportedCommonDataRowIterator defines the structure of imported common data row iterator | ||
type ImportedCommonDataRowIterator struct { | ||
commonDataTable *ImportedCommonDataTable | ||
innerIterator ImportedDataRowIterator | ||
} | ||
|
||
// HeaderColumnCount returns the total count of column in header row | ||
func (t *ImportedCommonDataTable) HeaderColumnCount() int { | ||
return len(t.innerDataTable.HeaderColumnNames()) | ||
} | ||
|
||
// HasColumn returns whether the data table has specified column name | ||
func (t *ImportedCommonDataTable) HasColumn(columnName string) bool { | ||
index, exists := t.dataColumnIndexes[columnName] | ||
return exists && index >= 0 | ||
} | ||
|
||
// DataRowCount returns the total count of common data row | ||
func (t *ImportedCommonDataTable) DataRowCount() int { | ||
return t.innerDataTable.DataRowCount() | ||
} | ||
|
||
// DataRowIterator returns the iterator of common data row | ||
func (t *ImportedCommonDataTable) DataRowIterator() CommonDataRowIterator { | ||
return &ImportedCommonDataRowIterator{ | ||
commonDataTable: t, | ||
innerIterator: t.innerDataTable.DataRowIterator(), | ||
} | ||
} | ||
|
||
// HasData returns whether the common data row has specified column data | ||
func (r *ImportedCommonDataRow) HasData(columnName string) bool { | ||
_, exists := r.rowData[columnName] | ||
return exists | ||
} | ||
|
||
// ColumnCount returns the total count of column in this data row | ||
func (r *ImportedCommonDataRow) ColumnCount() int { | ||
return len(r.rowData) | ||
} | ||
|
||
// GetData returns the data in the specified column name | ||
func (r *ImportedCommonDataRow) GetData(columnName string) string { | ||
return r.rowData[columnName] | ||
} | ||
|
||
// HasNext returns whether the iterator does not reach the end | ||
func (t *ImportedCommonDataRowIterator) HasNext() bool { | ||
return t.innerIterator.HasNext() | ||
} | ||
|
||
// CurrentRowId returns current row id | ||
func (t *ImportedCommonDataRowIterator) CurrentRowId() string { | ||
return t.innerIterator.CurrentRowId() | ||
} | ||
|
||
// Next returns the next common data row | ||
func (t *ImportedCommonDataRowIterator) Next() CommonDataRow { | ||
importedRow := t.innerIterator.Next() | ||
|
||
if importedRow == nil { | ||
return nil | ||
} | ||
|
||
rowData := make(map[string]string, len(t.commonDataTable.dataColumnIndexes)) | ||
|
||
for column, columnIndex := range t.commonDataTable.dataColumnIndexes { | ||
if columnIndex < 0 || columnIndex >= importedRow.ColumnCount() { | ||
continue | ||
} | ||
|
||
value := importedRow.GetData(columnIndex) | ||
rowData[column] = value | ||
} | ||
|
||
return &ImportedCommonDataRow{ | ||
rowData: rowData, | ||
} | ||
} | ||
|
||
// CreateNewImportedCommonDataTable returns common data table from imported data table | ||
func CreateNewImportedCommonDataTable(dataTable ImportedDataTable) *ImportedCommonDataTable { | ||
headerLineItems := dataTable.HeaderColumnNames() | ||
dataColumnIndexes := make(map[string]int, len(headerLineItems)) | ||
|
||
for i := 0; i < len(headerLineItems); i++ { | ||
dataColumnIndexes[headerLineItems[i]] = i | ||
} | ||
|
||
return &ImportedCommonDataTable{ | ||
innerDataTable: dataTable, | ||
dataColumnIndexes: dataColumnIndexes, | ||
} | ||
} |
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.