Skip to content

Commit

Permalink
update non-taskflow samplesheets tests (#1617)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed May 15, 2023
1 parent 3e383e8 commit 884ed2a
Show file tree
Hide file tree
Showing 19 changed files with 616 additions and 243 deletions.
30 changes: 15 additions & 15 deletions samplesheets/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
from test_plus.test import TestCase

# Projectroles dependency
from projectroles.models import Role, SODAR_CONSTANTS
from projectroles.models import SODAR_CONSTANTS
from projectroles.plugins import get_backend_api
from projectroles.tests.test_models import ProjectMixin, RoleAssignmentMixin
from projectroles.tests.test_models import (
ProjectMixin,
RoleMixin,
RoleAssignmentMixin,
)

# Sodarcache dependency
from sodarcache.models import JSONCacheItem
Expand All @@ -33,20 +37,19 @@


class TestSyncnamesCommand(
ProjectMixin, RoleAssignmentMixin, SampleSheetIOMixin, TestCase
ProjectMixin, RoleMixin, RoleAssignmentMixin, SampleSheetIOMixin, TestCase
):
"""Tests for the syncnames command"""

def setUp(self):
# Init roles
self.init_roles()
# Make owner user
self.user_owner = self.make_user('owner')
# Init project, role and assignment
# Init project and assignment
self.project = self.make_project(
'TestProject', SODAR_CONSTANTS['PROJECT_TYPE_PROJECT'], None
)
self.role_owner = Role.objects.get_or_create(
name=SODAR_CONSTANTS['PROJECT_ROLE_OWNER']
)[0]
self.owner_as = self.make_assignment(
self.project, self.user_owner, self.role_owner
)
Expand All @@ -68,18 +71,16 @@ def test_command(self):


class TestSyncstudytablesCommand(
ProjectMixin, RoleAssignmentMixin, SampleSheetIOMixin, TestCase
ProjectMixin, RoleMixin, RoleAssignmentMixin, SampleSheetIOMixin, TestCase
):
"""Tests for the syncstudytables command"""

def setUp(self):
# Init user and role
# Init roles
self.init_roles()
# Init user
self.user_owner = self.make_user('owner')
self.role_owner = Role.objects.get_or_create(
name=SODAR_CONSTANTS['PROJECT_ROLE_OWNER']
)[0]

# Init project
# Init project and assignment
self.project = self.make_project(
'TestProject', SODAR_CONSTANTS['PROJECT_TYPE_PROJECT'], None
)
Expand Down Expand Up @@ -132,7 +133,6 @@ def test_sync_all_existing(self):
self.cache_backend.set_cache_item(
APP_NAME, self.cache_name2, {}, project=self.project2
)

self.assertEqual(JSONCacheItem.objects.count(), 2)
call_command('syncstudytables')
self.assertEqual(JSONCacheItem.objects.count(), 2)
Expand Down
41 changes: 10 additions & 31 deletions samplesheets/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

# Projectroles dependency
from projectroles.models import Role, SODAR_CONSTANTS
from projectroles.tests.test_models import ProjectMixin, RoleAssignmentMixin
from projectroles.tests.test_models import (
ProjectMixin,
RoleMixin,
RoleAssignmentMixin,
)

from samplesheets.models import Investigation, ISATab
from samplesheets.io import SampleSheetIO
Expand Down Expand Up @@ -71,7 +75,6 @@ def read_isa(cls, path, project):

# Read investigation
input_file = sheet_io.get_import_file(zf, inv_file_path)

with warnings.catch_warnings(record=True):
isa_inv = InvestigationReader.from_stream(
input_file=input_file
Expand All @@ -82,11 +85,8 @@ def read_isa(cls, path, project):
isa_assays = {}
study_count = 0
assay_count = 0

# Read studies
for study_info in isa_inv.studies:
study_id = 'p{}-s{}'.format(project.pk, study_count)

with warnings.catch_warnings(record=True):
isa_studies[
str(study_info.info.path)
Expand All @@ -97,10 +97,8 @@ def read_isa(cls, path, project):
),
study_id=study_id,
).read()

# Read studies for assay
# Read assays for study
assay_paths = sorted([a.path for a in study_info.assays])

for assay_path in assay_paths:
isa_assay = next(
(
Expand All @@ -111,7 +109,6 @@ def read_isa(cls, path, project):
None,
)
assay_id = 'a{}'.format(assay_count)

with warnings.catch_warnings(record=True):
isa_assays[str(assay_path)] = AssayReader.from_stream(
study_id=study_id,
Expand All @@ -120,11 +117,8 @@ def read_isa(cls, path, project):
zf, sheet_io._get_zip_path(inv_dir, isa_assay.path)
),
).read()

assay_count += 1

study_count += 1

return isa_inv, isa_studies, isa_assays

@classmethod
Expand All @@ -148,13 +142,14 @@ def fail_isa(self, zip_name, ex):


class TestSampleSheetIOBase(
ProjectMixin, RoleAssignmentMixin, SampleSheetIOMixin, TestCase
ProjectMixin, RoleMixin, RoleAssignmentMixin, SampleSheetIOMixin, TestCase
):
def setUp(self):
# Init roles
self.init_roles()
# Make owner user
self.user_owner = self.make_user('owner')

# Init project, role and assignment
# Init project and assignment
self.project = self.make_project(
'TestProject', SODAR_CONSTANTS['PROJECT_TYPE_PROJECT'], None
)
Expand Down Expand Up @@ -190,17 +185,14 @@ def test_isa_import_batch(self):

for zip_name, zip_file in self.get_isatab_files().items():
msg = 'file={}'.format(zip_name)

try:
investigation = self.import_isa_from_file(
zip_file.path, self.project
)
except Exception as ex:
return self.fail_isa(zip_name, ex)

self.assertEqual(Investigation.objects.count(), 1, msg=msg)
self.assertEqual(ISATab.objects.count(), 1, msg=msg)

investigation.delete()
ISATab.objects.first().delete()
self.assertEqual(Investigation.objects.count(), 0, msg=msg)
Expand All @@ -214,14 +206,12 @@ def test_isa_export_batch(self):
investigation = self.import_isa_from_file(
zip_file.path, self.project
)

try:
export_data = self._get_flat_export_data(
sheet_io.export_isa(investigation)
)
except Exception as ex:
return self.fail_isa(zip_name, ex)

zf = ZipFile(zip_file.path, 'r')

for isa_path in [n for n in zf.namelist() if not n.endswith('/')]:
Expand Down Expand Up @@ -252,7 +242,6 @@ def test_isa_export_batch(self):
# Compare row and column lengths
self.assertEqual(import_rows, export_rows, msg=msg)
self.assertEqual(import_cols, export_cols, msg=msg)

# Compare headers
for i in range(len(import_headers)):
self.assertEqual(
Expand All @@ -268,7 +257,6 @@ def _get_row(row):
and not row[len(row) - 1]
):
return sorted(row[: len(row) - 1])

return sorted(row)

ib.seek(0)
Expand All @@ -283,10 +271,8 @@ def _get_row(row):

for i in range(len(import_cmp)):
self.assertIn(import_cmp[i], export_cmp, msg=msg)

for i in range(len(export_cmp)):
self.assertIn(export_cmp[i], import_cmp, msg=msg)

investigation.delete()

def test_isa_saving_batch(self):
Expand All @@ -298,15 +284,13 @@ def test_isa_saving_batch(self):
)
except Exception as ex:
return self.fail_isa(zip_name, ex)

