refactor tests, use pathlib instead of os
This commit is contained in:
parent
4844310707
commit
9c485d42b9
|
|
@ -1,4 +1,5 @@
|
||||||
import os
|
"""Tests for core read_ods function with different files"""
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import pytest
|
import pytest
|
||||||
|
|
@ -6,8 +7,8 @@ import pytest
|
||||||
from pandas_ods_reader import read_ods
|
from pandas_ods_reader import read_ods
|
||||||
|
|
||||||
|
|
||||||
root = os.path.dirname(os.path.abspath(__file__))
|
root = Path(__file__).parent
|
||||||
rsc = os.path.join(root, "rsc")
|
rsc = root / "rsc"
|
||||||
|
|
||||||
header_file = "example_headers.ods"
|
header_file = "example_headers.ods"
|
||||||
no_header_file = "example_no_headers.ods"
|
no_header_file = "example_no_headers.ods"
|
||||||
|
|
@ -18,81 +19,105 @@ mixed_dtypes_file = "mixed_dtypes.ods"
|
||||||
|
|
||||||
|
|
||||||
class TestOdsReader(object):
|
class TestOdsReader(object):
|
||||||
|
|
||||||
def test_header_file_with_int(self):
|
def test_header_file_with_int(self):
|
||||||
path = os.path.join(rsc, header_file)
|
|
||||||
|
path = rsc / header_file
|
||||||
df = read_ods(path, 1)
|
df = read_ods(path, 1)
|
||||||
|
|
||||||
assert isinstance(df, pd.DataFrame)
|
assert isinstance(df, pd.DataFrame)
|
||||||
assert len(df) == 10
|
assert len(df) == 10
|
||||||
assert (len(df.columns) == 5)
|
assert (len(df.columns) == 5)
|
||||||
|
|
||||||
def test_header_file_with_str(self):
|
def test_header_file_with_str(self):
|
||||||
path = os.path.join(rsc, header_file)
|
|
||||||
|
path = rsc / header_file
|
||||||
df = read_ods(path, "Sheet1")
|
df = read_ods(path, "Sheet1")
|
||||||
|
|
||||||
assert isinstance(df, pd.DataFrame)
|
assert isinstance(df, pd.DataFrame)
|
||||||
assert len(df) == 10
|
assert len(df) == 10
|
||||||
assert (len(df.columns) == 5)
|
assert (len(df.columns) == 5)
|
||||||
|
|
||||||
def test_header_file_with_cols(self):
|
def test_header_file_with_cols(self):
|
||||||
path = os.path.join(rsc, header_file)
|
|
||||||
|
path = rsc / header_file
|
||||||
columns = ["One", "Two", "Three", "Four", "Five"]
|
columns = ["One", "Two", "Three", "Four", "Five"]
|
||||||
df = read_ods(path, "Sheet1", columns=columns)
|
df = read_ods(path, "Sheet1", columns=columns)
|
||||||
|
|
||||||
assert list(df.columns) == columns
|
assert list(df.columns) == columns
|
||||||
assert len(df) == 10
|
assert len(df) == 10
|
||||||
assert (len(df.columns) == 5)
|
assert (len(df.columns) == 5)
|
||||||
|
|
||||||
def test_no_header_file_no_cols(self):
|
def test_no_header_file_no_cols(self):
|
||||||
path = os.path.join(rsc, no_header_file)
|
|
||||||
|
path = rsc / no_header_file
|
||||||
df = read_ods(path, 1, headers=False)
|
df = read_ods(path, 1, headers=False)
|
||||||
|
|
||||||
assert list(df.columns) == [
|
assert list(df.columns) == [
|
||||||
f"column.{i}" for i in range(len(df.columns))]
|
f"column.{i}" for i in range(len(df.columns))]
|
||||||
assert len(df) == 10
|
assert len(df) == 10
|
||||||
assert (len(df.columns) == 5)
|
assert (len(df.columns) == 5)
|
||||||
|
|
||||||
def test_no_header_file_with_cols(self):
|
def test_no_header_file_with_cols(self):
|
||||||
path = os.path.join(rsc, no_header_file)
|
|
||||||
|
path = rsc / no_header_file
|
||||||
columns = ["A", "B", "C", "D", "E"]
|
columns = ["A", "B", "C", "D", "E"]
|
||||||
df = read_ods(path, 1, headers=False, columns=columns)
|
df = read_ods(path, 1, headers=False, columns=columns)
|
||||||
|
|
||||||
assert list(df.columns) == columns
|
assert list(df.columns) == columns
|
||||||
assert len(df) == 10
|
assert len(df) == 10
|
||||||
|
|
||||||
def test_duplicated_column_names(self):
|
def test_duplicated_column_names(self):
|
||||||
path = os.path.join(rsc, duplicated_column_names_file)
|
|
||||||
|
path = rsc / duplicated_column_names_file
|
||||||
df = read_ods(path, 1)
|
df = read_ods(path, 1)
|
||||||
|
|
||||||
assert isinstance(df, pd.DataFrame)
|
assert isinstance(df, pd.DataFrame)
|
||||||
assert len(df.columns) == 4
|
assert len(df.columns) == 4
|
||||||
assert "website.1" in df.columns
|
assert "website.1" in df.columns
|
||||||
|
|
||||||
def test_header_file_col_len(self):
|
def test_header_file_col_len(self):
|
||||||
path = os.path.join(rsc, col_len_file)
|
|
||||||
|
path = rsc / col_len_file
|
||||||
df = read_ods(path, 1)
|
df = read_ods(path, 1)
|
||||||
|
|
||||||
assert isinstance(df, pd.DataFrame)
|
assert isinstance(df, pd.DataFrame)
|
||||||
assert len(df) == 10
|
assert len(df) == 10
|
||||||
assert (len(df.columns) == 5)
|
assert (len(df.columns) == 5)
|
||||||
|
|
||||||
def test_wrong_id_type(self):
|
def test_wrong_id_type(self):
|
||||||
path = os.path.join(rsc, header_file)
|
|
||||||
|
path = rsc / header_file
|
||||||
|
|
||||||
with pytest.raises(ValueError) as e_info:
|
with pytest.raises(ValueError) as e_info:
|
||||||
read_ods(path, 1.0)
|
read_ods(path, 1.0)
|
||||||
assert e_info.match("Sheet id has to be either `str` or `int`")
|
assert e_info.match("Sheet id has to be either `str` or `int`")
|
||||||
|
|
||||||
def test_non_existent_sheet(self):
|
def test_non_existent_sheet(self):
|
||||||
path = os.path.join(rsc, header_file)
|
|
||||||
|
path = rsc / header_file
|
||||||
sheet_name = "No_Sheet"
|
sheet_name = "No_Sheet"
|
||||||
|
|
||||||
with pytest.raises(ValueError) as e_info:
|
with pytest.raises(ValueError) as e_info:
|
||||||
read_ods(path, sheet_name)
|
read_ods(path, sheet_name)
|
||||||
assert e_info.match(f"There is no sheet named {sheet_name}")
|
assert e_info.match(f"There is no sheet named {sheet_name}")
|
||||||
|
|
||||||
def test_missing_header(self):
|
def test_missing_header(self):
|
||||||
path = os.path.join(rsc, missing_header_file)
|
|
||||||
|
path = rsc / missing_header_file
|
||||||
df = read_ods(path, 1)
|
df = read_ods(path, 1)
|
||||||
|
|
||||||
assert isinstance(df, pd.DataFrame)
|
assert isinstance(df, pd.DataFrame)
|
||||||
assert len(df) == 10
|
assert len(df) == 10
|
||||||
assert (len(df.columns) == 5)
|
assert (len(df.columns) == 5)
|
||||||
|
|
||||||
assert df.columns[2] == "unnamed.1"
|
assert df.columns[2] == "unnamed.1"
|
||||||
|
|
||||||
def test_mixed_dtypes(sefl):
|
def test_mixed_dtypes(sefl):
|
||||||
path = os.path.join(rsc, mixed_dtypes_file)
|
|
||||||
|
path = rsc / mixed_dtypes_file
|
||||||
df = read_ods(path, 1)
|
df = read_ods(path, 1)
|
||||||
|
|
||||||
assert isinstance(df, pd.DataFrame)
|
assert isinstance(df, pd.DataFrame)
|
||||||
assert len(df) == 10
|
assert len(df) == 10
|
||||||
assert (len(df.columns) == 5)
|
assert (len(df.columns) == 5)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue