Commit 5f9d38c3 authored by sebastien letort's avatar sebastien letort

test validators for WebappVersion.

Note1: it uses, also use in other branch, still don't know its final place (in main, in api or elsewhere).
Note2: I alter regarding PEP8/pylint (and I remove code unused)
Note3: pylint score is still very bad, I have pb with indentation expected, it doesn't enhanced lisibility IMO.
Note4: I also have pb with space around '=', I'd like very much to align those '=' to ease lisibility.

Last note: I had 2 errors due to 'from main.models' which I do not understand.
parent 610f9c90
Pipeline #74762 passed with stages
in 2 minutes and 21 seconds
#!/usr/bin/env python3
provide function to populate the DB for tests.
# USAGE : import populate_db
from main.models import User, Webapp, DockerOs, JobQueue, EmailAddress
def with_vitals():
"""Populate the DB with anything that is needed, but not specified."""
JobQueue.objects.create(name="default", is_default=True, )
def with_users(l_usernames):
"""Populate the DB with a list of users : create AllgoUsers.
Note: with_vitals should have been called first."""
# Note : bulk_create should be the most efficient way to create several users.
# but I cannot managed to build AllgoUser with this.
# ~ User.objects.bulk_create([
# ~ User( username='Bob', email='bob@localhost', password='' ),
# ~ User( username='Liz', email='Liz@localhost', password='' ),
# ~ User( username='Zaza', email='Zaza@localhost', password='' ),
# ~ User( username='Lol', email='lol@localhost', password='' ),
# ~ ])
# Note: It seems AllgoUsers are created without asking !
for name in l_usernames:
o_user = User.objects.create_user(
username = name,
email = name + '@localhost',
password = '',
# ~ print( name + " id=" + str( ) )
# ~ l_ = AllgoUser.objects.all()
# ~ [ print(str(x)) for x in l_ ]
EmailAddress.objects.create(user=o_user, email=name, verified=True)
# with_users
def with_webapps(apps):
"""apps = { user: [app_name1,app_name2,...] }
Will use the first docker_os.
Return { user: [app_id1,app_id2] }"""
o_docker_os = DockerOs.objects.first()
o_job_queue = JobQueue.objects.first()
out_ids = {}
for user in apps:
o_user = User.objects.get(username=user)
app_ids = []
for app_name in apps[user]: # apps is a dict
o_app = Webapp.objects.create(
name = app_name,
docker_os = o_docker_os,
job_queue = o_job_queue,
user = o_user,
out_ids[user] = app_ids
return out_ids
# with_webapps
#! /usr/bin/env python3
"""Basic test to check that exception are raised where a model is miscreated.
I think this file is complementary to
import logging
from django.core.validators import ValidationError
from django.test import TestCase
from main.models import Webapp, WebappVersion
from . import populate_db
LOG = logging.getLogger('test')
class TestValidators(TestCase):
"""Test all models validation."""
APP_NAME = 'Fake'
APP_ID = None
def setUpTestData(cls):
"""v"""":> setUpTestData.\nCreate one user, one app.")
populate_db.with_users(['Bob', ])
# app_ids is {cls.USERNAME: [app_id]}
app_ids = populate_db.with_webapps({cls.USERNAME: [cls.APP_NAME]})
cls.APP_ID = app_ids[cls.USERNAME][0]
# ----------------------------------------------------------------
def test_WebappVersion(self):
"""Check all attributes."""
with self.assertRaises(ValueError, msg="webapp should exist"):
published=True, ) \
o_app = Webapp.objects.get(name=self.APP_NAME)
with self.assertRaises(ValidationError, msg="number validator"):
published=True, ) \
with self.assertRaises(ValidationError, msg="state validator"):
published=True, ) \
with self.assertRaises(ValidationError, msg="published validator"):
published="bruit", ) \
# test_WebappVersion
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment