Exemple #1
0
def test_location_synchronizer_query_with_retries_exception(mock_send, cartodbtable):
    synchronizer = LocationSynchronizer(pk=cartodbtable.pk)
    mock_send.return_value = {
        'error': 'mocked error response'
    }
    query = " this is a mocked query string"
    with pytest.raises(CartoException):
        synchronizer.query_with_retries(query, 0)
Exemple #2
0
def test_location_synchronizer_get_cartodb_locations_exception(logger_mock, mock_send, cartodbtable):
    synchronizer = LocationSynchronizer(pk=cartodbtable.pk)
    mock_resp = requests.models.Response()
    mock_resp.status_code = 400

    mock_send.return_value = mock_resp
    with pytest.raises(CartoException):
        synchronizer.get_cartodb_locations()
    logger_mock.assert_called_with(f"Cannot fetch pagination prerequisites "
                                   f"from CartoDB for table {cartodbtable.table_name}")
Exemple #3
0
def test_location_synchronizer_clean_upper_level(logger_mock, cartodbtable):
    synchronizer = LocationSynchronizer(pk=cartodbtable.pk)
    location_1 = LocationFactory(p_code='RW', is_active=True)
    location_2 = LocationFactory(
        parent=location_1, p_code='RW01', is_active=False, admin_level=cartodbtable.admin_level - 1)

    synchronizer.clean_upper_level()
    location_1.refresh_from_db()
    assert location_1.is_active
    expected_calls = [
        call(f"Deleting parent {location_2}")]
    logger_mock.assert_has_calls(expected_calls)
Exemple #4
0
def test_location_synchronizer_get_cartodb_locations_failsafe_max(logger_mock, mock_send, cartodbtable):
    synchronizer = LocationSynchronizer(pk=cartodbtable.pk)
    mock_send.return_value = {
        'rows': [
            {
                'count': 3,
                'max': 1200
            }
        ]
    }
    with mock_send:
        rows = synchronizer.get_cartodb_locations()
    assert rows is not None
    logger_mock.assert_called_with("The CartoDB primary key seems off, pagination is not possible")
Exemple #5
0
def test_location_synchronizer_handle_obsolete_locations(logger_mock, cartodbtable):
    synchronizer = LocationSynchronizer(pk=cartodbtable.pk)
    location_1 = LocationFactory(p_code='RW', is_active=True)
    LocationFactory(parent=location_1, p_code='RW01', is_active=True)
    location_2 = LocationFactory(p_code='PER', is_active=True)
    assert location_1.is_active
    assert location_2.is_active

    synchronizer.handle_obsolete_locations(['RW', 'PER'])
    location_1.refresh_from_db()
    assert not location_1.is_active
    expected_calls = [
        call(f"Deactivating {location_1}"),
        call(f"Deleting {location_2}")]
    logger_mock.assert_has_calls(expected_calls)
Exemple #6
0
def test_get_remapping(mock_send, cartodbtable):
    cartodbtable.remap_table_name = 'Remap table name'
    cartodbtable.save(update_fields=['remap_table_name'])
    synchronizer = LocationSynchronizer(pk=cartodbtable.pk)
    mock_send.return_value = {
        'rows': [{
            'old_pcode': 'RWA',
            'new_pcode': 'RW',
            'matching': 1
        }, {
            'old_pcode': 'RW',
            'new_pcode': 'RWA',
            'matching': 1
        }]
    }

    acyclic_dict, to_deactivate = get_remapping(synchronizer.sql_client,
                                                cartodbtable)
    assert acyclic_dict == {
        'RW': 'temp1',
        'RWA': 'temp0',
        'temp0': 'RW',
        'temp1': 'RWA'
    }
    assert not to_deactivate
Exemple #7
0
def test_get_remapping_invalid(mock_send, cartodbtable):
    cartodbtable.remap_table_name = 'Remap table name'
    cartodbtable.save(update_fields=['remap_table_name'])
    synchronizer = LocationSynchronizer(pk=cartodbtable.pk)
    row = {'old_pcode': 'RWA', 'new_pcode': 'RW', 'matching': 1}
    mock_send.return_value = {'rows': [row, row]}
    with pytest.raises(InvalidRemap):
        get_remapping(synchronizer.sql_client, cartodbtable)
