Skip to content

Commit a844870

Browse files
Marco GorelliMarcoGorelli
authored andcommitted
✅ update tests so they use buf
1 parent cdb3fa9 commit a844870

File tree

1 file changed

+50
-31
lines changed

1 file changed

+50
-31
lines changed
Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,54 @@
1-
import pandas.util._test_decorators as td
1+
from io import StringIO
2+
3+
import pytest
24

35
import pandas as pd
46

7+
pytest.importorskip("tabulate")
8+
9+
10+
def test_simple():
11+
buf = StringIO()
12+
df = pd.DataFrame([1, 2, 3])
13+
df.to_markdown(buf=buf)
14+
result = buf.getvalue()
15+
assert (
16+
result == "| | 0 |\n|---:|----:|\n| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |"
17+
)
18+
19+
20+
def test_other_tablefmt():
21+
buf = StringIO()
22+
df = pd.DataFrame([1, 2, 3])
23+
df.to_markdown(buf=buf, tablefmt="jira")
24+
result = buf.getvalue()
25+
assert result == "|| || 0 ||\n| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |"
26+
27+
28+
def test_other_headers():
29+
buf = StringIO()
30+
df = pd.DataFrame([1, 2, 3])
31+
df.to_markdown(buf=buf, headers=["foo", "bar"])
32+
result = buf.getvalue()
33+
assert result == (
34+
"| foo | bar |\n|------:|------:|\n| 0 "
35+
"| 1 |\n| 1 | 2 |\n| 2 | 3 |"
36+
)
37+
38+
39+
def test_series():
40+
buf = StringIO()
41+
s = pd.Series([1, 2, 3], name="foo")
42+
s.to_markdown(buf=buf)
43+
result = buf.getvalue()
44+
assert result == (
45+
"| | foo |\n|---:|------:|\n| 0 | 1 "
46+
"|\n| 1 | 2 |\n| 2 | 3 |"
47+
)
48+
549

6-
@td.skip_if_no_tabulate
7-
class TestToMarkdown:
8-
def test_simple(self):
9-
df = pd.DataFrame([1, 2, 3])
10-
result = df.to_markdown()
11-
assert (
12-
result
13-
== "| | 0 |\n|---:|----:|\n| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |"
14-
)
15-
16-
def test_other_tablefmt(self):
17-
df = pd.DataFrame([1, 2, 3])
18-
result = df.to_markdown(tablefmt="jira")
19-
assert result == "|| || 0 ||\n| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |"
20-
21-
def test_other_headers(self):
22-
df = pd.DataFrame([1, 2, 3])
23-
result = df.to_markdown(headers=["foo", "bar"])
24-
assert result == (
25-
"| foo | bar |\n|------:|------:|\n| 0 "
26-
"| 1 |\n| 1 | 2 |\n| 2 | 3 |"
27-
)
28-
29-
def test_series(self):
30-
s = pd.Series([1, 2, 3], name="foo")
31-
result = s.to_markdown()
32-
assert result == (
33-
"| | foo |\n|---:|------:|\n| 0 | 1 "
34-
"|\n| 1 | 2 |\n| 2 | 3 |"
35-
)
50+
def test_no_buf(capsys):
51+
df = pd.DataFrame([1, 2, 3])
52+
df.to_markdown()
53+
out, _ = capsys.readouterr()
54+
assert out == "| | 0 |\n|---:|----:|\n| 0 | 1 |\n| 1 | 2 |\n| 2 | 3 |"

0 commit comments

Comments
 (0)