Ejemplo n.º 1
0
    def test_xlsx_input_utf8(self):
        """This is an xlsx file saved by OpenOffice. It seems to use UTF8 internally."""
        csvinput = XLSXInput(
            input_name='flattentool/tests/fixtures/xlsx/unicode.xlsx')

        csvinput.read_sheets()
        assert list(csvinput.get_sheet_lines('main'))[0]['id'] == 'éαГ😼𝒞人'
    def test_xlsx_input_utf8(self):
        """This is an xlsx file saved by OpenOffice. It seems to use UTF8 internally."""
        xlsxinput = XLSXInput(
            input_name="flattentool/tests/fixtures/xlsx/unicode.xlsx")

        xlsxinput.read_sheets()
        assert list(xlsxinput.get_sheet_lines("main"))[0]["id"] == "éαГ😼𝒞人"
Ejemplo n.º 3
0
 def test_xlsx_no_main_sheet(self):
     xlsxinput = XLSXInput(
         input_name='flattentool/tests/fixtures/xlsx/basic.xlsx',
         main_sheet_name='notmain')
     with pytest.raises(ValueError) as e:
         xlsxinput.read_sheets()
     assert 'Main sheet "notmain" not found in workbook.' in text_type(e)
Ejemplo n.º 4
0
 def test_xlsx_no_file(self, tmpdir):
     xlsxinput = XLSXInput(input_name=tmpdir.strpath.join('test.xlsx'), main_sheet_name='main')
     if sys.version > '3':
         with pytest.raises(FileNotFoundError):
             xlsxinput.read_sheets()
     else:
         with pytest.raises(IOError):
             xlsxinput.read_sheets()
Ejemplo n.º 5
0
    def test_xlsx_input_integer(self):
        xlsxinput = XLSXInput(input_name='flattentool/tests/fixtures/xlsx/integer.xlsx', main_sheet_name='main')
        assert xlsxinput.main_sheet_name == 'main'

        xlsxinput.read_sheets()

        assert list(xlsxinput.get_main_sheet_lines()) == \
            [{'colA': 1}]
        assert xlsxinput.sub_sheet_names == []
    def test_xlsx_vertical(self):
        xlsxinput = XLSXInput(input_name='flattentool/tests/fixtures/xlsx/basic_transpose.xlsx', vertical_orientation=True)

        xlsxinput.read_sheets()

        assert xlsxinput.sub_sheet_names == ['main', 'subsheet']
        assert list(xlsxinput.get_sheet_lines('main')) == \
            [{'colA': 'cell1', 'colB': 'cell2'}, {'colA': 'cell3', 'colB': 'cell4'}]
        assert list(xlsxinput.get_sheet_lines('subsheet')) == \
            [{'colC': 'cell5', 'colD': 'cell6'}, {'colC': 'cell7', 'colD': 'cell8'}]
Ejemplo n.º 7
0
    def test_xlsx_input_integer(self):
        xlsxinput = XLSXInput(
            input_name='flattentool/tests/fixtures/xlsx/integer.xlsx',
            main_sheet_name='main')
        assert xlsxinput.main_sheet_name == 'main'

        xlsxinput.read_sheets()

        assert list(xlsxinput.get_main_sheet_lines()) == \
            [{'colA': 1}]
        assert xlsxinput.sub_sheet_names == []
