Skip to content

Commit ee63d11

Browse files
committed
test(cursor): skips test using numpy, pandas if not available
1 parent 06452d2 commit ee63d11

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

test/integration/test_pandas.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
from test.utils import numpy_only, pandas_only
12
from warnings import filterwarnings
23

3-
import numpy as np # type: ignore
4-
import pandas as pd # type: ignore
54
import pytest # type: ignore
65

76
import redshift_connector
@@ -30,7 +29,11 @@ def fin():
3029
return con
3130

3231

32+
@pandas_only
3333
def test_fetch_dataframe(db_table):
34+
import numpy as np
35+
import pandas as pd
36+
3437
df = pd.DataFrame(
3538
np.array(
3639
[
@@ -53,7 +56,11 @@ def test_fetch_dataframe(db_table):
5356
assert result.columns[0] == "bookname"
5457

5558

59+
@pandas_only
5660
def test_write_dataframe(db_table):
61+
import numpy as np
62+
import pandas as pd
63+
5764
df = pd.DataFrame(
5865
np.array(
5966
[
@@ -70,6 +77,7 @@ def test_write_dataframe(db_table):
7077
assert len(np.array(result)) == 2
7178

7279

80+
@numpy_only
7381
def test_fetch_numpyarray(db_table):
7482
with db_table.cursor() as cursor:
7583
cursor.executemany(

test/unit/test_cursor.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import typing
2+
from test.utils import pandas_only
23

34
import pytest # type: ignore
45
from pytest_mock import mocker
@@ -11,6 +12,7 @@
1112
]
1213

1314

15+
@pandas_only
1416
@pytest.mark.parametrize("_input", test_warn_response_data)
1517
def test_fetch_dataframe_warns_user(_input, mocker):
1618
data, exp_warning_msg = _input
@@ -21,6 +23,7 @@ def test_fetch_dataframe_warns_user(_input, mocker):
2123
mock_cursor.fetch_dataframe(1)
2224

2325

26+
@pandas_only
2427
def test_fetch_dataframe_no_results(mocker):
2528
mock_cursor: Cursor = Cursor.__new__(Cursor)
2629
mocker.patch("redshift_connector.Cursor._getDescription", return_value=["test"])

test/utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .decorators import numpy_only, pandas_only

test/utils/decorators.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import pytest
2+
3+
4+
def is_numpy_installed() -> bool:
5+
try:
6+
import numpy
7+
8+
return True
9+
except ModuleNotFoundError:
10+
return False
11+
12+
13+
def is_pandas_installed() -> bool:
14+
try:
15+
import pandas
16+
17+
return True
18+
except ModuleNotFoundError:
19+
return False
20+
21+
22+
numpy_only: pytest.mark = pytest.mark.skipif(not is_numpy_installed(), reason="requires numpy")
23+
24+
pandas_only: pytest.mark = pytest.mark.skipif(not is_pandas_installed(), reason="requires pandas")

0 commit comments

Comments
 (0)