def test_create_flowcell(self): """ Ensure create flowcell behaves correctly. """ self.client.login(email='*****@*****.**', password='******') library1 = create_library(get_random_name(), 4) library2 = create_library(get_random_name(), 4) sample1 = create_sample(get_random_name(), 4) sample2 = create_sample(get_random_name(), 4) pool1 = create_pool(self.user, multiplier=4) pool1.libraries.add(library1) pool1.samples.add(sample1) pool2 = create_pool(self.user, multiplier=8) pool2.libraries.add(library2) pool2.samples.add(sample2) sequencer = create_sequencer(get_random_name()) flowcell_id = get_random_name() lanes1 = [{ 'name': 'Lane {}'.format(i + 1), 'pool_id': pool1.pk, } for i in range(4)] lanes2 = [{ 'name': 'Lane {}'.format(i + 1), 'pool_id': pool2.pk, } for i in range(4, 8)] response = self.client.post(reverse('flowcells-list'), { 'data': json.dumps({ 'flowcell_id': flowcell_id, 'sequencer': sequencer.pk, 'lanes': lanes1 + lanes2, }) }) data = response.json() self.assertEqual(response.status_code, 201) self.assertTrue(data['success']) flowcells = Flowcell.objects.values_list('flowcell_id', flat=True) self.assertIn(flowcell_id, flowcells) updated_library1 = library1.__class__.objects.get(pk=library1.pk) updated_library2 = library2.__class__.objects.get(pk=library2.pk) updated_sample1 = sample1.__class__.objects.get(pk=sample1.pk) updated_sample2 = sample2.__class__.objects.get(pk=sample2.pk) self.assertEqual(updated_library1.status, 5) self.assertEqual(updated_library2.status, 4) self.assertEqual(updated_sample1.status, 5) self.assertEqual(updated_sample2.status, 4)
def setUp(self): user = self.create_user() self.create_user('*****@*****.**', 'test', False) # non-staff user # Submission completed self.library1 = create_library(self._get_random_name(), status=1) self.sample1 = create_sample(self._get_random_name(), status=1) self.sample2 = create_sample(self._get_random_name(), status=1) # Failed library self.library2 = create_library(self._get_random_name(), status=-1) self.request = Request(user=user) self.request.save() self.request.libraries.add(*[self.library1, self.library2]) self.request.samples.add(*[self.sample1, self.sample2])
def test_update_request(self): """ Ensure update request behaves correctly. """ request = create_request(self.user) new_description = get_random_name() library = create_library(get_random_name()) sample = create_sample(get_random_name()) request.libraries.add(library) self.assertNotIn(sample, request.samples.all()) response = self.client.post( f'/api/requests/{request.pk}/edit/', { 'data': json.dumps({ 'description': new_description, 'records': [{ 'pk': library.pk, 'record_type': 'Library', }, { 'pk': sample.pk, 'record_type': 'Sample', }], # 'files': [], }), }) self.assertEqual(response.status_code, 200) self.assertTrue(response.json()['success']) updated_request = Request.objects.get(pk=request.pk) self.assertEqual(updated_request.description, new_description) self.assertIn(sample, updated_request.samples.all())
def test_create_request(self): """ Ensure create request behaves correctly. """ library = create_library(get_random_name()) sample = create_sample(get_random_name()) Library = library.__class__ response = self.client.post( '/api/requests/', { 'data': json.dumps({ 'description': get_random_name(), 'records': [{ 'pk': library.pk, 'record_type': 'Library', }, { 'pk': sample.pk, 'record_type': 'Sample', }], # 'files': [], }) }) self.assertEqual(response.status_code, 201) self.assertTrue(response.json()['success']) self.assertEqual( Library.objects.get(pk=library.pk).request.filter().count(), 1)
def test_flowcell_list(self): """ Ensure get flowcell list behaves correctly. """ self.client.login(email='*****@*****.**', password='******') library1 = create_library(get_random_name(), 4) library2 = create_library(get_random_name(), 4) sample1 = create_sample(get_random_name(), 4) sample2 = create_sample(get_random_name(), 4) pool1 = create_pool(self.user) pool1.libraries.add(library1) pool1.samples.add(sample1) pool2 = create_pool(self.user) pool2.libraries.add(library2) pool2.samples.add(sample2) sequencer = create_sequencer(get_random_name(), lanes=4) flowcell = create_flowcell(get_random_name(), sequencer) lanes1 = [] for i in range(2): name = 'Lane {}'.format(i + 1) lane = Lane(name=name, pool=pool1) lane.save() lanes1.append(lane.pk) lanes2 = [] for i in range(2, 4): name = 'Lane {}'.format(i + 1) lane = Lane(name=name, pool=pool2, completed=True) lane.save() lanes2.append(lane.pk) flowcell.lanes.add(*lanes1) flowcell.lanes.add(*lanes2) response = self.client.get(reverse('flowcells-list')) data = response.json() self.assertEqual(response.status_code, 200) lane_ids = [x['pk'] for x in data] self.assertIn(lanes1[0], lane_ids) self.assertIn(lanes1[1], lane_ids) self.assertNotIn(lanes2[0], lane_ids) self.assertNotIn(lanes2[1], lane_ids)
def test_create_library_preparation_object(self): """ Ensure a Library Preparation object is created when a sample is added to a pool. """ sample = create_sample(get_random_name(), 2) pool = create_pool(self.user) pool.samples.add(sample) self.assertEqual( LibraryPreparation.objects.filter(sample=sample).count(), 1)
def test_pool_list(self): """ Ensure get pool list behaves correctly. """ self.client.login(email='*****@*****.**', password='******') # Case 1: pool is ready pool1 = create_pool(self.user) library1 = create_library(get_random_name(), status=4) library2 = create_library(get_random_name(), status=-1) # failed P QC sample1 = create_sample(get_random_name(), status=4) pool1.libraries.add(*[library1.pk, library2.pk]) pool1.samples.add(sample1) # Case 2: pool is not ready pool2 = create_pool(self.user) library3 = create_library(get_random_name(), status=4) sample2 = create_sample(get_random_name(), status=3) pool2.libraries.add(library3) pool2.samples.add(sample2.pk) # Case 3: some of the pool's samples haven't reached the Pooling step pool3 = create_pool(self.user) sample3 = create_sample(get_random_name(), status=2) # no Pooling obj sample4 = create_sample(get_random_name(), status=-1) # failed P QC pool3.samples.add(*[sample3.pk, sample4.pk]) response = self.client.get(reverse('flowcells-pool-list')) data = response.json() self.assertEqual(response.status_code, 200) pools = [x['name'] for x in data] self.assertIn(pool1.name, pools) self.assertIn(pool2.name, pools) self.assertNotIn(pool3.name, pools) pool1_obj = [x for x in data if x['name'] == pool1.name][0] pool2_obj = [x for x in data if x['name'] == pool2.name][0] self.assertTrue(pool1_obj['ready']) self.assertFalse(pool2_obj['ready']) self.assertTrue(data[0]['read_length_name'], library1.read_length.name) self.assertTrue(data[1]['read_length_name'], sample1.read_length.name) self.assertTrue(data[0]['pool_size'], pool1.size.multiplier) self.assertTrue(data[1]['pool_size'], pool2.size.multiplier)
def setUp(self): user = self.create_user('*****@*****.**', 'foo-foo') self.client.login(email='*****@*****.**', password='******') library = create_library(self._get_random_name()) sample = create_sample(self._get_random_name()) self.request = Request(user=user) self.request.save() self.request.libraries.add(library) self.request.samples.add(sample)
def create_library_preparation_obj(sample_name, user, sample_status): sample = create_sample(sample_name, status=sample_status) sample.save() request = Request(user=user) request.save() request.samples.add(sample) pool = create_pool(user) pool.samples.add(sample) return LibraryPreparation.objects.get(sample=sample)
def test_total_records_count(self): request = Request(user=self.user) request.save() library = create_library(get_random_name()) sample = create_sample(get_random_name()) request.libraries.add(library) request.samples.add(sample) request = Request.objects.get(pk=request.pk) self.assertEqual(request.total_records_count, 2)
def test_quality_check_multiple(self): """ Ensure quality check has failed behaves correctly. """ self.client.login(email='*****@*****.**', password='******') sample1 = create_sample(self._get_random_name(), status=1) sample2 = create_sample(self._get_random_name(), status=1) response = self.client.post( reverse('incoming-libraries-edit'), { 'data': json.dumps([{ 'pk': sample1.pk, 'record_type': 'Sample', 'quality_check': 'failed', }, { 'pk': sample2.pk, 'record_type': 'Sample', 'quality_check': 'compromised', }]) }) self.assertEqual(response.status_code, 200) self.assertTrue(response.json()['success']) self.assertEqual(Sample.objects.get(pk=sample1.pk).status, -1) self.assertEqual(Sample.objects.get(pk=sample2.pk).status, -2)
def test_get_records(self): """ Ensure get request's records behaves correctly. """ request = create_request(self.user) library = create_library(get_random_name()) sample = create_sample(get_random_name()) request.libraries.add(library) request.samples.add(sample) response = self.client.get(f'/api/requests/{request.pk}/get_records/') self.assertEqual(response.status_code, 200) records = [x['name'] for x in response.json()] self.assertIn(library.name, records) self.assertIn(sample.name, records)
def test_create_pooling_object_from_sample(self): """ Ensure a Pooling object is created when a sample passed the quality check (and gets the status 4) after the Library Preparation step. """ sample = create_sample(get_random_name(), status=3) pool = create_pool(self.user) pool.samples.add(sample) # Update the sample instance because after being added to the pool, # it was modified sample = sample.__class__.objects.get(pk=sample.pk) sample.status = 3 # passed quality check sample.save() self.assertEqual(Pooling.objects.filter(sample=sample).count(), 1)
def create_pooling_object(user, save=True, add_library=False, add_sample=False, sample_failed=False): library = None sample = None if add_library: library = create_library(get_random_name(), 2) if add_sample: sample = create_sample(get_random_name(), 2) pool = create_pool(user) request = Request(user=user) request.save() if library: request.libraries.add(library) pool.libraries.add(library) pooling_object = Pooling.objects.get(library=library) if sample: request.samples.add(sample) pool.samples.add(sample) # Update the sample instance because after being added to the pool, # it was modified sample = sample.__class__.objects.get(pk=sample.pk) if sample_failed: sample.status = -1 # failed quality check sample.save() pooling_object = sample else: sample.status = 3 # passed quality check sample.save() pooling_object = Pooling.objects.get(sample=sample) if save: pooling_object.save() return pooling_object
def test_delete_request(self): """ Ensure all dependent libraries, samples and uploaded files are deleted, too. """ request = Request(user=self.user) request.save() library = create_library(get_random_name()) sample = create_sample(get_random_name()) Library = library.__class__ Sample = sample.__class__ request.libraries.add(library) request.samples.add(sample) request.delete() # TODO: create and delete files self.assertEqual(Library.objects.filter(pk=library.pk).count(), 0) self.assertEqual(Sample.objects.filter(pk=sample.pk).count(), 0)