Beispiel #1
0
    def import_file(self, html_file):
        """
        Imports a single HTMLFile into the database, parsing as appropriate.
        The team is created if it doesn't already exist
        """
        team = None

        with self.session_scope() as session:
            # find team in the database
            team_id = parse_team_id(html_file.HTML)
            result = session.query(Team).filter_by(id=team_id)

            if(result.count()):
                team = result.first()
            else:  # no team, so create it
                team = Team()
                team.id = team_id

            # pass the html to the appropriate parsing function
            if(html_file.type == PageTypes.Pavilion.value):
                team.add_ranking(parse_pavilion(html_file.HTML))
            elif(html_file.type == PageTypes.Squad.value):
                team.players = parse_players(html_file.HTML)

            # add the team and HTML file to the database
            session.add(team)
            session.add(html_file)
            session.flush()

        return html_file
Beispiel #2
0
def test_import_file_updates_db_with_players():
    """
    End to end test asserting that a HTML file is parsed and imported into an
    in-memory SQLite database. 
    """
    model = Model(echo=False)

    # open the HTML file
    file = open(
        path.join(PROJECT_SOURCE_PATH, 'test', 'resources', 'squad.html'), 'r'
    )
    html = file.read()
    file.close()

    # parse the players
    players = parse_players(html)

    with model.session_scope() as session:

        # open a session and import our HTML file
        html_file = HTMLFile.from_file(
            path.join(PROJECT_SOURCE_PATH, 'test', 'resources', 'squad.html'))

        model.import_file(html_file)

        # retrieve the team from the database
        # and check that it has imported correctly
        new_team = session.query(Team).first()
        assert_equal(len(new_team.players), len(players))
def test_parse_players_throws_BatParseException_if_no_player_info():
    parse_players("html with no player info")
 def setUpClass(cls):
     file = open(path.join(getcwd(), "test", "resources", "squad.html"), "r")
     html = file.read()
     cls.players = parse_players(html)
     cls.p = cls.players[0]
     file.close()