Skip to content

Commit

Permalink
stage test
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeMaster482 committed Dec 20, 2023
1 parent a9d1376 commit b5b25d2
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -474,99 +474,20 @@ func (r *transactionRep) GetTransactionForExport(ctx context.Context, userId uui
); err != nil {
return nil, fmt.Errorf("[repo] %w", err)
}
transactions = append(transactions, transaction)
}

return transactions, nil
}

/*
var transactions []models.Transaction
count := 1
var queryParamsSlice []interface{}
query := transactionGetFeedForExport
queryParamsSlice = append(queryParamsSlice, userId.String())
if queryGet.Account != uuid.Nil {
count++
query += " AND (account_income = $" + strconv.Itoa(count) + " OR account_outcome = $" + strconv.Itoa(count) + ")"
queryParamsSlice = append(queryParamsSlice, queryGet.Account.String())
}
if queryGet.Category != uuid.Nil {
count++
query += " AND id IN (SELECT transaction_id FROM TransactionCategory WHERE category_id = $" + strconv.Itoa(count) + ")"
queryParamsSlice = append(queryParamsSlice, queryGet.Category.String())
}
if queryGet.Income && queryGet.Outcome {
query += " AND income > 0 AND outcome > 0"
}
if !queryGet.Income && queryGet.Outcome {
query += " AND outcome > 0 AND income = 0"
}
if queryGet.Income && !queryGet.Outcome {
query += " AND income > 0 AND outcome = 0"
}
if !queryGet.StartDate.IsZero() || !queryGet.EndDate.IsZero() {
count++
if !queryGet.StartDate.IsZero() && !queryGet.EndDate.IsZero() {
query += " AND date BETWEEN $" + strconv.Itoa(count) + " AND $" + strconv.Itoa(count+1)
queryParamsSlice = append(queryParamsSlice, queryGet.StartDate, queryGet.EndDate)
} else if !queryGet.StartDate.IsZero() {
query += " AND date >= $" + strconv.Itoa(count)
queryParamsSlice = append(queryParamsSlice, queryGet.StartDate)
} else {
query += " AND date <= $" + strconv.Itoa(count)
queryParamsSlice = append(queryParamsSlice, queryGet.EndDate)
}
}
query += " ORDER BY date DESC;"
rows, err := r.db.Query(ctx, query, queryParamsSlice...)
if err != nil {
return nil, fmt.Errorf("[repo] %v", err)
}
for rows.Next() {
var transaction models.Transaction
if err := rows.Scan(
&transaction.ID,
&transaction.UserID,
&transaction.AccountIncomeID,
&transaction.AccountOutcomeID,
&transaction.Income,
&transaction.Outcome,
&transaction.Date,
&transaction.Payer,
&transaction.Description,
); err != nil {
return nil, fmt.Errorf("[repo] %w", err)
}

categories, err := r.getCategoriesForTransaction(ctx, transaction.ID)
if err != nil {
return nil, fmt.Errorf("[repo] %w", err)
}
transaction.Categories = categories
for _, data := range categories {
transaction.Categories = append(transaction.Categories, data.Name)
}

transactions = append(transactions, transaction)
}

if err := rows.Err(); err != nil {
return nil, fmt.Errorf("[repo] %w", err)
}
if len(transactions) == 0 {
return nil, fmt.Errorf("[repo] %w: %v", &models.NoSuchTransactionError{UserID: userId}, err)
}
return transactions, nil
*/
}

// func (r *transactionRep) Check(ctx context.Context, transactionID uuid.UUID) error {
// var exists bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -739,3 +739,211 @@ func TestCheckForbidden(t *testing.T) {
})
}
}

