Commit ecc1a831 authored by Bryan  BRANCOTTE's avatar Bryan BRANCOTTE
Browse files

splitting test_views

Merge branch 'split-test-views'
parents 8dc0dbd7 bacc39de
import logging
import os
from tempfile import NamedTemporaryFile
import pandas as pd
from basetheme_bootstrap.user_preferences import get_user_preferences_for_user
from django.contrib.auth import get_user_model
from django.core import mail
from django.core.exceptions import ValidationError
from django.core.files.uploadedfile import SimpleUploadedFile
from django.db.models import Count, Q, F
from django.test import TestCase
from django.urls import reverse
from django.utils import timezone
from viralhostrangedb import models, business_process
logger = logging.getLogger(__name__)
class ViewTestCase(TestCase):
test_data = "./test_data"
# __public_big_data_source_of_user_mapped = None
def write_in_tmp_file(self, response):
with NamedTemporaryFile(delete=False, suffix=".html") as f:
f.write(response.content)
print(response.content)
print(f.name)
def setUp(self):
# self.startTime = time.time()
self.user_pwd = "eil2guj4cuSho2Vai3hu"
################################################################################
get_user_model().objects.create(
username="tata",
)
################################################################################
get_user_model().objects.create(
username="titi",
)
################################################################################
self.user = get_user_model().objects.create(
username="user",
email="b@a.a",
)
self.user.set_password(self.user_pwd)
self.user.save()
################################################################################
self.staff_user = get_user_model().objects.create(
username="staff_user",
email="staff@a.a",
is_staff=True,
)
self.user.set_password(self.user_pwd)
self.user.save()
################################################################################
self.toto = get_user_model().objects.create(
username="toto",
email="a@a.a",
)
self.toto.set_password(self.user_pwd)
self.toto.save()
################################################################################
self.lysis = models.GlobalViralHostResponseValue.objects_mappable().get(name="Infection")
self.weak = models.GlobalViralHostResponseValue.objects_mappable().get(name="Intermediate")
self.no_lysis = models.GlobalViralHostResponseValue.objects_mappable().get(name="No infection")
self.not_mapped_yet = models.GlobalViralHostResponseValue.get_not_mapped_yet()
################################################################################
# set the pk to different starting value for each model to avoid
models.Virus.objects.create(name="won't last", pk=0).delete()
models.Host.objects.create(name="won't last", pk=100).delete()
models.DataSource.objects.create(
owner=self.toto,
name="won't last",
raw_name="won't last",
pk=200,
creation_date=timezone.now(),
).delete()
################################################################################
self.private_data_source_of_user = models.DataSource.objects.create(
owner=self.user,
public=False,
name="private user",
raw_name="ee",
kind="FILE",
)
filename = os.path.join(self.test_data, "simple.xlsx")
business_process.import_file(
data_source=self.private_data_source_of_user,
file=filename,
)
################################################################################
self.public_data_source_of_user = models.DataSource.objects.create(
owner=self.user,
public=True,
name="public user",
raw_name="ee",
kind="FILE",
)
self.public_data_source_of_user.allowed_users.add(self.user)
self.public_data_source_of_user.save()
filename = os.path.join(self.test_data, "three_reponse_simple.xlsx")
business_process.import_file(
data_source=self.public_data_source_of_user,
file=filename,
)
################################################################################
self.public_data_source_of_user_mapped = models.DataSource.objects.create(
owner=self.user,
public=True,
name="public user mapped",
raw_name="ee",
kind="FILE",
)
self.public_data_source_of_user_mapped.allowed_users.add(self.user)
self.public_data_source_of_user_mapped.save()
filename = os.path.join(self.test_data, "three_reponse_simple.xlsx")
business_process.import_file(
data_source=self.public_data_source_of_user_mapped,
file=filename,
)
url = reverse('viralhostrangedb:data-source-mapping-label-edit',
args=[self.public_data_source_of_user_mapped.pk])
self.client.force_login(self.user)
self.assertFalse(self.public_data_source_of_user_mapped.is_mapping_done)
form_data = {
"form-TOTAL_FORMS": 3,
"form-INITIAL_FORMS": 3,
"form-MIN_NUM_FORMS": 0,
"form-MAX_NUM_FORMS": 1000,
"form-0-raw_response": 0.0,
"form-0-mapping": self.no_lysis.pk,
"form-1-raw_response": 1.0,
"form-1-mapping": self.no_lysis.pk,
"form-2-raw_response": 2.0,
"form-2-mapping": self.lysis.pk,
}
self.client.post(url, form_data)
self.client.get(url)
self.client.logout()
################################################################################
self.private_data_source_of_user_mapped = models.DataSource.objects.create(
owner=self.user,
public=False,
name="private user mapped",
raw_name="ee",
kind="FILE",
)
self.private_data_source_of_user_mapped.allowed_users.add(self.user)
self.private_data_source_of_user_mapped.save()
filename = os.path.join(self.test_data, "three_reponse_simple_2.xlsx")
business_process.import_file(
data_source=self.private_data_source_of_user_mapped,
file=filename,
)
url = reverse('viralhostrangedb:data-source-mapping-label-edit',
args=[self.private_data_source_of_user_mapped.pk])
self.client.force_login(self.user)
self.assertFalse(self.private_data_source_of_user_mapped.is_mapping_done)
form_data = {
"form-TOTAL_FORMS": 3,
"form-INITIAL_FORMS": 3,
"form-MIN_NUM_FORMS": 0,
"form-MAX_NUM_FORMS": 1000,
"form-0-raw_response": 0.0,
"form-0-mapping": self.no_lysis.pk,
"form-1-raw_response": 1.0,
"form-1-mapping": self.weak.pk,
"form-2-raw_response": 2.0,
"form-2-mapping": self.lysis.pk,
}
self.client.post(url, form_data)
self.client.logout()
################################################################################
self.private_data_source_of_toto = models.DataSource.objects.create(
owner=self.toto,
public=False,
name="private toto",
raw_name="ee",
kind="FILE",
)
filename = os.path.join(self.test_data, "simple.xlsx")
business_process.import_file(
data_source=self.private_data_source_of_toto,
file=filename,
)
################################################################################
self.public_data_source_with_ids_of_toto = models.DataSource.objects.create(
owner=self.toto,
public=True,
name="public with_ids",
raw_name="ee",
kind="FILE",
)
filename = os.path.join(self.test_data, "simple-with-id.xlsx")
business_process.import_file(
data_source=self.public_data_source_with_ids_of_toto,
file=filename,
)
################################################################################
self.three_reponse_simple = models.DataSource.objects.create(
owner=self.user,
public=True,
name="three_reponse_simple mapped",
raw_name="ee",
kind="FILE",
)
business_process.import_file(
data_source=self.three_reponse_simple,
file=os.path.join(self.test_data, "three_reponse_simple.xlsx"),
)
url = reverse('viralhostrangedb:data-source-mapping-label-edit', args=[self.three_reponse_simple.pk])
self.client.force_login(self.user)
self.assertFalse(self.three_reponse_simple.is_mapping_done)
form_data = {
"form-TOTAL_FORMS": 3,
"form-INITIAL_FORMS": 3,
"form-MIN_NUM_FORMS": 0,
"form-MAX_NUM_FORMS": 1000,
"form-0-raw_response": 0.0,
"form-0-mapping": self.no_lysis.pk,
"form-1-raw_response": 1.0,
"form-1-mapping": self.weak.pk,
"form-2-raw_response": 2.0,
"form-2-mapping": self.lysis.pk,
}
self.client.post(url, form_data)
self.client.logout()
self.assertTrue(self.three_reponse_simple.is_mapping_done)
################################################################################
self.three_reponse_simple_2 = models.DataSource.objects.create(
owner=self.user,
public=True,
name="three_reponse_simple_2 mapped",
raw_name="ee",
kind="FILE",
)
business_process.import_file(
data_source=self.three_reponse_simple_2,
file=os.path.join(self.test_data, "three_reponse_simple_2.xlsx"),
)
url = reverse('viralhostrangedb:data-source-mapping-label-edit', args=[self.three_reponse_simple_2.pk])
self.client.force_login(self.user)
self.assertFalse(self.three_reponse_simple_2.is_mapping_done)
form_data = {
"form-TOTAL_FORMS": 3,
"form-INITIAL_FORMS": 3,
"form-MIN_NUM_FORMS": 0,
"form-MAX_NUM_FORMS": 1000,
"form-0-raw_response": 0.0,
"form-0-mapping": self.no_lysis.pk,
"form-1-raw_response": 1.0,
"form-1-mapping": self.weak.pk,
"form-2-raw_response": 2.0,
"form-2-mapping": self.lysis.pk,
}
self.client.post(url, form_data)
self.client.logout()
self.assertTrue(self.three_reponse_simple_2.is_mapping_done)
################################################################################
self.four_reponse_simple = models.DataSource.objects.create(
owner=self.user,
public=True,
name="four_reponse_simple mapped",
raw_name="ee",
kind="FILE",
)
business_process.import_file(
data_source=self.four_reponse_simple,
file=os.path.join(self.test_data, "four_reponse_simple.xlsx"),
)
url = reverse('viralhostrangedb:data-source-mapping-label-edit', args=[self.four_reponse_simple.pk])
self.client.force_login(self.user)
self.assertFalse(self.four_reponse_simple.is_mapping_done)
form_data = {
"form-TOTAL_FORMS": 4,
"form-INITIAL_FORMS": 4,
"form-MIN_NUM_FORMS": 0,
"form-MAX_NUM_FORMS": 1000,
"form-0-raw_response": 0.0,
"form-0-mapping": self.no_lysis.pk,
"form-1-raw_response": 1.0,
"form-1-mapping": self.weak.pk,
"form-2-raw_response": 3.2,
"form-2-mapping": self.lysis.pk,
"form-3-raw_response": 4.0,
"form-3-mapping": self.lysis.pk,
}
self.client.post(url, form_data)
self.client.logout()
self.assertTrue(self.four_reponse_simple.is_mapping_done)
################################################################################
self.four_reponse_simple_not_mapped = models.DataSource.objects.create(
owner=self.user,
public=True,
name="four_reponse_simple not mapped",
raw_name="ee",
kind="FILE",
)
business_process.import_file(
data_source=self.four_reponse_simple_not_mapped,
file=os.path.join(self.test_data, "four_reponse_simple.xlsx"),
)
self.assertFalse(self.four_reponse_simple_not_mapped.is_mapping_done)
################################################################################
self.private_data_source_of_toto_with_no_virus_or_host_shared = models.DataSource.objects.create(
owner=self.toto,
public=False,
name="private toto no_virus_or_host_shared",
raw_name="ee",
kind="FILE",
)
self.ds_no_shearing = self.private_data_source_of_toto_with_no_virus_or_host_shared
filename = os.path.join(self.test_data, "rxCx.xlsx")
business_process.import_file(
data_source=self.private_data_source_of_toto_with_no_virus_or_host_shared,
file=filename,
)
self.C1 = models.Host.objects.get(name="C1", identifier="")
self.C2 = models.Host.objects.get(name="C2", identifier="")
self.C3 = models.Host.objects.get(name="C3", identifier="")
self.C4 = models.Host.objects.get(name="C4", identifier="")
self.Cx = models.Host.objects.get(name="Cx", identifier="")
self.r1 = models.Virus.objects.get(name="r1", identifier="")
self.r2 = models.Virus.objects.get(name="r2", identifier="")
self.r3 = models.Virus.objects.get(name="r3", identifier="")
self.rx = models.Virus.objects.get(name="rx", identifier="")
self.client.logout()
class HomePageTestCase(ViewTestCase):
def test_works(self):
self.assertTrue(self.client.login(username="user", email="a@a.a", password=self.user_pwd))
response = self.client.get(reverse('viralhostrangedb:home'))
self.assertEqual(response.status_code, 200)
class FileImportTestCase(ViewTestCase):
def test_works(self):
url = reverse('viralhostrangedb:file-import-view')
response = self.client.get(url)
os.path.isfile(os.path.join(self.test_data, "empty.xlsx.json.not_needed")) or self.write_in_tmp_file(response)
self.assertRedirects(
response,
expected_url=reverse('basetheme_bootstrap:login') + "?next=" + url
)
self.assertTrue(self.client.login(username="user", email="a@a.a", password=self.user_pwd))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
def test_import_work(self):
url = reverse('viralhostrangedb:file-import-view')
self.client.force_login(self.toto)
f = open(os.path.join(self.test_data, "three_reponse_simple.xlsx"), "rb")
form_data = dict(
name='imported',
description="Nop",
public=True,
file=SimpleUploadedFile(f.name, f.read(), content_type="application/vnd.ms-excel"),
)
response = self.client.post(url, form_data, follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(self.toto.datasource_set.filter(name="imported").count(), 1)
self.assertEqual(self.toto.datasource_set.get(name="imported").responseindatasource.count(), 12)
# self.assertNotEqual(self.public_big_data_source_of_user_mapped.responseindatasource.count(), 0)
# self.assertTrue(self.public_big_data_source_of_user_mapped.is_mapping_done)
def test_import_skip_empty_cell(self):
url = reverse('viralhostrangedb:file-import-view')
self.client.force_login(self.toto)
f = open(os.path.join(self.test_data, "empty_row.xlsx"), "rb")
form_data = dict(
name='imported',
description="Nop",
public=True,
file=SimpleUploadedFile(f.name, f.read(), content_type="application/vnd.ms-excel"),
)
response = self.client.post(url, form_data, follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(self.toto.datasource_set.filter(name="imported").count(), 1)
self.assertEqual(self.toto.datasource_set.get(name="imported").responseindatasource.count(), 8)
self.assertTrue(models.Virus.objects.filter(name="r2", data_source__name="imported").exists())
self.assertFalse(models.Virus.objects.filter(name="r3", data_source__name="imported").exists())
f = open(os.path.join(self.test_data, "empty_col.xlsx"), "rb")
form_data = dict(
name='imported2',
description="Nop",
public=True,
file=SimpleUploadedFile(f.name, f.read(), content_type="application/vnd.ms-excel"),
)
self.client.post(url, form_data, follow=True)
self.assertEqual(self.toto.datasource_set.get(name="imported2").responseindatasource.count(), 9)
self.assertTrue(models.Host.objects.filter(name="C2", data_source__name="imported2").exists())
self.assertFalse(models.Host.objects.filter(name="C3", data_source__name="imported2").exists())
f = open(os.path.join(self.test_data, "empty_cell_random.xlsx"), "rb")
form_data = dict(
name='imported3',
description="Nop",
public=True,
file=SimpleUploadedFile(f.name, f.read(), content_type="application/vnd.ms-excel"),
)
self.client.post(url, form_data, follow=True)
self.assertEqual(self.toto.datasource_set.get(name="imported3").responseindatasource.count(), 7)
def test_works_messages_raised(self):
url = reverse('viralhostrangedb:file-import-view')
self.client.force_login(self.toto)
f = open(os.path.join(self.test_data, "trigger_importation_error_message.xlsx"), "rb")
form_data = dict(
name='imported',
description="Nop",
public=True,
file=SimpleUploadedFile(f.name, f.read(), content_type="application/vnd.ms-excel"),
)
response = self.client.post(url, form_data, follow=True)
self.assertEqual(response.status_code, 200)
messages = list(response.context['messages'])
self.assertEqual(len(messages), 3)
error_codes = set()
for m in messages:
parts = str(m).split("]")
if len(parts) > 1:
error_codes.add(parts[0][1:])
self.assertSetEqual(error_codes, {"ImportErr1", "ImportErr2"})
def test_works_messages_raised_with_too_long_virus(self):
url = reverse('viralhostrangedb:file-import-view')
self.client.force_login(self.toto)
f = open(os.path.join(self.test_data, "too_long_virus_name.xlsx"), "rb")
form_data = dict(
name='imported',
description="Nop",
public=True,
file=SimpleUploadedFile(f.name, f.read(), content_type="application/vnd.ms-excel"),
)
response = self.client.post(url, form_data, follow=True)
self.assertEqual(response.status_code, 200)
messages = list(response.context['messages'])
error_codes = set()
for m in messages:
parts = str(m).split("]")
if len(parts) > 1:
error_codes.add(parts[0][1:])
self.assertSetEqual(error_codes, {"ImportErr3"})
def test_works_messages_raised_with_too_long_host(self):
url = reverse('viralhostrangedb:file-import-view')
self.client.force_login(self.toto)
f = open(os.path.join(self.test_data, "too_long_host_name.xlsx"), "rb")
form_data = dict(
name='imported',
description="Nop",
public=True,
file=SimpleUploadedFile(f.name, f.read(), content_type="application/vnd.ms-excel"),
)
response = self.client.post(url, form_data, follow=True)
self.assertEqual(response.status_code, 200)
messages = list(response.context['messages'])
error_codes = set()
for m in messages:
parts = str(m).split("]")
if len(parts) > 1:
error_codes.add(parts[0][1:])
self.assertSetEqual(error_codes, {"ImportErr1"})
class DataSourceMappingPendingListViewTestCase(ViewTestCase):
def test_works(self):
url = reverse('viralhostrangedb:data-source-pending-mapping-list')
response = self.client.get(url)
self.assertRedirects(
response,
expected_url=reverse('basetheme_bootstrap:login') + "?next=" + url
)
self.assertTrue(self.client.login(username="user", email="a@a.a", password=self.user_pwd))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
class BrowseTestCase(ViewTestCase):
def test_works(self):
url = reverse('viralhostrangedb:browse')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertTrue(self.client.login(username="user", email="a@a.a", password=self.user_pwd))
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
class DataSourceListViewTestCase(ViewTestCase):
def test_works(self):
url = reverse('viralhostrangedb:data-source-list')
self.client.logout()
self.assertEqual(self.client.get(url).status_code, 200)
self.client.force_login(self.toto)
self.assertEqual(self.client.get(url).status_code, 200)
def test_private_selectively_shawn(self):
url = reverse('viralhostrangedb:data-source-list')
self.client.logout()
response = self.client.get(url)
link = self.private_data_source_of_toto.get_absolute_url()
self.assertFalse(link in str(response.content))
link = self.private_data_source_of_user.get_absolute_url()
self.assertFalse(link in str(response.content))
link = self.public_data_source_of_user.get_absolute_url()
self.assertTrue(link in str(response.content))