def test_sheet_import_as_report_sheet(self): importer = si.SheetImporter(( si.Field('Name', 'name', p.chain(p.parse_text, p.validate_not_empty)), si.Field('Job', 'job', p.parse_text), )) TestReportSheet = importer.as_report_sheet() assert TestReportSheet.pre_data_rows == importer.data_start_row
class TestImport(si.SheetImporter): fields = (si.Field('Name', 'name', p.chain(p.parse_text, p.validate_not_empty)), ) def modify_record(self, record): if record['name'] == 'Ed': # Filter out Eds return None record['age'] = 27 return record
def test_sheet_import_data_validation(self): parse_records = parse_records_with( si.SheetImporter([ si.Field('Name', 'name', p.chain(p.parse_text, p.validate_not_empty)), si.Field('Age', 'age', p.parse_int), ])) assert parse_records([['Name', 'Age']]) == [] assert parse_records([['Name', 'Age'], ['Ed', '27']]) == [{ 'name': 'Ed', 'age': 27 }] ed_n_joe = [ { 'name': 'Ed', 'age': 27 }, { 'name': 'Joe', 'age': 32 }, ] # Test with and without extra columns or formatting assert parse_records([['Name', 'Age'], ['Ed', '27'], ['Joe', '32']]) == ed_n_joe assert parse_records([['Name', 'Age'], ['Ed', '27.0'], ['Joe', '32.00']]) == ed_n_joe assert parse_records([['Name', 'Age'], ['Ed', ' 27 '], ['Joe', ' 32.0 ']]) == ed_n_joe assert parse_records([['Name', 'Age'], ['Ed', '27', '2'], ['Joe', '32', '3']]) == ed_n_joe # invalid header and/or data assert_import_errors({'Expected "Age" in header cell B1.'}, lambda: parse_records([['Name', 'SSN']])) assert_import_errors( {'Expected "Age" in header cell B1.' }, # header errors beat data errors lambda: parse_records([['Name', 'SSN'], ['', '2']])) assert_import_errors( { 'Unexpected value in cell A2: must not be empty', 'Unexpected value in cell B2: must be an integer' }, lambda: parse_records([['Name', 'Age'], ['', '3.9']])) assert_import_errors( { 'Unexpected value in cell A3: must not be empty', 'Unexpected value in cell B3: must be an integer' }, lambda: parse_records([['Name', 'Age'], ['Ed', '27'], ['', '3.9']]))
def test_sheet_import_data_validation(self): parse_records = parse_records_with(si.SheetImporter([ si.Field('Name', 'name', p.chain(p.parse_text, p.validate_not_empty)), si.Field('Age', 'age', p.parse_int), ])) assert parse_records([['Name', 'Age']]) == [] assert parse_records([['Name', 'Age'], ['Ed', '27']]) == [{'name': 'Ed', 'age': 27}] ed_n_joe = [ {'name': 'Ed', 'age': 27}, {'name': 'Joe', 'age': 32}, ] # Test with and without extra columns or formatting assert parse_records([['Name', 'Age'], ['Ed', '27'], ['Joe', '32']]) == ed_n_joe assert parse_records([['Name', 'Age'], ['Ed', '27.0'], ['Joe', '32.00']]) == ed_n_joe assert parse_records([['Name', 'Age'], ['Ed', ' 27 '], ['Joe', ' 32.0 ']]) == ed_n_joe assert parse_records([['Name', 'Age'], ['Ed', '27', '2'], ['Joe', '32', '3']]) == ed_n_joe # invalid header and/or data assert_import_errors( {'Expected "Age" in header cell B1.'}, lambda: parse_records([['Name', 'SSN']]) ) assert_import_errors( {'Expected "Age" in header cell B1.'}, # header errors beat data errors lambda: parse_records([['Name', 'SSN'], ['', '2']]) ) assert_import_errors( {'Unexpected value in cell A2: must not be empty', 'Unexpected value in cell B2: must be an integer'}, lambda: parse_records([['Name', 'Age'], ['', '3.9']]) ) assert_import_errors( {'Unexpected value in cell A3: must not be empty', 'Unexpected value in cell B3: must be an integer'}, lambda: parse_records([['Name', 'Age'], ['Ed', '27'], ['', '3.9']]) )
def test_chain(): assert p.chain(lambda _: 'Python')(0.99) == 'Python' assert p.chain(lambda x: x + 1, lambda x: x - 1)(0) == 0 assert p.chain(lambda x: x * 2, lambda x: x - 1)(1) == 1 assert p.chain(lambda x: x - 1, lambda x: x * 2)(1) == 0