示例#1
0
    def test_mergeStuff(self):
        '''
        Test the method tradestyle.TradeFormat.mergeStuff. Specificaly test that a merged cell
        group exists where it is should be after saving and opening an xlsx file.
        '''
        wb = Workbook()
        ws = wb.active

        begin = (1, 1)
        end = (5, 5)
        anchor = (3, 3)
        t = TradeFormat(wb)

        b, e = t.mergeStuff(ws, begin=begin, end=end, anchor=anchor)

        dispath = "out/SCHNOrK.xlsx"
        if os.path.exists(dispath):
            os.remove(dispath)
        wb.save(dispath)

        wb2 = load_workbook(dispath)
        ws2 = wb2.active

        x = ws2.merged_cells.ranges

        # print(b, (x[0].min_col, x[0].min_row))
        # print(e, (x[0].max_col, x[0].max_row))
        self.assertEqual(b, (x[0].min_col, x[0].min_row))
        self.assertEqual(e, (x[0].max_col, x[0].max_row))
        os.remove(dispath)
示例#2
0
    def test_TradeFormatAddNamedStyle(self):
        '''
        Test the object formation and the creation of the styles in __init__ and addNamedStyle for
        the Workbook. Specifically test that the named style is created in the workbook and is
        still there when you close it and open it.
        '''

        wb = Workbook()
        ws = wb.active
        tf = TradeFormat(wb)

        # Use a list to verify the order
        styleList = list(tf.styles.keys())
        if not os.path.exists("out/"):
            os.mkdir("out/")

        dispath = "out/SCHNOrK.xlsx"
        if os.path.exists(dispath):
            os.remove(dispath)

        # Write one cell for each named style in TradeFormat
        for i, key in enumerate(styleList):
            #     print(key, c((1, i+1)))
            ws[c((1, i + 1))] = key
            ws[c((1, i + 1))].style = tf.styles[key]
        wb.save(dispath)

        wb2 = load_workbook(dispath)
        ws2 = wb2.active

        # Open it back up and check the the named styles are where we think they are
        for i, key in enumerate(styleList):
            #     print(key,ws2[c((1, i+1))].style )
            self.assertEqual(key, ws2[c((1, i + 1))].style)
示例#3
0
    def test_formatTrade(self):
        '''
        Test the method TradeFormat.formatTrade. Specifically test that each of the elements in
        srf.tfcolumns has a corresponding elementcorrectly styled in the re-opened workbook and
        that each merged element in srf.tfcolumns is found in the the worsheet.
        '''
        wb = Workbook()
        ws = wb.active

        t = TradeFormat(wb)
        srf = SumReqFields()
        ws = wb.active

        t.formatTrade(ws, srf)

        dispath = "out/SCHNOrK.xlsx"
        if os.path.exists(dispath):
            os.remove(dispath)
        wb.save(dispath)

        wb2 = load_workbook(dispath)
        ws2 = wb2.active

        # test that each listed item in srf.tfcolumns has a corresponding style set in the
        # appropriate cell of the re-opened workbook
        for x in srf.tfcolumns:
            address = srf.tfcolumns[x][0]
            st = srf.tfcolumns[x][1]
            if isinstance(address, list):
                self.assertEqual(ws2[c(address[0])].style, st)
            else:
                self.assertEqual(ws2[c(address)].style, st)

        # test that each list element has a corresponding merged cell group in the worbook
        listofmerge = [
            c(srf.tfcolumns[x][0][0], srf.tfcolumns[x][0][1])
            for x in srf.tfcolumns if isinstance(srf.tfcolumns[x][0], list)
        ]
        wsmerged = ws.merged_cells.ranges
        self.assertEqual(len(listofmerge), len(wsmerged))
        for msmerge in wsmerged:
            self.assertTrue(str(msmerge) in listofmerge)