Skip to content

Commit

Permalink
update api view tests (#1509)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Dec 12, 2022
1 parent c11dce7 commit fa1cac7
Showing 1 changed file with 95 additions and 12 deletions.
107 changes: 95 additions & 12 deletions samplesheets/tests/test_views_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@
# Projectroles dependency
from projectroles.app_settings import AppSettingAPI
from projectroles.models import SODAR_CONSTANTS
from projectroles.plugins import get_backend_api
from projectroles.tests.test_models import RemoteSiteMixin, RemoteProjectMixin
from projectroles.tests.test_views_api import TestAPIViewsBase

# Sodarcache dependency
from sodarcache.models import JSONCacheItem

# Landingzones dependency
from landingzones.models import LandingZone
from landingzones.tests.test_models import LandingZoneMixin

from samplesheets.io import SampleSheetIO
from samplesheets.models import Investigation, Assay, GenericMaterial, ISATab
from samplesheets.rendering import SampleSheetTableBuilder
from samplesheets.rendering import (
SampleSheetTableBuilder,
STUDY_TABLE_CACHE_ITEM,
)
from samplesheets.sheet_config import SheetConfigAPI
from samplesheets.tests.test_io import (
SampleSheetIOMixin,
Expand Down Expand Up @@ -48,6 +55,8 @@
SHEET_TSV_DIR = SHEET_DIR + 'i_small2/'
SHEET_PATH = SHEET_DIR + 'i_small2.zip'
SHEET_PATH_EDITED = SHEET_DIR + 'i_small2_edited.zip'
SHEET_NAME_ALT = 'i_small.zip'
SHEET_PATH_ALT = SHEET_DIR + SHEET_NAME_ALT
SHEET_PATH_ALT = SHEET_DIR + 'i_small2_alt.zip'
SHEET_PATH_NO_PLUGIN_ASSAY = SHEET_DIR_SPECIAL + 'i_small_assay_no_plugin.zip'
IRODS_FILE_MD5 = '0b26e313ed4a7ca6904b0e9369e5b957'
Expand Down Expand Up @@ -124,9 +133,10 @@ class TestSheetImportAPIView(

def setUp(self):
super().setUp()
self.cache_backend = get_backend_api('sodar_cache')

def test_post_zip(self):
"""Test SampleSheetImportAPIView post() with a zip archive"""
def test_import_zip(self):
"""Test importing sheets as zip archive"""
self.assertEqual(
Investigation.objects.filter(project=self.project).count(), 0
)
Expand All @@ -148,8 +158,8 @@ def test_post_zip(self):
)
self.assertEqual(ISATab.objects.filter(project=self.project).count(), 1)

def test_post_tsv(self):
"""Test SampleSheetImportAPIView post() with ISA-Tab tsv files"""
def test_import_tsv(self):
"""Test Test importing sheets as ISA-Tab tsv files"""
self.assertEqual(
Investigation.objects.filter(project=self.project).count(), 0
)
Expand Down Expand Up @@ -180,7 +190,7 @@ def test_post_tsv(self):
)
self.assertEqual(ISATab.objects.filter(project=self.project).count(), 1)

def test_post_replace(self):
def test_replace(self):
"""Test replacing sheets"""
investigation = self.import_isa_from_file(SHEET_PATH, self.project)
app_settings.set_app_setting(
Expand Down Expand Up @@ -216,7 +226,7 @@ def test_post_replace(self):
GenericMaterial.objects.filter(name='0816').first()
)

def test_post_replace_display_config_keep(self):
def test_replace_display_config_keep(self):
"""Test replacing sheets and ensure user display configs are kept"""
investigation = self.import_isa_from_file(SHEET_PATH, self.project)
inv_tables = table_builder.build_inv_tables(investigation)
Expand Down Expand Up @@ -268,7 +278,7 @@ def test_post_replace_display_config_keep(self):
display_config,
)

def test_post_replace_display_config_delete(self):
def test_replace_display_config_delete(self):
"""Test replacing sheets and ensure user display configs are deleted"""
investigation = self.import_isa_from_file(SHEET_PATH, self.project)
inv_tables = table_builder.build_inv_tables(investigation)
Expand Down Expand Up @@ -320,7 +330,7 @@ def test_post_replace_display_config_delete(self):
{},
)

