Exemplo n.º 1
0
def test_natl_match_lineup_insert(session, national_data, person_data,
                                  position_data):
    match = mn.NationalGroupMatches(
        group_round=enums.GroupRoundType.first_round,
        group='C',
        matchday=2,
        **national_data)
    session.add(match)
    session.commit()

    nation_from_db = session.query(
        mco.Countries).filter(mco.Countries.name == u"Mexico").one()

    player_data = person_data['player'][0]
    del player_data['country']
    player_data['country_id'] = nation_from_db.id
    player = mcp.Players(position=position_data[0], **player_data)
    session.add(player)
    session.commit()

    lineup = mn.NationalMatchLineups(match_id=match.id,
                                     team_id=nation_from_db.id,
                                     player_id=player.player_id,
                                     position_id=player.position_id)
    session.add(lineup)

    lineup_from_db = session.query(mn.NationalMatchLineups).join(mn.NationalGroupMatches).\
        filter(mn.NationalGroupMatches.id == match.id)

    assert lineup_from_db.count() == 1
    assert unicode(lineup_from_db[0]) == u"<NationalMatchLineup(match={}, player=Miguel Ángel Ponce, team=Mexico, " \
                                         u"position=Left back, starter=False, captain=False)>".format(match.id)
Exemplo n.º 2
0
def test_lineup_designate_captain(session, match_data, person_data,
                                  position_data):
    capn_indx = 1
    lineups = [
        mcm.MatchLineups(match=mcm.Matches(**match_data),
                         player=mcp.Players(**plyr),
                         position=pos,
                         is_starting=True,
                         is_captain=(j == capn_indx))
        for j, (plyr,
                pos) in enumerate(zip(person_data['player'], position_data))
    ]
    session.add_all(lineups)

    capn_position = position_data[capn_indx]

    lineup_from_db = session.query(mcm.MatchLineups).join(
        mcp.Positions).filter(mcp.Positions.name == capn_position.name).all()
    assert len(lineup_from_db) == 1
    assert lineup_from_db[0].is_captain is True

    other_lineup_from_db = session.query(mcm.MatchLineups).join(
        mcp.Positions).filter(mcp.Positions.name != capn_position.name).all()
    for others in other_lineup_from_db:
        assert others.is_captain is False
Exemplo n.º 3
0
def test_player_insert(session, person_data, position_data):
    player_data = [data for key, records in person_data.items() for data in records if key in ['player']]
    for player, position in zip(player_data, position_data):
        player['position'] = position
    players = [mcp.Players(**data) for data in player_data]
    session.add_all(players)

    players_from_db = session.query(mcp.Players)
    assert players_from_db.count() == len(players)
Exemplo n.º 4
0
def test_player_history_representation(session, person_data, player_history_data):
    player_data = dict(position=mcp.Positions(name=u'Central Midfielder', type=enums.PositionType.midfielder),
                       **person_data['generic'])
    generic_player = mcp.Players(**player_data)
    player_history = [mcp.PlayerHistory(**dict(player=generic_player, **data)) for data in player_history_data]
    session.add_all(player_history)

    history_from_db = session.query(mcp.PlayerHistory).join(mcp.Players).\
        filter(mcp.Players.last_name == u"Doe", mcp.PlayerHistory.date == date(1998, 7, 15)).one()

    assert repr(history_from_db) == u"<PlayerHistory(name=John Doe, date=1998-07-15, height=1.74, weight=76)>"
Exemplo n.º 5
0
def test_player_history_insert(session, person_data, player_history_data):
    player_data = dict(position=mcp.Positions(name=u'Central Midfielder', type=enums.PositionType.midfielder),
                       **person_data['generic'])
    generic_player = mcp.Players(**player_data)
    player_history = [mcp.PlayerHistory(**dict(player=generic_player, **data)) for data in player_history_data]
    session.add_all(player_history)

    history_from_db = session.query(mcp.PlayerHistory).join(mcp.Players).\
        filter(mcp.Players.last_name == u"Doe")

    assert history_from_db.count() == len(player_history)
Exemplo n.º 6
0
    def players(self, data_frame):
        player_set = set()
        player_records = []
        remote_countryids = []
        remote_ids = []
        local_ids = []
        fields = ['known_first_name', 'first_name', 'middle_name', 'last_name', 'second_last_name',
                  'nick_name', 'birth_date', 'order', 'country_id', 'position_id', 'remote_id',
                  'remote_country_id']

        for _, row in data_frame.iterrows():
            player_set.add(tuple([(field, row[field]) for field in fields
                                  if field in row and row[field] is not None]))
        logger.info("{} players in data feed".format(len(player_set)))
        for elements in player_set:
            player_dict = dict(elements)
            remote_id = player_dict.pop('remote_id')
            remote_country_id = player_dict.pop('remote_country_id', None)
            if not self.record_exists(mcs.PlayerMap, remote_id=remote_id):
                if not self.record_exists(mcp.Players, **player_dict):
                    player_dict.update(id=uuid.uuid4(), person_id=uuid.uuid4())
                    player_records.append(mcp.Players(**player_dict))
                    local_ids.append(player_dict['id'])
                    remote_ids.append(remote_id)
                    remote_countryids.append(remote_country_id)
                else:
                    player_id = self.session.query(mcp.Players).filter_by(**player_dict).one().id
                    local_ids.append(player_id)
                    remote_ids.append(remote_id)
            else:
                player_id = self.session.query(mcs.PlayerMap).filter_by(remote_id=remote_id).one().id
                if not self.record_exists(mcp.Players, **player_dict):
                    updated_records = self.session.query(mcp.Players).\
                        filter(mcp.Players.person_id == mcp.Persons.person_id).\
                        filter(mcp.Players.id == player_id)
                    for rec in updated_records:
                        for field, value in player_dict.items():
                            setattr(rec, field, value)
        if self.session.dirty:
            self.session.commit()

        logger.info("{} player records ingested".format(len(player_records)))
        self.session.bulk_save_objects(player_records)
        map_records = [mcs.PlayerMap(id=local_id, remote_id=remote_id, supplier_id=self.supplier_id)
                       for remote_id, local_id in zip(remote_ids, local_ids) if remote_id]
        self.session.bulk_save_objects(map_records)
        self.session.commit()

        for remote_id, player_record in zip(remote_countryids, player_records):
            if remote_id and not self.record_exists(mcs.CountryMap, remote_id=remote_id,
                                                    supplier_id=self.supplier_id):
                self.session.add(mcs.CountryMap(id=player_record.country_id, remote_id=remote_id,
                                                supplier_id=self.supplier_id))
                self.session.commit()