saved_isatab = ISATab.objects.first()
zf = ZipFile(zip_file.path)

for f in [f for f in zf.filelist if f.file_size > 0]:
msg = 'zip={}, file={}'.format(zip_name, f.filename)
zip_data = zf.open(f.filename).read().decode('utf-8')
file_name = f.filename.split('/')[-1]

if file_name.startswith('i_'):
self.assertEqual(
saved_isatab.data['investigation']['tsv'], zip_data, msg
Expand All @@ -323,7 +307,6 @@ def test_isa_saving_batch(self):
zip_data,
msg,
)

investigation.delete()
ISATab.objects.first().delete()

Expand All @@ -341,7 +324,6 @@ def setUp(self):

def test_import_ref_val(self):
"""Test _import_ref_val()"""

# Ontology value
in_data = (
self.isa_studies['s_BII-S-1.txt']
Expand Down Expand Up @@ -371,7 +353,6 @@ def test_import_ref_val(self):

def test_import_multi_val(self):
"""Test _import_multi_val()"""

# List with a single ontology value (should return just a single dict)
in_data = (
self.isa_studies['s_BII-S-1.txt']
Expand Down Expand Up @@ -479,10 +460,8 @@ def setUp(self):

def test_export_value(self):
"""Test _export_value()"""

# TODO: String value (see issue #434)
# TODO: List value (see issue #434)

# Dict value (ontology term reference)
in_data = self.investigation.studies.first().study_design[0]['type']
out_data = self.sheet_io._export_val(in_data)
Expand Down
Loading

0 comments on commit 884ed2a

Please sign in to comment.