From f56a37edfa0176c7e853a157360280da89dabdcc Mon Sep 17 00:00:00 2001 From: Tomas Gavenciak Date: Sun, 29 Mar 2015 14:52:07 +0200 Subject: [PATCH] GET tests, improve testdata generation, rename views --- seminar/management/commands/testdata.py | 4 +- seminar/tests.py | 112 +++++++----------------- seminar/testutils.py | 88 +++++++++++++++++++ seminar/urls.py | 4 +- 4 files changed, 125 insertions(+), 83 deletions(-) create mode 100644 seminar/testutils.py diff --git a/seminar/management/commands/testdata.py b/seminar/management/commands/testdata.py index 775161c5..dda7b097 100644 --- a/seminar/management/commands/testdata.py +++ b/seminar/management/commands/testdata.py @@ -9,7 +9,7 @@ from django.core.management import call_command from django.conf import settings from seminar.models import Skola, Resitel, Rocnik, Cislo, Problem, Reseni, PrilohaReseni, Nastaveni -from seminar.tests import create_test_data +from seminar.testutils import create_test_data import django.contrib.auth User = django.contrib.auth.get_user_model() @@ -25,7 +25,7 @@ class Command(NoArgsCommand): self.stderr.write('Stara databaze prejmenovana na "%s"' % (dbfile + '.old')) call_command('migrate', noinput=True) self.stdout.write('Vytvarim uzivatele "admin" (heslo "admin") a pseudo-nahodna data ...') - create_test_data() + create_test_data(size=8) self.stdout.write('Vytvoreno %d uzivatelu, %d skol, %d resitelu, %d rocniku, %d cisel, %d problemu, %d reseni.' % (User.objects.count(), Skola.objects.count(), Resitel.objects.count(), Rocnik.objects.count(), Cislo.objects.count(), Problem.objects.count(), Reseni.objects.count())) diff --git a/seminar/tests.py b/seminar/tests.py index b70512ae..b9a2fe38 100644 --- a/seminar/tests.py +++ b/seminar/tests.py @@ -2,90 +2,44 @@ import datetime import random +import django.contrib.auth from unittest import TestCase +from django.test import Client +from django.core.urlresolvers import reverse +from django.core.management import call_command -import django.contrib.auth -from django.db import transaction from seminar.models import Skola, Resitel, Rocnik, Cislo, Problem, Reseni, PrilohaReseni, Nastaveni +from seminar.testutils import create_test_data -User = django.contrib.auth.get_user_model() - -@transaction.atomic -def create_test_data(): - # pevna pseudo-nahodnost - rnd = random.Random(x=42) - - # users - admin = User.objects.create_superuser(username='admin', email='', password='admin') - - orgs = [] - for org in ['anet', 'bara', 'cyril', 'david', 'eva', 'filip']: - o = User.objects.create_user(username=org, password=org) - o.first_name = org.capitalize() - o.save() - orgs.append(o) - - # skoly - Skola.objects.create(mesto = u'Praha', stat='CZ', psc='101 00', ulice=u'Krátká 5', nazev=u'První SŠ') - Skola.objects.create(mesto = u'Praha', stat='CZ', psc='102 00', ulice=u'Dlouhá 5', nazev=u'Druhá SŠ') - Skola.objects.create(mesto = u'Praha', stat='CZ', psc='103 00', ulice=u'Široká 3', nazev=u'Třetí SŠ') - Skola.objects.create(mesto = u'Ostrava', stat='CZ', psc='700 00', ulice=u'Hluboká 42', nazev=u'Hutní gympl') - Skola.objects.create(mesto = u'Humenné', stat='SK', psc='012 34', ulice=u'Pltká 1', nazev=u'Sredná škuola') - - # resitele - jmena_m = ['Aleš', 'Tomáš', 'Martin', 'Jakub', 'Petr', 'Lukáš', 'Cyril'] - jmena_f = ['Eva', 'Karolína', 'Zuzana', 'Sylvie', 'Iva', 'Jana', 'Marie'] - prijmeni_m = ['Novotný', 'Svoboda', 'Pecha', 'Kořen', 'Holan', 'Uhlíř', 'Chytráček', 'Pokora', 'Koch', 'Szegedy', 'Rudý'] - prijmeni_f = ['Novotná', 'Svobodová', 'Machová', 'Zelená', 'Yu-Xin', 'Mlsná', 'Dubná', 'Mrkvová', 'Suchá', 'Lovelace', 'Holcová'] - for i in range(20): - skola = rnd.choice(Skola.objects.all()) - pohlavi = rnd.randint(0,1) - jmeno = rnd.choice([jmena_m, jmena_f][pohlavi]) - prijmeni = rnd.choice([prijmeni_m, prijmeni_f][pohlavi]) - Resitel.objects.create(skola = skola, datum_prihlaseni = datetime.date(rnd.randint(2002, 2014), rnd.randint(1,12), 1), - jmeno =jmeno, prijmeni = prijmeni, rok_maturity = rnd.randint(2015, 2019), - stat = skola.stat, zasilat = Resitel.ZASILAT_NIKAM, pohlavi_muz = pohlavi) - resitele = list(Resitel.objects.all()) - - # rocniky - for ri in range(17, 22): - r = Rocnik.objects.create(prvni_rok = 1993 + ri, rocnik = ri) - - # cisla - cisel = rnd.randint(4, 6) - cs = {} - for ci in range(1, cisel + 1): - vydano = datetime.date(r.prvni_rok, ci + 6, 1) - deadline = datetime.date(r.prvni_rok, ci + 8, 1) if ci + 2 < cisel else None - c = Cislo.objects.create(rocnik = r, cislo = str(ci), datum_vydani=vydano, datum_deadline=deadline) - cs[ci] = c - - # problemy resene v ci - seq='#ABCDEFGHIJKLMNOPQRSTUVWXYZ' - if ci >= 3: - for pi in range(1, 4): - p = Problem.objects.create(autor = rnd.choice(orgs), cislo_zadani=cs[ci-2], cislo_reseni=cs[ci], - opravovatel = rnd.choice(orgs), kod = str(pi), nazev = u'Dummy úloha %s-%s' % (seq[ci], seq[pi]), - stav = Problem.STAV_ZADANY, typ = Problem.TYP_ULOHA, body = rnd.randint(1, 5)) - p.text_problemu = (u"

