Example #1
0
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)
Example #2
0
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)
Example #3
0
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
Example #4
0
def put_state():
    absorb = request.args.get('absorb_conflicts')
    return {
        "modified_state":
        StateTag.put(State.from_dict(request.json), absorb == 'True'
                     or absorb == 'true')
    }
Example #5
0
    def get_many(cursor, state_ids):
        if len(state_ids) == 0:
            return []
        cursor.execute(
            '''
            SELECT 
                states.state_id, states.validity_start, states.validity_end,  
                state_names.name, state_names.validity_start, state_names.validity_end, state_names.color
            FROM states
                INNER JOIN state_names ON state_names.state_id=states.state_id
            WHERE 
                states.state_id IN %s 
            ORDER BY states.state_id,  state_names.validity_start

        ''', (tuple(state_ids), ))
        records = cursor.fetchall()
        current_state_id = None
        res = []
        for row in records:
            (state_id, validity_start, validity_end, name, name_validity_start,
             name_validity_end, color) = row
            representation = StateRepresentation(name, name_validity_start,
                                                 name_validity_end, color)
            if current_state_id != state_id:
                current_state = State(state_id,
                                      validity_start=validity_start,
                                      validity_end=validity_end,
                                      representations=[representation])
                current_state_id = state_id
                res.append(current_state)
            else:
                current_state.representations.append(representation)
        return res
Example #6
0
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'
            }
        ]
    })
Example #7
0
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)
Example #8
0
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)
Example #9
0
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
Example #10
0
 def get(cursor, id):
     assert isinstance(id, int)
     cursor.execute(
         '''
         SELECT states.validity_start, states.validity_end,  state_names.name, state_names.validity_start, state_names.validity_end, state_names.color
         FROM states
             INNER JOIN state_names ON state_names.state_id=states.state_id
         WHERE 
             states.state_id=%s 
         ORDER BY state_names.validity_start
     ''', (id, ))
     rows = cursor.fetchall()
     if len(rows) == 0:
         raise NotFound(f'State not found : {id}')
     state_start, state_end = rows[0][0], rows[0][1]
     res = State(id, state_start, state_end, [])
     for row in rows:
         name, start, end, color = row[2], row[3], row[4], row[5]
         res.representations.append(
             StateRepresentation(name, start, end, color))
     return res
Example #11
0
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)
Example #12
0
def post_state():
    return {"added_state": StateTag.post(State.from_dict(request.json))}
Example #13
0
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