def test_assign_to_state_territory_color_were_preserved(): state1 = State.from_dict({ 'validity_start': '1918-01-01T00:00:00Z', 'validity_end': '1919-01-01T00:00:00Z', 'representations': [ { 'name': 'test ' + datetime.now().isoformat() + str(randint(1, 1e6)), 'validity_start': '1918-01-01T00:00:00Z', 'validity_end': '1919-01-01T00:00:00Z', 'color' : '#FF0000' # red } ] }) state2 = State.from_dict({ 'validity_start': '1918-02-01T00:00:00Z', 'validity_end': '1918-12-01T00:00:00Z', 'representations': [ { 'name': 'test ' + datetime.now().isoformat() + str(randint(1, 1e6)), 'validity_start': '1918-02-01T00:00:00Z', 'validity_end': '1918-12-01T00:00:00Z', 'color' : '#0000FF' #blue } ] }) # territory exists in jan and dec 1918 and state2 does not territory = get_example_territory() territory.color = None # will be red at first with get_api_with_state_and_territory((state1, territory), (state2, None)) as [(_, territory_with_id), (state2_w_id, _)]: with get_cursor() as cursor : created_ids = TerritoryTag._assign_to_state(cursor, territory_with_id, state2_w_id.state_id) # The reassigned territory was set to red (so the assign did not change the color) assert TerritoryTag.get(territory_with_id.territory_id).color=='#FF0000' assert all(TerritoryTag.get(created_id).color == None for created_id in created_ids)
def test_assign_to_state_with_unmatching_period(): state1 = State.from_dict({ 'validity_start': '1918-01-01T00:00:00Z', 'validity_end': '1919-01-01T00:00:00Z', 'representations': [ { 'name': 'test ' + datetime.now().isoformat() + str(randint(1, 1e6)), 'validity_start': '1918-01-01T00:00:00Z', 'validity_end': '1919-01-01T00:00:00Z', 'color' : '#000000' } ] }) state2 = State.from_dict({ 'validity_start': '1919-01-01T00:00:00Z', 'validity_end': '1920-01-01T00:00:00Z', 'representations': [ { 'name': 'test ' + datetime.now().isoformat() + str(randint(1, 1e6)), 'validity_start': '1919-01-01T00:00:00Z', 'validity_end': '1920-01-01T00:00:00Z', 'color' : '#000000' } ] }) territory = get_example_territory() # state2 and territory have no period in common with get_api_with_state_and_territory((state1, territory), (state2, None)) as [(_, territory_with_id), (state2_w_id, _)]: with pytest.raises(BadRequest): with get_cursor() as cursor : TerritoryTag._assign_to_state(cursor, territory_with_id, state2_w_id.state_id)
def test_assign_to_state_territory_starts_before_new_state(): state1 = State.from_dict({ 'validity_start': '1918-01-01T00:00:00Z', 'validity_end': '1919-01-01T00:00:00Z', 'representations': [ { 'name': 'test ' + datetime.now().isoformat() + str(randint(1, 1e6)), 'validity_start': '1918-01-01T00:00:00Z', 'validity_end': '1919-01-01T00:00:00Z', 'color' : '#000000' } ] }) state2 = State.from_dict({ 'validity_start': '1918-02-01T00:00:00Z', 'validity_end': '1919-01-01T00:00:00Z', 'representations': [ { 'name': 'test ' + datetime.now().isoformat() + str(randint(1, 1e6)), 'validity_start': '1918-02-01T00:00:00Z', 'validity_end': '1919-01-01T00:00:00Z', 'color' : '#000000' } ] }) # territory exists in jan 1918 and state2 does not territory = get_example_territory() with get_api_with_state_and_territory((state1, territory), (state2, None)) as [(state1_with_id, territory_with_id), (state2_w_id, _)]: territory_count = TerritoryTag.count() with get_cursor() as cursor : created_ids = TerritoryTag._assign_to_state(cursor, territory_with_id, state2_w_id.state_id) # a territory was created for jan 1918 and it still belongs to state1 assert TerritoryTag.count() == territory_count + 1 assert len(created_ids) == 1 assert TerritoryTag.get(created_ids[0]).state_id == state1_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_example_state(): return State.from_dict({ 'validity_start': '1918-01-01T00:00:00Z', 'validity_end': '1919-01-01T00:00:00Z', 'representations': [ { 'name': 'test ' + datetime.now().isoformat() + str(randint(1, 1e6)), 'validity_start': '1918-01-01T00:00:00Z', 'validity_end': '1919-01-01T00:00:00Z', 'color' : '#000000' } ] })
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_get_by_name_several_matches(): france_2001_2003 = State.from_dict({ 'validity_start': year_to_date(2001).isoformat(), 'validity_end': year_to_date(2003).isoformat(), 'representations': [ { 'name': 'France', 'validity_start': year_to_date(2001).isoformat(), 'validity_end': year_to_date(2003).isoformat(), 'color': '#000000' }, ] }) with get_db_with_states_cursor(france_2012_2018, france_2001_2003) as cursor: res = StateCRUD.get_by_name(cursor, 'frAnce', year_to_date(2002), year_to_date(2015)) assert len(res) >= 2 assert any(st.equals(france_2012_2018) for st in res) assert any(st.equals(france_2001_2003) for st in res)
def post_state(): return {"added_state": StateTag.post(State.from_dict(request.json))}
from resources.BoundingBox import BoundingBox from werkzeug.exceptions import Conflict import copy import contextlib from datetime import datetime from dateutil import parser example_state = State.from_dict({ 'validity_start': '1919-01-24T00:23:00Z', 'validity_end': '1924-01-01T00:00:00Z', 'representations': [{ 'name': 'Austria', 'validity_start': '1919-01-24T00:23:00Z', 'validity_end': '1920-01-01T00:00:00Z', 'color': '#000000' }, { 'name': '', 'validity_start': '1920-01-01T00:00:00Z', 'validity_end': '1924-01-01T00:00:00Z', 'color': '#000000' }] }) def test_get_state_not_found(): with get_cursor() as curs: with pytest.raises(NotFound): StateCRUD.get(curs, -12)
return parser.parse(f'{y}-01-01T00:00:00Z') france_1912_2018 = State.from_dict({ 'validity_start': year_to_date(1912).isoformat(), 'validity_end': year_to_date(2018).isoformat(), 'representations': [ { 'name': 'France', 'validity_start': year_to_date(1912).isoformat(), 'validity_end': '2015-02-02T00:00:00Z', 'color': '#000000' }, { 'name': 'france with other name', 'validity_start': '2015-02-02T00:00:00Z', 'validity_end': '2015-02-03T00:00:00Z', 'color': '#000000' }, { 'name': 'France', 'validity_start': '2015-02-03T00:00:00Z', 'validity_end': year_to_date(2018).isoformat(), 'color': '#000000' }, ] }) @contextlib.contextmanager