Ejemplo n.º 8
0
    def test_xlsx_input(self):
        xlsxinput = XLSXInput(input_name='flattentool/tests/fixtures/xlsx/basic.xlsx', main_sheet_name='main')
        assert xlsxinput.main_sheet_name == 'main'

        xlsxinput.read_sheets()

        assert list(xlsxinput.get_main_sheet_lines()) == \
            [{'colA': 'cell1', 'colB': 'cell2'}, {'colA': 'cell3', 'colB': 'cell4'}]
        assert xlsxinput.sub_sheet_names == ['subsheet']
        assert list(xlsxinput.get_sheet_lines('subsheet')) == \
            [{'colC': 'cell5', 'colD': 'cell6'}, {'colC': 'cell7', 'colD': 'cell8'}]
    def test_xlsx_input_integer2(self):
        xlsxinput = XLSXInput(input_name='flattentool/tests/fixtures/xlsx/integer2.xlsx')

        xlsxinput.read_sheets()

        assert list(xlsxinput.get_sheet_lines('Sheet1')) == \
            [{'activity-status/@code': 2}]
        # This is a float, but is converted to an int in the unflatten step, see
        # test_input_SpreadsheetInput_unflatten.py
        # 'Basic with float'
        assert type(list(xlsxinput.get_sheet_lines('Sheet1'))[0]['activity-status/@code']) == float
        assert xlsxinput.sub_sheet_names == ['Sheet1']
    def test_xlsx_input_formula(self):
        """ When a forumla is present, we should use the value, rather than the
        formula itself. """

        xlsxinput = XLSXInput(input_name='flattentool/tests/fixtures/xlsx/formula.xlsx')

        xlsxinput.read_sheets()

        assert xlsxinput.sub_sheet_names == ['main', 'subsheet']
        assert list(xlsxinput.get_sheet_lines('main')) == \
            [{'colA': 1, 'colB': 2}, {'colA': 2, 'colB': 4}]
        assert list(xlsxinput.get_sheet_lines('subsheet')) == \
            [{'colC': 3, 'colD': 9}, {'colC': 4, 'colD': 12}]
Ejemplo n.º 11
0
    def test_bad_xlsx(self):
        """ XLSX file that is not a XLSX"""

        xlsxinput = XLSXInput(
            input_name='flattentool/tests/fixtures/xlsx/file.xlsx')

        try:
            xlsxinput.read_sheets()
        except Exception as e:
            assert str(
                e
            ) == "The supplied file has extension .xlsx but isn't an XLSX file."
            return

        assert False, "No Exception Raised"
    def test_xlsx_input(self):
        xlsxinput = XLSXInput(
            input_name="flattentool/tests/fixtures/xlsx/basic.xlsx")

        xlsxinput.read_sheets()

        assert xlsxinput.sub_sheet_names == ["main", "subsheet"]
        assert list(xlsxinput.get_sheet_lines("main")) == [
            {
                "colA": "cell1",
                "colB": "cell2"
            },
            {
                "colA": "cell3",
                "colB": "cell4"
            },
        ]
        assert list(xlsxinput.get_sheet_lines("subsheet")) == [
            {
                "colC": "cell5",
                "colD": "cell6"
            },
            {
                "colC": "cell7",
                "colD": "cell8"
            },
        ]
    def test_xlsx_input_formula(self):
        """ When a forumla is present, we should use the value, rather than the
        formula itself. """

        xlsxinput = XLSXInput(
            input_name="flattentool/tests/fixtures/xlsx/formula.xlsx")

        xlsxinput.read_sheets()

        assert xlsxinput.sub_sheet_names == ["main", "subsheet"]
        assert list(xlsxinput.get_sheet_lines("main")) == [
            {
                "colA": 1,
                "colB": 2
            },
            {
                "colA": 2,
                "colB": 4
            },
        ]
        assert list(xlsxinput.get_sheet_lines("subsheet")) == [
            {
                "colC": 3,
                "colD": 9
            },
            {
                "colC": 4,
                "colD": 12
            },
        ]
    def test_xlsx_include_ignore(self):
        xlsxinput = XLSXInput(input_name='flattentool/tests/fixtures/xlsx/basic_meta.xlsx', 
                              include_sheets=['Meta'], vertical_orientation=True
                             )
        xlsxinput.read_sheets()
        assert xlsxinput.sub_sheet_names == ['Meta']
        assert list(xlsxinput.get_sheet_lines('Meta')) == \
            [{'a': 'a1', 'b': 'b1', 'c': 'c1'}]

        xlsxinput = XLSXInput(input_name='flattentool/tests/fixtures/xlsx/basic_meta.xlsx', 
                              exclude_sheets=['Meta'])
        xlsxinput.read_sheets()

        assert xlsxinput.sub_sheet_names == ['main', 'subsheet']
        assert list(xlsxinput.get_sheet_lines('main')) == \
            [{'colA': 'cell1', 'colB': 'cell2'}, {'colA': 'cell3', 'colB': 'cell4'}]
        assert list(xlsxinput.get_sheet_lines('subsheet')) == \
            [{'colC': 'cell5', 'colD': 'cell6'}, {'colC': 'cell7', 'colD': 'cell8'}]
