【问题标题】:Expressions can only be used to update, not to insert表达式只能用于更新,不能用于插入
【发布时间】:2020-07-03 18:32:21
【问题描述】:

我在尝试使用 Factory boy 和 unittest.mock 创建对象来模拟付款时遇到了这个问题

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

【问题讨论】:

  • 嘿!如果没有模型、工厂和模拟的代码,很难了解正在发生的事情;)如果您能够提供其中的一部分——也许是使用 MagicMock 的部分——这将有助于提供一个正确的答案!

标签: django pytest factory-boy vcr python-unittest.mock


【解决方案1】:

这是我正在使用并导致问题的代码

@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() 和 tockenize_card() 和 save_card() 正在使用用于调用支付 api 的 call_tap_api() 函数,使用 @mock.patch 我得到的值就像 &lt;MagicMock name='call_tap_api().get().resolve_expression()' id='140651554367360'&gt; 我已经解决了这个问题:

@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

现在 customer_id 和 card_token 和 card_id 得到一个生成的值,而不是

<MagicMock name='call_tap_api().get().resolve_expression()' id='140651554367360'>

【讨论】:

    猜你喜欢
    • 2021-11-17
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多