如何解决有一个无效的外键 Django TestCase
我有一个模型购买和一个模型交易,交易有一个来自 pruchase 的外键,当尝试运行测试时,第一个 test_payment_request 成功,但第二个 test_payment_transaction_state 失败,启动下一个错误:
django.db.utils.IntegrityError: The row in table 'transactions_transactionmodel' with primary key '0664aefce71447699d8ca9e7677ba4cc' has an invalid foreign key: transactions_transactionmodel.purchase_id contains a value 'ba7dc5ac0e1c4b9eb009e772f405f5db' that does not have a corresponding value in purchases_purchasemodel.id.
这是我的测试:
import datetime
import socket
from django.test import TestCase
from .payment import PaymentTransactions
from apps.purchases.models import PurchaseModel
class PaymentTransactionsTestCase(TestCase):
def setUp(self):
self.purchase = {"purchase":PurchaseModel( total_value = 124236,products = [
{
"name": "Aretes","value": "6490"
},{
"name": "Manilla","value": "6.000"
}
],purchase_date = datetime.datetime.utcnow()
),"value":124236,"client_ip": socket.gethostbyname(socket.gethostname())
}
def test_payment_request(self):
error,payment,transaction = PaymentTransactions().\
payment_transaction_request(**self.purchase)
self.assertFalse(error)
self.assertTrue(payment)
self.assertIn("tpaga_payment_url",payment)
self.assertIn("token",payment)
self.assertEquals(transaction.token,payment["token"])
print("paso prueba 1")
def test_payment_transaction_state(self):
purchase = {"purchase":PurchaseModel( total_value = 124236,"client_ip": socket.gethostbyname(socket.gethostname())
}
error,transaction = PaymentTransactions().\
payment_transaction_request(**purchase)
self.assertFalse(error)
error,transaction_created = PaymentTransactions().\
payment_transaction_state(transaction.id)
self.assertFalse(error)
self.assertEquals(transaction_created.state,transaction.state)
但我不知道会发生什么,如果有人知道,请解释一下。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。