def test_follow_returns_new_value():
    municipality = factories.MunicipalityFactory(insee="12345")
    municipality.insee = "54321"
    municipality.increment_version()
    municipality.save()
    assert Redirect.select().count() == 1
    assert Redirect.follow("municipality", "insee", "12345") == [municipality.id]
Example #2
0
def test_can_delete_redirect_with_other_identifier(client):
    municipality = factories.MunicipalityFactory(insee='54321')
    Redirect.add(identifier='insee', value='12345', instance=municipality)
    assert Redirect.select().count()
    resp = client.delete('/municipality/insee:54321/redirects/insee:12345')
    assert resp.status_code == 204
    assert not Redirect.select().count()
def test_can_delete_redirect_with_other_identifier(client):
    municipality = factories.MunicipalityFactory(insee='54321')
    Redirect.add(identifier='insee', value='12345', instance=municipality)
    assert Redirect.select().count()
    resp = client.delete('/municipality/insee:54321/redirects/insee:12345')
    assert resp.status_code == 204
    assert not Redirect.select().count()
Example #4
0
def test_should_not_be_deleted_if_instance_remove_fails():
    housenumber = factories.HouseNumberFactory()
    position = factories.PositionFactory(housenumber=housenumber)
    Redirect.add(position, 'pk', 32)
    assert Redirect.select().count() == 1
    with pytest.raises(peewee.IntegrityError):
        housenumber.delete_instance()
    assert Redirect.select().count() == 1
def test_should_not_be_deleted_if_instance_remove_fails():
    housenumber = factories.HouseNumberFactory()
    position = factories.PositionFactory(housenumber=housenumber)
    Redirect.add(position, "pk", 32)
    assert Redirect.select().count() == 1
    with pytest.raises(peewee.IntegrityError):
        housenumber.delete_instance()
    assert Redirect.select().count() == 1
Example #6
0
def test_follow_returns_new_value():
    municipality = factories.MunicipalityFactory(insee="12345")
    municipality.insee = '54321'
    municipality.increment_version()
    municipality.save()
    assert Redirect.select().count() == 1
    assert Redirect.follow('municipality', 'insee', '12345') == [
        municipality.id]
Example #7
0
def test_can_create_redirect_with_other_identifier(client):
    municipality = factories.MunicipalityFactory(insee='54321')
    resp = client.put('/municipality/insee:54321/redirects/insee:12345')
    assert resp.status_code == 201
    assert Redirect.select().count()
    redirect = Redirect.first()
    assert redirect.identifier == 'insee'
    assert redirect.value == '12345'
    assert redirect.model_id == municipality.id
def test_can_create_redirect_with_other_identifier(client):
    municipality = factories.MunicipalityFactory(insee='54321')
    resp = client.put('/municipality/insee:54321/redirects/insee:12345')
    assert resp.status_code == 201
    assert Redirect.select().count()
    redirect = Redirect.first()
    assert redirect.identifier == 'insee'
    assert redirect.value == '12345'
    assert redirect.model_id == municipality.id
def test_resource_update_creates_redirect_if_some_identifier_changed():
    municipality = factories.MunicipalityFactory(insee="12345")
    municipality.insee = "54321"
    municipality.increment_version()
    municipality.save()
    assert Redirect.select().count() == 1
    redirect = Redirect.first()
    assert redirect.model_name == "municipality"
    assert redirect.identifier == "insee"
    assert redirect.value == "12345"
    assert redirect.model_id == municipality.id
Example #10
0
def test_resource_update_creates_redirect_if_some_identifier_changed():
    municipality = factories.MunicipalityFactory(insee="12345")
    municipality.insee = '54321'
    municipality.increment_version()
    municipality.save()
    assert Redirect.select().count() == 1
    redirect = Redirect.first()
    assert redirect.model_name == 'municipality'
    assert redirect.identifier == 'insee'
    assert redirect.value == '12345'
    assert redirect.model_id == municipality.id
def test_multiple_redirects_should_return_300(get):
    municipality1 = MunicipalityFactory(insee="23456")
    municipality2 = MunicipalityFactory(insee="34567")
    # Let's say this municipality has been split.
    Redirect.add(municipality1, 'insee', '12345')
    Redirect.add(municipality2, 'insee', '12345')
    resp = get('/municipality/insee:12345')
    assert resp.status_code == 300
    assert '/municipality/{}'.format(municipality1.id) in resp.headers['Link']
    assert '/municipality/{}'.format(municipality1.id) in resp.json['choices']
    assert '/municipality/{}'.format(municipality2.id) in resp.headers['Link']
    assert '/municipality/{}'.format(municipality2.id) in resp.json['choices']
