def test_redirect(session, monkeypatch):
    monkeypatch.setattr('ban.commands.helpers.confirm', lambda *x, **wk: True)
    mun1 = factories.MunicipalityFactory(insee='33001', name='Mun1')
    mun2 = factories.MunicipalityFactory(insee='33002', name='Mun2')
    merge(mun1.insee, sources=[mun2.insee], name='Toto', label='TOTO')
    assert versioning.Redirect.select().count() == 2
    assert versioning.Redirect.follow('Municipality', 'insee',
                                      '33002') == [mun1.id]
def test_modify_group_municipality(session, monkeypatch):
    monkeypatch.setattr('ban.commands.helpers.confirm', lambda *x, **wk: True)
    mun1 = factories.MunicipalityFactory(insee='33001', name='Mun1')
    mun2 = factories.MunicipalityFactory(insee='33002', name='Mun2')
    gr = factories.GroupFactory(municipality=mun2, name='GrToto')
    merge(mun1.insee, sources=[mun2.insee], name='Toto', label='TOTO')
    gr = models.Group.select().where(models.Group.name == 'GrToto').first()
    assert gr.municipality == mun1
def test_delete_source(session, monkeypatch):
    monkeypatch.setattr('ban.commands.helpers.confirm', lambda *x, **wk: True)
    mun1 = factories.MunicipalityFactory(insee='33001', name='Mun1')
    mun2 = factories.MunicipalityFactory(insee='33002', name='Mun2')
    merge(mun1.insee, sources=[mun2.insee], name='Toto', label='TOTO')
    mun = models.Municipality.select().where(
        models.Municipality.insee == '33002').first()
    assert mun is None
def test_create_group_area(session, monkeypatch):
    monkeypatch.setattr('ban.commands.helpers.confirm', lambda *x, **wk: True)
    mun1 = factories.MunicipalityFactory(insee='33001', name='Mun1')
    mun2 = factories.MunicipalityFactory(insee='33002', name='Mun2')
    merge(mun1.insee, sources=[mun2.insee], name='Toto', label='TOTO')
    gr = models.Group.select().where(models.Group.name == mun2.name).first()
    assert gr.municipality == mun1
    assert gr.version == 1
    assert gr.attributes == {'insee': mun2.insee}
def test_no_label_is_aborted():
    factories.MunicipalityFactory(insee='33001', name='Mun1')
    factories.MunicipalityFactory(insee='33002', name='Mun2')
    with pytest.raises(SystemExit):
        merge('33001', sources=['33002'], name='Toto')
    mun = models.Municipality.get(models.Municipality.insee == '33001')
    assert mun.version == 1
    mun = models.Municipality.get(models.Municipality.insee == '33002')
    assert mun.version == 1
def test_modify_postcode_without_complement(session, monkeypatch):
    monkeypatch.setattr('ban.commands.helpers.confirm', lambda *x, **wk: True)
    mun1 = factories.MunicipalityFactory(insee='33001', name='Mun1')
    mun2 = factories.MunicipalityFactory(insee='33002', name='Mun2')
    pc = factories.PostCodeFactory(municipality=mun2, name='PC2', code='33002')
    merge(mun1.insee, sources=[mun2.insee], name='Toto', label='TOTO')
    pc = models.PostCode.select().where(
        models.PostCode.code == '33002').first()
    assert pc.municipality == mun1
    assert pc.complement == 'PC2'
    assert pc.name == 'TOTO'
def test_double_source_process_once(session, monkeypatch):
    monkeypatch.setattr('ban.commands.helpers.confirm', lambda *x, **wk: True)
    mun1 = factories.MunicipalityFactory(insee='33001', name='Mun1')
    mun2 = factories.MunicipalityFactory(insee='33002', name='Mun2')
    merge(mun1.insee,
          sources=[mun2.insee, mun2.insee],
          name='Toto',
          label='TOTO')
    gr = models.Group.select().where(models.Group.name == mun1.name)
    assert len(gr) == 1
    assert gr[0].version == 1
def test_modify_housenumber_ancestors(session, monkeypatch):
    monkeypatch.setattr('ban.commands.helpers.confirm', lambda *x, **wk: True)
    mun1 = factories.MunicipalityFactory(insee='33001', name='Mun1')
    mun2 = factories.MunicipalityFactory(insee='33002', name='Mun2')
    gr = factories.GroupFactory(municipality=mun2, name='GrToto')
    hn = factories.HouseNumberFactory(parent=gr, number='1')
    merge(mun1.insee, sources=[mun2.insee], name='Toto', label='TOTO')
    hn = models.HouseNumber.select().where(
        models.HouseNumber.number == '1').first()
    gr_area = models.Group.select().where(
        models.Group.name == mun2.name).first()
    assert hn.ancestors == gr_area
def test_unknown_destination_is_aborted():
    factories.MunicipalityFactory(insee='33001', name='Mun')
    with pytest.raises(SystemExit):
        merge('12345', sources=['33001'], name='Toto', label='TOTO')
    mun = models.Municipality.get(models.Municipality.insee == '33001')
    assert mun.version == 1
def test_no_sources_is_aborted():
    factories.MunicipalityFactory(insee='33001', name='Mun')
    with pytest.raises(SystemExit):
        merge('33001')