Example #1
0
    def test_taxonomy_from_list_unknown_sample(self):
        # NOTE: do not delete this test when converting _alt methods to non
        # _alt methods
        # One ID not found (out of two)
        with patch.object(TaxonomyRepo, 'exists') as mock_exists, \
                patch.object(TaxonomyRepo, 'resources') as mock_resources:
            mock_resources.return_value = ['foo-table']
            mock_exists.side_effect = [True, False]
            response, code = _summarize_group(
                ['sample-1', 'sample-baz-bat'],
                'foo-table',
                taxonomy_repo=TaxonomyRepo(),
            )

        api_out = json.loads(response.data)
        self.assertListEqual(api_out['missing_ids'], ['sample-baz-bat'])
        self.assertRegex(api_out['text'], r'Sample ID\(s\) not found.')
        self.assertEqual(code, 404)

        # Multiple IDs do not exist
        with patch.object(TaxonomyRepo, 'exists') as mock_exists, \
                patch.object(TaxonomyRepo, 'resources') as mock_metrics:
            mock_metrics.return_value = ['bar-table']
            mock_exists.side_effect = [False, False]
            response, code = _summarize_group(
                ['sample-foo-bar', 'sample-baz-bat'],
                'bar-table',
                taxonomy_repo=TaxonomyRepo(),
            )
        api_out = json.loads(response.data)
        self.assertListEqual(api_out['missing_ids'],
                             ['sample-foo-bar', 'sample-baz-bat'])
        self.assertRegex(api_out['text'], r'Sample ID\(s\) not found.')
        self.assertEqual(code, 404)
Example #2
0
    def test_taxonomy_from_list_summarize_group_one_sample_with_variance(self):
        # NOTE: do not delete this test when converting _alt methods to non
        # _alt methods
        with patch(
                'microsetta_public_api.repo._taxonomy_repo.TaxonomyRepo.'
                'tables',
                new_callable=PropertyMock) as mock_tables:
            mock_tables.return_value = {
                'some-table': {
                    'table': self.table,
                    'feature-data-taxonomy': self.taxonomy_df,
                    'variances': self.table_vars,
                },
            }
            response, code = _summarize_group(
                ['sample-1'],
                "some-table",
                taxonomy_repo=TaxonomyRepo(),
            )

        self.assertEqual(code, 200)
        exp_keys = [
            'taxonomy', 'features', 'feature_values', 'feature_variances'
        ]
        obs = json.loads(response)

        self.assertCountEqual(exp_keys, obs.keys())
        self.assertEqual('((((((feature-2)e)d)c)b,(((feature-3)h)g)f)a);',
                         obs['taxonomy'])
        self.assertListEqual(['feature-2', 'feature-3'], obs['features'])
        assert_allclose([2. / 5, 3. / 5], obs['feature_values'])
        assert_allclose([2.0, 3.0], obs['feature_variances'])
Example #3
0
    def test_taxonomy_from_list_summarize_group_simple_with_variances(self):
        with patch(
                'microsetta_public_api.repo._taxonomy_repo.TaxonomyRepo.'
                'tables',
                new_callable=PropertyMock) as mock_tables:
            mock_tables.return_value = {
                'some-table': {
                    'table': self.table,
                    'feature-data-taxonomy': self.taxonomy_df,
                    'variances': self.table_vars,
                },
            }
            response, code = _summarize_group(self.post_body['sample_ids'],
                                              "some-table")
        self.assertEqual(code, 200)
        exp_keys = [
            'taxonomy', 'features', 'feature_values', 'feature_variances'
        ]
        obs = json.loads(response)

        self.assertCountEqual(exp_keys, obs.keys())
        self.assertEqual(
            '((((feature-1,((feature-2)e)d)c)b,'
            '(((feature-3)h)g)f)a);', obs['taxonomy'])
        self.assertListEqual(['feature-1', 'feature-2', 'feature-3'],
                             obs['features'])
        assert_allclose([1. / 10, 6. / 10, 3. / 10], obs['feature_values'])
        assert_allclose([0, 0, 0], obs['feature_variances'])
Example #4
0
    def test_taxonomy_from_list_unknown_resource(self):
        with patch.object(TaxonomyRepo, 'resources') as mock_resources:
            mock_resources.return_value = ['alpha', 'beta']
            response, code = _summarize_group(list(self.post_body.values()),
                                              'some-other-table')

        api_out = json.loads(response.data)
        self.assertRegex(
            api_out['text'], r"Requested resource: 'some-other-table' is "
            r"unavailable. "
            r"Available resource\(s\): \[(.*)\]")
        self.assertEqual(code, 404)