Skip to content

Commit

Permalink
Constraint (#116)
Browse files Browse the repository at this point in the history
* Constraint

* capture exception and test

* isort

* Update views.py

* Update views.py

* test

Co-authored-by: Arturo Pacheco <[email protected]>
  • Loading branch information
rogelioLpz and pachCode authored Apr 15, 2020
1 parent ae2701b commit 9b06dab
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
5 changes: 5 additions & 0 deletions speid/models/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class Transaction(Document, BaseModel):
iva = StringField()

events = ListField(ReferenceField(Event))
meta = {
'indexes': [
{'fields': ('clave_rastreo', 'fecha_operacion'), 'unique': True}
]
}

def set_state(self, state: Estado):
callback_helper.set_status_transaction(self.speid_id, state.value)
Expand Down
5 changes: 4 additions & 1 deletion speid/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os

from flask import abort, request
from mongoengine import DoesNotExist
from mongoengine import DoesNotExist, NotUniqueError
from sentry_sdk import capture_exception, capture_message
from stpmex.exc import StpmexException

Expand Down Expand Up @@ -65,6 +65,9 @@ def create_orden():

r = request.json
r['estado'] = Estado.convert_to_stp_state(transaction.estado)
except (NotUniqueError, AssertionError) as e:
r = dict(estado='LIQUIDACION')
capture_exception(e)
except Exception as e:
r = dict(estado='LIQUIDACION')
transaction.estado = Estado.error
Expand Down
30 changes: 30 additions & 0 deletions tests/models/test_transactions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from datetime import datetime

import pytest
from mongoengine import NotUniqueError
from pydantic import ValidationError
from stpmex.types import TipoCuenta

Expand Down Expand Up @@ -48,6 +51,33 @@ def test_transaction():
transaction.delete()


def test_not_duplicate_transaction():
transaction_data = dict(
concepto_pago='PRUEBA',
institucion_ordenante='646',
cuenta_beneficiario='072691004495711499',
institucion_beneficiaria='072',
monto=1020,
nombre_beneficiario='Rogelio Lopez',
nombre_ordenante='BANCO',
cuenta_ordenante='646180157000000004',
rfc_curp_ordenante='ND',
speid_id='speid_id',
fecha_operacion=datetime.today(),
clave_rastreo='abc123',
)

transaction = Transaction(**transaction_data)
transaction.save()
assert transaction.id is not None

transaction_duplicated = Transaction(**transaction_data)
with pytest.raises(NotUniqueError):
transaction_duplicated.save()
assert transaction_duplicated.id is None
transaction.delete()


def test_transaction_stp_input():
data = dict(
Clave=2456304,
Expand Down
9 changes: 4 additions & 5 deletions tests/views/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,9 @@ def test_create_orden_duplicated(
transactions = Transaction.objects(
clave_rastreo=default_income_transaction['ClaveRastreo']
).order_by('-created_at')
assert len(transactions) == 2
assert transactions[0].stp_id == 2456304
assert transactions[0].estado is Estado.error
assert transactions[1].stp_id == 2456303
assert transactions[1].estado is Estado.succeeded
assert len(transactions) == 1
assert transactions[0].stp_id == 2456303
assert transactions[0].estado is Estado.succeeded
assert resp.status_code == 201
assert resp.json['estado'] == 'LIQUIDACION'
for t in transactions:
Expand Down Expand Up @@ -234,6 +232,7 @@ def test_get_transactions(
resp = client.get('/transactions')
assert resp.status_code == 200
assert len(resp.json) == 2
trx_out.delete()


@pytest.mark.vcr
Expand Down

0 comments on commit 9b06dab

Please sign in to comment.