Skip to content

Commit

Permalink
test: speed up tests using fixture
Browse files Browse the repository at this point in the history
Subset of HTS007 with single cell line for some tests, to increase
speed.
Also, replace use of pkg_resources with importlib.
  • Loading branch information
alubbock committed Sep 4, 2024
1 parent 63b5192 commit ab30c35
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 57 deletions.
Binary file not shown.
9 changes: 4 additions & 5 deletions thunorweb/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import pkg_resources
import importlib.resources
import os
import thunor


def get_thunor_test_file(filename):
try:
return pkg_resources.resource_stream('thunor', filename)
return importlib.resources.files('thunor').joinpath(filename)
except OSError:
# Provide a method to load from filesystem within Docker container
fn = os.path.join('thunor/thunor', filename)
fn = os.path.join('thunorcore/thunor', filename)
if os.path.exists(fn) and os.path.isfile(fn):
return open(fn, 'rb')
return fn

raise
9 changes: 3 additions & 6 deletions thunorweb/tests/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ def setUpTestData(cls):
dataset_id = resp_json['id']
cls.d = HTSDataset.objects.get(pk=dataset_id)

hts007 = get_thunor_test_file('testdata/hts007.h5')
try:
with open(get_thunor_test_file('testdata/hts007.h5'), 'rb') as hts007:
response = c.post(reverse('thunorweb:ajax_upload_platefiles'),
{'file_field[]': hts007, 'dataset_id':
cls.d.id})
finally:
hts007.close()
{'file_field[]': hts007,
'dataset_id': cls.d.id})

assert response.status_code == HTTP_OK
assert cls.d.plate_set.count() == 16
Expand Down
24 changes: 6 additions & 18 deletions thunorweb/tests/test_plate_mapper.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from django.test import TestCase
from django.core.files import File
from thunorweb.plate_parsers import PlateFileParser
from thunorweb.models import HTSDataset, CellLine, Drug
from thunorweb.tests import get_thunor_test_file
from django.contrib.auth import get_user_model
from django.urls import reverse
import json
Expand All @@ -12,24 +9,15 @@


class TestPlateMapper(TestCase):
fixtures = ['testing-hts007-hcc1143.json']

@classmethod
def setUpTestData(cls):
UserModel = get_user_model()
cls.user = UserModel.objects.create_user(
email='[email protected]', password='test')

cls.d = HTSDataset.objects.create(owner=cls.user, name='test')
hts007 = get_thunor_test_file('testdata/hts007.h5')
try:
f = File(hts007, name='hts007.h5')
pfp = PlateFileParser(f, dataset=cls.d)
results = pfp.parse_all()
finally:
hts007.close()

assert len(results) == 1
assert results[0]['success']
assert results[0]['file_format'] == 'HDF5'
cls.user = UserModel.objects.get(
email='[email protected]')

cls.d = HTSDataset.objects.get(owner=cls.user, name='test')

def test_load_save_plate(self):
plate_id = self.d.plate_set.first().id
Expand Down
14 changes: 4 additions & 10 deletions thunorweb/tests/test_plate_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@ def setUpTestData(cls):
# HDF parsing is tested elsewhere

def test_parse_vanderbilt_csv(self):
hts007 = get_thunor_test_file('testdata/hts007.h5')

with io.StringIO() as csv_buffer:
# convert HDF to CSV in memory
try:
with open(get_thunor_test_file('testdata/hts007.h5'), 'rb') as hts007:
thunor.io.write_vanderbilt_hts(
df_data=thunor.io.read_hdf(hts007),
filename=csv_buffer,
sep=','
)
finally:
hts007.close()

csv_buffer.seek(0)

Expand All @@ -41,12 +37,10 @@ def test_parse_vanderbilt_csv(self):
assert results[0]['file_format'] == 'Vanderbilt HTS Core'

def test_parse_incucyte(self):
testfile = get_thunor_test_file('testdata/test_incucyte_minimal.txt')

try:
with open(get_thunor_test_file('testdata/test_incucyte_minimal.txt'),
'rb') as testfile:
filedata = io.BytesIO(testfile.read())
finally:
testfile.close()

pfp = PlateFileParser(File(filedata, name='test.txt'),
dataset=self.d)
results = pfp.parse_all()
Expand Down
25 changes: 7 additions & 18 deletions thunorweb/tests/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,20 @@


class TestPlots(TestCase):
fixtures = ['testing-hts007-hcc1143.json']

@classmethod
def setUpTestData(cls):
UserModel = get_user_model()
cls.user = UserModel.objects.create_user(
email='[email protected]', password='test')
cls.user = UserModel.objects.get(
email='[email protected]')
cls.other_user = UserModel.objects.create_user(
email='[email protected]', password='test')

c = Client()
c.force_login(cls.user)

resp = c.post(reverse('thunorweb:ajax_create_dataset'),
{'name': 'test'})
resp_json = json.loads(resp.content)
dataset_id = resp_json['id']
cls.d = HTSDataset.objects.get(pk=dataset_id)

hts007 = get_thunor_test_file('testdata/hts007.h5')
try:
response = c.post(reverse('thunorweb:ajax_upload_platefiles'),
{'file_field[]': hts007, 'dataset_id':
cls.d.id})
finally:
hts007.close()

assert response.status_code == HTTP_OK
cls.d = HTSDataset.objects.get()

# Create some cell line and drug tags
entities_per_tag = 3
Expand All @@ -58,7 +47,7 @@ def setUpTestData(cls):
for tag_num in range(num_tags):
idx = range(tag_num * entities_per_tag,
tag_num * (entities_per_tag + 1))
cltags[tag_num].cell_lines.set([cell_lines[i] for i in idx])
cltags[tag_num].cell_lines.set([cell_lines[0]])
drtags[tag_num].drugs.set([drugs[i] for i in idx])

# Get the dataset groupings
Expand Down

0 comments on commit ab30c35

Please sign in to comment.