Ejemplo n.º 15
0
 def test_xlsx_no_file(self, tmpdir):
     xlsxinput = XLSXInput(input_name=tmpdir.strpath.join('test.xlsx'))
     if sys.version > '3':
         with pytest.raises(FileNotFoundError):
             xlsxinput.read_sheets()
     else:
         with pytest.raises(IOError):
             xlsxinput.read_sheets()
Ejemplo n.º 16
0
    def test_xlsx_input(self):
        xlsxinput = XLSXInput(
            input_name='flattentool/tests/fixtures/xlsx/basic.xlsx')

        xlsxinput.read_sheets()

        assert xlsxinput.sub_sheet_names == ['main', 'subsheet']
        assert list(xlsxinput.get_sheet_lines('main')) == \
            [{'colA': 'cell1', 'colB': 'cell2'}, {'colA': 'cell3', 'colB': 'cell4'}]
        assert list(xlsxinput.get_sheet_lines('subsheet')) == \
            [{'colC': 'cell5', 'colD': 'cell6'}, {'colC': 'cell7', 'colD': 'cell8'}]
Ejemplo n.º 17
0
    def test_xlsx_input_integer(self):
        xlsxinput = XLSXInput(
            input_name='flattentool/tests/fixtures/xlsx/integer.xlsx')

        xlsxinput.read_sheets()

        assert list(xlsxinput.get_sheet_lines('main')) == \
            [{'colA': 1}]
        if sys.version_info[0] == 2:
            assert type(list(
                xlsxinput.get_sheet_lines('main'))[0]['colA']) == long
        else:
            assert type(list(
                xlsxinput.get_sheet_lines('main'))[0]['colA']) == int
        assert xlsxinput.sub_sheet_names == ['main']
    def test_xlsx_input_integer(self):
        xlsxinput = XLSXInput(input_name='flattentool/tests/fixtures/xlsx/integer.xlsx')

        xlsxinput.read_sheets()

        assert list(xlsxinput.get_sheet_lines('main')) == \
            [{'colA': 1}]
        if sys.version_info[0] == 2:
            assert type(list(xlsxinput.get_sheet_lines('main'))[0]['colA']) == long
        else:
            assert type(list(xlsxinput.get_sheet_lines('main'))[0]['colA']) == int
        assert xlsxinput.sub_sheet_names == ['main']
Ejemplo n.º 19
0
    def test_xlsx_input_formula(self):
        """ When a forumla is present, we should use the value, rather than the
        formula itself. """

        xlsxinput = XLSXInput(
            input_name='flattentool/tests/fixtures/xlsx/formula.xlsx')

        xlsxinput.read_sheets()

        assert xlsxinput.sub_sheet_names == ['main', 'subsheet']
        assert list(xlsxinput.get_sheet_lines('main')) == \
            [{'colA': 1, 'colB': 2}, {'colA': 2, 'colB': 4}]
        assert list(xlsxinput.get_sheet_lines('subsheet')) == \
            [{'colC': 3, 'colD': 9}, {'colC': 4, 'colD': 12}]
Ejemplo n.º 20
0
    def test_xlsx_input_integer2(self):
        xlsxinput = XLSXInput(
            input_name='flattentool/tests/fixtures/xlsx/integer2.xlsx')

        xlsxinput.read_sheets()

        assert list(xlsxinput.get_sheet_lines('Sheet1')) == \
            [{'activity-status/@code': 2}]
        # This is a float, but is converted to an int in the unflatten step, see
        # test_input_SpreadsheetInput_unflatten.py
        # 'Basic with float'
        assert type(
            list(xlsxinput.get_sheet_lines('Sheet1'))[0]
            ['activity-status/@code']) == float
        assert xlsxinput.sub_sheet_names == ['Sheet1']
    def test_xlsx_input_integer2(self):
        xlsxinput = XLSXInput(
            input_name="flattentool/tests/fixtures/xlsx/integer2.xlsx")

        xlsxinput.read_sheets()

        assert list(xlsxinput.get_sheet_lines("Sheet1")) == [{
            "activity-status/@code":
            2
        }]
        # This is a float, but is converted to an int in the unflatten step, see
        # test_input_SpreadsheetInput_unflatten.py
        # 'Basic with float'
        assert (type(
            list(xlsxinput.get_sheet_lines("Sheet1"))[0]
            ["activity-status/@code"]) == float)
        assert xlsxinput.sub_sheet_names == ["Sheet1"]
    def test_xlsx_input_types(self):
        xlsxinput = XLSXInput(
            input_name="flattentool/tests/fixtures/xlsx/types.xlsx")

        xlsxinput.read_sheets()

        assert list(xlsxinput.get_sheet_lines("main")) == [{
            "colInt":
            1,
            "colFloat":
            1.2,
            "colDate":
            datetime.datetime(2020, 3, 5),
            "colDateTime":
            datetime.datetime(2020, 2, 7, 16, 41, 0, 1),
        }]
        assert type(list(
            xlsxinput.get_sheet_lines("main"))[0]["colInt"]) == int
        assert type(list(
            xlsxinput.get_sheet_lines("main"))[0]["colFloat"]) == float
        assert xlsxinput.sub_sheet_names == ["main"]
