Exemplo n.º 1
0
def test_cacheable_pageable_json(os_makedirs, pj_read_json, ju_read,
                                 cache_style, safe_read_url_get_msg5):
    with patch("allensdk.core.json_utilities.read_url_get",
               side_effect=safe_read_url_get_msg5) as ju_read_url_get:

        archive_templates = \
            {"cam_cell_queries": [
                {'name': 'cam_cell_metric',
                'description': 'see name',
                'model': 'ApiCamCellMetric',
                'num_rows': 1000,
                'count': False
                } ] }

        rmat = RmaTemplate(query_manifest=archive_templates)

        @cacheable()
        @pageable(num_rows=2000)
        def get_cam_cell_metrics(*args, **kwargs):
            return rmat.template_query("cam_cell_queries", 'cam_cell_metric',
                                       *args, **kwargs)

        with patch(builtins.__name__ + '.open', mock_open(),
                   create=True) as open_mock:
            open_mock.return_value.read = \
                MagicMock(name='read',
                        return_value=[{'whatever': True},
                                        {'whatever': True},
                                        {'whatever': True},
                                        {'whatever': True},
                                        {'whatever': True}])
            cam_cell_metrics = \
                get_cam_cell_metrics(strategy='create',
                                    path='/path/to/cam_cell_metrics.json',
                                    num_rows=1,
                                    total_rows='all',
                                    **cache_style())

        os_makedirs.assert_called_once_with('/path/to')

        base_query = \
            ('http://api.brain-map.org/api/v2/data/query.json?'
            'q=model::ApiCamCellMetric,'
            'rma::options%5Bnum_rows$eq1%5D%5Bstart_row$eq{}%5D'
            '%5Bcount$eqfalse%5D')

        expected_calls = map(lambda c: call(base_query.format(c)),
                             [0, 1, 2, 3, 4, 5])

        open_mock.assert_called_once_with('/path/to/cam_cell_metrics.json',
                                          'wb')
        open_mock.return_value.write.assert_called_once_with(
            '[\n  {\n    "whatever": true\n  },\n  {\n    "whatever": true\n  },\n  {\n    "whatever": true\n  },\n  {\n    "whatever": true\n  },\n  {\n    "whatever": true\n  }\n]'
        )
        assert ju_read_url_get.call_args_list == list(expected_calls)
        assert len(cam_cell_metrics) == 5
Exemplo n.º 2
0
def test_cacheable_pageable_csv(os_makedirs, read_csv, cache_style,
                                safe_read_url_get_msg5):
    with patch("allensdk.core.json_utilities.read_url_get",
               side_effect=safe_read_url_get_msg5) as ju_read_url_get:
        archive_templates = \
            {"cam_cell_queries": [
                {'name': 'cam_cell_metric',
                'description': 'see name',
                'model': 'ApiCamCellMetric',
                'num_rows': 1000,
                'count': False
                } ] }

        rmat = RmaTemplate(query_manifest=archive_templates)

        @cacheable()
        @pageable(num_rows=2000)
        def get_cam_cell_metrics(*args, **kwargs):
            return rmat.template_query("cam_cell_queries", 'cam_cell_metric',
                                       *args, **kwargs)

        with patch(builtins.__name__ + '.open', mock_open(),
                   create=True) as open_mock:
            with patch('csv.DictWriter.writerow') as csv_writerow:
                cam_cell_metrics = \
                    get_cam_cell_metrics(strategy='create',
                                        path='/path/to/cam_cell_metrics.csv',
                                        num_rows=1,
                                        total_rows='all',
                                        **cache_style())

        os_makedirs.assert_called_once_with('/path/to')

        base_query = ('http://api.brain-map.org/api/v2/data/query.json?'
                      'q=model::ApiCamCellMetric,'
                      'rma::options%5Bnum_rows$eq1%5D%5Bstart_row$eq{}%5D'
                      '%5Bcount$eqfalse%5D')

        expected_calls = map(lambda c: call(base_query.format(c)),
                             [0, 1, 2, 3, 4, 5])

        assert ju_read_url_get.call_args_list == list(expected_calls)
        read_csv.assert_called_once_with('/path/to/cam_cell_metrics.csv',
                                         parse_dates=True)

        assert csv_writerow.call_args_list == [
            call({'whatever': 'whatever'}),
            call({'whatever': True}),
            call({'whatever': True}),
            call({'whatever': True}),
            call({'whatever': True}),
            call({'whatever': True})
        ]
