Search code examples
pythondjangodjango-testingdjango-testsmodel-mommy

django tests - model instance not created


I'm experiencing issues on creating fixtures for a particular model in a test class with Django and the DRF. I'm using model-mommy, but even creating a single instance just using the Django ORM for that model does not work:

    from rest_framework.test import APIRequestFactory, APITestCase
    from model_mommy import mommy

    from api.v1 import views
    from company.models.models import CompanyUserProfile, ManagerRelation, DynamicRelation


    User = get_user_model()


    class APIViewsTestCase(APITestCase):

        @classmethod
        def setUpTestData(cls):
            supervisory_id = "1337"
            emp_group_id = "119"
            prd_scope_id = "1334"
            user = mommy.make(User)
            setattr(cls, 'user', user)
            company_user_profile_1 = mommy.make(CompanyUserProfile)
            requestor = mommy.make(
                CompanyUserProfile, is_manager=True,
                supervisory_id=supervisory_id, emp_group_id=emp_group_id,
                prd_scope_id=prd_scope_id, user=user
            )
            cls.user = user
            manager_relation = mommy.make(
                ManagerRelation, manager_id=requestor.id, mgr_type="Direct Mgr", _quantity=5
            )
            dynamic_relations = mommy.make(
                DynamicRelation,target_id=requestor.id, _quantity=5
            )

        def setUp(self) -> None:
            super().setUp()
            self.client.force_authenticate(user=self.user)

        def test_employee_view(self):
            CompanyUserProfile.objects.create(
                guid=uuid.uuid4(),
                csod_user_id='idcbijnccdocwocd', csod_username='djchbdcjnwdskjcn',
                id=3333
            )
            print('cup count:', CompanyUserProfile.objects.count())
            print('user count:', User.objects.count())
            print('m_rels count:', ManagerRelation.objects.count())
            print('d_rels count:', DynamicRelation.objects.count())
            rsp = self.client.get('/api/v1/employees/')
            self.assertEqual(rsp.status_code, HTTPStatus.OK)

however CompanyUserProfile records are not created, both using mommy.make or CompanyUserProfile.objects.create:

-- test run output
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
cup count: 0
user count: 1
m_rels count: 5
d_rels count: 5
Destroying test database for alias 'default'...

CompanyUserProfile inherits from AbstractCornerstoneUserProfile (abstract base model), that's the main difference between this and the other models.

Any idea of what can be the root cause of this?


Solution

  • UPDATE: solved -> 100% my fault, the custom ModelManager of CompanyUserProfile was excluding created objects on CompanyUserProfile.objects.count().