def setUp(self): loc_tree = Mock() loc_tree.get_hierarchy_path.return_value = None self.dbm = Mock(spec=DatabaseManager) self.submission_handler_mock = Mock(spec=SubmissionHandler) self.parser = XlsParser() self.xls_data = """ FORM_CODE,ID,BEDS,DIRECTOR,MEDS CLF1,CL001,10,Dr. A,201 CLF1,CL002,11,Dr. B,202 CLF2,CL003,12,Dr. C,203 CLF1,CL004,13,Dr. D,204 CLF1,CL005,14,Dr. E,205 """ self.file_name = 'test.xls' wb = xlwt.Workbook() ws = wb.add_sheet('test') for row_number, row in enumerate(self.xls_data.split('\n')): for col_number, val in enumerate(row.split(',')): ws.write(row_number, col_number, val) wb.save(self.file_name) self.player = XlsPlayer(self.dbm, self.submission_handler_mock, self.parser, loc_tree) self.generate_code_patcher = patch( "mangrove.transport.player.player.Player._handle_registration_form" ) self.generate_code_patcher.start()
class TestXlsPlayer(unittest.TestCase): def setUp(self): loc_tree = Mock() loc_tree.get_hierarchy_path.return_value = None self.dbm = Mock(spec=DatabaseManager) self.submission_handler_mock = Mock(spec=SubmissionHandler) self.parser = XlsParser() self.xls_data = """ FORM_CODE,ID,BEDS,DIRECTOR,MEDS CLF1,CL001,10,Dr. A,201 CLF1,CL002,11,Dr. B,202 CLF2,CL003,12,Dr. C,203 CLF1,CL004,13,Dr. D,204 CLF1,CL005,14,Dr. E,205 """ self.file_name = 'test.xls' wb = xlwt.Workbook() ws = wb.add_sheet('test') for row_number, row in enumerate(self.xls_data.split('\n')): for col_number, val in enumerate(row.split(',')): ws.write(row_number, col_number, val) wb.save(self.file_name) self.player = XlsPlayer(self.dbm, self.submission_handler_mock, self.parser, loc_tree) self.generate_code_patcher = patch( "mangrove.transport.player.player.Player._handle_registration_form" ) self.generate_code_patcher.start() def tearDown(self): self.generate_code_patcher.stop() def test_should_import_xls_string(self): self.player.accept(file_contents=open(self.file_name).read()) self.assertEqual(5, self.submission_handler_mock.accept.call_count) def test_should_process_next_submission_if_exception_with_prev(self): def expected_side_effect(*args, **kwargs): request = kwargs.get('request') or args[0] if request.form_code == 'clf2': raise FormModelDoesNotExistsException('') return SubmissionResponse(success=True, submission_id=1) self.submission_handler_mock.accept.side_effect = expected_side_effect response = self.player.accept( file_contents=open(self.file_name).read()) self.assertEqual(5, len(response)) self.assertEqual(False, response[2].success) success = len([index for index in response if index.success]) total = len(response) self.assertEqual(4, success) self.assertEqual(5, total) def tearDown(self): os.remove(self.file_name)
class TestXlsPlayer(unittest.TestCase): def setUp(self): loc_tree = Mock() loc_tree.get_hierarchy_path.return_value = None self.dbm = Mock(spec=DatabaseManager) self.submission_handler_mock = Mock(spec=SubmissionHandler) self.parser = XlsParser() self.xls_data = """ FORM_CODE,ID,BEDS,DIRECTOR,MEDS CLF1,CL001,10,Dr. A,201 CLF1,CL002,11,Dr. B,202 CLF2,CL003,12,Dr. C,203 CLF1,CL004,13,Dr. D,204 CLF1,CL005,14,Dr. E,205 """ self.file_name = 'test.xls' wb = xlwt.Workbook() ws = wb.add_sheet('test') for row_number, row in enumerate(self.xls_data.split('\n')): for col_number, val in enumerate(row.split(',')): ws.write(row_number, col_number, val) wb.save(self.file_name) self.player = XlsPlayer(self.dbm, self.submission_handler_mock, self.parser, loc_tree) self.generate_code_patcher = patch("mangrove.transport.player.player.Player._handle_registration_form") self.generate_code_patcher.start() def tearDown(self): self.generate_code_patcher.stop() def test_should_import_xls_string(self): self.player.accept(file_contents=open(self.file_name).read()) self.assertEqual(5, self.submission_handler_mock.accept.call_count) def test_should_process_next_submission_if_exception_with_prev(self): def expected_side_effect(*args, **kwargs): request = kwargs.get('request') or args[0] if request.form_code == 'clf2': raise FormModelDoesNotExistsException('') return SubmissionResponse(success=True, submission_id=1) self.submission_handler_mock.accept.side_effect = expected_side_effect response = self.player.accept(file_contents=open(self.file_name).read()) self.assertEqual(5, len(response)) self.assertEqual(False, response[2].success) success = len([index for index in response if index.success]) total = len(response) self.assertEqual(4, success) self.assertEqual(5, total) def tearDown(self): os.remove(self.file_name)
def _handle_uploaded_file(file_name,file,manager): base_name, extension = os.path.splitext(file_name) if extension == '.csv': csv_player = CsvPlayer(dbm=manager, submission_handler=SubmissionHandler(manager), parser=CsvParser()) response = csv_player.accept(file) elif extension == '.xls': xls_player = XlsPlayer(dbm=manager, submission_handler=SubmissionHandler(manager), parser=XlsParser()) response = xls_player.accept(file) else: raise InvalidFileFormatException() return response
def _handle_uploaded_file(file_name, file, manager): base_name, extension = os.path.splitext(file_name) if extension == '.csv': csv_player = CsvPlayer(dbm=manager, submission_handler=SubmissionHandler(manager), parser=CsvParser()) response = csv_player.accept(file) elif extension == '.xls': xls_player = XlsPlayer(dbm=manager, submission_handler=SubmissionHandler(manager), parser=XlsParser()) response = xls_player.accept(file) else: raise InvalidFileFormatException() return response
def setUp(self): loc_tree = Mock() loc_tree.get_hierarchy_path.return_value = None self.dbm = Mock(spec=DatabaseManager) self.submission_handler_mock = Mock(spec=SubmissionHandler) self.parser = XlsParser() self.xls_data = """ FORM_CODE,ID,BEDS,DIRECTOR,MEDS CLF1,CL001,10,Dr. A,201 CLF1,CL002,11,Dr. B,202 CLF2,CL003,12,Dr. C,203 CLF1,CL004,13,Dr. D,204 CLF1,CL005,14,Dr. E,205 """ self.file_name = 'test.xls' wb = xlwt.Workbook() ws = wb.add_sheet('test') for row_number, row in enumerate(self.xls_data.split('\n')): for col_number, val in enumerate(row.split(',')): ws.write(row_number, col_number, val) wb.save(self.file_name) self.player = XlsPlayer(self.dbm, self.submission_handler_mock, self.parser, loc_tree) self.generate_code_patcher = patch("mangrove.transport.player.player.Player._handle_registration_form") self.generate_code_patcher.start()