def test_read_illegal(self, input_dir: Path):
     with open(str(input_dir / 'example_illegal_empty_cell.csv')) as f:
         with pytest.raises(ValueError):
             read_csv(f)
     with open(str(input_dir / 'example_illegal_str_in_num_col.csv')) as f:
         with pytest.raises(ValueError):
             read_csv(f)
    def test_read_csv_missing_separators(self, input_dir: Path):
        with open(str(input_dir / 'example_missing_separators.csv')) as f:
            try:
                read_csv(f, missing_separators='ignore')  # 'ignore' is default
            except pd.errors.ParserError as e:
                pytest.fail(f'No exception should be thrown with missing_separators=\'ignore\'... {e}')

        # should fail with pandas.errors.ParserError
        with open(str(input_dir / 'example_missing_separators.csv')) as f:
            with pytest.raises(pd.errors.ParserError) as excinfo:
                read_csv(f, missing_separators='raise')
            assert "Error tokenizing data" in str(excinfo.value)
 def test_read_csv_comma_decimal(self, input_dir):
     # PS-14  Can't control decimal separators when reading CSV files
     with open(str(input_dir / 'example_comma_decimal.csv')) as f:
         b = read_csv(f, decimal=',')
     assert len(b.tables) == 3
     t = b.tables[0]
     df = t.df
     assert df.iloc[4, 2] == 9.876
 def test_read_csv_no_extra_delimiters_on_tables(self, input_dir):
     # PS-19 Reading from CSV can fail if not enough column delimiters on first line of CSV file
     with open(str(input_dir / 'example_comma_decimal.csv')) as f:
         b = read_csv(f)
     assert len(b.tables) == 3
     t = b.tables[0]
     df = t.df
     assert df.iloc[4, 2] == 9.876
    def test_read_csv_utf8bom_with_open(self, input_dir: Path):
        # old approach where the user wraps with "with open"
        with open(str(input_dir / 'example_saveas_unicode_UTF8-BOM.csv')) as f:
            try:
                bundle = read_csv(f)
            except pd.errors.ParserError as e:
                pytest.fail(f'No exception should be thrown with missing_separators=\'ignore\'... {e}')

            assert len(bundle.tables) == 1  # load_ULS1_incl_LF
    def test_read_csv_with_header(self, csv_path_with_header: Path):
        with open(str(csv_path_with_header)) as f:
            b = read_csv(f)
        assert len(b.tables) == 2
        t = b.tables[0]
        assert t.name == 'layer_cake'
        assert t.col_names == ['height', 'segment_top', 'diameter', 'colour']
        assert t.col_units == ['mm', 'mm', 'cm', 'text']
        assert t.destinations == ['birthday_party']
        df = t.df
        assert df.iloc[2, 0] == 65
        assert df.iloc[2, 1] == 75
        assert df.loc[0, 'colour'] == 'pink'

        t2 = b.tables[1]
        assert t2.name == 'beverages'
        assert t.destinations == ['birthday_party']
        df = t2.df
        assert df.loc[0, 'name'] == 'orange juice'
        assert df.loc[1, 'volume'] == 1000
        assert df.loc[3, 'quantity'] == 24
    def test_read_csv(self, csv_path: Path):
        with open(str(csv_path)) as f:
            b = read_csv(f)
        assert len(b.tables) == 4
        t = b.tables[0]
        assert t.name == 'farm_animals'
        assert t.col_names == ['species', 'n_legs', 'avg_weight']
        assert t.col_units == ['text', '-', 'kg']
        assert t.destinations == ['your_farm', 'my_farm', 'farms_galore']
        df = t.df
        assert df.iloc[4, 2] == 9
        assert df.iloc[1, 2] == '{{(* age 30)}}'
        assert np.isnan(df.iloc[2, 2])
        assert np.isnan(df.iloc[3, 1])  # PS-15 Accept 'NaN' as NaN marker
        assert df.iloc[5, 0] == '1234'  # PS-28 Numerical data in text columns gets read
        assert df.shape == (6, 3)
        t2 = b.tables[2]
        assert t2.name == 'taxidermy'
        assert t2.df.iloc[3, 3] == pd.Timestamp('2012-05-01 12:34')
        assert pd.isna(t2.df.iloc[1, 3])
        assert t.evaluate_expressions({'age': 3}).df.iloc[1, 2] == 90

        assert len(b.tables[3]) == 0  # PS-5 Empty StarTables are unjustly ignored / omitted
 def test_read_csv_faulty(self, input_dir: Path):
     # new approach where the user relies on startables/pandas to do the open/close
     # should fail with ValueError
     with pytest.raises(ValueError) as excinfo:
         read_csv(str(input_dir / 'example_huge_file_missing_unit_and_values.csv'))
     assert "Illegal empty cell in numerical column '-' of table 'load_markovMatrixSetup" in str(excinfo.value)
 def test_read_csv_utf8bom_without_open(self, input_dir: Path):
     # new approach where the user relys on startables/pandas to do the open/close
     bundle = read_csv(str(input_dir / 'example_saveas_unicode_UTF8-BOM.csv'))
     assert len(bundle.tables) == 1  # load_ULS1_incl_LF
Exemplo n.º 10
0
 def test_pop_tables_by_name(self, csv_path: Path):
     with open(str(csv_path)) as f:
         b = read_csv(f)
     taxidermy_tables = b.pop_tables(name='taxidermy')
     assert len(taxidermy_tables) == 1
     assert len(b.tables) == 3
Exemplo n.º 11
0
 def test_pop_tables_noargs(self, csv_path: Path):
     with open(str(csv_path)) as f:
         b = read_csv(f)
     p = b.pop_tables()
     assert len(p) == 4
     assert len(b.tables) == 0
Exemplo n.º 12
0
 def some_bundle(self, csv_path: Path):
     with open(str(csv_path)) as f:
         return read_csv(f)