示例#1
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)
示例#2
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)
示例#3
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