func TestGetExportInfo(t *testing.T) {
userID := uuid.New()
transactionID1 := uuid.New()
categoryID := uuid.New()
time := time.Now()
categories := []models.CategoryName{{ID: categoryID, Name: "ffdsf"}}
tests := []struct {
name string
rows *pgxmock.Rows
rowsCategory *pgxmock.Rows
rowsCategoryErr error
err error
rowsErr error
expected []models.Transaction
expectedLast bool
errTransaction bool
}{
{
name: "ValidFeed",
rows: pgxmock.NewRows([]string{
"id", "user_id", "account_income_id", "account_outcome_id", "income", "outcome", "date", "payer", "description",
}).AddRow(
transactionID1, userID, transactionID1, transactionID1, 100.0, 0.0, time, "John Doe", "Transaction 1",
),
rowsCategory: pgxmock.NewRows([]string{
"category_id",
"name",
}).AddRow(
categoryID,
"ffdsf",
),

err: nil,
rowsErr: nil,
rowsCategoryErr: nil,
expected: []models.Transaction{{ID: transactionID1, UserID: userID, AccountIncomeID: transactionID1, AccountOutcomeID: transactionID1, Income: 100.0, Outcome: 0.0, Date: time, Payer: "John Doe", Description: "Transaction 1", Categories: categories}},
expectedLast: false,
errTransaction: true,
},
{
name: "Invalid Scan Category",
rows: pgxmock.NewRows([]string{
"id", "user_id", "account_income_id", "account_outcome_id", "income", "outcome", "date", "payer", "description",
}).AddRow(
transactionID1, userID, transactionID1, transactionID1, 100.0, 0.0, time, "John Doe", "Transaction 1",
),
rowsCategory: pgxmock.NewRows([]string{
"category_id",
"name",
}).AddRow(
"dff",
"sfd",
),

err: fmt.Errorf("[repo] Scanning value error for column 'category_id': Scan: invalid UUID length: 3"),
rowsErr: nil,
rowsCategoryErr: nil,
expected: nil,
expectedLast: false,
errTransaction: true,
},
{
name: "Invalid Scan transaction",
rows: pgxmock.NewRows([]string{
"id", "user_id", "account_income_id", "account_outcome_id", "income", "outcome", "date", "payer", "description",
}).AddRow(
"fff", userID, transactionID1, transactionID1, 100.0, 0.0, time, "John Doe", "Transaction 1",
),

err: fmt.Errorf("[repo] Scanning value error for column 'id': Scan: invalid UUID length: 3"),
rowsErr: nil,
rowsCategoryErr: nil,
expected: nil,
expectedLast: false,
errTransaction: false,
},
{
name: "INValid category",
rows: pgxmock.NewRows([]string{
"id", "user_id", "account_income_id", "account_outcome_id", "income", "outcome", "date", "payer", "description",
}).AddRow(
transactionID1, userID, transactionID1, transactionID1, 100.0, 0.0, time, "John Doe", "Transaction 1",
),
rowsCategory: pgxmock.NewRows([]string{
"category_id",
"name",
}).AddRow(
categoryID,
"dddd",
),

err: fmt.Errorf("[repo] err"),
rowsErr: nil,
rowsCategoryErr: errors.New("err"),
expected: nil,
expectedLast: false,
errTransaction: true,
},
{
name: "Rows err",
rows: pgxmock.NewRows([]string{
"id", "user_id", "account_income_id", "account_outcome_id", "income", "outcome", "date", "payer", "description",
}).RowError(0, errors.New("err")),
rowsCategory: pgxmock.NewRows([]string{
"category_id",
"name",
}),

err: fmt.Errorf("[repo] err"),
rowsErr: nil,
rowsCategoryErr: nil,
expected: nil,
expectedLast: false,
errTransaction: false,
},
{
name: "Rows err",
rows: pgxmock.NewRows([]string{
"id", "user_id", "account_income_id", "account_outcome_id", "income", "outcome", "date", "payer", "description",
}).AddRow(
transactionID1, userID, transactionID1, transactionID1, 100.0, 0.0, time, "John Doe", "Transaction 1",
),
rowsCategory: pgxmock.NewRows([]string{
"category_id",
"name",
}).RowError(0, errors.New("err")),
err: fmt.Errorf("[repo] err"),
rowsErr: nil,
rowsCategoryErr: nil,
expected: nil,
expectedLast: false,
errTransaction: true,
},
{
name: "NoRows",
rows: pgxmock.NewRows([]string{
"id", "user_id", "account_income_id", "account_outcome_id", "income", "outcome", "date", "payer", "description",
}),
rowsErr: nil,
rowsCategoryErr: nil,
rowsCategory: pgxmock.NewRows([]string{
"category_id",
"name",
}),
err: fmt.Errorf("[repo] %w: <nil>", &models.NoSuchTransactionError{UserID: userID}),
expected: nil,
expectedLast: false,
errTransaction: false,
},
{
name: "DatabaseError",
rows: pgxmock.NewRows([]string{
"id", "user_id", "account_income_id", "account_outcome_id", "income", "outcome", "date", "payer", "description",
}),
rowsErr: errors.New("err"),
rowsCategory: pgxmock.NewRows([]string{
"category_id",
"name",
}).AddRow(
uuid.New(),
"ffff",
),
rowsCategoryErr: errors.New("err"),
err: fmt.Errorf("[repo] err"),
expected: nil,
expectedLast: false,
errTransaction: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
mock, _ := pgxmock.NewPool()
ctl := gomock.NewController(t)
defer ctl.Finish()
logger := *logger.NewLogger(context.TODO())
repo := NewRepository(mock, logger)

escapedQuery := regexp.QuoteMeta(transactionGetFeed + " ORDER BY date DESC;")
mock.ExpectQuery(escapedQuery).
WithArgs(userID.String()).
WillReturnRows(test.rows).
WillReturnError(test.rowsErr)

if test.errTransaction {
escapedQueryCategory := regexp.QuoteMeta(transactionGetCategory)
mock.ExpectQuery(escapedQueryCategory).
WithArgs(transactionID1).
WillReturnRows(test.rowsCategory).
WillReturnError(test.rowsCategoryErr)
}
transactions, err := repo.GetFeed(context.Background(), userID, &models.QueryListOptions{})

if !reflect.DeepEqual(transactions, test.expected) {
t.Errorf("Expected transactions: %v, but got: %v", test.expected, transactions)
}

if (test.err == nil && err != nil) || (test.err != nil && err == nil) || (test.err != nil && err != nil && test.err.Error() != err.Error()) {
t.Errorf("Expected error: %v, but got: %v", test.err, err)
}

if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("There were unfulfilled expectations: %s", err)
}
})
}
}

0 comments on commit b5b25d2

Please sign in to comment.