Skip to content

Commit

Permalink
add cytof tests (#1980), add missing init files (#2014)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Sep 26, 2024
1 parent 9f87f1c commit 7591aaa
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 33 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Added
- **Samplesheets**
- REST API list view pagination (#1994)
- ``notify_email_irods_request`` user app setting (#1939)
- Assay app unit tests (#1980)
- Missing assay plugin ``__init__.py`` files (#2014)

Changed
-------
Expand Down
Empty file.
Empty file.
145 changes: 145 additions & 0 deletions samplesheets/assayapps/cytof/tests/test_plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
"""Plugin tests for the the cytof assay plugin"""

import os

from copy import deepcopy

from django.conf import settings

from samplesheets.models import Investigation
from samplesheets.rendering import SampleSheetTableBuilder, SIMPLE_LINK_TEMPLATE
from samplesheets.plugins import SampleSheetAssayPluginPoint, get_backend_api
from samplesheets.tests.test_views import (
SheetTemplateCreateMixin,
SamplesheetsViewTestBase,
)
from samplesheets.views import CUBI_TPL_DICT, MISC_FILES_COLL


table_builder = SampleSheetTableBuilder()


# Local constants
PLUGIN_NAME = 'samplesheets_assay_cytof'
ASSAY_NAME = 'testassayname'
PANEL_NAME = 'testpanel'
REPORT_NAME = 'report.txt'
FILE_NAME = 'file1.txt'
FILE_NAME2 = 'file2.txt'
FILE_NAME3 = 'file3.txt'


class TestCytofPlugin(SheetTemplateCreateMixin, SamplesheetsViewTestBase):
"""Tests for cytof assay plugin"""

def setUp(self):
super().setUp()
self.make_sheets_from_cubi_tpl(CUBI_TPL_DICT['mass_cytometry'])
self.investigation = Investigation.objects.get(project=self.project)
self.study = self.investigation.studies.first()
self.assay = self.study.assays.first()
self.study_tables = table_builder.build_study_tables(self.study)
self.assay_table = self.study_tables['assays'][
str(self.assay.sodar_uuid)
]
self.irods_backend = get_backend_api('omics_irods')
self.assay_path = self.irods_backend.get_path(self.assay)
self.base_url = settings.IRODS_WEBDAV_URL + self.assay_path
self.plugin = SampleSheetAssayPluginPoint.get_plugin(PLUGIN_NAME)

def test_get_row_path_filled(self):
"""Test get_row_path() with filled out assay name"""
self.assay_table['table_data'][0][20]['value'] = ASSAY_NAME
row_path = self.plugin.get_row_path(
self.assay_table['table_data'][0],
self.assay_table,
self.assay,
self.assay_path,
)
expected = os.path.join(self.assay_path, ASSAY_NAME)
self.assertEqual(row_path, expected)

def test_get_row_path_default(self):
"""Test get_row_path() with default template values"""
# NOTE: Assay name is not filled by default, so we return None
row_path = self.plugin.get_row_path(
self.assay_table['table_data'][0],
self.assay_table,
self.assay,
self.assay_path,
)
self.assertEqual(row_path, None)

def test_update_row_filled_panel(self):
"""Test update_row() with filled panel name"""
# Update unset values
self.assay_table['table_data'][0][15]['value'] = PANEL_NAME
self.assay_table['table_data'][0][20]['value'] = ASSAY_NAME
self.assay_table['table_data'][0][26]['value'] = FILE_NAME
self.assay_table['table_data'][0][31]['value'] = FILE_NAME2
self.assay_table['table_data'][0][36]['value'] = FILE_NAME3
# Set expected data
row_ex = deepcopy(self.assay_table['table_data'][0])
row_ex[15]['value'] = SIMPLE_LINK_TEMPLATE.format(
label=PANEL_NAME,
url=os.path.join(self.base_url, MISC_FILES_COLL, PANEL_NAME),
)
row_ex[26]['link'] = os.path.join(self.base_url, ASSAY_NAME, FILE_NAME)
row_ex[31]['link'] = os.path.join(self.base_url, ASSAY_NAME, FILE_NAME2)
row_ex[36]['link'] = os.path.join(self.base_url, ASSAY_NAME, FILE_NAME3)
row = self.plugin.update_row(
self.assay_table['table_data'][0], self.assay_table, self.assay, 0
)
self.assertEqual(row, row_ex)

def test_update_row_filled_report(self):
"""Test update_row() with filled report file"""
self.assay_table['table_data'][0][16]['value'] = REPORT_NAME
self.assay_table['table_data'][0][20]['value'] = ASSAY_NAME
self.assay_table['table_data'][0][26]['value'] = FILE_NAME
self.assay_table['table_data'][0][31]['value'] = FILE_NAME2
self.assay_table['table_data'][0][36]['value'] = FILE_NAME3
row_ex = deepcopy(self.assay_table['table_data'][0])
row_ex[16]['value'] = SIMPLE_LINK_TEMPLATE.format(
label=REPORT_NAME,
url=os.path.join(self.base_url, ASSAY_NAME, REPORT_NAME),
)
row_ex[26]['link'] = os.path.join(self.base_url, ASSAY_NAME, FILE_NAME)
row_ex[31]['link'] = os.path.join(self.base_url, ASSAY_NAME, FILE_NAME2)
row_ex[36]['link'] = os.path.join(self.base_url, ASSAY_NAME, FILE_NAME3)
row = self.plugin.update_row(
self.assay_table['table_data'][0], self.assay_table, self.assay, 0
)
self.assertEqual(row, row_ex)

def test_update_row_filled_barcode(self):
"""Test update_row() with filled barcode key"""
# Rename header
self.assay_table['field_header'][15]['value'] = 'Barcode Key'
self.assay_table['table_data'][0][15]['value'] = PANEL_NAME
self.assay_table['table_data'][0][20]['value'] = ASSAY_NAME
self.assay_table['table_data'][0][26]['value'] = FILE_NAME
self.assay_table['table_data'][0][31]['value'] = FILE_NAME2
self.assay_table['table_data'][0][36]['value'] = FILE_NAME3
row_ex = deepcopy(self.assay_table['table_data'][0])
row_ex[15]['value'] = SIMPLE_LINK_TEMPLATE.format(
label=PANEL_NAME,
url=os.path.join(self.base_url, MISC_FILES_COLL, PANEL_NAME),
)
row_ex[26]['link'] = os.path.join(self.base_url, ASSAY_NAME, FILE_NAME)
row_ex[31]['link'] = os.path.join(self.base_url, ASSAY_NAME, FILE_NAME2)
row_ex[36]['link'] = os.path.join(self.base_url, ASSAY_NAME, FILE_NAME3)
row = self.plugin.update_row(
self.assay_table['table_data'][0], self.assay_table, self.assay, 0
)
self.assertEqual(row, row_ex)

# TODO: Test with empty file names after fixing #2017

def test_update_row_default(self):
"""Test update_row() with default template values"""
row_ex = deepcopy(self.assay_table['table_data'][0])
row = self.plugin.update_row(
self.assay_table['table_data'][0], self.assay_table, self.assay, 0
)
self.assertEqual(row, row_ex)
Empty file.
Empty file.
Empty file.
Empty file.
89 changes: 56 additions & 33 deletions samplesheets/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,54 @@
# TODO: Add testing for study table cache updates


# Base Classes and Mixins ------------------------------------------------------


class SheetTemplateCreateMixin:
"""Sheet template creation helpers"""

def get_tpl_post_data(self, sheet_tpl):
"""
Return POST data for creating sheet from template.
:param sheet_tpl: IsaTabTemplate object
:return: Dict
"""
ret = {TPL_DIR_FIELD: clean_sheet_dir_name(self.project.title)}
if not isinstance(sheet_tpl, IsaTabTemplate): # Custom template
tpl_config = json.loads(sheet_tpl.configuration)
else: # CUBI template
tpl_config = sheet_tpl.configuration
for k, v in tpl_config.items():
if isinstance(v, str):
if '{{' in v or '{%' in v:
continue
ret[k] = v
elif isinstance(v, list):
ret[k] = v[0]
elif isinstance(v, dict):
ret[k] = json.dumps(v)
return ret

def make_sheets_from_cubi_tpl(self, sheet_tpl):
"""
Create investigation from CUBI templates by posting to the template
create view.
:param sheet_tpl: IsaTabTemplate object
"""
url = reverse(
'samplesheets:template_create',
kwargs={'project': self.project.sodar_uuid},
)
with self.login(self.user):
response = self.client.post(
url + '?sheet_tpl=' + sheet_tpl.name,
data=self.get_tpl_post_data(sheet_tpl),
)
self.assertEqual(response.status_code, 302, msg=sheet_tpl.name)


class SamplesheetsViewTestBase(
ProjectMixin, RoleMixin, RoleAssignmentMixin, SampleSheetIOMixin, TestCase
):
Expand Down Expand Up @@ -184,6 +232,9 @@ def setUp(self):
)


# Test Cases -------------------------------------------------------------------


class TestProjectSheetsView(SamplesheetsViewTestBase):
"""Tests for the project sheets view"""

Expand Down Expand Up @@ -806,35 +857,13 @@ def test_get_custom_backend_disabled(self):


class TestSheetTemplateCreateView(
SheetTemplateCreateMixin,
CookiecutterISATemplateMixin,
CookiecutterISAFileMixin,
SamplesheetsViewTestBase,
):
"""Tests for SheetTemplateCreateView"""

def _get_post_data(self, sheet_tpl):
"""
Return POST data for creation from template
:param sheet_tpl: IsaTabTemplate object
:return: Dict
"""
ret = {TPL_DIR_FIELD: clean_sheet_dir_name(self.project.title)}
if not isinstance(sheet_tpl, IsaTabTemplate): # Custom template
tpl_config = json.loads(sheet_tpl.configuration)
else: # CUBI template
tpl_config = sheet_tpl.configuration
for k, v in tpl_config.items():
if isinstance(v, str):
if '{{' in v or '{%' in v:
continue
ret[k] = v
elif isinstance(v, list):
ret[k] = v[0]
elif isinstance(v, dict):
ret[k] = json.dumps(v)
return ret

def _make_custom_template(self):
"""Make custom template with data"""
with open(TEMPLATE_JSON_PATH, 'rb') as f:
Expand Down Expand Up @@ -930,15 +959,9 @@ def test_post_batch_cubi(self):
"""Test POST with supported templates and default values"""
for t in ISA_TEMPLATES:
self.assertIsNone(self.project.investigations.first())
post_data = self._get_post_data(t)
with self.login(self.user):
response = self.client.post(
self.url + '?sheet_tpl=' + t.name,
data=post_data,
)
self.make_sheets_from_cubi_tpl(t)
isa_tab = ISATab.objects.first()
self.assertEqual(isa_tab.tags, ['CREATE'])
self.assertEqual(response.status_code, 302, msg=t.name)
self.assertIsNotNone(
self.project.investigations.first(), msg=t.name
)
Expand All @@ -948,7 +971,7 @@ def test_post_batch_file_name_slash(self):
"""Test POST with slashes in values used for file names"""
for t in ISA_TEMPLATES:
self.assertIsNone(self.project.investigations.first())
post_data = self._get_post_data(t)
post_data = self.get_tpl_post_data(t)
for k in TPL_FILE_NAME_FIELDS:
if k in post_data:
post_data[k] += '/test'
Expand All @@ -967,7 +990,7 @@ def test_post_multiple(self):
"""Test POST with multiple requests (should fail)"""
tpl = ISA_TEMPLATES[0]
url = self.url + '?sheet_tpl=' + tpl.name
post_data = self._get_post_data(tpl)
post_data = self.get_tpl_post_data(tpl)
with self.login(self.user):
response = self.client.post(url, data=post_data)
self.assertEqual(response.status_code, 302)
Expand All @@ -980,7 +1003,7 @@ def test_post_custom(self):
"""Test POST with custom template"""
self._make_custom_template()
self.assertIsNone(self.project.investigations.first())
post_data = self._get_post_data(self.template)
post_data = self.get_tpl_post_data(self.template)
with self.login(self.user):
response = self.client.post(
self.url + '?sheet_tpl=' + self.template.name,
Expand Down

0 comments on commit 7591aaa

Please sign in to comment.