def setUp(self): 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._write_to_xls(data) self.parser = XlsParser()
def build(cls, manager, extension, default_parser=None, form_code=None, is_datasender=False): channels = dict({ ".xls": Channel.XLS, ".xlsx": Channel.XLSX, ".csv": Channel.CSV }) try: channel = channels[extension] except KeyError: raise InvalidFileFormatException() if default_parser is not None: parser = default_parser() elif extension == '.csv': parser = CsvParser() elif extension == '.xls': parser = XlsParser() elif extension == '.xlsx': parser = XlsxParser() else: raise InvalidFileFormatException() location_bridge = LocationBridge( get_location_tree(), get_loc_hierarchy=get_location_hierarchy) player = FilePlayer(manager, parser, channel, location_tree=location_bridge, is_datasender=is_datasender) player.form_code = form_code return player
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 _init_xls_data(self): self.parser = XlsParser() self.file_name = 'test.xls' wb = xlwt.Workbook() ws = wb.add_sheet('test') for row_number, row in enumerate(UPLOAD_DATA.split('\n')): for col_number, val in enumerate(row.split(',')): ws.write(row_number, col_number, val) wb.save(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
class TestXlsParser(TestCase): def _write_to_xls(self, data): self.file_name = "test.xls" wb = xlwt.Workbook() ws = wb.add_sheet('test') for row_number, row in enumerate(data.split('\n')): for col_number, val in enumerate(row.split(',')): ws.write(row_number, col_number, val) wb.save(self.file_name) def setUp(self): 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._write_to_xls(data) self.parser = XlsParser() def test_should_parse_xls_contents(self): with open(self.file_name) as input_file: submissions = self.parser.parse(input_file.read()) self.assertEqual(5, len(submissions)) form_code, values = submissions[0] self.assertEqual(u"clf1", form_code) self.assertEqual({u"id": u'CL001', u'beds': u'10', u'director': u'Dr. A', u'meds': u'201'}, values) def test_should_raise_exception_for_invalid_format(self): data = """ """ self._write_to_xls(data) with open(self.file_name) as input_file: with self.assertRaises(XlsParserInvalidHeaderFormatException): self.parser.parse(input_file.read()) def test_should_parse_xls_contents_with_extra_field_values(self): data = """ FORM_CODE,ID,BEDS,DIRECTOR,MEDS CLF1,CL001,10,Dr. A,201 CLF1,CL002,11,Dr. B,202, extra field CLF2,CL003,12,Dr. C,203 CLF1,CL004,13,Dr. D,204, extra1, extra 2 CLF1,CL005,14,Dr. E,205 """ self._write_to_xls(data) with open(self.file_name) as input_file: submissions = self.parser.parse(input_file.read()) self.assertEqual(5, len(submissions)) form_code, values = submissions[1] self.assertEqual("clf1", form_code) self.assertEqual({u"id": u'CL002', u'beds': u'11', u'director': u'Dr. B', u'meds': u'202'}, values) form_code, values = submissions[3] self.assertEqual("clf1", form_code) self.assertEqual({u"id": u'CL004', u'beds': u'13', u'director': u'Dr. D', u'meds': u'204'}, values) def test_should_parse_xls_contents_less_field_values_than_header(self): data = """ FORM_CODE,ID,BEDS,DIRECTOR,MEDS CLF1,CL001,10,Dr. A,201 CLF1,CL002,,Dr. B,202 CLF2,CL003,12,Dr. C,203 CLF1,CL004,13,Dr. D,204 CLF1,CL005,14,Dr. E,205 """ self._write_to_xls(data) with open(self.file_name) as input_file: submissions = self.parser.parse(input_file.read()) self.assertEqual(5, len(submissions)) form_code, values = submissions[1] self.assertEqual("clf1", form_code) self.assertEqual({u"id": u'CL002', u'beds': u'', u'director': u'Dr. B', u'meds': u'202'}, values) def tearDown(self): os.remove(self.file_name) pass
class TestXlsParser(TestCase): def _write_to_xls(self, data): self.file_name = "test.xls" wb = xlwt.Workbook() ws = wb.add_sheet('test') for row_number, row in enumerate(data.split('\n')): for col_number, val in enumerate(row.split(',')): ws.write(row_number, col_number, val) wb.save(self.file_name) def setUp(self): 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._write_to_xls(data) self.parser = XlsParser() def test_should_parse_xls_contents(self): with open(self.file_name) as input_file: submissions = self.parser.parse(input_file.read()) self.assertEqual(5, len(submissions)) form_code, values = submissions[0] self.assertEqual(u"clf1", form_code) self.assertEqual( { u"id": u'CL001', u'beds': u'10', u'director': u'Dr. A', u'meds': u'201' }, values) def test_should_raise_exception_for_invalid_format(self): data = """ """ self._write_to_xls(data) with open(self.file_name) as input_file: with self.assertRaises(XlsParserInvalidHeaderFormatException): self.parser.parse(input_file.read()) def test_should_parse_xls_contents_with_extra_field_values(self): data = """ FORM_CODE,ID,BEDS,DIRECTOR,MEDS CLF1,CL001,10,Dr. A,201 CLF1,CL002,11,Dr. B,202, extra field CLF2,CL003,12,Dr. C,203 CLF1,CL004,13,Dr. D,204, extra1, extra 2 CLF1,CL005,14,Dr. E,205 """ self._write_to_xls(data) with open(self.file_name) as input_file: submissions = self.parser.parse(input_file.read()) self.assertEqual(5, len(submissions)) form_code, values = submissions[1] self.assertEqual("clf1", form_code) self.assertEqual( { u"id": u'CL002', u'beds': u'11', u'director': u'Dr. B', u'meds': u'202' }, values) form_code, values = submissions[3] self.assertEqual("clf1", form_code) self.assertEqual( { u"id": u'CL004', u'beds': u'13', u'director': u'Dr. D', u'meds': u'204' }, values) def test_should_parse_xls_contents_less_field_values_than_header(self): data = """ FORM_CODE,ID,BEDS,DIRECTOR,MEDS CLF1,CL001,10,Dr. A,201 CLF1,CL002,,Dr. B,202 CLF2,CL003,12,Dr. C,203 CLF1,CL004,13,Dr. D,204 CLF1,CL005,14,Dr. E,205 """ self._write_to_xls(data) with open(self.file_name) as input_file: submissions = self.parser.parse(input_file.read()) self.assertEqual(5, len(submissions)) form_code, values = submissions[1] self.assertEqual("clf1", form_code) self.assertEqual( { u"id": u'CL002', u'beds': u'', u'director': u'Dr. B', u'meds': u'202' }, values) def tearDown(self): os.remove(self.file_name) pass