コード例 #1
0
ファイル: test_base.py プロジェクト: viro76/qcat-api-scripts
def test_extract_radio(api_details_1, api_list_1):
    config = {
        'qcat_attributes': [{
            'path': [
                'section_specifications',
                'children',
                'tech__2',
                'children',
                'tech__2__6',
                'children',
                'tech_qg_160',
                'children',
                'tech_implementation_decades',
                'value',
                'values',
            ],
            'name':
            'implementation_approximate'
        }]
    }
    eqd = QcatDataCsv(arguments=Mock(), config_object=get_config_class(config))
    mock_query = Mock()
    mock_query.return_value = api_details_1
    eqd._query = mock_query
    list_entry = api_list_1['results'][0]
    attrs = eqd.extract_attributes(list_entry)
    assert attrs == {
        'code': list_entry['code'],
        'updated': list_entry['updated'],
        'url': get_full_url(list_entry['url']),
        'implementation_approximate': 'less than 10 years ago (recently)',
    }
コード例 #2
0
ファイル: test_base.py プロジェクト: viro76/qcat-api-scripts
def test_extract_image_checkbox(api_details_1, api_list_1):
    config = {
        'qcat_attributes': [{
            'path': [
                'section_specifications',
                'children',
                'tech__3',
                'children',
                'tech__3__2',
                'children',
                'tech_qg_9',
                'children',
                'tech_landuse',
                'value',
                'values',
            ],
            'name':
            'land_use'
        }]
    }
    eqd = QcatDataCsv(arguments=Mock(), config_object=get_config_class(config))
    mock_query = Mock()
    mock_query.return_value = api_details_1
    eqd._query = mock_query
    list_entry = api_list_1['results'][0]
    attrs = eqd.extract_attributes(list_entry)
    assert attrs == {
        'code': list_entry['code'],
        'updated': list_entry['updated'],
        'url': get_full_url(list_entry['url']),
        'land_use': 'Grazing land',
    }
コード例 #3
0
ファイル: test_base.py プロジェクト: viro76/qcat-api-scripts
def test_extract_simple_attribute_int(api_details_2, api_list_1):
    config = {
        'qcat_attributes': [{
            'path': [
                'section_specifications',
                'children',
                'tech__2',
                'children',
                'tech__2__6',
                'children',
                'tech_qg_160',
                'children',
                'tech_implementation_year',
                'value',
                'value',
            ],
            'name':
            'implementation_year'
        }]
    }
    eqd = QcatDataCsv(arguments=Mock(), config_object=get_config_class(config))
    mock_query = Mock()
    mock_query.return_value = api_details_2
    eqd._query = mock_query
    list_entry = api_list_1['results'][0]
    attrs = eqd.extract_attributes(list_entry)
    assert attrs == {
        'code': list_entry['code'],
        'updated': list_entry['updated'],
        'url': get_full_url(list_entry['url']),
        'implementation_year': '2011',
    }
コード例 #4
0
ファイル: test_base.py プロジェクト: viro76/qcat-api-scripts
def test_extract_simple_attribute(api_details_1, api_list_1):
    config = {
        'qcat_attributes': [{
            'path': [
                'section_general_information',
                'children',
                'tech__1',
                'children',
                'tech__1__1',
                'children',
                'qg_location',
                'children',
                'country',
                'value',
                'value',
            ],
            'name':
            'country'
        }]
    }
    eqd = QcatDataCsv(arguments=Mock(), config_object=get_config_class(config))
    mock_query = Mock()
    mock_query.return_value = api_details_1
    eqd._query = mock_query
    list_entry = api_list_1['results'][0]
    attrs = eqd.extract_attributes(list_entry)
    assert attrs == {
        'code': list_entry['code'],
        'updated': list_entry['updated'],
        'url': get_full_url(list_entry['url']),
        'country': 'Afghanistan',
    }
コード例 #5
0
ファイル: test_base.py プロジェクト: viro76/qcat-api-scripts
def test_extract_grouped_attributes(api_details_1, api_list_1):
    config = {
        'qcat_attributes': [{
            'grouped': [{
                'path': [
                    'section_specifications',
                    'children',
                    'tech__4',
                    'children',
                    'tech__4__5',
                    'children',
                    'tech_qg_36',
                    'children',
                    'tech_input_est_specify',
                    'value',
                    'value',
                ]
            }, {
                'path': [
                    'section_specifications',
                    'children',
                    'tech__4',
                    'children',
                    'tech__4__5',
                    'children',
                    'tech_qg_36',
                    'children',
                    'tech_input_est_unit',
                    'value',
                    'value',
                ]
            }],
            'name':
            'costs_establishment_labour'
        }]
    }
    eqd = QcatDataCsv(arguments=Mock(), config_object=get_config_class(config))
    mock_query = Mock()
    mock_query.return_value = api_details_1
    eqd._query = mock_query
    list_entry = api_list_1['results'][0]
    attrs = eqd.extract_attributes(list_entry)
    assert attrs == {
        'code':
        list_entry['code'],
        'updated':
        list_entry['updated'],
        'url':
        get_full_url(list_entry['url']),
        'costs_establishment_labour':
        'Demarcation of the contour lines|ha#'
        'Excavation of the trenches and '
        'construction of the soil bunds|'
        'persons/day/ha',
    }
