def test_full_args(self):
        ''' Verifica se os parâmetros são criados corretamente '''
        r_args = {
            "categorias": 'a,b',
            "valor": 'c,d',
            "agregacao": 'e,f',
            "ordenacao": 'g,h',
            "filtros": r'eq-o-comma\,separated,and,eq-p-q',
            "pivot": 'i,j',
            "limit": '10',
            "offset": '11',
            "calcs": 'k,l',
            "partition": 'm,n'
        }

        opts = QueryOptionsBuilder.build_options(r_args)

        self.assertEqual(
            opts, {
                "categorias": ['a', 'b'],
                "valor": ['c', 'd'],
                "agregacao": ['e', 'f'],
                "ordenacao": ['g', 'h'],
                "where": ['eq-o-comma,separated', 'and', 'eq-p-q'],
                "pivot": ['i', 'j'],
                "limit": '10',
                "offset": '11',
                "calcs": ['k', 'l'],
                "partition": ['m', 'n']
            })
    def test_main_theme(self):
        ''' Verifica se os parâmetros são criados corretamente '''
        r_args = {
            "categorias": 'a,b',
            "valor": 'c,d',
            "agregacao": 'e,f',
            "ordenacao": 'g,h',
            "where": r'eq-o-comma\,separated,and,eq-p-q',
            "pivot": 'i,j',
            "limit": '10',
            "offset": '11',
            "calcs": 'k,l',
            "partition": 'm,n'
        }

        opts = QueryOptionsBuilder.build_options(r_args)
        self.assertEqual(opts.get('theme'), None)
Пример #3
0
    def api_to_options(cls, api_call_obj, custom_args=None):
        ''' Transforms API string into datahub options '''
        url = cls.get_api_url(api_call_obj, custom_args)
        url_parts = urllib.parse.urlparse(url)

        args_dict = {arg.split('=')[0]: arg.split('=')[1] for arg in url_parts.query.split('&')}
        options = QueryOptionsBuilder.build_options(args_dict)

        path_parts = url_parts.path.split('/')
        if path_parts[0] == '':
            path_parts.pop(0)

        if path_parts[0] == 'thematic':
            options['theme'] = path_parts[1]
        elif '-' in path_parts[-1]:
            options['theme'] = ''.join(path_parts[:-1])
            options['operation'] = path_parts[-1]
        else:
            options['theme'] = ''.join(path_parts)

        return options
Пример #4
0
    def get_template_data_collection(self, structure, options):
        ''' Fetches data from impala '''
        data_collection = {}
        any_nodata = False
        for each_obj_struct in structure:
            each_options = self.remove_templates(each_obj_struct['endpoint'],
                                                 options)
            if each_options.get('theme') is None:
                each_options["theme"] = options.get("theme")

            # Adds complimentary options
            each_options = QueryOptionsBuilder.build_options(each_options)
            each_options["as_pandas"] = True

            # Builds the options to query impala
            each_obj = self.find_dataset(each_options)

            # Transforms the dataset with coefficient
            if 'coefficient' in options:
                each_obj = TemplateHelper.apply_coefficient(
                    options['coefficient'], each_obj)

            # Runs formatters from config
            if 'formatters' in each_obj_struct:
                each_obj = TemplateHelper.run_formatters(
                    each_obj_struct['formatters'], each_obj)

            # Gets derived attributes
            any_blank = False
            if 'instances' in each_obj_struct:
                (data_collection,
                 any_blank) = self.build_derivatives(each_obj_struct, options,
                                                     each_obj, data_collection)
            if any_blank:
                any_nodata = True

            # Converts pandas dataframe to conventional dict, to add to response
            each_obj['dataset'] = each_obj['dataset'].to_dict('records')
            data_collection[each_obj_struct['name']] = each_obj
        return (data_collection, any_nodata)
Пример #5
0
 def build_options(r_args, rules='query'):
     ''' Constrói as opções da pesquisa '''
     return QueryOptionsBuilder.build_options(r_args, rules)