Example #12
0
def test_multiple_redirects_should_return_300(get):
    municipality1 = MunicipalityFactory(insee="23456")
    municipality2 = MunicipalityFactory(insee="34567")
    # Let's say this municipality has been split.
    Redirect.add(municipality1, 'insee', '12345')
    Redirect.add(municipality2, 'insee', '12345')
    resp = get('/municipality/insee:12345')
    assert resp.status_code == 300
    assert '/municipality/{}'.format(municipality1.id) in resp.headers['Link']
    assert '/municipality/{}'.format(municipality1.id) in resp.json['choices']
    assert '/municipality/{}'.format(municipality2.id) in resp.headers['Link']
    assert '/municipality/{}'.format(municipality2.id) in resp.json['choices']
Example #13
0
def test_cannot_duplicate_redirection():
    position = factories.PositionFactory()
    Redirect.add(position, "pk", "939")
    assert Redirect.select().count() == 1
    Redirect.add(position, "pk", "939")
    assert Redirect.select().count() == 1
    assert Redirect.follow("Position", "pk", "939") == [position.id]
Example #14
0
def test_cannot_duplicate_redirection():
    position = factories.PositionFactory()
    Redirect.add(position, 'pk', '939')
    assert Redirect.select().count() == 1
    Redirect.add(position, 'pk', '939')
    assert Redirect.select().count() == 1
    assert Redirect.follow('Position', 'pk', '939') == [position.id]
Example #15
0
def test_can_create_multiple_redirections():
    position1 = factories.PositionFactory()
    position2 = factories.PositionFactory()
    Redirect.add(position1, 'pk', '939')
    assert Redirect.select().count() == 1
    Redirect.add(position2, 'pk', '939')
    assert Redirect.select().count() == 2
    redirects = Redirect.follow('Position', 'pk', '939')
    assert position1.id in redirects
    assert position2.id in redirects
Example #16
0
def test_can_create_multiple_redirections():
    position1 = factories.PositionFactory()
    position2 = factories.PositionFactory()
    Redirect.add(position1, "pk", "939")
    assert Redirect.select().count() == 1
    Redirect.add(position2, "pk", "939")
    assert Redirect.select().count() == 2
    redirects = Redirect.follow("Position", "pk", "939")
    assert position1.id in redirects
    assert position2.id in redirects
Example #17
0
def test_resource_update_should_propagate_if_target_is_becomming_source():
    municipality = factories.MunicipalityFactory(insee="12345")
    municipality.insee = "54321"
    municipality.increment_version()
    municipality.save()
    assert Redirect.select().count() == 1
    assert Redirect.follow("municipality", "insee", "12345") == [municipality.id]
    municipality2 = factories.MunicipalityFactory(insee="12321")
    # Should also update '12345'
    Redirect.add(municipality2, "insee", "54321")
    municipality.delete_instance()
    assert Redirect.select().count() == 2
    assert Redirect.follow("municipality", "insee", "54321") == [municipality2.id]
    assert Redirect.follow("municipality", "insee", "12345") == [municipality2.id]
Example #18
0
def test_resource_update_should_propagate_if_target_is_becomming_source():
    municipality = factories.MunicipalityFactory(insee='12345')
    municipality.insee = '54321'
    municipality.increment_version()
    municipality.save()
    assert Redirect.select().count() == 1
    assert Redirect.follow('municipality', 'insee', '12345') == [
        municipality.id]
    municipality2 = factories.MunicipalityFactory(insee='12321')
    # Should also update '12345'
    Redirect.add(municipality2, 'insee', '54321')
    municipality.delete_instance()
    assert Redirect.select().count() == 2
    assert Redirect.follow('municipality', 'insee', '54321') == [
        municipality2.id]
    assert Redirect.follow('municipality', 'insee', '12345') == [
        municipality2.id]
Example #19
0
def test_cannot_create_cyclic_redirect():
    position = factories.PositionFactory(ign='123456789')
    with pytest.raises(ValueError):
        Redirect.add(position, 'ign', '123456789')
    assert not Redirect.select().count()