def test_post_replace_alt_sheet(self):
def test_replace_alt_sheet(self):
"""Test replacing with an alternative sheet and irods_status=False"""
investigation = self.import_isa_from_file(SHEET_PATH, self.project)
app_settings.set_app_setting(
Expand Down Expand Up @@ -351,7 +361,7 @@ def test_post_replace_alt_sheet(self):
)
self.assertEqual(ISATab.objects.filter(project=self.project).count(), 2)

def test_post_replace_alt_sheet_irods(self):
def test_replace_alt_sheet_irods(self):
"""Test replacing with alternative sheet and irods (should fail)"""
investigation = self.import_isa_from_file(SHEET_PATH, self.project)
investigation.irods_status = True # fake irods status
Expand All @@ -378,7 +388,7 @@ def test_post_replace_alt_sheet_irods(self):
)
self.assertEqual(ISATab.objects.filter(project=self.project).count(), 1)

def test_post_replace_zone(self):
def test_replace_zone(self):
"""Test replacing sheets with exising landing zone"""
investigation = self.import_isa_from_file(SHEET_PATH, self.project)
investigation.irods_status = True
Expand Down Expand Up @@ -439,7 +449,80 @@ def test_post_replace_zone(self):
).first(),
)

def test_post_no_plugin_assay(self):
def test_replace_study_cache(self):
"""Test replacing sheets with existing study table cache"""
investigation = self.import_isa_from_file(SHEET_PATH, self.project)
conf_api.get_sheet_config(investigation)
study = investigation.studies.first()
study_uuid = str(study.sodar_uuid)

# Build study tables and cache item
study_tables = table_builder.build_study_tables(study)
cache_name = STUDY_TABLE_CACHE_ITEM.format(study=study_uuid)
self.cache_backend.set_cache_item(
APP_NAME, cache_name, study_tables, 'json', self.project
)
cache_args = [APP_NAME, cache_name, self.project]
cache_item = self.cache_backend.get_cache_item(*cache_args)
self.assertEqual(cache_item.data, study_tables)
self.assertEqual(JSONCacheItem.objects.count(), 1)

url = reverse(
'samplesheets:api_import',
kwargs={'project': self.project.sodar_uuid},
)
with open(SHEET_PATH_EDITED, 'rb') as file:
post_data = {'file': file}
response = self.request_knox(
url, method='POST', format='multipart', data=post_data
)

self.assertEqual(response.status_code, 200)
self.assertEqual(
Investigation.objects.filter(project=self.project).count(), 1
)
self.assertEqual(ISATab.objects.filter(project=self.project).count(), 2)
cache_item = self.cache_backend.get_cache_item(*cache_args)
self.assertEqual(cache_item.data, {})
self.assertEqual(JSONCacheItem.objects.count(), 1)

def test_replace_study_cache_new_sheet(self):
"""Test replacing with study table cache and different sheet"""
investigation = self.import_isa_from_file(SHEET_PATH, self.project)
conf_api.get_sheet_config(investigation)
study = investigation.studies.first()
study_uuid = str(study.sodar_uuid)

study_tables = table_builder.build_study_tables(study)
cache_name = STUDY_TABLE_CACHE_ITEM.format(study=study_uuid)
self.cache_backend.set_cache_item(
APP_NAME, cache_name, study_tables, 'json', self.project
)
cache_args = [APP_NAME, cache_name, self.project]
cache_item = self.cache_backend.get_cache_item(*cache_args)
self.assertEqual(cache_item.data, study_tables)
self.assertEqual(JSONCacheItem.objects.count(), 1)

url = reverse(
'samplesheets:api_import',
kwargs={'project': self.project.sodar_uuid},
)
with open(SHEET_PATH_ALT, 'rb') as file:
post_data = {'file': file}
response = self.request_knox(
url, method='POST', format='multipart', data=post_data
)

self.assertEqual(response.status_code, 200)
self.assertEqual(
Investigation.objects.filter(project=self.project).count(), 1
)
self.assertEqual(ISATab.objects.filter(project=self.project).count(), 2)
cache_item = self.cache_backend.get_cache_item(*cache_args)
self.assertIsNone(cache_item)
self.assertEqual(JSONCacheItem.objects.count(), 0)

def test_import_no_plugin_assay(self):
"""Test post() with an assay without plugin"""
self.assertEqual(
Investigation.objects.filter(project=self.project).count(), 0
Expand Down

0 comments on commit fa1cac7

Please sign in to comment.