tenant-schemas-celery icon indicating copy to clipboard operation
tenant-schemas-celery copied to clipboard

Celery does not match models in run pytest

Open iamunadike opened this issue 2 years ago • 4 comments

I get this message from tenant_schemas_celery ] Signal handler <function switch_schema at 0x7effb0c4e710> raised: DoesNotExist('Client matching query does not exist.') Traceback (most recent call last): File "/home/hacker/.local/lib/python3.10/site-packages/celery/utils/dispatch/signal.py", line 276, in send

when running pytests because the aspect of a code is expected to run a background job to update another model It seems like celery is not aware of the test models instead it checks with the real models and so raises the above exception Any help on how to arrange my conftests.py with celery and pytest will be nice , alsso rabbit is the message broker

iamunadike avatar Aug 02 '22 18:08 iamunadike

Have you tried using always eager celery during your tests?

wt., 2 sie 2022, 20:46 użytkownik Michael You @.***> napisał:

I get this message from tenant_schemas_celery

] Signal handler <function switch_schema at 0x7effb0c4e710> raised: DoesNotExist('Client matching query does not exist.') Traceback (most recent call last): File "/home/hacker/.local/lib/python3.10/site-packages/celery/utils/dispatch/signal.py", line 276, in send

when running pytests because the aspect of a code is expected to run a background job to update another model It seems like celery is not aware of the test models instead it checks with the real models and so raises the above exception Any help on how to arrange my conftests.py with celery and pytest will be nice , alsso rabbit is the message broker

— Reply to this email directly, view it on GitHub https://github.com/maciej-gol/tenant-schemas-celery/issues/57, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOGQTOKADK3FS23ACGG2MLVXFUJDANCNFSM55MFP7GQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

maciej-gol avatar Aug 02 '22 19:08 maciej-gol

the thing here is celery works but I dont know if in the test it is schema sensitive because I am running tests based on schema so when a A model is saved it creates a background job to make a B model update ccertain fields so I have to get the model B in the task but passing the id simply shows that Object does not exist, which I presume is because celery is not looking at the fixture but at the default real model of the real app

iamunadike avatar Aug 02 '22 19:08 iamunadike

Its because your tests are running in a database transaction. In order for your tasks to see the results of your tests, you need to commit it or enable always eager celery

maciej-gol avatar Aug 03 '22 14:08 maciej-gol

the task """

@app.task
def task_to_update_good_quantity_if_saved(qty, svs):
    services = json.loads(svs)
    for s in services:
        import pdb; pdb.set_trace()
        service = Service.objects.get(id=s.id)
        #print("this is service", service)
        if service.category.type == 'g':
            service.quantity  = service.quantity - qty
            service.save()

the test

**@pytest.mark.django_db
@override_settings(CELERY_TASK_ALWAYS_EAGER=True,CELERY_TASK_EAGER_PROPOGATES=True)
def test_stock_creation_of_reduces_goods_by_sline_quantity(api_client, stock_payload_for_goods, nums):
    """ tests stock using a stock payload assuming only goods"""
    res = api_client.post(reverse_lazy("order_api:stocks-list"), stock_payload_for_goods, format="json")
    assert res.status_code == 201
    _json =  json.loads(res.content)
    for line in _json['stocklines']:
        _id = line['services'][0]['id']
        sr = Service.objects.get(id=_id)
        #import pdb; pdb.set_trace()
        assert sr.quantity == 100**

I run celery task with .delay() in a serializer when the api call is successful but the challenge here is to get celery to give the changes so I can assert it correctly.

iamunadike avatar Aug 04 '22 20:08 iamunadike

Its because your tests are running in a database transaction. In order for your tasks to see the results of your tests, you need to commit it or enable always eager celery

wt., 2 sie 2022, 21:19 użytkownik Michael You @.***> napisał:

the thing here is celery works but I dont know if in the test it is schema sensitive because I am running tests based on schema so when a A model is saved it creates a background job to make a B model update ccertain fields so I have to get the model B in the task but passing the id simply shows that Object does not exist

— Reply to this email directly, view it on GitHub https://github.com/maciej-gol/tenant-schemas-celery/issues/57#issuecomment-1203117610, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOGQTPMAMNK5OSRQI5HQWLVXFYCXANCNFSM55MFP7GQ . You are receiving this because you commented.Message ID: @.***>

maciej-gol avatar Oct 11 '22 09:10 maciej-gol