Exemplo n.º 7
0
def test_match_lineup_generic_insert(session, match_data, person_data,
                                     position_data):
    lineup = mcm.MatchLineups(match=mcm.Matches(**match_data),
                              player=mcp.Players(**person_data['player'][1]),
                              position=position_data[1])
    session.add(lineup)

    lineup_from_db = session.query(mcm.MatchLineups).one()
    match_from_db = session.query(mcm.Matches).one()
    player_from_db = session.query(mcp.Players).one()

    assert lineup_from_db.is_starting is False
    assert lineup_from_db.is_captain is False
    assert lineup_from_db.match_id == match_from_db.id
    assert lineup_from_db.player_id == player_from_db.id
Exemplo n.º 8
0
def test_player_representation(session, person_data, position_data):
    player_data = [data for key, records in person_data.items() for data in records if key in ['player']]
    for player, position in zip(player_data, position_data):
        player['position'] = position
    players = [mcp.Players(**data) for data in player_data]
    session.add_all(players)

    korean_player = session.query(mcp.Players).join(mco.Countries).\
        filter(mco.Countries.name == u"Korea Republic").one()
    assert repr(korean_player) == u"<Player(name=Son Heung-Min, DOB=1992-07-08, " \
                                  u"country=Korea Republic, position=Second striker)>"

    ronaldo = session.query(mcp.Players).filter(mcp.Players.nick_name == u"Cristiano Ronaldo").one()
    assert repr(ronaldo) == u"<Player(name=Cristiano Ronaldo, DOB=1985-02-05, " \
                            u"country=Portugal, position=Forward)>"

    mexican_player = session.query(mcp.Players).join(mco.Countries).filter(mco.Countries.name == u"Mexico").one()
    assert unicode(mexican_player) == u"<Player(name=Miguel Ángel Ponce, DOB=1989-04-12, " \
                                      u"country=Mexico, position=Left back)>"
Exemplo n.º 9
0
def club_match_lineup(session, club_data, person_data, position_data):
    match = mc.ClubLeagueMatches(matchday=15, **club_data)
    session.add(match)
    session.commit()

    club = session.query(mc.Clubs).filter(mc.Clubs.name == u"Arsenal FC").one()

    lineups = [
        mc.ClubMatchLineups(match_id=match.id,
                            team_id=club.id,
                            player=mcp.Players(**plyr),
                            position=pos,
                            is_starting=True,
                            is_captain=False)
        for j, (plyr,
                pos) in enumerate(zip(person_data['player'], position_data))
    ]
    session.add_all(lineups)
    session.commit()

    return club, match, lineups
Exemplo n.º 10
0
def natl_match_lineup(session, national_data, person_data, position_data):
    match = mn.NationalFriendlyMatches(**national_data)
    session.add(match)
    session.commit()

    nation = session.query(
        mco.Countries).filter(mco.Countries.name == u"Mexico").one()

    lineups = [
        mn.NationalMatchLineups(match_id=match.id,
                                team_id=nation.id,
                                player=mcp.Players(**plyr),
                                position=pos,
                                is_starting=True,
                                is_captain=False)
        for j, (plyr,
                pos) in enumerate(zip(person_data['player'], position_data))
    ]
    session.add_all(lineups)
    session.commit()

    return nation, match, lineups
Exemplo n.º 11
0
def test_club_match_lineup_insert(session, club_data, person_data,
                                  position_data):
    match = mc.ClubLeagueMatches(matchday=15, **club_data)
    session.add(match)
    player = mcp.Players(position=position_data[0], **person_data['player'][0])
    session.add(player)
    session.commit()

    club_from_db = session.query(
        mc.Clubs).filter(mc.Clubs.name == u"Arsenal FC").one()

    lineup = mc.ClubMatchLineups(match_id=match.id,
                                 team_id=club_from_db.id,
                                 player_id=player.player_id,
                                 position_id=player.position_id)
    session.add(lineup)

    lineup_from_db = session.query(mc.ClubMatchLineups).join(mc.ClubLeagueMatches)\
        .filter(mc.ClubLeagueMatches.id == match.id)

    assert lineup_from_db.count() == 1
    assert unicode(lineup_from_db[0]) == u"<ClubMatchLineup(match={}, player=Miguel Ángel Ponce, team=Arsenal FC, " \
                                         u"position=Left back, starter=False, captain=False)>".format(match.id)