From dfef31d03cca0a4dda7d08a47fe037994bb0e08f Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Fri, 14 Sep 2018 15:50:12 -0500 Subject: [PATCH 1/5] Attempt to fix clipboard tests --- ci/script_single.sh | 11 +++++++---- pandas/tests/io/test_clipboard.py | 1 + setup.cfg | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/ci/script_single.sh b/ci/script_single.sh index ed12ee35b9151..46c4f27c51e54 100755 --- a/ci/script_single.sh +++ b/ci/script_single.sh @@ -25,17 +25,20 @@ if [ "$DOC" ]; then echo "We are not running pytest as this is a doc-build" elif [ "$COVERAGE" ]; then - echo pytest -s -m "single" -r xXs --strict --cov=pandas --cov-report xml:/tmp/cov-single.xml --junitxml=/tmp/single.xml $TEST_ARGS pandas - pytest -s -m "single" -r xXs --strict --cov=pandas --cov-report xml:/tmp/cov-single.xml --junitxml=/tmp/single.xml $TEST_ARGS pandas + echo pytest -s -m "single and not clipboard" -r xXs --strict --cov=pandas --cov-report xml:/tmp/cov-single.xml --junitxml=/tmp/single.xml $TEST_ARGS pandas + pytest -s -m "single and not clipboard" -r xXs --strict --cov=pandas --cov-report xml:/tmp/cov-single.xml --junitxml=/tmp/single.xml $TEST_ARGS pandas echo pytest -s -r xXs --strict scripts pytest -s -r xXs --strict scripts else - echo pytest -m "single" -r xXs --junitxml=/tmp/single.xml --strict $TEST_ARGS pandas - pytest -m "single" -r xXs --junitxml=/tmp/single.xml --strict $TEST_ARGS pandas # TODO: doctest + echo pytest -m "single and not clipboard" -r xXs --junitxml=/tmp/single.xml --strict $TEST_ARGS pandas + pytest -m "single and not clipboard" -r xXs --junitxml=/tmp/single.xml --strict $TEST_ARGS pandas # TODO: doctest fi +echo pytest -s -m "clipboard" -r xXs --strict --cov=pandas --cov-report xml:/tmp/cov-single.xml --junitxml=/tmp/single.xml $TEST_ARGS pandas +pytest -s -m "clipboard" -r xXs --strict $TEST_ARGS pandas/tests/io/test_clipboard.py + RET="$?" exit "$RET" diff --git a/pandas/tests/io/test_clipboard.py b/pandas/tests/io/test_clipboard.py index a6b331685e72a..d92122baa4f5d 100644 --- a/pandas/tests/io/test_clipboard.py +++ b/pandas/tests/io/test_clipboard.py @@ -76,6 +76,7 @@ def df(request): raise ValueError +@pytest.mark.clipboard @pytest.mark.single @pytest.mark.skipif(not _DEPS_INSTALLED, reason="clipboard primitives not installed") diff --git a/setup.cfg b/setup.cfg index 5fc0236066b93..021159bad99de 100644 --- a/setup.cfg +++ b/setup.cfg @@ -38,6 +38,7 @@ markers = slow: mark a test as slow network: mark a test as network high_memory: mark a test as a high-memory only + clipboard: mark a pd.read_clipboard test doctest_optionflags = NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL addopts = --strict-data-files From 688d993255e3c0c67b276f4e6db900629c77d296 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Fri, 14 Sep 2018 15:51:18 -0500 Subject: [PATCH 2/5] note --- ci/script_single.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/ci/script_single.sh b/ci/script_single.sh index 46c4f27c51e54..628ffa73217bb 100755 --- a/ci/script_single.sh +++ b/ci/script_single.sh @@ -36,6 +36,7 @@ else fi +# Try to work around regular clipboard failures on travis echo pytest -s -m "clipboard" -r xXs --strict --cov=pandas --cov-report xml:/tmp/cov-single.xml --junitxml=/tmp/single.xml $TEST_ARGS pandas pytest -s -m "clipboard" -r xXs --strict $TEST_ARGS pandas/tests/io/test_clipboard.py From 9142b4e33cf619b040ee38026122a1c04523be92 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Sat, 15 Sep 2018 15:12:31 -0500 Subject: [PATCH 3/5] update --- ci/script_single.sh | 12 ++++-------- pandas/tests/io/test_clipboard.py | 32 ++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/ci/script_single.sh b/ci/script_single.sh index 628ffa73217bb..ed12ee35b9151 100755 --- a/ci/script_single.sh +++ b/ci/script_single.sh @@ -25,21 +25,17 @@ if [ "$DOC" ]; then echo "We are not running pytest as this is a doc-build" elif [ "$COVERAGE" ]; then - echo pytest -s -m "single and not clipboard" -r xXs --strict --cov=pandas --cov-report xml:/tmp/cov-single.xml --junitxml=/tmp/single.xml $TEST_ARGS pandas - pytest -s -m "single and not clipboard" -r xXs --strict --cov=pandas --cov-report xml:/tmp/cov-single.xml --junitxml=/tmp/single.xml $TEST_ARGS pandas + echo pytest -s -m "single" -r xXs --strict --cov=pandas --cov-report xml:/tmp/cov-single.xml --junitxml=/tmp/single.xml $TEST_ARGS pandas + pytest -s -m "single" -r xXs --strict --cov=pandas --cov-report xml:/tmp/cov-single.xml --junitxml=/tmp/single.xml $TEST_ARGS pandas echo pytest -s -r xXs --strict scripts pytest -s -r xXs --strict scripts else - echo pytest -m "single and not clipboard" -r xXs --junitxml=/tmp/single.xml --strict $TEST_ARGS pandas - pytest -m "single and not clipboard" -r xXs --junitxml=/tmp/single.xml --strict $TEST_ARGS pandas # TODO: doctest + echo pytest -m "single" -r xXs --junitxml=/tmp/single.xml --strict $TEST_ARGS pandas + pytest -m "single" -r xXs --junitxml=/tmp/single.xml --strict $TEST_ARGS pandas # TODO: doctest fi -# Try to work around regular clipboard failures on travis -echo pytest -s -m "clipboard" -r xXs --strict --cov=pandas --cov-report xml:/tmp/cov-single.xml --junitxml=/tmp/single.xml $TEST_ARGS pandas -pytest -s -m "clipboard" -r xXs --strict $TEST_ARGS pandas/tests/io/test_clipboard.py - RET="$?" exit "$RET" diff --git a/pandas/tests/io/test_clipboard.py b/pandas/tests/io/test_clipboard.py index d92122baa4f5d..6f1dde07e5c18 100644 --- a/pandas/tests/io/test_clipboard.py +++ b/pandas/tests/io/test_clipboard.py @@ -76,11 +76,41 @@ def df(request): raise ValueError -@pytest.mark.clipboard +# our local clipboard for tests +_mock_data = {} + + +@pytest.fixture +def mock_clipboard(mock, request): + def _mock_set(data): + _mock_data[request.node.name] = data + + def _mock_get(): + return _mock_data[request.node.name] + + mock_set = mock.patch("pandas.io.clipboard.clipboard_set", + side_effect=_mock_set) + mock_get = mock.patch("pandas.io.clipboard.clipboard_get", + side_effect=_mock_get) + with mock_get, mock_set: + yield + + @pytest.mark.single +@pytest.mark.clipboard @pytest.mark.skipif(not _DEPS_INSTALLED, reason="clipboard primitives not installed") +@pytest.mark.usefixtures("mock_clipboard") class TestClipboard(object): + + @pytest.mark.clipboard + def test_mock_clipboard(self): + import pandas.io.clipboard + pandas.io.clipboard.clipboard_set("abc") + assert "abc" in set(_mock_data.values()) + result = pandas.io.clipboard.clipboard_get() + assert result == "abc" + def check_round_trip_frame(self, data, excel=None, sep=None, encoding=None): data.to_clipboard(excel=excel, sep=sep, encoding=encoding) From 66123ef0335d03ef8ba5dedb1913ebd222704fac Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Sat, 15 Sep 2018 15:18:46 -0500 Subject: [PATCH 4/5] update --- pandas/tests/io/test_clipboard.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pandas/tests/io/test_clipboard.py b/pandas/tests/io/test_clipboard.py index 6f1dde07e5c18..86805af5b6bc1 100644 --- a/pandas/tests/io/test_clipboard.py +++ b/pandas/tests/io/test_clipboard.py @@ -149,15 +149,15 @@ def test_copy_delim_warning(self, df): # delimited and excel="True" @pytest.mark.parametrize('sep', ['\t', None, 'default']) @pytest.mark.parametrize('excel', [True, None, 'default']) - def test_clipboard_copy_tabs_default(self, sep, excel, df): + def test_clipboard_copy_tabs_default(self, sep, excel, df, request): kwargs = build_kwargs(sep, excel) df.to_clipboard(**kwargs) if PY2: # to_clipboard copies unicode, to_csv produces bytes. This is # expected behavior - assert clipboard_get().encode('utf-8') == df.to_csv(sep='\t') + assert _mock_data[request.node.name].encode('utf-8') == df.to_csv(sep='\t') else: - assert clipboard_get() == df.to_csv(sep='\t') + assert _mock_data[request.node.name] == df.to_csv(sep='\t') # Tests reading of white space separated tables @pytest.mark.parametrize('sep', [None, 'default']) @@ -169,7 +169,7 @@ def test_clipboard_copy_strings(self, sep, excel, df): assert result.to_string() == df.to_string() assert df.shape == result.shape - def test_read_clipboard_infer_excel(self): + def test_read_clipboard_infer_excel(self, request): # gh-19010: avoid warnings clip_kwargs = dict(engine="python") @@ -178,7 +178,7 @@ def test_read_clipboard_infer_excel(self): 1 2 4 Harry Carney """.strip()) - clipboard_set(text) + _mock_data[request.node.name] = text df = pd.read_clipboard(**clip_kwargs) # excel data is parsed correctly @@ -190,7 +190,7 @@ def test_read_clipboard_infer_excel(self): 1 2 3 4 """.strip()) - clipboard_set(text) + _mock_data[request.node.name] = text res = pd.read_clipboard(**clip_kwargs) text = dedent(""" @@ -198,7 +198,7 @@ def test_read_clipboard_infer_excel(self): 1 2 3 4 """.strip()) - clipboard_set(text) + _mock_data[request.node.name] = text exp = pd.read_clipboard(**clip_kwargs) tm.assert_frame_equal(res, exp) From f1ed15bbb9cc7c92c5ebd2ac2ad6d5eaf4148f2f Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Sun, 16 Sep 2018 06:57:29 -0500 Subject: [PATCH 5/5] doc --- pandas/tests/io/test_clipboard.py | 57 +++++++++++++++++++------------ 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/pandas/tests/io/test_clipboard.py b/pandas/tests/io/test_clipboard.py index 86805af5b6bc1..bb73c6bc6b38b 100644 --- a/pandas/tests/io/test_clipboard.py +++ b/pandas/tests/io/test_clipboard.py @@ -13,7 +13,6 @@ from pandas.util import testing as tm from pandas.util.testing import makeCustomDataframe as mkdf from pandas.io.clipboard.exceptions import PyperclipException -from pandas.io.clipboard import clipboard_set, clipboard_get try: @@ -76,12 +75,23 @@ def df(request): raise ValueError -# our local clipboard for tests -_mock_data = {} - - @pytest.fixture def mock_clipboard(mock, request): + """Fixture mocking clipboard IO. + + This mocks pandas.io.clipboard.clipboard_get and + pandas.io.clipboard.clipboard_set. + + This uses a local dict for storing data. The dictionary + key used is the test ID, available with ``request.node.name``. + + This returns the local dictionary, for direct manipulation by + tests. + """ + + # our local clipboard for tests + _mock_data = {} + def _mock_set(data): _mock_data[request.node.name] = data @@ -93,7 +103,16 @@ def _mock_get(): mock_get = mock.patch("pandas.io.clipboard.clipboard_get", side_effect=_mock_get) with mock_get, mock_set: - yield + yield _mock_data + + +@pytest.mark.clipboard +def test_mock_clipboard(mock_clipboard): + import pandas.io.clipboard + pandas.io.clipboard.clipboard_set("abc") + assert "abc" in set(mock_clipboard.values()) + result = pandas.io.clipboard.clipboard_get() + assert result == "abc" @pytest.mark.single @@ -103,14 +122,6 @@ def _mock_get(): @pytest.mark.usefixtures("mock_clipboard") class TestClipboard(object): - @pytest.mark.clipboard - def test_mock_clipboard(self): - import pandas.io.clipboard - pandas.io.clipboard.clipboard_set("abc") - assert "abc" in set(_mock_data.values()) - result = pandas.io.clipboard.clipboard_get() - assert result == "abc" - def check_round_trip_frame(self, data, excel=None, sep=None, encoding=None): data.to_clipboard(excel=excel, sep=sep, encoding=encoding) @@ -149,15 +160,18 @@ def test_copy_delim_warning(self, df): # delimited and excel="True" @pytest.mark.parametrize('sep', ['\t', None, 'default']) @pytest.mark.parametrize('excel', [True, None, 'default']) - def test_clipboard_copy_tabs_default(self, sep, excel, df, request): + def test_clipboard_copy_tabs_default(self, sep, excel, df, request, + mock_clipboard): kwargs = build_kwargs(sep, excel) df.to_clipboard(**kwargs) if PY2: # to_clipboard copies unicode, to_csv produces bytes. This is # expected behavior - assert _mock_data[request.node.name].encode('utf-8') == df.to_csv(sep='\t') + result = mock_clipboard[request.node.name].encode('utf-8') + expected = df.to_csv(sep='\t') + assert result == expected else: - assert _mock_data[request.node.name] == df.to_csv(sep='\t') + assert mock_clipboard[request.node.name] == df.to_csv(sep='\t') # Tests reading of white space separated tables @pytest.mark.parametrize('sep', [None, 'default']) @@ -169,7 +183,8 @@ def test_clipboard_copy_strings(self, sep, excel, df): assert result.to_string() == df.to_string() assert df.shape == result.shape - def test_read_clipboard_infer_excel(self, request): + def test_read_clipboard_infer_excel(self, request, + mock_clipboard): # gh-19010: avoid warnings clip_kwargs = dict(engine="python") @@ -178,7 +193,7 @@ def test_read_clipboard_infer_excel(self, request): 1 2 4 Harry Carney """.strip()) - _mock_data[request.node.name] = text + mock_clipboard[request.node.name] = text df = pd.read_clipboard(**clip_kwargs) # excel data is parsed correctly @@ -190,7 +205,7 @@ def test_read_clipboard_infer_excel(self, request): 1 2 3 4 """.strip()) - _mock_data[request.node.name] = text + mock_clipboard[request.node.name] = text res = pd.read_clipboard(**clip_kwargs) text = dedent(""" @@ -198,7 +213,7 @@ def test_read_clipboard_infer_excel(self, request): 1 2 3 4 """.strip()) - _mock_data[request.node.name] = text + mock_clipboard[request.node.name] = text exp = pd.read_clipboard(**clip_kwargs) tm.assert_frame_equal(res, exp)