Ejemplo n.º 23
0
    def test_xlsx_input_utf8(self):
        """This is an xlsx file saved by OpenOffice. It seems to use UTF8 internally."""
        xlsxinput = XLSXInput(input_name='flattentool/tests/fixtures/xlsx/unicode.xlsx', main_sheet_name='main')

        xlsxinput.read_sheets()
        assert list(xlsxinput.get_main_sheet_lines())[0]['id'] == 'éαГ😼𝒞人'
Ejemplo n.º 24
0
 def test_xlsx_no_main_sheet(self):
     xlsxinput = XLSXInput(input_name='flattentool/tests/fixtures/xlsx/basic.xlsx', main_sheet_name='notmain')
     with pytest.raises(ValueError) as e:
         xlsxinput.read_sheets()
     assert 'Main sheet "notmain" not found in workbook.' in text_type(e)
Ejemplo n.º 25
0
    def test_xlsx_include_ignore(self):
        xlsxinput = XLSXInput(
            input_name='flattentool/tests/fixtures/xlsx/basic_meta.xlsx',
            include_sheets=['Meta'],
            vertical_orientation=True)
        xlsxinput.read_sheets()
        assert xlsxinput.sub_sheet_names == ['Meta']
        assert list(xlsxinput.get_sheet_lines('Meta')) == \
            [{'a': 'a1', 'b': 'b1', 'c': 'c1'}]

        xlsxinput = XLSXInput(
            input_name='flattentool/tests/fixtures/xlsx/basic_meta.xlsx',
            exclude_sheets=['Meta'])
        xlsxinput.read_sheets()

        assert xlsxinput.sub_sheet_names == ['main', 'subsheet']
        assert list(xlsxinput.get_sheet_lines('main')) == \
            [{'colA': 'cell1', 'colB': 'cell2'}, {'colA': 'cell3', 'colB': 'cell4'}]
        assert list(xlsxinput.get_sheet_lines('subsheet')) == \
            [{'colC': 'cell5', 'colD': 'cell6'}, {'colC': 'cell7', 'colD': 'cell8'}]
    def test_xlsx_include_ignore(self):
        xlsxinput = XLSXInput(
            input_name="flattentool/tests/fixtures/xlsx/basic_meta.xlsx",
            include_sheets=["Meta"],
            vertical_orientation=True,
        )
        xlsxinput.read_sheets()
        assert xlsxinput.sub_sheet_names == ["Meta"]
        assert list(xlsxinput.get_sheet_lines("Meta")) == [{
            "a": "a1",
            "b": "b1",
            "c": "c1"
        }]

        xlsxinput = XLSXInput(
            input_name="flattentool/tests/fixtures/xlsx/basic_meta.xlsx",
            exclude_sheets=["Meta"],
        )
        xlsxinput.read_sheets()

        assert xlsxinput.sub_sheet_names == ["main", "subsheet"]
        assert list(xlsxinput.get_sheet_lines("main")) == [
            {
                "colA": "cell1",
                "colB": "cell2"
            },
            {
                "colA": "cell3",
                "colB": "cell4"
            },
        ]
        assert list(xlsxinput.get_sheet_lines("subsheet")) == [
            {
                "colC": "cell5",
                "colD": "cell6"
            },
            {
                "colC": "cell7",
                "colD": "cell8"
            },
        ]
 def test_xlsx_no_file(self, tmpdir):
     xlsxinput = XLSXInput(input_name=tmpdir.join("test.xlsx").strpath)
     with pytest.raises(FileNotFoundError):
         xlsxinput.read_sheets()