Example #1
0
def test_community_model_create(session, problem_name, org_name, geo_name,
                                num_followers):
    """Test simple community model interaction"""
    from intertwine.communities.models import Community
    from intertwine.geos.models import Geo
    from intertwine.problems.models import Problem

    problem = Problem(name=problem_name) if problem_name else None
    org = org_name if org_name else None
    geo = Geo(name=geo_name) if geo_name else None
    community = Community(problem=problem,
                          org=org,
                          geo=geo,
                          num_followers=num_followers)

    session.add(community)
    session.commit()

    assert Community[community.derive_key()] is community
    assert problem is community.problem
    assert org == community.org
    assert geo is community.geo

    community_from_db = Community.query.filter_by(problem=problem,
                                                  org=org,
                                                  geo=geo).one()

    assert community_from_db is community
    assert community_from_db.problem is problem
    assert community_from_db.org == org
    assert community_from_db.geo is geo
    assert community_from_db.num_followers == num_followers
    assert community_from_db.name == problem.name + (
        ' at ' + org_name if org_name else
        '') + (' in ' + geo.display(show_abbrev=False) if geo else '')
Example #2
0
def create_geo_data(session):
    """Util to create geos referenced in problem JSON"""
    assert Geo.query.all() == []
    us = Geo(name='United States', abbrev='U.S.')
    tx = Geo(name='Texas', abbrev='TX', path_parent=us, parents=[us])
    austin = Geo(name='Austin', path_parent=tx, parents=[tx])
    session.add_all((us, tx, austin))
    session.commit()
    geo = Geo.query.filter_by(human_id='us/tx/austin').one()
    assert geo is Geo['us/tx/austin']
Example #3
0
def test_vardygrify(session, problem_name, org_name, geo_name, num_followers):
    """Test vardygrify by comparing vardygr and real communities"""
    from intertwine.communities.models import Community
    from intertwine.geos.models import Geo
    from intertwine.problems.models import Problem
    from intertwine.utils.vardygr import vardygrify

    problem = Problem(name=problem_name) if problem_name else None
    org = org_name if org_name else None
    geo = Geo(name=geo_name) if geo_name else None
    community_kwds = dict(problem=problem, org=org, geo=geo,
                          num_followers=num_followers)
    real_community = Community(**community_kwds)

    session.add(real_community)
    session.commit()

    vardygr_community = vardygrify(Community, **community_kwds)

    # Hide ids, since they will differ
    hide = Community.ID_FIELDS

    real_community_payload = real_community.jsonify(hide=hide)
    vardygr_community_payload = vardygr_community.jsonify(hide=hide)
    assert real_community_payload == vardygr_community_payload

    real_community_json = json.dumps(real_community_payload)
    vardygr_community_json = json.dumps(vardygr_community_payload)
    assert real_community_json == vardygr_community_json
Example #4
0
def test_geo_level_model(session):
    """Test simple geo level model interaction"""
    level = 'place'
    designation = 'city'
    geo = Geo(name='Test Geo Place')
    glvl = GeoLevel(geo=geo, level=level, designation=designation)

    session.add(geo)
    session.add(glvl)
    session.commit()

    assert geo.levels[level] is glvl
    assert glvl.geo is geo
    assert glvl.level == level
    assert glvl.designation == designation

    assert GeoLevel[glvl.derive_key()] is glvl

    glvl_from_db = session.query(GeoLevel).filter(
        GeoLevel.geo == geo, GeoLevel.level == level).first()

    assert glvl_from_db is glvl
    assert glvl_from_db.geo is geo
    assert glvl_from_db.level == level
    assert glvl_from_db.designation == designation
Example #5
0
def test_problem_connection_rating_model(session):
    """Tests simple problem connection rating model interaction"""
    problem_name_base = 'Test Problem'
    problem1 = Problem(problem_name_base + ' 01')
    problem2 = Problem(problem_name_base + ' 02')
    connection = ProblemConnection('causal', problem1, problem2)
    org = None
    geo = Geo('Test Geo')
    rating = ProblemConnectionRating(rating=2,
                                     weight=1,
                                     connection=connection,
                                     problem=problem1,
                                     org=org,
                                     geo=geo,
                                     user='******')

    session.add(geo)
    session.add(problem1)
    session.add(problem2)
    session.add(connection)
    session.add(rating)
    session.commit()
    assert ProblemConnectionRating[rating.derive_key()] is rating
    ratings = session.query(ProblemConnectionRating).all()
    assert len(ratings) == 1
    r = ratings[0]
    assert r is rating
    assert r.problem is problem1
    assert r.connection is connection
    assert r.org == org
    assert r.geo is geo
    assert r.rating == 2
