Skip to content

Commit

Permalink
On payment update, get original version from DB. Fix e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
volmedo committed Jun 4, 2019
1 parent 5a9c377 commit 8d0896a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
4 changes: 1 addition & 3 deletions e2e_test/features/update_payment.feature
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ Feature: Update payment
When I update the payment with ID "4ee3a8d8-ca7b-4290-a52c-dd5b6165ec43" with new details in JSON:
"""
{
"type": "Payment",
"id": "4ee3a8d8-ca7b-4290-a52c-dd5b6165ec43",
"version": 0,
"organisation_id": "743d5b63-8e6f-432e-a8fa-c5d8d2ee5fcb",
"attributes": {
"amount": "150.00",
Expand Down Expand Up @@ -132,7 +130,7 @@ Feature: Update payment
{
"type": "Payment",
"id": "4ee3a8d8-ca7b-4290-a52c-dd5b6165ec43",
"version": 0,
"version": 1,
"organisation_id": "743d5b63-8e6f-432e-a8fa-c5d8d2ee5fcb",
"attributes": {
"amount": "150.00",
Expand Down
18 changes: 8 additions & 10 deletions pkg/service/db_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,12 @@ func (dbpr *DBPaymentRepository) List(offset, limit int64) ([]*models.Payment, e
//
// Update returns an error if the paymentID does not exist in the collection
func (dbpr *DBPaymentRepository) Update(paymentID strfmt.UUID, payment *models.Payment) (*models.Payment, error) {
// Look for the ID in the DB to check if the payment exists and get its version
original, err := dbpr.Get(paymentID)
if err != nil {
return nil, err
}

updateStmt := `
UPDATE payments
SET
Expand Down Expand Up @@ -668,10 +674,10 @@ func (dbpr *DBPaymentRepository) Update(paymentID strfmt.UUID, payment *models.P
sponsor_party.bank_id_code = $42
WHERE id = $1`

version := *payment.Version + 1
version := *original.Version + 1
attrs := payment.Attributes
amounts := senderChargesToAmounts(attrs.ChargesInformation.SenderCharges)
res, err := dbpr.db.Exec(updateStmt,
_, err = dbpr.db.Exec(updateStmt,
payment.ID, // id,
payment.OrganisationID, // organisation,
version, // version,
Expand Down Expand Up @@ -719,14 +725,6 @@ func (dbpr *DBPaymentRepository) Update(paymentID strfmt.UUID, payment *models.P
return nil, fmt.Errorf("db: error executing update: %v", err)
}

count, err := res.RowsAffected()
if err != nil {
return nil, fmt.Errorf("db: error getting rows affected by update: %v", err)
}
if count == 0 {
return nil, newErrNoResults(fmt.Sprintf("db: payment with ID %s not found", paymentID))
}

// Ignore the original type attribute and fix it to TYPE_PAYMENT
updated := copyPayment(payment)
updated.Type = TYPE_PAYMENT
Expand Down
17 changes: 9 additions & 8 deletions pkg/service/db_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ func TestUpdate(t *testing.T) {
testPayment := generateDummyPayments(1)[0]
// Modify the test payment to check that it gets the right type
testPayment.Type = TYPE_PAYMENT + "BAD"
rows := paymentsToRows([]*models.Payment{testPayment})
mock.ExpectQuery(`^SELECT (.+) FROM payments WHERE id = \$1$`).
WithArgs(*testPayment.ID).
WillReturnRows(rows)

args := make([]driver.Value, 42)
for i := range args {
args[i] = sqlmock.AnyArg()
Expand All @@ -429,7 +434,7 @@ func TestUpdate(t *testing.T) {

updated, err := testRepo.Update(*testPayment.ID, testPayment)
if err != nil {
t.Errorf("Unexpected error updating payment: %v", err)
t.Fatalf("Unexpected error updating payment: %v", err)
}

if err := mock.ExpectationsWereMet(); err != nil {
Expand All @@ -454,13 +459,9 @@ func TestUpdateNonExistent(t *testing.T) {
defer testRepo.Close()

testPayment := generateDummyPayments(1)[0]
args := make([]driver.Value, 42)
for i := range args {
args[i] = sqlmock.AnyArg()
}
mock.ExpectExec(`^UPDATE payments SET (.+) WHERE id = \$1$`).
WithArgs(args...).
WillReturnResult(sqlmock.NewResult(0, 0))
mock.ExpectQuery(`^SELECT (.+) FROM payments WHERE id = \$1$`).
WithArgs(*testPayment.ID).
WillReturnError(sql.ErrNoRows)

_, err = testRepo.Update(*testPayment.ID, testPayment)
e, ok := err.(ErrNoResults)
Expand Down

0 comments on commit 8d0896a

Please sign in to comment.