def knockout_matches(self, data_frame): condition_records = [] match_records = [] remote_ids = [] local_ids = [] fields = ['match_date', 'competition_id', 'season_id', 'venue_id', 'home_team_id', 'away_team_id', 'home_manager_id', 'away_manager_id', 'referee_id', 'attendance', 'matchday', 'ko_round', 'extra_time'] condition_fields = ['kickoff_time', 'kickoff_temp', 'kickoff_humidity', 'kickoff_weather', 'halftime_weather', 'fulltime_weather'] for idx, row in data_frame.iterrows(): match_dict = {field: row[field] for field in fields if field in row and row[field] is not None} condition_dict = {field: row[field] for field in condition_fields if field in row and row[field] is not None} if not self.record_exists(mc.ClubKnockoutMatches, **match_dict): match_dict.update(id=uuid.uuid4()) match_records.append(mc.ClubKnockoutMatches(**match_dict)) condition_records.append(mcm.MatchConditions(id=match_dict['id'], **condition_dict)) remote_ids.append(row['remote_id']) local_ids.append(match_dict['id']) self.session.bulk_save_objects(match_records) self.session.bulk_save_objects(condition_records) map_records = [mcs.MatchMap(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()
def test_match_conditions_humid_error(session, match_data, match_condition_data): match_condition_data['match'] = mcm.Matches(**match_data) for out_of_range in [-1.0, 102.0]: match_condition_data['kickoff_humidity'] = out_of_range match_conditions = mcm.MatchConditions(**match_condition_data) with pytest.raises(IntegrityError): session.add(match_conditions) session.commit() session.rollback()
def test_match_conditions_insert(session, match_data, match_condition_data): match_condition_data['match'] = mcm.Matches(**match_data) match_conditions = mcm.MatchConditions(**match_condition_data) session.add(match_conditions) match_from_db = session.query(mcm.Matches).one() conditions_from_db = session.query(mcm.MatchConditions).one() assert repr(conditions_from_db) == "<MatchCondition(id={}, kickoff=19:30, temp=15.0, " \ "humid=68.0, kickoff_weather=Partly Cloudy)>".format(match_from_db.id)