Example #6
0
def test_geo_id_model(session):
    """Test simple geo id model interaction"""
    TEST_STANDARD = 'Test Standard'

    GeoID.STANDARDS.add(TEST_STANDARD)
    standard = TEST_STANDARD
    code = '12345'

    geo = Geo(name='Test Geo Place')
    glvl = GeoLevel(geo=geo, level='place', designation='city')
    gid = GeoID(level=glvl, standard=standard, code=code)

    session.add(geo)
    session.add(glvl)
    session.add(gid)
    session.commit()

    assert gid.level is glvl
    assert gid.standard == standard
    assert gid.code == code

    assert GeoID[gid.derive_key()] is gid
    assert GeoID[(standard, code)] is gid

    gid_from_db = session.query(GeoID).filter(GeoID.standard == standard,
                                              GeoID.code == code).first()

    assert gid_from_db is gid
    assert gid_from_db.level is glvl
    assert gid_from_db.standard == standard
    assert gid_from_db.code == code
Example #7
0
def test_jsonify_depth2(session, problem_name, org_name, geo_name,
                        num_followers):
    """Test jsonify by producing depth 2 community JSON"""
    from intertwine.communities.models import Community
    from intertwine.geos.models import Geo
    from intertwine.problems.models import Problem

    problem = Problem(name=problem_name) if problem_name else None
    org = org_name if org_name else None
    geo = Geo(name=geo_name) if geo_name else None
    community = Community(
        problem=problem, org=org, geo=geo, num_followers=num_followers)

    session.add(community)
    session.commit()

    community_payload = community.jsonify(depth=2)

    root = community_payload[Community.JSON_ROOT]
    community_json = community_payload[root]

    problem_key = community_json['problem']
    if problem:
        assert problem_key
        assert problem_key in community_payload

    org_key = community_json['org']
    if org:
        assert org_key
        # Include once there is an org model
        # assert org_key in community_payload

    geo_key = community_json['geo']
    if geo:
        assert geo_key
        assert geo_key in community_payload

    json.dumps(community_payload)
Example #8
0
def test_geo_data_model(session):
    """Test simple geo data model interaction"""
    total_pop, urban_pop = 1000, 800
    latitude, longitude = 30.0, -97.0
    land_area, water_area = 4321, 1234

    geo = Geo(name='Test Geo')
    GeoData(geo=geo,
            total_pop=total_pop,
            urban_pop=urban_pop,
            latitude=latitude,
            longitude=longitude,
            land_area=land_area,
            water_area=water_area)

    session.add(geo)
    session.commit()

    assert geo.data.total_pop == total_pop
    assert geo.data.urban_pop == urban_pop
    assert geo.data.latitude.value == latitude
    assert geo.data.longitude.value == longitude
    assert geo.data.land_area == land_area
    assert geo.data.water_area == water_area

    assert GeoData[geo.data.derive_key()] is geo.data

    geo_data_from_db = session.query(GeoData).filter(
        GeoData.geo == geo).first()

    assert geo_data_from_db is geo.data
    assert geo_data_from_db.total_pop == geo.data.total_pop
    assert geo_data_from_db.urban_pop == geo.data.urban_pop
    assert geo_data_from_db.latitude == geo.data.latitude
    assert geo_data_from_db.longitude == geo.data.longitude
    assert geo_data_from_db.land_area == geo.data.land_area
    assert geo_data_from_db.water_area == geo.data.water_area
