...
 
Commits (1)
  • sebastien letort's avatar
    test validators for WebappVersion. · 5f9d38c3
    sebastien letort authored
    Note1: it uses populate_db.py, also use in other branch, still don't know its final place (in main, in api or elsewhere).
    Note2: I alter populate_db.py 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.
    5f9d38c3
#!/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."""
DockerOs.objects.create(name="debian:stable")
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( o_user.id ) )
# ~ 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,
)
app_ids.append(o_app.id)
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 test_models.py.
"""
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
USERNAME = 'Bob'
@classmethod
def setUpTestData(cls):
"""v"""
LOG.info(":> setUpTestData.\nCreate one user, one app.")
populate_db.with_vitals()
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"):
WebappVersion(webapp="bruit",
number=self.APP_NAME,
state=WebappVersion.SANDBOX,
published=True, ) \
.save()
o_app = Webapp.objects.get(name=self.APP_NAME)
with self.assertRaises(ValidationError, msg="number validator"):
WebappVersion(webapp=o_app,
number="(@v@)'",
state=WebappVersion.SANDBOX,
published=True, ) \
.save()
with self.assertRaises(ValidationError, msg="state validator"):
WebappVersion(webapp=o_app,
number=self.APP_NAME,
state="bruit",
published=True, ) \
.save()
with self.assertRaises(ValidationError, msg="published validator"):
WebappVersion(webapp=o_app,
number=self.APP_NAME,
state=WebappVersion.SANDBOX,
published="bruit", ) \
.save()
# test_WebappVersion