コード例 #6
0
ファイル: test_base.py プロジェクト: viro76/qcat-api-scripts
def test_extract_path_int():
    eqd = QcatDataCsv(arguments=Mock(), config_object=Mock())
    data = {
        'key_1': {
            'value': [{
                'value': 123,
            }]
        }
    }
    path = ['key_1', 'value', 'value']
    data = eqd.extract_attributes_by_path(data, path)
    assert data == [['123']]
コード例 #7
0
ファイル: test_base.py プロジェクト: viro76/qcat-api-scripts
def test_extract_path_checkbox():
    eqd = QcatDataCsv(arguments=Mock(), config_object=Mock())
    data = {
        'key_1': {
            'value': [{
                'values': ['Value 1', 'Value 2'],
            }]
        }
    }
    path = ['key_1', 'value', 'values']
    data = eqd.extract_attributes_by_path(data, path)
    assert data == [['Value 1', 'Value 2']]
コード例 #8
0
ファイル: test_base.py プロジェクト: viro76/qcat-api-scripts
def test_extract_grouped_attributes_empty(api_details_2):
    config = {
        'qcat_attributes': [{
            'grouped': [{
                'path': [
                    'section_specifications',
                    'children',
                    'tech__4',
                    'children',
                    'tech__4__5',
                    'children',
                    'tech_qg_36',
                    'children',
                    'tech_input_est_specify',
                    'value',
                    'value',
                ]
            }, {
                'path': [
                    'section_specifications',
                    'children',
                    'tech__4',
                    'children',
                    'tech__4__5',
                    'children',
                    'tech_qg_36',
                    'children',
                    'tech_input_est_unit',
                    'value',
                    'value',
                ]
            }],
            'name':
            'costs_establishment_labour'
        }]
    }
    eqd = QcatDataCsv(arguments=Mock(), config_object=get_config_class(config))
    mock_query = Mock()
    mock_query.return_value = api_details_2
    eqd._query = mock_query
    list_entry = {
        'code': '',
        'updated': '',
        'url': '',
    }
    attrs = eqd.extract_attributes(list_entry)
    assert attrs == {
        'code': list_entry['code'],
        'updated': list_entry['updated'],
        'url': get_full_url(list_entry['url']),
        'costs_establishment_labour': '',
    }
コード例 #9
0
ファイル: test_base.py プロジェクト: viro76/qcat-api-scripts
def test_extract_path_repeating():
    eqd = QcatDataCsv(arguments=Mock(), config_object=Mock())
    data = {
        'key_1': {
            'value': [{
                'value': 'Value 1',
            }, {
                'value': 'Value 2'
            }]
        }
    }
    path = ['key_1', 'value', 'value']
    data = eqd.extract_attributes_by_path(data, path)
    assert data == [['Value 1'], ['Value 2']]
コード例 #10
0
ファイル: test_base.py プロジェクト: viro76/qcat-api-scripts
def test_extract_checkbox_repeating_question(api_details_2, api_list_1):
    config = {
        'qcat_attributes': [{
            'path': [
                'section_specifications',
                'children',
                'tech__3',
                'children',
                'tech__3__1',
                'children',
                'tech_qg_6',
                'children',
                'tech_main_purpose',
                'value',
                'values',
            ],
            'name':
            'purpose'
        }]
    }
    eqd = QcatDataCsv(arguments=Mock(), config_object=get_config_class(config))
    mock_query = Mock()
    mock_query.return_value = api_details_2
    eqd._query = mock_query
    list_entry = api_list_1['results'][0]
    attrs = eqd.extract_attributes(list_entry)
    assert attrs == {
        'code':
        list_entry['code'],
        'updated':
        list_entry['updated'],
        'url':
        get_full_url(list_entry['url']),
        'purpose':
        'protect a watershed/ downstream areas – in combination '
        'with other Technologies#reduce risk of disasters',
    }
コード例 #11
0
ファイル: test_base.py プロジェクト: viro76/qcat-api-scripts
def test_extract_path_none():
    eqd = QcatDataCsv(arguments=Mock(), config_object=Mock())
    data = {}
    path = ['key_1', 'value', 'value']
    data = eqd.extract_attributes_by_path(data, path)
    assert data == [[]]
コード例 #12
0
ファイル: test_base.py プロジェクト: viro76/qcat-api-scripts
def test_accepts_config_object():
    args = Mock()
    config_object = Mock()
    eqd = QcatDataCsv(arguments=args, config_object=config_object)
    assert eqd.config == config_object
コード例 #13
0
ファイル: test_base.py プロジェクト: viro76/qcat-api-scripts
def test_invalid_config_file():
    args = Mock(config_file=__file__)
    with pytest.raises(SystemExit):
        QcatDataCsv(args)
コード例 #14
0
ファイル: test_base.py プロジェクト: viro76/qcat-api-scripts
def test_no_config_file():
    args = Mock(config_file='foo.py')
    with pytest.raises(SystemExit):
        QcatDataCsv(args)
コード例 #15
0
from scripts.base import ArgParser
from scripts.data_csv import QcatDataCsv

if __name__ == '__main__':

    desc = 'Extract data from the QCAT API and save it as a CSV file. A ' \
           'configuration file is required.'

    args = ArgParser(description=desc).parse()

    QcatDataCsv(args).handle()