Text problému %s.%s %s [id %d] za %d body.

" % - (p.cislo_zadani.kod(), p.kod, p.nazev, p.id, p.body)) - p.text_problemu_org = u"

Neveřejný text problému.

" - p.save() - - res_sel = rnd.sample(resitele, min(rnd.randint(3, 10), len(resitele) - 2)) - for resitel in res_sel: - res = Reseni.objects.create(problem = p, resitel = resitel, - body = rnd.randint(0, p.body), cislo_body = cs[ci]) - - nastaveni = Nastaveni.objects.create(aktualni_rocnik = Rocnik.objects.last(), - aktualni_cislo = Cislo.objects.last()) - - -class SeminarTests(TestCase): +class SeminarBasicTests(TestCase): def setUp(self): - create_test_data() + create_test_data(size=2) + self.client = Client() + + def tearDown(self): + call_command('flush', noinput=True, verbosity=0, interactive=False) + self.cleint = None def test_rocniky(self): - r19 = Rocnik.objects.get(rocnik=19) - self.assertEqual(r19.roman(), 'XIX') + r19 = Rocnik.objects.get(rocnik=21) + self.assertEqual(r19.roman(), 'XXI') + + def test_render_cislo_e2e(self): + cs = Cislo.objects.all() + for c in cs[:8]: + url = reverse('seminar.cislo', args=(c.id,)) + r = self.client.get(url) + assert r.status_code == 200 + assert len(r.content) >= 100 + # TODO: Validate cntent as HTML + + def test_render_problem_e2e(self): + ps = Problem.objects.all() + for p in ps[:10]: + url = reverse('seminar.problem', args=(p.id,)) + r = self.client.get(url) + assert r.status_code == 200 + assert len(r.content) >= 100 + # TODO: Validate cntent as HTML + diff --git a/seminar/testutils.py b/seminar/testutils.py new file mode 100644 index 00000000..d26ad272 --- /dev/null +++ b/seminar/testutils.py @@ -0,0 +1,88 @@ +# -*- coding: utf-8 -*- + +import datetime +import random +import django.contrib.auth +from django.db import transaction + +from seminar.models import Skola, Resitel, Rocnik, Cislo, Problem, Reseni, PrilohaReseni, Nastaveni + +User = django.contrib.auth.get_user_model() + +@transaction.atomic +def create_test_data(size = 6, rnd = None): + assert size >= 1 + # pevna pseudo-nahodnost + rnd = rnd or random.Random(x=42) + + # users + admin = User.objects.create_superuser(username='admin', email='', password='admin') + + usernames = ['anet', 'bara', 'cyril', 'david', 'eva', 'filip'] + orgs = [] + for org in usernames[:size]: + o = User.objects.create_user(username=org, password=org) + o.first_name = org.capitalize() + o.save() + orgs.append(o) + + # skoly + Skola.objects.create(mesto = u'Praha', stat='CZ', psc='101 00', ulice=u'Krátká 5', nazev=u'První SŠ') + Skola.objects.create(mesto = u'Praha', stat='CZ', psc='102 00', ulice=u'Dlouhá 5', nazev=u'Druhá SŠ') + Skola.objects.create(mesto = u'Praha', stat='CZ', psc='103 00', ulice=u'Široká 3', nazev=u'Třetí SŠ') + Skola.objects.create(mesto = u'Ostrava', stat='CZ', psc='700 00', ulice=u'Hluboká 42', nazev=u'Hutní gympl') + Skola.objects.create(mesto = u'Humenné', stat='SK', psc='012 34', ulice=u'Pltká 1', nazev=u'Sredná škuola') + + # resitele + jmena_m = ['Aleš', 'Tomáš', 'Martin', 'Jakub', 'Petr', 'Lukáš', 'Cyril'] + jmena_f = ['Eva', 'Karolína', 'Zuzana', 'Sylvie', 'Iva', 'Jana', 'Marie'] + prijmeni_m = ['Novotný', 'Svoboda', 'Pecha', 'Kořen', 'Holan', 'Uhlíř', 'Chytráček', 'Pokora', 'Koch', 'Szegedy', 'Rudý'] + prijmeni_f = ['Novotná', 'Svobodová', 'Machová', 'Zelená', 'Yu-Xin', 'Mlsná', 'Dubná', 'Mrkvová', 'Suchá', 'Lovelace', 'Holcová'] + for i in range(3*size): + skola = rnd.choice(Skola.objects.all()) + pohlavi = rnd.randint(0,1) + jmeno = rnd.choice([jmena_m, jmena_f][pohlavi]) + prijmeni = rnd.choice([prijmeni_m, prijmeni_f][pohlavi]) + Resitel.objects.create(skola = skola, datum_prihlaseni = datetime.date(rnd.randint(2002, 2014), rnd.randint(1,12), 1), + jmeno =jmeno, prijmeni = prijmeni, rok_maturity = rnd.randint(2015, 2019), + stat = skola.stat, zasilat = Resitel.ZASILAT_NIKAM, pohlavi_muz = pohlavi) + resitele = list(Resitel.objects.all()) + + # rocniky + last_rocnik = 21 + for ri in range(last_rocnik - size, last_rocnik + 1): + r = Rocnik.objects.create(prvni_rok = 1993 + ri, rocnik = ri) + + # cisla + cisel = rnd.randint(4, 6) + if ri == last_rocnik: + cisel = 1 + + cs = {} + for ci in range(1, cisel + 1): + vydano = datetime.date(r.prvni_rok, ci + 6, 1) + deadline = datetime.date(r.prvni_rok, ci + 8, 1) if ci + 2 < cisel else None + c = Cislo.objects.create(rocnik = r, cislo = str(ci), datum_vydani=vydano, datum_deadline=deadline) + cs[ci] = c + + # problemy resene v ci + seq='#ABCDEFGHIJKLMNOPQRSTUVWXYZ' + if ci >= 3: + for pi in range(1, ((size + 1) // 2) + 1): + p = Problem.objects.create(autor = rnd.choice(orgs), cislo_zadani=cs[ci-2], cislo_reseni=cs[ci], + opravovatel = rnd.choice(orgs), kod = str(pi), nazev = u'Dummy úloha %s-%s' % (seq[ci], seq[pi]), + stav = Problem.STAV_ZADANY, typ = Problem.TYP_ULOHA, body = rnd.randint(1, 5)) + p.text_problemu = (u"

