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_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_quality_check_completed(self): """ Ensure quality check has completed behaves correctly. """ self.client.login(email='*****@*****.**', password='******') library = create_library(get_random_name(), 4) pool = create_pool(self.user) pool.libraries.add(library) sequencer = create_sequencer(get_random_name(), lanes=1) flowcell = create_flowcell(get_random_name(), sequencer) lane = Lane(name=get_random_name(len=6), pool=pool) lane.save() flowcell.lanes.add(lane) response = self.client.post(reverse('flowcells-edit'), { 'data': json.dumps([{ 'pk': lane.pk, 'quality_check': 'completed', }]) }) updated_lane = Lane.objects.get(pk=lane.pk) self.assertEqual(response.status_code, 200) self.assertTrue(response.json()['success']) self.assertTrue(updated_lane.completed)
def test_update_lane_contains_invalid(self): """ Ensure update lanes containing invalid lanes behaves correctly. """ self.client.login(email='*****@*****.**', password='******') library = create_library(get_random_name(), 4) pool = create_pool(self.user) pool.libraries.add(library) sequencer = create_sequencer(get_random_name(), lanes=2) flowcell = create_flowcell(get_random_name(), sequencer) lane1 = Lane(name=get_random_name(len=6), pool=pool) lane1.save() lane2 = Lane(name=get_random_name(len=6), pool=pool) lane2.save() flowcell.lanes.add(*[lane1.pk, lane2.pk]) response = self.client.post(reverse('flowcells-edit'), { 'data': json.dumps([{ 'pk': lane1.pk, 'loading_concentration': 1.0, }, { 'pk': lane2.pk, 'loading_concentration': 'blah', }]) }) data = response.json() self.assertEqual(response.status_code, 200) self.assertTrue(response.json()['success']) self.assertEqual(data['message'], 'Some records cannot be updated.') self.assertEqual( Lane.objects.get(pk=lane1.pk).loading_concentration, 1.0)
def test_create_flowcell_some_lanes_not_loaded(self): """ Ensure error is thrown if not all lanes are loaded. """ self.client.login(email='*****@*****.**', password='******') library = create_library(get_random_name(), 4) pool = create_pool(self.user) pool.libraries.add(library) lanes = [{ 'name': 'Lane {}'.format(i + 1), 'pool_id': pool.pk, } for i in range(4)] sequencer = create_sequencer(get_random_name()) flowcell_id = get_random_name() response = self.client.post(reverse('flowcells-list'), { 'data': json.dumps({ 'flowcell_id': flowcell_id, 'sequencer': sequencer.pk, 'lanes': lanes, }) }) data = response.json() self.assertEqual(response.status_code, 400) self.assertFalse(data['success']) self.assertEqual(data['message'], 'Invalid payload.') self.assertIn('All lanes must be loaded.', data['errors']['lanes'])
def test_create_pooling_object_from_library(self): """ Ensure a Pooling object is created when a library is added to a pool. """ library = create_library(get_random_name()) pool = create_pool(self.user) pool.libraries.add(library) self.assertEqual(Pooling.objects.filter(library=library).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_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 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_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_quality_check_passed(self): """ Ensure quality check has passed behaves correctly. """ self.client.login(email='*****@*****.**', password='******') library = create_library(self._get_random_name(), status=1) response = self.client.post( reverse('incoming-libraries-edit'), { 'data': json.dumps([{ 'pk': library.pk, 'record_type': 'Library', 'quality_check': 'passed', }]) }) self.assertEqual(response.status_code, 200) self.assertTrue(response.json()['success']) self.assertEqual(Library.objects.get(pk=library.pk).status, 2)
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_update_library(self): """ Ensure update library behaves correctly. """ self.client.login(email='*****@*****.**', password='******') library = create_library(self._get_random_name(), status=1) response = self.client.post( reverse('incoming-libraries-edit'), { 'data': json.dumps([{ 'pk': library.pk, 'record_type': 'Library', 'dilution_factor': 2, 'concentration_facility': 2.0, }]) }) updated_library = Library.objects.get(pk=library.pk) self.assertEqual(response.status_code, 200) self.assertTrue(response.json()['success']) self.assertEqual(updated_library.dilution_factor, 2) self.assertEqual(updated_library.concentration_facility, 2.0)
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)
def test_update_request_no_records(self): """ Ensure error is thrown if no records are provided when updating a request. """ request = create_request(self.user) library = create_library(get_random_name()) request.libraries.add(library) response = self.client.post( f'/api/requests/{request.pk}/edit/', { 'data': json.dumps({ 'description': get_random_name(), 'records': [], # 'files': [], }), }) self.assertEqual(response.status_code, 400) self.assertFalse(response.json()['success'])