Mocking model.User using mixer throughs error

310 Views Asked by At

Im trying to add unit test for my Django project. Im using mixer to mock the models. The model looks as below

from django.contrib.auth.models import User

class Mytable(Model):
    username = models.OneToOneField(User, on_delete=models.CASCADE, db_column='username')
    ...
    ...

My testcase looks like

class MyTest:
    def test_test1(self):
        mock_user = mixer.blend('django.contrib.auth.models.User')
        stock_mock = mixer.blend('app.Mytable', username=mock_user)

But im hitting "too many values to unpack" while mocking User model

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/kketan/Documents/projects/Extractor/.venv/lib/python3.8/site-packages/mixer/main.py", line 566, in blend
    type_mixer = self.get_typemixer(scheme)
  File "/Users/kketan/Documents/projects/Extractor/.venv/lib/python3.8/site-packages/mixer/main.py", line 583, in get_typemixer
    return self.type_mixer_cls(
  File "/Users/kketan/Documents/projects/Extractor/.venv/lib/python3.8/site-packages/mixer/main.py", line 47, in __call__
    cls_type = cls.__load_cls(cls_type)
  File "/Users/kketan/Documents/projects/Extractor/.venv/lib/python3.8/site-packages/mixer/backend/django.py", line 137, in __load_cls
    app_label, model_name = cls_type.split(".")
ValueError: too many values to unpack (expected 2)

Am I mocking the models right way ? If yes is this error known ? If no can you please suggest better way ?

1

There are 1 best solutions below

3
On

From the documentation of mixer:

You can use class or string with model name.

[1] Model name supports two formats. Use ‘app_name.model_name’ for preventing conflicts. Or you can use just ‘model_name’ for models with unique names.

Hence the format in which you write the model name is incorrect. You need to write it in the form <pp_name>.<model_name>. Hence instead of 'django.contrib.auth.models.User' you need to write 'auth.User':

class MyTest:
    def test_test1(self):
        mock_user = mixer.blend('auth.User')
        stock_mock = mixer.blend('app.Mytable', username=mock_user)