i got this issue when trying to create object using Factory boy and unittest.mock for mocking payment
self = <django.db.models.sql.compiler.SQLInsertCompiler object at 0x7f5aa2913400>, field = <django.db.models.fields.CharField: card_id>
value = <MagicMock name='call_tap_api().get().resolve_expression()' id='140027256369728'>
def prepare_value(self, field, value):
"""
Prepare a value to be used in a query by resolving it if it is an
expression and otherwise calling the field's get_db_prep_save().
"""
if hasattr(value, 'resolve_expression'):
value = value.resolve_expression(self.query, allow_joins=False, for_save=True)
# Don't allow values containing Col expressions. They refer to
# existing columns on a row, but in the case of insert the row
# doesn't exist yet.
if value.contains_column_references:
raise ValueError(
'Failed to insert expression "%s" on %s. F() expressions '
> 'can only be used to update, not to insert.' % (value, field)
)
E ValueError: Failed to insert expression "<MagicMock name='call_tap_api().get().resolve_expression()' id='140027256369728'>" on tap.TapSubscription.card_id. F() expressions can only be used to update, not to insert.
/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py:1171: ValueError
this is the code that i was using and caused the issue
@pytest.mark.django_db
@tap_vcr.use_cassette(match_on=('method', 'path'))
@override_settings(TAP_SECRET_KEY='test')
@mock.patch('core.payment.gateway.tap.utils.initiate_payment')
def test_subscription_handler(client, user):
user = UserFactory.create()
with mock.patch('core.payment.gateway.tap.utils.call_tap_api') as call_tap_api:
customer_id = create_customer(user)
card_token = tockenize_card('123456789',
'01',
'01',
'100',
user.username)
card_id = save_card(customer_id, card_token)
instance = TapSubscriptionFactory.create(user=user, card_id= card_id)
assert instance.id
create_customer() and tockenize_card() and save_card() are using call_tap_api() function that used to call payment api , with using @mock.patch the value i got was like <MagicMock name='call_tap_api().get().resolve_expression()' id='140651554367360'>
i've solved the issue by :
@pytest.mark.django_db
@tap_vcr.use_cassette(match_on=('method', 'path'))
@override_settings(TAP_SECRET_KEY='test')
@mock.patch('core.payment.gateway.tap.utils.initiate_payment')
def test_subscription_handler(client, user):
user = UserFactory.create()
with mock.patch('core.payment.gateway.tap.utils.call_tap_api') as call_tap_api:
customer_id = user.tap_customer_id
card_token = factory.fuzzy.FuzzyInteger(1, 9999)
card_id = factory.fuzzy.FuzzyInteger(1, 9999)
instance = TapSubscriptionFactory.create(user=user, card_id= card_id)
assert instance.id
now customer_id and card_token and card_id get a generated value instead of
<MagicMock name='call_tap_api().get().resolve_expression()' id='140651554367360'>