Example #20
0
def test_cannot_create_redirect_with_invalid_identifier():
    position = factories.PositionFactory()
    with pytest.raises(ValueError):
        Redirect.add(position, 'invalid', '939')
    assert not Redirect.select().count()
Example #21
0
def test_resource_create_does_not_create_redirect():
    factories.PositionFactory()
    assert not Redirect.select().count()
Example #22
0
def test_cannot_create_cyclic_redirect():
    position = factories.PositionFactory(ign="123456789")
    with pytest.raises(ValueError):
        Redirect.add(position, "ign", "123456789")
    assert not Redirect.select().count()
Example #23
0
def test_can_list_redirects(client):
    municipality = factories.MunicipalityFactory(insee='54321')
    Redirect.add(identifier='insee', value='12345', instance=municipality)
    resp = client.get('/municipality/insee:54321/redirects')
    assert resp.status_code == 200
    assert resp.json['collection'] == ['insee:12345']
Example #24
0
def test_cannot_create_redirect_if_not_authorized(client):
    factories.MunicipalityFactory(insee='54321')
    resp = client.put('/municipality/insee:54321/redirects/insee:12345')
    assert resp.status_code == 401
    assert not Redirect.select().count()
Example #25
0
def test_can_point_from_an_identifier_to_another():
    municipality = factories.MunicipalityFactory()
    Redirect.add(municipality, 'insee', '12345')
    assert Redirect.select().count() == 1
    assert Redirect.follow('municipality', 'insee', '12345') == [
        municipality.id]
Example #26
0
def test_resource_create_does_not_create_redirect():
    factories.PositionFactory()
    assert not Redirect.select().count()
Example #27
0
def test_can_remove_a_redirect():
    position = factories.PositionFactory()
    Redirect.add(position, "pk", 32)
    assert Redirect.select().count() == 1
    Redirect.remove(position, "pk", 32)
    assert not Redirect.select().count()
Example #28
0
def test_can_point_from_an_identifier_to_another():
    municipality = factories.MunicipalityFactory()
    Redirect.add(municipality, "insee", "12345")
    assert Redirect.select().count() == 1
    assert Redirect.follow("municipality", "insee", "12345") == [municipality.id]
Example #29
0
def test_can_remove_a_redirect():
    position = factories.PositionFactory()
    Redirect.add(position, 'pk', 32)
    assert Redirect.select().count() == 1
    Redirect.remove(position, 'pk', 32)
    assert not Redirect.select().count()
Example #30
0
def test_deleting_resource_should_remove_redirects_pointing_to_it():
    position = factories.PositionFactory()
    Redirect.add(position, 'pk', 32)
    assert Redirect.select().count() == 1
    position.delete_instance()
    assert not Redirect.select().count()
Example #31
0
def test_can_list_redirects(client):
    municipality = factories.MunicipalityFactory(insee='54321')
    Redirect.add(identifier='insee', value='12345', instance=municipality)
    resp = client.get('/municipality/insee:54321/redirects')
    assert resp.status_code == 200
    assert resp.json['collection'] == ['insee:12345']
Example #32
0
def test_resource_update_does_not_create_redirect_if_no_identifier_changed():
    municipality = factories.MunicipalityFactory(insee="12345")
    municipality.name = 'Another Name'
    municipality.increment_version()
    municipality.save()
    assert not Redirect.select().count()
Example #33
0
def test_cannot_create_redirect_if_not_authorized(client):
    factories.MunicipalityFactory(insee='54321')
    resp = client.put('/municipality/insee:54321/redirects/insee:12345')
    assert resp.status_code == 401
    assert not Redirect.select().count()
Example #34
0
def test_resource_update_does_not_create_redirect_if_no_identifier_changed():
    municipality = factories.MunicipalityFactory(insee="12345")
    municipality.name = "Another Name"
    municipality.increment_version()
    municipality.save()
    assert not Redirect.select().count()
Example #35
0
def test_deleting_resource_should_remove_redirects_pointing_to_it():
    position = factories.PositionFactory()
    Redirect.add(position, "pk", 32)
    assert Redirect.select().count() == 1
    position.delete_instance()
    assert not Redirect.select().count()
Example #36
0
def test_cannot_create_redirect_with_invalid_identifier():
    position = factories.PositionFactory()
    with pytest.raises(ValueError):
        Redirect.add(position, "invalid", "939")
    assert not Redirect.select().count()