Example #9
0
def test_geo_model_create(session, parent_name, parent_abbrev, child_name):
    """Test simple geo model interaction"""
    parent = Geo(name=parent_name, abbrev=parent_abbrev)
    child = Geo(name=child_name, path_parent=parent, parents=[parent])

    session.add(parent)
    session.add(child)
    session.commit()

    assert Geo[parent.derive_key()] is parent
    assert Geo[child.derive_key()] is child
    assert parent[child_name] is child

    parent_from_db = (session.query(Geo).filter(
        Geo.human_id == parent.human_id).first())

    child_from_db = (session.query(Geo).filter(
        Geo.human_id == child.human_id).first())

    assert parent_from_db is parent
    assert parent_from_db.name == parent_name
    assert parent_from_db.abbrev == parent_abbrev
    assert parent_from_db.uses_the is True
    assert parent_from_db.human_id == parent_abbrev.lower()
    assert parent_from_db.path_children.all()[0] is child_from_db
    assert parent_from_db.children.all()[0] is child_from_db

    assert child_from_db is child
    assert child_from_db.name == child_name
    assert child_from_db.abbrev is None
    assert child_from_db.uses_the is False
    assert child_from_db.human_id == Geo.PATH_DELIMITER.join(
        [child.path_parent.human_id,
         child.name.lower().replace(' ', '_')])

    assert child_from_db.path_parent is parent_from_db
    assert child_from_db.parents.all()[0] is parent_from_db
Example #10
0
def test_add_rated_problem_connection(session, client, connection_category,
                                      is_real_community):
    """Tests aggregate problem connection rating model interaction"""
    problem_name_base = 'Test Problem'
    problem1 = Problem(problem_name_base + ' 01')
    org = 'University of Texas'
    geo = Geo('Austin')
    session.add(problem1)
    session.add(geo)

    if is_real_community:
        community = Community(problem=problem1, org=org, geo=geo)
        session.add(community)
    else:
        community = vardygrify(Community, problem=problem1, org=org, geo=geo)

    session.commit()

    axis = (PC.CAUSAL if connection_category in {PC.DRIVERS, PC.IMPACTS}
            else PC.SCOPED)
    problem2_name = problem_name_base + ' 02'
    problem_a_name, problem_b_name = (
        (problem1.name, problem2_name)
        if connection_category in {PC.IMPACTS, PC.NARROWER}
        else (problem2_name, problem1.name))

    aggregation = APCR.STRICT

    request_payload = {
        'connection': {
            'axis': axis,
            'problem_a': problem_a_name,
            'problem_b': problem_b_name
        },
        'community': {
            'problem': problem1.human_id,
            'org': org,
            'geo': geo.human_id
        },
        'aggregation': aggregation
    }

    request_data = json.dumps(request_payload)
    url = 'http://localhost:5000/problems/rated_connections'

    response = client.post(url, data=request_data,
                           content_type='application/json')

    assert response is not None

    problem2 = Problem[Problem.create_key(name=problem2_name)]
    assert problem2 is not None

    response_data = response.get_data(as_text=True)
    response_payload = json.loads(response_data)
    root = response_payload['root']
    rated_connection = response_payload[root]

    assert rated_connection['adjacent_problem_name'] == problem2_name
    assert rated_connection['aggregation'] == APCR.STRICT

    assert rated_connection['community'] == community.json_key()

    problem_a_human_id = Problem.convert_name_to_human_id(problem_a_name)
    problem_a = Problem[problem_a_human_id]
    problem_b_human_id = Problem.convert_name_to_human_id(problem_b_name)
    problem_b = Problem[problem_b_human_id]
    problem_connection = PC[PC.create_key(
        axis, problem_a, problem_b)]
    assert rated_connection['connection'] == problem_connection.json_key()

    assert rated_connection['connection_category'] == connection_category
