def test_populate_cols_dict_no_titles(self): """Raise a ValueError if there is no data in the first row.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) with pytest.raises(ValueError): sws.populate_cols_dict()
def test_save_row_to_db(self): """save_row_to_db should be an abstract method in this class.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) with pytest.raises(NotImplementedError): sws.save_row_to_db(None)
def test_setup_new_no_titles(self): """Raise a value error if _setup is run without titles on new sheet.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) with pytest.raises(ValueError): sws._setup()
def test_add_not_iterable(self): """Do not suppress TypeError if given non-iterable.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) with pytest.raises(TypeError): sws.add(42)
def test_add_one(self): """add_one should be an abstract method in this class.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) with pytest.raises(NotImplementedError): sws.add_one(None)
def test_add_bad_data(self, m_ao): """Warn user when iterable contains bad types instead of halting.""" m_ao.side_effect = TypeError('Bad data, yo!') wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) with pytest.warns(UserWarning): sws.add((1, 2, 3, 4)) assert m_ao.call_count == 4
def test_setup_existing(self, m_s): """Call _setup with no data if sheet titles already populated.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) sws.set_column_titles(('Index', 'Description')) iws = IndexesWorksheet(sws._ws) iws.setup() assert m_s.call_args_list == [mock.call()]
def test_setup_existing(self, m_s): """Call _setup with no data if titles row already populated.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) sws.set_column_titles(('One', 'Two', 'Three')) cnws = CommonNamesWorksheet(sws._ws) cnws.setup() assert m_s.call_args_list == [mock.call()]
def test_setup_existing(self, m_pcd, m_sct): """Do not call set_column_titles if data in first row already.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) sws._ws['A1'].value = 'One' sws._setup() assert m_pcd.called assert not m_sct.called
def test_setup_new(self, m_pcd, m_sct): """Call set_column_titles and populate_cols_dict when sheet blank.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) titles = ('One', 'Two', 'Three') sws._setup(titles) assert m_pcd.called assert m_sct.called_with(titles)
def test_setup_existing(self, m_s): """Call _setup with no data if titles already present.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) sws.set_column_titles(('One', 'Two', 'Three')) bnws = BotanicalNamesWorksheet(sws._ws) bnws.setup() assert m_s.call_args_list == [mock.call()]
def test_setup_existing(self, m_s): """Run _setup with no arguments if titles already exist.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) sws.set_column_titles(('One', 'Two', 'Three')) cvws = CultivarsWorksheet(sws._ws) cvws.setup() assert m_s.call_args_list == [mock.call()]
def test_set_column_titles_existing_sheet(self): """Raise a ValueError if there is already data in the top row.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) titles = ['One', 'Two', 'Three', 'Four'] sws._ws.append(titles) with pytest.raises(ValueError): sws.set_column_titles(titles)
def test_setup_existing(self, m_s): """Run _setup with no args on setup of sheet with titles.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) sws.set_column_titles(('One', 'Two', 'Three')) pws = PacketsWorksheet(sws._ws) pws.setup() assert m_s.call_args_list == [mock.call()]
def test_has_data(self): """Return True if there is data in cell A1, else False.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) assert not sws.has_data() sws._ws['A2'].value = 'Stuff' assert not sws.has_data() # Okay; should never happen in usage. sws._ws['A1'].value = 'More stuff' assert sws.has_data()
def test_setup_existing_with_titles(self, m_pcd, m_sct): """Set up as normal but discard new titles.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) sws._ws['A1'].value = 'One' titles = ('One', 'Two', 'Three') with pytest.warns(UserWarning): sws._setup(titles) assert m_pcd.called assert not m_sct.called
def test_set_column_titles(self): """Set the top row of the worksheet to a list of titles.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) titles = ['One', 'Two', 'Three', 'Four'] sws.set_column_titles(titles) assert sws._ws['A1'].value == 'One' assert sws._ws['B1'].value == 'Two' assert sws._ws['C1'].value == 'Three' assert sws._ws['D1'].value == 'Four'
def test_cell(self): """Return a cell given integer coordinates.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) ws['A1'].value = 'A1' ws['B1'].value = 'B1' ws['A2'].value = 'A2' assert sws.cell(1, 1) is ws['A1'] assert sws.cell(2, 1) is ws['A2'] assert sws.cell(1, 2) is ws['B1']
def test_populate_cols_dict(self): """Set cols keys to values in first row, and vals to col numbers.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) titles = ['One', 'Two', 'Three', 'Four'] sws.set_column_titles(titles) sws.populate_cols_dict() assert sws.cols['One'] == 1 assert sws.cols['Two'] == 2 assert sws.cols['Three'] == 3 assert sws.cols['Four'] == 4
def test_add(self, m_ao): """add should call add_one for each item in iterable.""" messages = StringIO() wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) sws.add(('Test',), stream=messages) m_ao.assert_called_with('Test', stream=messages) messages.seek(0) msgs = messages.read() assert '-- BEGIN adding data to SeedsWorksheet. --' in msgs assert '-- END adding data to SeedsWorksheet. --' in msgs
def test_beautify(self): """Configure worksheet to be more human-readable.""" wb = Workbook() ws = wb.active sws = SeedsWorksheet(ws) sws._ws.append(('One', 'Two', 'Three')) sws._ws.append(('Four', 'Five', 'Six')) sws.beautify(width=42, height=21) assert sws._ws.freeze_panes == 'A2' assert sws._ws.column_dimensions['A'].width == 42 assert sws._ws.column_dimensions['B'].width == 42 assert sws._ws.column_dimensions['C'].width == 42 assert sws._ws.row_dimensions[2].height == 21
def test_title_setter(self): """Set title of contained sheet.""" sheet = mock.MagicMock() sws = SeedsWorksheet(sheet) sws.title = 'Worksheet 2: Electric Boogaloo' assert sws._ws.title == 'Worksheet 2: Electric Boogaloo'
def test_freeze_title_row(self): """Freeze the top row of the worksheet.""" wb = Workbook() sws = SeedsWorksheet(wb.active) sws.freeze_title_row() assert sws._ws.freeze_panes == 'A2'