Example #1
0
 def test_create_all_sheets(self,
                            m_pkt,
                            m_cv,
                            m_sr,
                            m_bn,
                            m_cn,
                            m_idx,
                            m_remove):
     """Remove all worksheets, then create all worksheets."""
     with mock.patch('app.seeds.excel.SeedsWorkbook.create_all_sheets'):
         swb = SeedsWorkbook()
     swb.create_all_sheets()
     assert m_remove.called
     assert swb.indexes._ws is swb._wb['Indexes']
     assert m_idx.called
     assert swb.common_names._ws is swb._wb['Common Names']
     assert m_cn.called
     assert swb.botanical_names._ws is swb._wb['Botanical Names']
     assert m_bn.called
     assert swb.section._ws is swb._wb['Section']
     assert m_sr.called
     assert swb.cultivars._ws is swb._wb['Cultivars']
     assert m_cv.called
     assert swb.packets._ws is swb._wb['Packets']
     assert m_pkt.called
Example #2
0
 def test_load(self, m_lasfw, m_lw):
     """Load a workbook into _wb, and load all sheets from it."""
     swb = SeedsWorkbook()
     wb = Workbook()
     m_lw.return_value = wb
     swb.load('file.xlsx')
     m_lw.assert_called_with('file.xlsx')
     assert swb._wb is wb
     assert m_lasfw.called
Example #3
0
 def test_add_all_data_to_sheets(self,
                                 m_pkt,
                                 m_cv,
                                 m_sr,
                                 m_bn,
                                 m_cn,
                                 m_idx,
                                 m_a):
     """Call <sheet>.save_to_db(<obj>.query.all()) for each worksheet."""
     messages = StringIO()
     swb = SeedsWorkbook()
     swb.add_all_data_to_sheets(stream=messages)
     m_a.assert_any_call(m_pkt.all(), stream=messages)
     m_a.assert_any_call(m_cv.all(), stream=messages)
     m_a.assert_any_call(m_sr.all(), stream=messages)
     m_a.assert_any_call(m_bn.all(), stream=messages)
     m_a.assert_any_call(m_cn.all(), stream=messages)
     m_a.assert_any_call(m_idx.all(), stream=messages)
Example #4
0
 def test_save_all_sheets_to_db(self,
                                m_pkt,
                                m_cv,
                                m_sr,
                                m_bn,
                                m_cn,
                                m_idx):
     """Call save_to_db for each worksheet."""
     messages = StringIO()
     swb = SeedsWorkbook()
     swb.save_all_sheets_to_db(stream=messages)
     messages.seek(0)
     msgs = messages.read()
     m_idx.assert_called_with(stream=messages)
     m_cn.assert_called_with(stream=messages)
     m_bn.assert_called_with(stream=messages)
     m_cv.assert_called_with(stream=messages)
     m_sr.assert_called_with(stream=messages)
     m_pkt.assert_called_with(stream=messages)
     assert '-- BEGIN saving all worksheets to database. --' in msgs
     assert '-- END saving all worksheets to database. --' in msgs
Example #5
0
 def test_load_all_sheets_from_workbook(self,
                                        m_pkt,
                                        m_cv,
                                        m_sr,
                                        m_bn,
                                        m_cn,
                                        m_idx):
     """Load all sheets from self._wb into appropriate attributes."""
     swb = SeedsWorkbook()
     swb.load_all_sheets_from_workbook()
     assert swb.indexes._ws is swb._wb['Indexes']
     assert m_idx.called
     assert swb.common_names._ws is swb._wb['Common Names']
     assert m_cn.called
     assert swb.botanical_names._ws is swb._wb['Botanical Names']
     assert m_bn.called
     assert swb.section._ws is swb._wb['Section']
     assert m_sr.called
     assert swb.cultivars._ws is swb._wb['Cultivars']
     assert m_cv.called
     assert swb.packets._ws is swb._wb['Packets']
     assert m_pkt.called
Example #6
0
 def test_beautify_all_sheets(self, m_b):
     """Call beautify on all sheets in workbook."""
     swb = SeedsWorkbook()
     swb.beautify_all_sheets(width=42, height=12)
     assert m_b.call_count == 6
     m_b.assert_any_call(width=42, height=12)
Example #7
0
 def test_getitem(self):
     """Forward contained workbook when accessing via []."""
     swb = SeedsWorkbook()
     swb._wb = mock.MagicMock()
     swb._wb = {'key': 'value'}
     assert swb['key'] == 'value'
Example #8
0
 def test_save(self, m_s):
     """Beautify a SeedsWorkbook and to a file."""
     swb = SeedsWorkbook()
     swb.save('file.xlsx')
     m_s.assert_called_with('file.xlsx')
Example #9
0
def excel(load=None, save=None, logfile=None):
    """Interact with the excel module to utilize spreadsheets."""
    if logfile:
        if os.path.exists(logfile):
            print('WARNING: The file specified by logfile \'{0}\' already '
                  'exists. Would you like to overwrite it?'
                  .format(logfile))
            while True:
                choice = input('Y/n: ').upper()
                if choice == 'Y' or choice == 'YES' or choice == '':
                    break
                elif choice == 'N' or choice == 'NO':
                    print('Action cancelled due to invalid logfile.')
                    sys.exit(0)
                else:
                    print('Invalid input, please answer \'Y\' if you want to '
                          'overwrite the file, or \'N\' if you do not. Would'
                          'you like to overwite the file \'{0}\'?'
                          .format(logfile))
        stream = open(logfile, 'w', encoding='utf-8')
    else:
        stream = sys.stdout
    if load and save:
        raise ValueError('Cannot load and save at the same time!')
    if load:
        if os.path.exists(load):
            swb = SeedsWorkbook()
            swb.load(load)
        else:
            raise FileNotFoundError('The file \'{0}\' does not exist!'
                                    .format(load))
        swb.save_all_sheets_to_db(stream=stream)
    if save:
        if os.path.exists(save):
            print('WARNING: The file {0} exists. Would you like to overwrite '
                  'it?'.format(save))
            while True:
                choice = input('Y/n:  ').upper()
                if choice == 'Y' or choice == 'YES' or choice == '':
                    break
                elif choice == 'N' or choice == 'NO':
                    print('Save cancelled. Exiting.')
                    sys.exit(0)
                else:
                    print('Invalid input, please answer \'Y\' if you want to '
                          'overwrite the file, or \'N\' if you do not. Would'
                          'you like to overwite the file \'{0}\'?'
                          .format(save))
        swb = SeedsWorkbook()
        print('*** BEGIN saving all data to worksheet \'{0}\'. ***'
              .format(save), file=stream)
        swb.add_all_data_to_sheets(stream=stream)
        swb.beautify_all_sheets()
        swb.save(save)
        print('*** END saving all data to worksheet \'{0}\'. ***'
              .format(save), file=stream)
    if stream is not sys.stdout:
        stream.close()