def test_pipeline(self): expected_counts = { 'batches': 2, 'cohorts': 2, 'genes': 65, 'projects': 1, 'results_per_sample': [ { 'batch': 'batch1', 'sample': 'NA12891', 'count': 1963, }, { 'batch': 'batch1', 'sample': 'NA12892', 'count': 1963, }, { 'batch': 'batch1', 'sample': 'NA12878', 'count': 1963, }, { 'batch': 'batch2', 'sample': 'NA12891', 'count': 2094, }, { 'batch': 'batch2', 'sample': 'NA12892', 'count': 2094, }, { 'batch': 'batch2', 'sample': 'NA12878', 'count': 2094, }, ], 'samples': 6, 'transcripts': 108, 'variant_effects': 8788, 'variants': 4057, 'samples_per_batch': [(1, 3), (2, 3)], } expected_counts['results'] = \ sum([x['count'] for x in expected_counts['results_per_sample']]) # Immediately validates and creates a sample management.call_command('samples', 'queue') # Synchronously work on queue worker1 = get_worker('variants') worker2 = get_worker('default') # Ensure sample-related entries are created.. self.assertEqual(Project.objects.count(), expected_counts['projects']) self.assertEqual(Batch.objects.count(), expected_counts['batches']) self.assertEqual(Sample.objects.count(), expected_counts['samples']) # World and project cohort.. self.assertEqual(Cohort.objects.count(), expected_counts['cohorts']) # Nothing published yet.. self.assertEqual( Sample.objects.filter(published=False).count(), expected_counts['samples']) self.assertEqual( Cohort.objects.filter(count=0, published=False).count(), expected_counts['cohorts']) self.assertEqual( Batch.objects.filter(count=0, published=False).count(), expected_counts['batches']) # Manifests are stored self.assertEqual(SampleManifest.objects.count(), expected_counts['samples']) for manifest in SampleManifest.objects.all(): self.assertNotEqual(manifest.content, '') self.assertFalse(manifest.content_has_changed()) # Work on variants... worker1.work(burst=True) self.assertEqual(Variant.objects.count(), expected_counts['variants']) # Work on effects... worker2.work(burst=True) self.assertEqual(Gene.objects.count(), expected_counts['genes']) self.assertEqual(Transcript.objects.count(), expected_counts['transcripts']) self.assertEqual(VariantEffect.objects.count(), expected_counts['variant_effects']) self.assertEqual(Sift.objects.count(), 0) self.assertEqual(PolyPhen2.objects.count(), 0) self.assertEqual(ThousandG.objects.count(), 0) self.assertEqual(EVS.objects.count(), 0) # Results loaded.. self.assertEqual(Result.objects.count(), expected_counts['results']) # Batches are now published.. self.assertEqual( Batch.objects.filter(published=True).count(), expected_counts['batches']) # Ensure the counts are accurate for each sample.. for ec in expected_counts['results_per_sample']: sample = Sample.objects.get(name=ec['sample'], batch__name=ec['batch']) self.assertTrue(sample.published) self.assertEqual(sample.count, ec['count']) # Batches are created with the samples, but are unpublished for pk, count in expected_counts['samples_per_batch']: batch = Batch.objects.get(pk=pk) self.assertTrue(batch.published) self.assertEqual(batch.count, count) # Ensure the state changes were logged.. system = System.get(Sample.objects.all()[0]) self.assertEqual(len(system), 3)
def setUp(self): self.system = System() self.system.save()
def test_pipeline(self): # Immediately validates and creates a sample management.call_command('samples', 'queue') # Synchronously work on queue worker1 = get_worker('variants') worker2 = get_worker('default') # Ensure sample-related entries are created.. self.assertEqual(Project.objects.count(), 1) self.assertEqual(Batch.objects.count(), 4) self.assertEqual(Sample.objects.count(), 15) # World and project cohort.. self.assertEqual(Cohort.objects.count(), 2) # Nothing published yet.. self.assertEqual(Sample.objects.filter(published=False).count(), 15) self.assertEqual( Cohort.objects.filter(count=0, published=False).count(), 2) self.assertEqual( Batch.objects.filter(count=0, published=False).count(), 4) # Manifests are stored self.assertEqual(SampleManifest.objects.count(), 15) for manifest in SampleManifest.objects.all(): self.assertNotEqual(manifest.content, '') self.assertFalse(manifest.content_has_changed()) # Work on variants... worker1.work(burst=True) self.assertEqual(Variant.objects.count(), 674) # Work on effects... worker2.work(burst=True) self.assertEqual(Gene.objects.count(), 104) self.assertEqual(Transcript.objects.count(), 255) self.assertEqual(VariantEffect.objects.count(), 1418) self.assertEqual(Sift.objects.count(), 0) self.assertEqual(PolyPhen2.objects.count(), 0) self.assertEqual(ThousandG.objects.count(), 0) self.assertEqual(EVS.objects.count(), 0) # Results loaded.. self.assertEqual(Result.objects.count(), 3436) # Batches are now published.. self.assertEqual(Batch.objects.filter(published=True).count(), 3) # Ensure the counts are accurate for each sample.. for pk, count in [(1, 289), (2, 281), (3, 268), (4, 295), (5, 296), (6, 293), (7, 264), (8, 289), (9, 264), (10, 293), (11, 289), (12, 315)]: sample = Sample.objects.get(pk=pk) self.assertTrue(sample.published) self.assertEqual(sample.count, count) # Batches are created with the samples, but are unpublished for pk, count in [(1, 5), (2, 3), (3, 4)]: batch = Batch.objects.get(pk=pk) self.assertTrue(batch.published) self.assertEqual(batch.count, count) # Ensure the state changes were logged.. system = System.get(Sample.objects.all()[0]) self.assertEqual(len(system), 3)
class SystemTestCase(TestCase): def setUp(self): self.system = System() self.system.save() def test_simple(self): system = self.system system.transition('Initialize', 'Initialized') self.assertEqual(len(system), 1) def test_long(self): system = self.system # Specify the event.. system.start_transition('Initialize') self.assertTrue(system.in_transition()) self.assertEqual(system.current_state(), State.TRANSITION) self.assertEqual(len(system), 1) self.assertRaises(STSError, system.start_transition, 'Initialize') # Sleep for 2 seconds.. time.sleep(2) # Specify the state.. trans = system.end_transition('Initialized') self.assertTrue(trans.duration, 2) self.assertFalse(system.in_transition()) self.assertEqual(system.current_state().name, 'Initialized') self.assertEqual(len(system), 1) self.assertRaises(STSError, system.end_transition, 'Initialized') system.transition('Saved', event='Save by foobar') system.transition('Saved', event='Save by admin') self.assertEqual(len(system), 3) self.assertEqual(State.objects.count(), 3) self.assertEqual(Event.objects.count(), 3) def test_iteration(self): system = self.system system.transition('Shoe Buckled', event='Buckle Shoe') system.transition('Door Closed', event='Close Door') # Test iteration self.assertEqual([str(t.state) for t in system], [ 'Shoe Buckled', 'Door Closed', ]) def test_getitem(self): system = self.system self.assertEqual(system[:3], []) self.assertEqual(system[2:3], []) self.assertRaises(IndexError, system.__getitem__, 5) self.assertRaises(IndexError, system.__getitem__, slice(None, None, 2)) self.assertRaises(ValueError, system.__getitem__, slice(None, None)) for i in range(1, 6): system.transition('Count {0}'.format(i), event='Incr 1') # Don't be confused.. indexing is zero-based, while counting is 1-based self.assertEqual([str(t.state) for t in system[:3]], ['Count 1', 'Count 2', 'Count 3']) self.assertEqual([str(t.state) for t in system[-3:]], ['Count 3', 'Count 4', 'Count 5']) self.assertEqual([str(t.state) for t in system[:-3]], ['Count 1', 'Count 2']) self.assertEqual([str(t.state) for t in system[1:3]], ['Count 2', 'Count 3']) self.assertEqual(str(system[3].state), 'Count 4') self.assertEqual(str(system[-1].state), 'Count 5') # Bad slices.. self.assertEqual(system[-1:-3], []) self.assertEqual(system[-1:2], []) self.assertEqual(system[1:1], []) def test_shortcuts(self): from django.contrib.auth.models import User from sts.shortcuts import transition, start_transition, end_transition user = User.objects.create_user(username='******', password='******', email='*****@*****.**') transition(user, 'Creating User', 'User Created') start_transition(user, 'Creating User') end_transition(user, 'User Created') def test_context_manager(self): from sts.contextmanagers import transition with transition('Sleeper', 'Awake', event='Nap') as trans: time.sleep(2) trans.message = 'That was a short nap!' system = System.objects.get(name='Sleeper') self.assertEqual(system.current_state().name, 'Awake') trans = system[0] self.assertEqual(trans.message, 'That was a short nap!') self.assertTrue(2000 < trans.duration < 3000) try: with transition('Sleeper', 'Awake', event='Nap', fail_state='Annoyed'): raise Exception except: pass self.assertEqual(system.current_state().name, 'Annoyed')
def test_pipeline(self): expected_counts = { 'batches': 2, 'cohorts': 2, 'genes': 65, 'projects': 1, 'results_per_sample': [ { 'batch': 'batch1', 'sample': 'NA12891', 'count': 1963, }, { 'batch': 'batch1', 'sample': 'NA12892', 'count': 1963, }, { 'batch': 'batch1', 'sample': 'NA12878', 'count': 1963, }, { 'batch': 'batch2', 'sample': 'NA12891', 'count': 2094, }, { 'batch': 'batch2', 'sample': 'NA12892', 'count': 2094, }, { 'batch': 'batch2', 'sample': 'NA12878', 'count': 2094, }, ], 'samples': 6, 'transcripts': 108, 'variant_effects': 8788, 'variants': 4057, 'samples_per_batch': [(1, 3), (2, 3)], } expected_counts['results'] = \ sum([x['count'] for x in expected_counts['results_per_sample']]) # Immediately validates and creates a sample management.call_command('samples', 'queue') # Synchronously work on queue worker1 = get_worker('variants') worker2 = get_worker('default') # Ensure sample-related entries are created.. self.assertEqual(Project.objects.count(), expected_counts['projects']) self.assertEqual(Batch.objects.count(), expected_counts['batches']) self.assertEqual(Sample.objects.count(), expected_counts['samples']) # World and project cohort.. self.assertEqual(Cohort.objects.count(), expected_counts['cohorts']) # Nothing published yet.. self.assertEqual(Sample.objects.filter(published=False).count(), expected_counts['samples']) self.assertEqual( Cohort.objects.filter(count=0, published=False).count(), expected_counts['cohorts']) self.assertEqual( Batch.objects.filter(count=0, published=False).count(), expected_counts['batches']) # Manifests are stored self.assertEqual(SampleManifest.objects.count(), expected_counts['samples']) for manifest in SampleManifest.objects.all(): self.assertNotEqual(manifest.content, '') self.assertFalse(manifest.content_has_changed()) # Work on variants... worker1.work(burst=True) self.assertEqual(Variant.objects.count(), expected_counts['variants']) # Work on effects... worker2.work(burst=True) self.assertEqual(Gene.objects.count(), expected_counts['genes']) self.assertEqual(Transcript.objects.count(), expected_counts['transcripts']) self.assertEqual(VariantEffect.objects.count(), expected_counts['variant_effects']) self.assertEqual(Sift.objects.count(), 0) self.assertEqual(PolyPhen2.objects.count(), 0) self.assertEqual(ThousandG.objects.count(), 0) self.assertEqual(EVS.objects.count(), 0) # Results loaded.. self.assertEqual(Result.objects.count(), expected_counts['results']) # Batches are now published.. self.assertEqual(Batch.objects.filter(published=True).count(), expected_counts['batches']) # Ensure the counts are accurate for each sample.. for ec in expected_counts['results_per_sample']: sample = Sample.objects.get(name=ec['sample'], batch__name=ec['batch']) self.assertTrue(sample.published) self.assertEqual(sample.count, ec['count']) # Batches are created with the samples, but are unpublished for pk, count in expected_counts['samples_per_batch']: batch = Batch.objects.get(pk=pk) self.assertTrue(batch.published) self.assertEqual(batch.count, count) # Ensure the state changes were logged.. system = System.get(Sample.objects.all()[0]) self.assertEqual(len(system), 3)