def test_post_territory_nominal(): state_id = StateTag.post(get_example_state()) example_territory = get_example_territory() example_territory.state_id = state_id territory_id = TerritoryTag.post(example_territory) assert isinstance(territory_id, int) retrieved = TerritoryTag.get(territory_id) assert len(retrieved.representations) > 1 TerritoryTag.delete(territory_id) StateTag.delete(state_id)
def test_merge_conflict_date_overflows(): #1918-1919 sovereign = get_example_state() #1917-1919 to_merge = get_example_state() to_merge.validity_start = to_merge.validity_start.replace(year=1917) with get_api_with_state_and_territory((sovereign, None), (to_merge,None)) as ((sov_with_id, _), (to_merge_with_id, _)): with pytest.raises(Conflict): StateTag.merge(to_merge_with_id.state_id, sov_with_id.state_id)
def test_post_territory_state_conflict_on_period(): example_state = get_example_state() state_id = StateTag.post(example_state) example_territory = get_example_territory() example_territory.validity_end = example_territory.validity_end.replace( year=example_state.validity_end.year+1) example_territory.state_id = state_id logging.warning(f'About to put a territory ending in {example_territory.validity_end} to a state ending in {example_state.validity_end}') with pytest.raises(Conflict): TerritoryTag.post(example_territory) StateTag.delete(state_id)
def test_merge_ok(): #1918-1919 sovereign = get_example_state() #1918-1919 to_merge = get_example_state() t = get_example_territory() with get_api_with_state_and_territory((sovereign, None), (to_merge,t)) as ((sov_with_id, _), (to_merge_with_id, t_with_id)): StateTag.merge(to_merge_with_id.state_id, sov_with_id.state_id) assert TerritoryTag.get(t_with_id.territory_id).state_id == sov_with_id.state_id with pytest.raises(NotFound) : StateTag.get(to_merge_with_id.state_id)
def put_state(): absorb = request.args.get('absorb_conflicts') return { "modified_state": StateTag.put(State.from_dict(request.json), absorb == 'True' or absorb == 'true') }
def get_api_with_state_and_territory(*states_and_territories): if not len(states_and_territories): states_and_territories = [(get_example_state(), get_example_territory())] for (state, territory) in states_and_territories : state_id = StateTag.post(state) state.state_id = state_id if territory: territory.state_id = state_id territory_id = TerritoryTag.post(territory) territory.territory_id = territory_id try : yield states_and_territories finally: for state, _ in states_and_territories : StateTag.delete(state.state_id)
def test_add_state_empty_name_no_conflict(): to_add = State.from_dict({ 'validity_start' : '1919-01-24T00:23:00Z', 'validity_end' : '1920-01-01T00:00:00Z', 'representations' : [ { 'name' : '', 'validity_start' : '1919-01-24T00:23:00Z', 'color' : '#000000', 'validity_end' : '1920-01-01T00:00:00Z', } ] }) StateTag.post(to_add) # should not raise errors StateTag.post(to_add)
def test_add_state(): to_add = State.from_dict({ 'validity_start' : '1919-01-24T00:23:00Z', 'validity_end' : '1920-01-01T00:00:00Z', 'representations' : [ { 'name' : 'test ' + datetime.now().isoformat(), 'validity_start' : '1919-01-24T00:23:00Z', 'validity_end' : '1920-01-01T00:00:00Z', 'color' : '#000000' } ] }) state_id = StateTag.post(to_add) assert isinstance(state_id, int) with pytest.raises(Conflict): StateTag.post(to_add)
def test_add_and_get_state() : to_add = State.from_dict({ 'validity_start' : '1919-01-24T00:23:00Z', 'validity_end' : '1920-01-01T00:00:00Z', 'representations' : [ { 'name' : '', 'validity_start' : '1919-01-24T00:23:00Z', 'validity_end' : '1920-01-01T00:00:00Z', 'color' : '#000000' } ] }) st_id = StateTag.post(to_add) state = StateTag.get(st_id) assert isinstance(state, State) assert state.state_id==st_id assert state.validity_start==to_add.validity_start
def test_merge_colors_were_set_from_state(): sovereign = get_example_state() sovereign.representations[0].color='#00000' #black to_merge = get_example_state() to_merge.representations[0].color = '#FF0000' #red t = get_example_territory() t.color = None with get_api_with_state_and_territory((sovereign, None), (to_merge,t)) as ((sov_with_id, _), (to_merge_with_id, t_with_id)): # Territory has the color of its state : red assert t_with_id.color==None #Merge checks the color so they remain consistent StateTag.merge(to_merge_with_id.state_id, sov_with_id.state_id) t_merged = TerritoryTag.get(t_with_id.territory_id) # merge ensured territory remained blue even if its parent is now black assert t_merged.color == '#FF0000'
def search_state(): pattern = request.args.get('pattern') if not pattern or not pattern.strip(): raise BadRequest('Empty pattern to search') return {"search_results": StateTag.search(pattern.strip())}
def get_state(state_id): return StateTag.get(state_id).to_dict()
def merge_states(state_id, sovereign_state_id): return {"merged_into": StateTag.merge(state_id, sovereign_state_id)}
def post_state(): return {"added_state": StateTag.post(State.from_dict(request.json))}