Example #11
0
def test_aggregate_problem_connection_rating_model(session):
    """Tests aggregate problem connection rating model interaction"""
    problem_name_base = 'Test Problem'
    problem1 = Problem(problem_name_base + ' 01')
    problem2 = Problem(problem_name_base + ' 02')
    connection12 = ProblemConnection('causal', problem1, problem2)
    org1 = 'University of Texas'
    geo1 = Geo('Austin')
    community1 = Community(problem=problem1, org=org1, geo=geo1)
    user1, user2, user3, user4 = 'user1', 'user2', 'user3', 'user4'

    session.add(geo1)
    session.add(problem1)
    session.add(problem2)
    session.add(connection12)
    session.add(community1)
    session.commit()

    rating1 = ProblemConnectionRating(rating=1,
                                      weight=1,
                                      connection=connection12,
                                      problem=problem1,
                                      org=org1,
                                      geo=geo1,
                                      user=user1)
    rating2 = ProblemConnectionRating(rating=2,
                                      weight=2,
                                      connection=connection12,
                                      problem=problem1,
                                      org=org1,
                                      geo=geo1,
                                      user=user2)
    rating3 = ProblemConnectionRating(rating=3,
                                      weight=3,
                                      connection=connection12,
                                      problem=problem1,
                                      org=org1,
                                      geo=geo1,
                                      user=user3)
    rating4 = ProblemConnectionRating(rating=4,
                                      weight=4,
                                      connection=connection12,
                                      problem=problem1,
                                      org=org1,
                                      geo=geo1,
                                      user=user4)

    for rating in connection12.ratings:
        session.add(rating)

    session.commit()

    rs = session.query(ProblemConnectionRating).order_by(
        ProblemConnectionRating.id).all()

    assert rs[0].rating is rating1.rating
    assert rs[1].rating is rating2.rating
    assert rs[2].rating is rating3.rating
    assert rs[3].rating is rating4.rating

    ar1 = AggregateProblemConnectionRating(connection=connection12,
                                           community=community1,
                                           aggregation='strict')

    assert AggregateProblemConnectionRating[ar1.derive_key()] is ar1
    adjacent_problem = problem2
    assert ar1.adjacent_problem_name == adjacent_problem.name
    adjacent_community_url = Community.form_uri(
        Community.Key(adjacent_problem, org1, geo1))
    assert ar1.adjacent_community_url == adjacent_community_url

    session.add(ar1)
    session.commit()

    ars = session.query(AggregateProblemConnectionRating).order_by(
        AggregateProblemConnectionRating.id).all()
    assert len(ars) == 1
    assert ars[0] is ar1
    assert round(ar1.rating, 1) == 3.0
    assert round(ar1.weight, 1) == 10.0

    # Change rating and validate that aggregate rating updated
    rating3.rating = 0
    assert round(ar1.rating, 1) == 2.1
    assert round(ar1.weight, 1) == 10.0
Example #12
0
def test_geo_aliases(session):
    """Test creation of geo aliases and promoting an alias"""
    geo_data_dict = {
        'total_pop': 100,
        'urban_pop': 80,
        'latitude': 42,
        'longitude': -71,
        'land_area': 321,
        'water_area': 123
    }

    level = 'place'
    geo = Geo(name='Test Geo')
    gdata = GeoData(geo=geo, **geo_data_dict)
    glvl = GeoLevel(geo=geo, level=level, designation='city')

    parent_geo = Geo(name='Test Parent Geo', abbrev='TPG')
    geo.path_parent = parent_geo
    geo.parents = [parent_geo]

    sibling_geo = Geo(name='Test Sibling Geo')
    sibling_geo.path_parent = parent_geo
    sibling_geo.parents = [parent_geo]

    child_geo = Geo(name='Test Child Geo')
    child_geo.path_parent = geo
    child_geo.parents = [geo]

    geo_alias_1 = Geo(name='Test Geo Alias 1',
                      alias_targets=[geo],
                      path_parent=geo)
    geo_alias_2 = Geo(name='Test Geo Alias 2',
                      alias_targets=[geo],
                      path_parent=parent_geo)
    geo_alias_3 = Geo(name='Test Geo Alias 3',
                      alias_targets=[geo],
                      path_parent=parent_geo)

    session.add(geo)
    session.commit()

    assert geo.data is gdata
    assert geo.levels[level] is glvl
    assert parent_geo in geo.parents
    assert geo in child_geo.parents

    assert geo.path_parent is parent_geo
    assert child_geo.path_parent is geo
    assert geo_alias_1.path_parent is geo
    assert geo_alias_2.path_parent is parent_geo
    assert geo_alias_3.path_parent is parent_geo

    assert geo_alias_1.alias_targets[0] is geo
    assert geo_alias_2.alias_targets[0] is geo
    assert geo_alias_3.alias_targets[0] is geo

    aliases = geo.aliases.all()
    assert geo_alias_1 in aliases
    assert geo_alias_2 in aliases
    assert geo_alias_3 in aliases

    geo_from_db = session.query(Geo).filter_by(human_id=geo.human_id).first()

    aliases = geo_from_db.aliases.all()
    assert geo_alias_1 in aliases
    assert geo_alias_2 in aliases
    assert geo_alias_3 in aliases

    geo_alias_1.promote_to_alias_target()
    assert not geo_alias_1.alias_targets
    assert geo_alias_2.alias_targets[0] is geo_alias_1
    assert geo_alias_3.alias_targets[0] is geo_alias_1
    assert geo.alias_targets[0] is geo_alias_1

    assert geo_alias_1.data is gdata
    assert geo_alias_1.levels[level] is glvl
    assert parent_geo in geo_alias_1.parents
    assert geo_alias_1 in child_geo.parents

    # paths are unchanged by design
    assert geo.path_parent is parent_geo
    assert child_geo.path_parent is geo
    assert geo_alias_1.path_parent is geo
    assert geo_alias_2.path_parent is parent_geo
    assert geo_alias_3.path_parent is parent_geo
