def test_import_file_calls_parse_players_if_squad_file(self, mock_parse_team_id, mock_add, mock_first, mock_count, mock_parse_players): """ Tests that parse_players is called if the HTML file is a Squad file """ html_file = HTMLFile() html_file.type = PageTypes.Squad.value html_file.HTML = 'some HTML' mock_parse_players.return_value = [] mock_parse_team_id.return_value = 3000 team = Team() mock_first.result = team self.model.import_file(html_file) mock_parse_players.assert_called_with(html_file.HTML)
def test_import_file_calls_parse_pavilion_if_pavilion_file(self, mock_ranking,mock_parse_team_id, mock_first, mock_parse_pavilion): html_file = HTMLFile() html_file.type = PageTypes.Pavilion.value html_file.HTML = 'some HTML' mock_parse_pavilion.return_value = [] mock_parse_team_id.return_value = 3000 team = Team() team.rankings.append = mock.Mock() mock_first.result = team self.model.import_file(html_file) assert mock_parse_team_id.called
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_HTMLFile_is_squad_page_when_parsing_squad_HTML(self): """ Tests that a squad.html page is correctly recognised as a squad file""" # return a fake HTML Squad file self.mock_open.return_value.read.return_value = '<html><title>Battrick - Squad</title></html>' htmlFile = HTMLFile.from_file('a_file') assert_is(htmlFile.type, PageTypes.Squad.value)
def test_readfile_adds_imported_time(self): """ Tests that the 'current' time is added to the object """ test_date = datetime(2010, 10, 8, 12, 50, 33) with patch('core.PyBatBase.datetime') as mock_date: # fake today's date mock_date.today.return_value = test_date htmlFile = HTMLFile.from_file('a_file') assert_equal(htmlFile.date_imported, test_date)
def importfiles(self): """ Imports the files provided as a list of local filenames in the request. """ files = (cherrypy.request.json['files']) for filename in files: # generate HTMLFile objects from the filenames self.model.import_file(HTMLFile.from_file(filename)) # TODO: add response with import success/failure return "true"
def upload(self, file, data): """ Directly upload a HTML file. """ file_contents = file.file.read() timestamp = datetime.strptime(data[:data.index('.')], self.date_format) print(timestamp) self.model.import_file( HTMLFile.from_memory(file_contents, timestamp)) return
def test_import_file_retrieves_team_from_file(self, mock_add, mock_query, mock_parse_team_id): test_id = 49800 team = Team() html_file = HTMLFile() html_file.HTML = 'Some HTML' mock_query.return_value.filter_by = mock.Mock() mock_parse_team_id.return_value = test_id mock_result = mock.Mock(spec=Query) mock_result.count = mock.Mock() mock_result.count.return_value = 1 mock_query.return_value.filter_by.return_value = mock_result self.model.import_file(html_file) mock_query.assert_called_with(Team) assert mock_parse_team_id.called mock_query.return_value.filter_by.assert_called_with(id=test_id)
def test_HTMLfile_from_file_adds_modified_time(self): """ Tests that the file modified time is added to the object """ htmlFile = HTMLFile.from_file('a_file') assert_equal(htmlFile.date_modified, self.test_date)
def test_HTMLFile_throws_PageParseException_if_wrong_title(self): """ Tests that a PageParseException is raised if the title is wrong """ self.mock_open.return_value.read.return_value = '<html><head><title>WrongTitle</title></head></html>' HTMLFile.from_file('a_file')
def test_HTMLFile_throws_PageParseException_if_empty_title(self): """ Tests that a PageParseException is raised if no <title> exists """ self.mock_open.return_value.read.return_value = '<html></html>' HTMLFile.from_file('a_file')
def test_HTMLFile_inits_throws_PageParseException_if_page_not_recognised(self): """ Tests that a PageParseException is raised if an incorrect page is parsed """ self.mock_open.return_value.read.return_value = '<html><title>Battrick - WrongPage</title></html>' HTMLFile.from_file('a_file')