def test_get_municipality_versions_by_datetime(get): municipality = MunicipalityFactory(name="Cabour") municipality.version = 2 municipality.name = "Cabour2" municipality.save() # Artificialy change versions periods. period = [datetime(2015, 1, 1), datetime(2016, 1, 1)] Version.update(period=period).where(Version.sequential == 1).execute() period = [datetime(2016, 1, 1), None] Version.update(period=period).where(Version.sequential == 2).execute() # Should work with a simple datetime. resp = get('/municipality/{}/versions/2015-06-01T01:02:03+00:00' .format(municipality.id)) assert resp.status_code == 200 assert resp.json['data']['name'] == 'Cabour' # Should work with a naive datetime too. resp = get('/municipality/{}/versions/2015-06-01 01:02:03' .format(municipality.id)) assert resp.status_code == 200 assert resp.json['data']['name'] == 'Cabour' # Should work with a simple date too. resp = get('/municipality/{}/versions/2015-06-01'.format(municipality.id)) assert resp.status_code == 200 assert resp.json['data']['name'] == 'Cabour' # Now ask in the range of the second version resp = get('/municipality/{}/versions/2016-06-01'.format(municipality.id)) assert resp.status_code == 200 assert resp.json['data']['name'] == 'Cabour2' # Asking for the common bound should return the new version. resp = get('/municipality/{}/versions/2016-01-01 00:00:00' .format(municipality.id)) assert resp.status_code == 200 assert resp.json['data']['name'] == 'Cabour2'
def test_get_municipality_versions_by_datetime(get): municipality = MunicipalityFactory(name="Cabour") municipality.version = 2 municipality.name = "Cabour2" municipality.save() # Artificialy change versions periods. period = [datetime(2015, 1, 1), datetime(2016, 1, 1)] Version.update(period=period).where(Version.sequential == 1).execute() period = [datetime(2016, 1, 1), None] Version.update(period=period).where(Version.sequential == 2).execute() # Should work with a simple datetime. resp = get('/municipality/{}/versions/2015-06-01T01:02:03+00:00'.format( municipality.id)) assert resp.status_code == 200 assert resp.json['data']['name'] == 'Cabour' # Should work with a naive datetime too. resp = get('/municipality/{}/versions/2015-06-01 01:02:03'.format( municipality.id)) assert resp.status_code == 200 assert resp.json['data']['name'] == 'Cabour' # Should work with a simple date too. resp = get('/municipality/{}/versions/2015-06-01'.format(municipality.id)) assert resp.status_code == 200 assert resp.json['data']['name'] == 'Cabour' # Now ask in the range of the second version resp = get('/municipality/{}/versions/2016-06-01'.format(municipality.id)) assert resp.status_code == 200 assert resp.json['data']['name'] == 'Cabour2' # Asking for the common bound should return the new version. resp = get('/municipality/{}/versions/2016-01-01 00:00:00'.format( municipality.id)) assert resp.status_code == 200 assert resp.json['data']['name'] == 'Cabour2'
def test_save_should_be_rollbacked_if_version_save_fails(): municipality = MunicipalityFactory() assert Version.select().count() == 1 # Artificially create a version municipality.increment_version() municipality.store_version() assert Version.select().count() == 2 with pytest.raises(peewee.IntegrityError): municipality.save() assert Version.select().count() == 2 models.Municipality.select().count() == 1