3

I am new to Django and Celery.

high level:

I am working on a Django application. From an admin page uses will submit Requests (jobs). Those Requests will get sent to Redis. Celery will then poll Redis and pull a job from the queue. Once the task is finished the results will be stored in postgres.

Here is an example task that is used to kick off some tests via pytest.main().

# task for running tests by marker 
def run_tests_mark(test_marker):
    os.chdir('/service/lib/tests')
    # only allow specific strings to be passed in by the user
    if test_marker not in ['smoke', 'regression']: # update as more tages are introduced to the project
        return 'You have entered an invalid term. Please use either smoke of regression.'
    # run pytest command with self contained reporting
    results = pytest.main(['-v', '--json-report', '-m', test_marker])
    # TODO: after tests run send json report to postgres

The code will run the tests, but as you can see by the last comment I want to take the resulting .json.report and store it in a postgres db [please note I use a helper to grab the actual report generated by results]

Now here is where I get confused.

Do I need to first create a model based on all the keys in the json report that is going to be generated by pytest?

If so would I then use something like TestResultModel.objects.create(.....) to then insert the report into postgres?

Here is a sample of the the json that gets output by pytest.main

{"created": 1535570420.542123, "duration": 215.14111948013306, "exitcode": 1, "root": "/test", "environment": {"Python": "3.6.6", "Platform": "Linux-4.9.93-linuxkit-aufs-x86_64-with-debian-9.5", "Packages": {"pytest": "3.6.2", "py": "1.5.4", "pluggy": "0.6.0"}, "Plugins": {"xdist": "1.22.5", "metadata": "1.7.0", "json-report": "0.7.0", "forked": "0.2", "django": "3.3.3", "cov": "2.5.1", "celery": "4.2.1"}}, "summary": {"passed": 34, "failed": 7, "total": 41}

Lombax
  • 851
  • 4
  • 9
  • 25

1 Answers1

2

Do I need to first create a model based on all the keys in the json report that is going to be generated by pytest?

Generally the answer would be yes. But it doesn't look like relational data that you are saving in your db. So you can make use of the JSONField and insert everything in there in one go. The JSONField corresponds to the JSONB fields in postgresql which is designed to store json objects and allows those objects to be searched, modified etc.

You might also want to see https://stackoverflow.com/a/32091771/267540

e4c5
  • 52,766
  • 11
  • 101
  • 134