Exemplo n.º 3
0
class ApiTemplateTests(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(ApiTemplateTests, self).__init__(*args, **kwargs)
        self.templates = None
    
    
    def setUp(self):
        self.templates = \
            {"ontology_queries": [
                {'name': 'structures_by_graph_ids',
                 'description': 'see name',
                 'model': 'Structure',
                 'criteria': '[graph_id$in{{ graph_ids }}]',
                 'order': ['structures.graph_order'],
                 'num_rows': 'all',
                 'count': False,
                 'criteria_params': ['graph_ids']
                },
                {'name': 'structures_by_graph_names',
                 'description': 'see name',
                 'model': 'Structure',
                 'criteria': 'graph[structure_graphs.name$in{{ graph_names }}]',
                 'order': ['structures.graph_order'],
                 'num_rows': 'all',
                 'count': False,
                 'criteria_params': ['graph_names']
                },
                {'name': 'structures_by_set_ids',
                 'description': 'see name',
                 'model': 'Structure',
                 'criteria': '[structure_set_id$in{{ set_ids }}]',
                 'order': ['structures.graph_order'],
                 'num_rows': 'all',
                 'count': False,
                 'criteria_params': ['set_ids']
                },
                {'name': 'structures_by_set_names',
                 'description': 'see name',
                 'model': 'Structure',
                 'criteria': 'structure_sets[name$in{{ set_names }}]',
                 'order': ['structures.graph_order'],
                 'num_rows': 'all',
                 'count': False,
                 'criteria_params': ['set_names']
                },
                {'name': 'structure_graphs_list',
                 'description': 'see name',
                 'model': 'StructureGraph',
                 'num_rows': 'all',
                 'count': False
                },
                {'name': 'structure_sets_list',
                 'description': 'see name',
                 'model': 'StructureSet',
                 'num_rows': 'all',
                 'count': False
                },
                {'name': 'atlases_list',
                 'description': 'see name',
                 'model': 'Atlas',
                 'num_rows': 'all',
                 'count': False
                },
                {'name': 'atlases_table',
                 'description': 'see name',
                 'model': 'Atlas',
                 'criteria': '{% if graph_ids is defined %}[graph_id$in{{ graph_ids }}],{% endif %}structure_graph(ontology),graphic_group_labels',
                 'include': '[structure_graph(ontology),graphic_group_labels',
                 'num_rows': 'all',
                 'count': False,
                 'criteria_params': ['graph_ids']
                },
                {'name': 'atlases_table_brief',
                 'description': 'see name',
                 'model': 'Atlas',
                 'criteria': 'structure_graph(ontology),graphic_group_labels',
                 'include': 'structure_graph(ontology),graphic_group_labels',
                 'only': ['atlases.id',
                           'atlases.name',
                           'atlases.image_type',
                           'ontologies.id',
                           'ontologies.name',
                           'structure_graphs.id',
                           'structure_graphs.name',
                           'graphic_group_labels.id',
                           'graphic_group_labels.name'],
                 'num_rows': 'all',
                 'count': False
                }
            ]}
        self.rma = RmaTemplate(query_manifest=self.templates)
    
    
    def tearDown(self):
        self.rma= None
        self.templates = None
        
    
    def test_atlases_list(self):
        ju.read_url_get = \
            MagicMock(name='read_url_get',
                      return_value={ 'msg': [{ 'whatever': True }] })
        
        self.rma.template_query('ontology_queries',
                                'atlases_list')
        
        ju.read_url_get.assert_called_once_with('http://api.brain-map.org/api/v2/data/query.json?q=model::Atlas,rma::options%5Bnum_rows$eq%27all%27%5D%5Bcount$eqfalse%5D')
    
    
    def test_structure_graphs_list(self):
        ju.read_url_get = \
            MagicMock(name='read_url_get',
                      return_value={ 'msg': [{ 'whatever': True }] })
        
        self.rma.template_query('ontology_queries',
                                'structure_graphs_list')
        
        ju.read_url_get.assert_called_once_with('http://api.brain-map.org/api/v2/data/query.json?q=model::StructureGraph,rma::options%5Bnum_rows$eq%27all%27%5D%5Bcount$eqfalse%5D')
    
    
    def test_structure_sets_list(self):
        ju.read_url_get = \
            MagicMock(name='read_url_get',
                      return_value={ 'msg': [{ 'whatever': True }] })
        
        self.rma.template_query('ontology_queries',
                                'structure_sets_list')
        
        ju.read_url_get.assert_called_once_with('http://api.brain-map.org/api/v2/data/query.json?q=model::StructureSet,rma::options%5Bnum_rows$eq%27all%27%5D%5Bcount$eqfalse%5D')
    
    
    def test_structures_by_graph_ids(self):
        ju.read_url_get = \
            MagicMock(name='read_url_get',
                      return_value={ 'msg': [{ 'whatever': True }] })
        
        self.rma.template_query('ontology_queries',
                                'structures_by_graph_ids',
                                graph_ids='1')
        
        ju.read_url_get.assert_called_once_with('http://api.brain-map.org/api/v2/data/query.json?q=model::Structure,rma::criteria,%5Bgraph_id$in1%5D,rma::options%5Bnum_rows$eq%27all%27%5D%5Border$eqstructures.graph_order%5D%5Bcount$eqfalse%5D')
    
    
    def test_structures_by_two_graph_ids(self):
        ju.read_url_get = \
            MagicMock(name='read_url_get',
                      return_value={ 'msg': [{ 'whatever': True }] })
        
        self.rma.template_query('ontology_queries',
                                'structures_by_graph_ids',
                                graph_ids=[1, 2])
        
        ju.read_url_get.assert_called_once_with('http://api.brain-map.org/api/v2/data/query.json?q=model::Structure,rma::criteria,%5Bgraph_id$in1,2%5D,rma::options%5Bnum_rows$eq%27all%27%5D%5Border$eqstructures.graph_order%5D%5Bcount$eqfalse%5D')
    
    
    def test_structures_by_graph_names(self):
        ju.read_url_get = \
            MagicMock(name='read_url_get',
                      return_value={ 'msg': [{ 'whatever': True }] })
        
        self.rma.template_query('ontology_queries',
                                'structures_by_graph_names',
                                graph_names=self.rma.quote_string('Human+Brain+Atlas'))
        
        ju.read_url_get.assert_called_once_with('http://api.brain-map.org/api/v2/data/query.json?q=model::Structure,rma::criteria,graph%5Bstructure_graphs.name$in%27Human+Brain+Atlas%27%5D,rma::options%5Bnum_rows$eq%27all%27%5D%5Border$eqstructures.graph_order%5D%5Bcount$eqfalse%5D')
    
    
    def test_structures_by_set_ids(self):
        ju.read_url_get = \
            MagicMock(name='read_url_get',
                      return_value={ 'msg': [{ 'whatever': True }] })
        
        self.rma.template_query('ontology_queries',
                                'structures_by_graph_ids',
                                graph_ids='1')
        
        ju.read_url_get.assert_called_once_with('http://api.brain-map.org/api/v2/data/query.json?q=model::Structure,rma::criteria,%5Bgraph_id$in1%5D,rma::options%5Bnum_rows$eq%27all%27%5D%5Border$eqstructures.graph_order%5D%5Bcount$eqfalse%5D')
    
    
    def test_atlases_table(self):
        ju.read_url_get = \
            MagicMock(name='read_url_get',
                      return_value={ 'msg': [{ 'whatever': True }] })
        
        self.rma.template_query('ontology_queries',
                                'atlases_table')
        
        ju.read_url_get.assert_called_once_with('http://api.brain-map.org/api/v2/data/query.json?q=model::Atlas,rma::criteria,structure_graph%28ontology%29,graphic_group_labels,rma::include,%5Bstructure_graph%28ontology%29,graphic_group_labels,rma::options%5Bnum_rows$eq%27all%27%5D%5Bcount$eqfalse%5D')
    
    
    def test_atlases_table_one_graph(self):
        ju.read_url_get = \
            MagicMock(name='read_url_get',
                      return_value={ 'msg': [{ 'whatever': True }] })
        
        self.rma.template_query('ontology_queries',
                                'atlases_table',
                                graph_ids=1)
        
        ju.read_url_get.assert_called_once_with('http://api.brain-map.org/api/v2/data/query.json?q=model::Atlas,rma::criteria,%5Bgraph_id$in1%5D,structure_graph%28ontology%29,graphic_group_labels,rma::include,%5Bstructure_graph%28ontology%29,graphic_group_labels,rma::options%5Bnum_rows$eq%27all%27%5D%5Bcount$eqfalse%5D')
    
    
    def test_atlases_table_brief(self):
        ju.read_url_get = \
            MagicMock(name='read_url_get',
                      return_value={ 'msg': [{ 'whatever': True }] })
        
        self.rma.template_query('ontology_queries',
                                'atlases_table_brief')
        
        ju.read_url_get.assert_called_once_with('http://api.brain-map.org/api/v2/data/query.json?q=model::Atlas,rma::criteria,structure_graph%28ontology%29,graphic_group_labels,rma::include,structure_graph%28ontology%29,graphic_group_labels,rma::options%5Bonly$eq%27atlases.id,atlases.name,atlases.image_type,ontologies.id,ontologies.name,structure_graphs.id,structure_graphs.name,graphic_group_labels.id,graphic_group_labels.name%27%5D%5Bnum_rows$eq%27all%27%5D%5Bcount$eqfalse%5D')
Exemplo n.º 4
0
def rma():
    templates = \
        {"ontology_queries": [
            {'name': 'structures_by_graph_ids',
             'description': 'see name',
             'model': 'Structure',
             'criteria': '[graph_id$in{{ graph_ids }}]',
             'order': ['structures.graph_order'],
             'num_rows': 'all',
             'count': False,
             'criteria_params': ['graph_ids']
            },
            {'name': 'structures_by_graph_names',
             'description': 'see name',
             'model': 'Structure',
             'criteria': 'graph[structure_graphs.name$in{{ graph_names }}]',
             'order': ['structures.graph_order'],
             'num_rows': 'all',
             'count': False,
             'criteria_params': ['graph_names']
            },
            {'name': 'structures_by_set_ids',
             'description': 'see name',
             'model': 'Structure',
             'criteria': '[structure_set_id$in{{ set_ids }}]',
             'order': ['structures.graph_order'],
             'num_rows': 'all',
             'count': False,
             'criteria_params': ['set_ids']
            },
            {'name': 'structures_by_set_names',
             'description': 'see name',
             'model': 'Structure',
             'criteria': 'structure_sets[name$in{{ set_names }}]',
             'order': ['structures.graph_order'],
             'num_rows': 'all',
             'count': False,
             'criteria_params': ['set_names']
            },
            {'name': 'structure_graphs_list',
             'description': 'see name',
             'model': 'StructureGraph',
             'num_rows': 'all',
             'count': False
            },
            {'name': 'structure_sets_list',
             'description': 'see name',
             'model': 'StructureSet',
             'num_rows': 'all',
             'count': False
            },
            {'name': 'atlases_list',
             'description': 'see name',
             'model': 'Atlas',
             'num_rows': 'all',
             'count': False
            },
            {'name': 'atlases_table',
             'description': 'see name',
             'model': 'Atlas',
             'criteria': '{% if graph_ids is defined %}[graph_id$in{{ graph_ids }}],{% endif %}structure_graph(ontology),graphic_group_labels',
             'include': '[structure_graph(ontology),graphic_group_labels',
             'num_rows': 'all',
             'count': False,
             'criteria_params': ['graph_ids']
            },
            {'name': 'atlases_table_brief',
             'description': 'see name',
             'model': 'Atlas',
             'criteria': 'structure_graph(ontology),graphic_group_labels',
             'include': 'structure_graph(ontology),graphic_group_labels',
             'only': ['atlases.id',
                      'atlases.name',
                      'atlases.image_type',
                      'ontologies.id',
                      'ontologies.name',
                      'structure_graphs.id',
                      'structure_graphs.name',
                      'graphic_group_labels.id',
                      'graphic_group_labels.name'],
             'num_rows': 'all',
             'count': False
            }
        ]}
    rma = RmaTemplate(query_manifest=templates)

    return rma
Exemplo n.º 5
0
 def setUp(self):
     self.templates = \
         {"ontology_queries": [
             {'name': 'structures_by_graph_ids',
              'description': 'see name',
              'model': 'Structure',
              'criteria': '[graph_id$in{{ graph_ids }}]',
              'order': ['structures.graph_order'],
              'num_rows': 'all',
              'count': False,
              'criteria_params': ['graph_ids']
             },
             {'name': 'structures_by_graph_names',
              'description': 'see name',
              'model': 'Structure',
              'criteria': 'graph[structure_graphs.name$in{{ graph_names }}]',
              'order': ['structures.graph_order'],
              'num_rows': 'all',
              'count': False,
              'criteria_params': ['graph_names']
             },
             {'name': 'structures_by_set_ids',
              'description': 'see name',
              'model': 'Structure',
              'criteria': '[structure_set_id$in{{ set_ids }}]',
              'order': ['structures.graph_order'],
              'num_rows': 'all',
              'count': False,
              'criteria_params': ['set_ids']
             },
             {'name': 'structures_by_set_names',
              'description': 'see name',
              'model': 'Structure',
              'criteria': 'structure_sets[name$in{{ set_names }}]',
              'order': ['structures.graph_order'],
              'num_rows': 'all',
              'count': False,
              'criteria_params': ['set_names']
             },
             {'name': 'structure_graphs_list',
              'description': 'see name',
              'model': 'StructureGraph',
              'num_rows': 'all',
              'count': False
             },
             {'name': 'structure_sets_list',
              'description': 'see name',
              'model': 'StructureSet',
              'num_rows': 'all',
              'count': False
             },
             {'name': 'atlases_list',
              'description': 'see name',
              'model': 'Atlas',
              'num_rows': 'all',
              'count': False
             },
             {'name': 'atlases_table',
              'description': 'see name',
              'model': 'Atlas',
              'criteria': '{% if graph_ids is defined %}[graph_id$in{{ graph_ids }}],{% endif %}structure_graph(ontology),graphic_group_labels',
              'include': '[structure_graph(ontology),graphic_group_labels',
              'num_rows': 'all',
              'count': False,
              'criteria_params': ['graph_ids']
             },
             {'name': 'atlases_table_brief',
              'description': 'see name',
              'model': 'Atlas',
              'criteria': 'structure_graph(ontology),graphic_group_labels',
              'include': 'structure_graph(ontology),graphic_group_labels',
              'only': ['atlases.id',
                        'atlases.name',
                        'atlases.image_type',
                        'ontologies.id',
                        'ontologies.name',
                        'structure_graphs.id',
                        'structure_graphs.name',
                        'graphic_group_labels.id',
                        'graphic_group_labels.name'],
              'num_rows': 'all',
              'count': False
             }
         ]}
     self.rma = RmaTemplate(query_manifest=self.templates)