Django testing - TypeError: int() argument must be a string, a bytes-like object or a number, not 'User'

382 views Asked by At

I'm writing test cases for my first Django application and using mixer to generate random values for some modules.

Test case written for testing on the models is like

test_model.py

from datetime import datetime, timedelta

from django.core.exceptions import ValidationError
from tzlocal import get_localzone

import pytest
from django.test import TestCase
from mixer.backend.django import mixer

from transactions.models import ModeOfPayment, AmountGiven

pytestmark = pytest.mark.django_db

@pytest.mark.django_db
class TestAmountReturned(TestCase):
    def test_model_amount_return_add(self):
        amount_returned = mixer.blend(
            'transactions.AmountReturned',
            amount_given=mixer.blend(
                'transactions.AmountGiven',
                given_date=datetime.now(get_localzone()) - timedelta(days=300)
            ),
            amount=100.00,
            return_date=datetime.now(get_localzone()) - timedelta(days=50)
        )

        assert str(amount_returned) == str(amount_returned.amount), '__str__ should return amount string'

    def test_model_amount_due(self):
        amount = 10000.00
        interest_rate = 9.5
        duration = 365
        given_date = datetime.now(get_localzone()) - timedelta(days=200)
        returned_amount = 150.00

        amount_given = mixer.blend(
            'transactions.AmountGiven',
            contact=mixer.blend('contacts.Contact'),
            amount=amount,
            interest_rate=interest_rate,
            duration=duration,
            given_date=given_date,
            mode_of_payment=mixer.blend('transactions.ModeOfPayment', title='Cash')
        )

        mixer.blend(
            'transactions.AmountReturned',
            amount_given=amount_given,
            amount=returned_amount,
            return_date=datetime.now(get_localzone()) - timedelta(days=50)
        )

        assert amount_given.amount_due == amount_given.total_payable - returned_amount, 'Should return dues amount'

But on running the testing

pipenv run py.test

It is giving the following error

______________________________ TestAmountReturned.test_model_amount_due _______________________

self = <django.db.models.fields.AutoField: id>, value = <User: khess>

    def to_python(self, value):
        if value is None:
            return value
        try:
>           return int(value)
E           TypeError: int() argument must be a string, a bytes-like object or a number, not 'User'

../../../.local/share/virtualenvs/koober-py-McGChbzt/lib/python3.6/site-packages/django/db/models/fields/__init__.py:940: TypeError

During handling of the above exception, another exception occurred:

self = <transactions.tests.test_models.TestAmountReturned testMethod=test_model_amount_due>

    def test_model_amount_due(self):
        amount = 10000.00
        interest_rate = 9.5
        duration = 365
        given_date = datetime.now(get_localzone()) - timedelta(days=200)
        returned_amount = 150.00

        amount_given = mixer.blend(
            'transactions.AmountGiven',
>           contact=mixer.blend('contacts.Contact'),
            amount=amount,
            interest_rate=interest_rate,
            duration=duration,
            given_date=given_date,
            mode_of_payment=mixer.blend('transactions.ModeOfPayment', title='Cash')
        )

src/transactions/tests/test_models.py:182: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

location of the error in my test case is pointing to

contact=mixer.blend('contacts.Contact'),

but could not figure out which column this error is occurring for. I have used the mixer to blend contacts with other locations also, few of them are working fine.

0

There are 0 answers