Text problému %s.%s %s [id %d] za %d body.

" % + (p.cislo_zadani.kod(), p.kod, p.nazev, p.id, p.body)) + p.text_problemu_org = u"

Neveřejný text problému.

" + p.save() + + poc_reseni = rnd.randint(size // 2, size * 2) + res_sel = rnd.sample(resitele, min(poc_reseni, len(resitele) - 2)) + for resitel in res_sel: + res = Reseni.objects.create(problem = p, resitel = resitel, + body = rnd.randint(0, p.body), cislo_body = cs[ci]) + + nastaveni = Nastaveni.objects.create(aktualni_rocnik = Rocnik.objects.last(), + aktualni_cislo = Cislo.objects.last()) + diff --git a/seminar/urls.py b/seminar/urls.py index 7aca9c7a..869b59ad 100644 --- a/seminar/urls.py +++ b/seminar/urls.py @@ -2,7 +2,7 @@ from django.conf.urls import patterns, url from . import views urlpatterns = patterns('', - url(r'^problem/(?P\d+)/$', views.ProblemView.as_view(), name='problem'), - url(r'^cislo/(?P\d+)/$', views.CisloView.as_view(), name='cislo'), + url(r'^problem/(?P\d+)/$', views.ProblemView.as_view(), name='seminar.problem'), + url(r'^cislo/(?P\d+)/$', views.CisloView.as_view(), name='seminar.cislo'), url(r'^zadani/$', 'seminar.views.AktualZadaniView'), )