Exemple #8
0
def test_location_synchronizer_get_cartodb_locations(mock_send, cartodbtable):
    synchronizer = LocationSynchronizer(pk=cartodbtable.pk)
    mock_send.return_value = {
        'rows': [
            {
                'count': 1,
                'max': 1,
            }],
        'time': 0.053,
        'fields': {
            'count': {'type': 'number', 'pgtype': 'int8'},
            'max': {'type': 'number', 'pgtype': 'int4'}
        },
        'total_rows': 1
    }
    with mock_send:
        rows = synchronizer.get_cartodb_locations()
    assert rows is not None
Exemple #9
0
def test_get_remapping_exception(mock_send, cartodbtable):
    cartodbtable.remap_table_name = 'Remap table name'
    cartodbtable.save(update_fields=['remap_table_name'])
    synchronizer = LocationSynchronizer(pk=cartodbtable.pk)
    mock_resp = requests.models.Response()
    mock_resp.status_code = 400

    mock_send.return_value = mock_resp
    with pytest.raises(CartoException):
        get_remapping(synchronizer.sql_client, cartodbtable)
Exemple #10
0
def test_location_synchronizer_create_or_update_locations(logger_mock, mock_cartodb_locations,
                                                          cartodbtable, carto_response):
    LocationFactory(p_code='RW', is_active=True)
    cartodbtable.parent_code_col = 'parent_code_col'
    cartodbtable.save(update_fields=['parent_code_col'])
    synchronizer = LocationSynchronizer(pk=cartodbtable.pk)
    mock_cartodb_locations.return_value = carto_response

    # test new location
    new, updated, skipped, error = synchronizer.create_or_update_locations()
    assert new == 1
    assert skipped == updated == error == 0

    # test updated location
    new, updated, skipped, error = synchronizer.create_or_update_locations()
    assert updated == 1
    assert new == skipped == error == 0

    # test error: multiple location exception
    LocationFactory(p_code='RW01', is_active=True)
    with pytest.raises(CartoException):
        new, updated, skipped, error = synchronizer.create_or_update_locations()
        assert new == updated == skipped == 0
        logger_mock.assert_called_with(
            f"Multiple locations found for: {cartodbtable.admin_level}, "
            f"{carto_response[0][cartodbtable.name_col]} ({carto_response[0][cartodbtable.pcode_col]})")

    # test skipped location
    mock_cartodb_locations.return_value[0][cartodbtable.pcode_col] = ''
    new, updated, skipped, error = synchronizer.create_or_update_locations()
    assert skipped == 1
    assert new == updated == error == 0
Exemple #11
0
def test_location_synchronizer_sync(logger_mock, mock_cartodb_locations,
                                    cartodbtable, carto_response):
    synchronizer = LocationSynchronizer(pk=cartodbtable.pk)
    location_1 = LocationFactory(p_code='RW', is_active=True)
    location_2 = LocationFactory(
        parent=location_1, p_code='RW01', is_active=False, admin_level=cartodbtable.admin_level - 1)
    mock_cartodb_locations.return_value = carto_response

    # test new and deleted-leaf location
    new, updated, skipped, error = synchronizer.sync()
    assert new == 1
    assert skipped == updated == error == 0
    location_1.refresh_from_db()
    assert location_1.is_active
    expected_calls = [
        call(f"Deleting parent {location_2}")]
    logger_mock.assert_has_calls(expected_calls)

    # test updated location
    new, updated, skipped, error = synchronizer.sync()
    assert updated == 1
    assert new == skipped == error == 0
Exemple #12
0
def import_locations(self, carto_table_pk):
    """Import locations from carto"""
    LocationSynchronizer(carto_table_pk).sync()
Exemple #13
0
def test_location_synchronizer_init(cartodbtable):
    synchronizer = LocationSynchronizer(pk=cartodbtable.pk)
    assert synchronizer.carto is not None
    assert synchronizer.sql_client is not None