Example #13
0
def test_form_aggregate_geo(session):
    """Test geo creation that aggregates children data at a geo level"""
    data_level = 'place'

    child_a_dict = {
        'total_pop': 100,
        'urban_pop': 80,
        'latitude': 42.0,
        'longitude': -71.0,
        'land_area': 321,
        'water_area': 123
    }

    child_a_geo = Geo(name='Child Test Geo A')
    GeoData(geo=child_a_geo, **child_a_dict)
    GeoLevel(geo=child_a_geo, level='place', designation='village')

    child_b_dict = {
        'total_pop': 300,
        'urban_pop': 240,
        'latitude': 44.0,
        'longitude': -73.0,
        'land_area': 645,
        'water_area': 21
    }

    child_b_geo = Geo(name='Child Test Geo B')
    GeoData(geo=child_b_geo, **child_b_dict)
    GeoLevel(geo=child_b_geo, level='place', designation='town')

    child_c_dict = {
        'total_pop': 1000,
        'urban_pop': 800,
        'latitude': 30.0,
        'longitude': -97.0,
        'land_area': 4321,
        'water_area': 1234
    }

    child_c_geo = Geo(name='Child Test Geo C')
    GeoData(geo=child_c_geo, **child_c_dict)
    GeoLevel(geo=child_c_geo, level='subdivision2', designation='county')

    child_a_area = child_a_dict['land_area'] + child_a_dict['water_area']
    child_b_area = child_b_dict['land_area'] + child_b_dict['water_area']

    geo_location_a = GeoLocation(child_a_dict['latitude'],
                                 child_a_dict['longitude'])
    geo_location_b = GeoLocation(child_b_dict['latitude'],
                                 child_b_dict['longitude'])

    geo_location = GeoLocation.combine_locations(
        (geo_location_a, child_a_area), (geo_location_b, child_b_area))

    sum_fields = ('total_pop', 'urban_pop', 'land_area', 'water_area')

    aggregate_dict = {f: child_a_dict[f] + child_b_dict[f] for f in sum_fields}

    aggregate_dict['latitude'], aggregate_dict['longitude'] = (
        geo_location.coordinates)

    parent_geo = Geo(name='Parent Test Geo',
                     children=[child_a_geo, child_b_geo, child_c_geo],
                     child_data_level=data_level)  # aggregate places only

    session.add(parent_geo)
    session.add(child_a_geo)
    session.add(child_b_geo)
    session.add(child_c_geo)
    session.commit()

    parent_data = parent_geo.data
    assert parent_data is not None
    assert parent_data.total_pop == aggregate_dict['total_pop']
    assert parent_data.urban_pop == aggregate_dict['urban_pop']
    assert parent_data.latitude == aggregate_dict['latitude']
    assert parent_data.longitude == aggregate_dict['longitude']
    assert parent_data.land_area == aggregate_dict['land_area']
    assert parent_data.water_area == aggregate_dict['water_area']

    parent_geo_data_from_db = session.query(GeoData).filter(
        GeoData.geo == parent_geo).first()

    assert parent_geo_data_from_db is not None
    assert parent_geo_data_from_db is parent_geo.data
    assert parent_geo_data_from_db.total_pop == aggregate_dict['total_pop']
    assert parent_geo_data_from_db.urban_pop == aggregate_dict['urban_pop']
    assert parent_geo_data_from_db.latitude == aggregate_dict['latitude']
    assert parent_geo_data_from_db.longitude == aggregate_dict['longitude']
    assert parent_geo_data_from_db.land_area == aggregate_dict['land_area']
    assert parent_geo_data_from_db.water_area == aggregate_dict['water_area']