Skip to content

Commit 96adc53

Browse files
committed
tests: use dedent, add tests for markdown
1 parent 194e07d commit 96adc53

File tree

2 files changed

+123
-42
lines changed

2 files changed

+123
-42
lines changed

tests/unit/command/test_metrics.py

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import textwrap
2+
13
from dvc.cli import parse_args
24
from dvc.command.metrics import CmdMetricsDiff, CmdMetricsShow, _show_diff
35

@@ -37,25 +39,30 @@ def test_metrics_diff(dvc, mocker):
3739
def test_metrics_show_json_diff():
3840
assert _show_diff(
3941
{"metrics.json": {"a.b.c": {"old": 1, "new": 2, "diff": 3}}}
40-
) == (
41-
" Path Metric Value Change\n"
42-
"metrics.json a.b.c 2 3 "
42+
) == textwrap.dedent(
43+
"""\
44+
Path Metric Value Change
45+
metrics.json a.b.c 2 3"""
4346
)
4447

4548

4649
def test_metrics_show_raw_diff():
47-
assert _show_diff({"metrics": {"": {"old": "1", "new": "2"}}}) == (
48-
" Path Metric Value Change \n"
49-
"metrics 2 diff not supported"
50+
assert _show_diff(
51+
{"metrics": {"": {"old": "1", "new": "2"}}}
52+
) == textwrap.dedent(
53+
"""\
54+
Path Metric Value Change
55+
metrics 2 diff not supported"""
5056
)
5157

5258

5359
def test_metrics_diff_no_diff():
5460
assert _show_diff(
5561
{"other.json": {"a.b.d": {"old": "old", "new": "new"}}}
56-
) == (
57-
" Path Metric Value Change \n"
58-
"other.json a.b.d new diff not supported"
62+
) == textwrap.dedent(
63+
"""\
64+
Path Metric Value Change
65+
other.json a.b.d new diff not supported"""
5966
)
6067

6168

@@ -66,18 +73,20 @@ def test_metrics_diff_no_changes():
6673
def test_metrics_diff_new_metric():
6774
assert _show_diff(
6875
{"other.json": {"a.b.d": {"old": None, "new": "new"}}}
69-
) == (
70-
" Path Metric Value Change \n"
71-
"other.json a.b.d new diff not supported"
76+
) == textwrap.dedent(
77+
"""\
78+
Path Metric Value Change
79+
other.json a.b.d new diff not supported"""
7280
)
7381

7482

7583
def test_metrics_diff_deleted_metric():
7684
assert _show_diff(
7785
{"other.json": {"a.b.d": {"old": "old", "new": None}}}
78-
) == (
79-
" Path Metric Value Change \n"
80-
"other.json a.b.d None diff not supported"
86+
) == textwrap.dedent(
87+
"""\
88+
Path Metric Value Change
89+
other.json a.b.d None diff not supported"""
8190
)
8291

8392

@@ -114,9 +123,10 @@ def test_metrics_show(dvc, mocker):
114123
def test_metrics_diff_prec():
115124
assert _show_diff(
116125
{"other.json": {"a.b": {"old": 0.0042, "new": 0.0043, "diff": 0.0001}}}
117-
) == (
118-
" Path Metric Value Change\n"
119-
"other.json a.b 0.0043 0.0001"
126+
) == textwrap.dedent(
127+
"""\
128+
Path Metric Value Change
129+
other.json a.b 0.0043 0.0001"""
120130
)
121131

122132

@@ -129,9 +139,38 @@ def test_metrics_diff_sorted():
129139
"a.b.c": {"old": 1, "new": 2, "diff": 1},
130140
}
131141
}
132-
) == (
133-
" Path Metric Value Change\n"
134-
"metrics.yaml a.b.c 2 1 \n"
135-
"metrics.yaml a.d.e 4 1 \n"
136-
"metrics.yaml x.b 6 1 "
142+
) == textwrap.dedent(
143+
"""\
144+
Path Metric Value Change
145+
metrics.yaml a.b.c 2 1
146+
metrics.yaml a.d.e 4 1
147+
metrics.yaml x.b 6 1"""
148+
)
149+
150+
151+
def test_metrics_diff_markdown_empty():
152+
assert _show_diff({}, markdown=True) == textwrap.dedent(
153+
"""\
154+
| Path | Metric | Value | Change |
155+
|--------|----------|---------|----------|"""
156+
)
157+
158+
159+
def test_metrics_diff_markdown():
160+
assert _show_diff(
161+
{
162+
"metrics.yaml": {
163+
"x.b": {"old": 5, "new": 6},
164+
"a.d.e": {"old": 3, "new": 4, "diff": 1},
165+
"a.b.c": {"old": 1, "new": 2, "diff": 1},
166+
}
167+
},
168+
markdown=True,
169+
) == textwrap.dedent(
170+
"""\
171+
| Path | Metric | Value | Change |
172+
|--------------|----------|---------|--------------------|
173+
| metrics.yaml | a.b.c | 2 | 1 |
174+
| metrics.yaml | a.d.e | 4 | 1 |
175+
| metrics.yaml | x.b | 6 | diff not supported |"""
137176
)

tests/unit/command/test_params.py

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import textwrap
23

34
from dvc.cli import parse_args
45
from dvc.command.params import CmdParamsDiff, _show_diff
@@ -21,25 +22,32 @@ def test_params_diff(dvc, mocker):
2122

2223

2324
def test_params_diff_changed():
24-
assert _show_diff({"params.yaml": {"a.b.c": {"old": 1, "new": 2}}}) == (
25-
" Path Param Old New\n" "params.yaml a.b.c 1 2 "
25+
assert _show_diff(
26+
{"params.yaml": {"a.b.c": {"old": 1, "new": 2}}}
27+
) == textwrap.dedent(
28+
"""\
29+
Path Param Old New
30+
params.yaml a.b.c 1 2"""
2631
)
2732

2833

2934
def test_params_diff_list():
3035
assert _show_diff(
3136
{"params.yaml": {"a.b.c": {"old": 1, "new": [2, 3]}}}
32-
) == (
33-
" Path Param Old New \n"
34-
"params.yaml a.b.c 1 [2, 3]"
37+
) == textwrap.dedent(
38+
"""\
39+
Path Param Old New
40+
params.yaml a.b.c 1 [2, 3]"""
3541
)
3642

3743

3844
def test_params_diff_unchanged():
3945
assert _show_diff(
4046
{"params.yaml": {"a.b.d": {"old": "old", "new": "new"}}}
41-
) == (
42-
" Path Param Old New\n" "params.yaml a.b.d old new"
47+
) == textwrap.dedent(
48+
"""\
49+
Path Param Old New
50+
params.yaml a.b.d old new"""
4351
)
4452

4553

@@ -50,25 +58,30 @@ def test_params_diff_no_changes():
5058
def test_params_diff_new():
5159
assert _show_diff(
5260
{"params.yaml": {"a.b.d": {"old": None, "new": "new"}}}
53-
) == (
54-
" Path Param Old New\n" "params.yaml a.b.d None new"
61+
) == textwrap.dedent(
62+
"""\
63+
Path Param Old New
64+
params.yaml a.b.d None new"""
5565
)
5666

5767

5868
def test_params_diff_deleted():
5969
assert _show_diff(
6070
{"params.yaml": {"a.b.d": {"old": "old", "new": None}}}
61-
) == (
62-
" Path Param Old New \n" "params.yaml a.b.d old None"
71+
) == textwrap.dedent(
72+
"""\
73+
Path Param Old New
74+
params.yaml a.b.d old None"""
6375
)
6476

6577

6678
def test_params_diff_prec():
6779
assert _show_diff(
6880
{"params.yaml": {"train.lr": {"old": 0.0042, "new": 0.0043}}}
69-
) == (
70-
" Path Param Old New \n"
71-
"params.yaml train.lr 0.0042 0.0043"
81+
) == textwrap.dedent(
82+
"""\
83+
Path Param Old New
84+
params.yaml train.lr 0.0042 0.0043"""
7285
)
7386

7487

@@ -94,9 +107,38 @@ def test_params_diff_sorted():
94107
"a.b.c": {"old": 1, "new": 2},
95108
}
96109
}
97-
) == (
98-
" Path Param Old New\n"
99-
"params.yaml a.b.c 1 2 \n"
100-
"params.yaml a.d.e 3 4 \n"
101-
"params.yaml x.b 5 6 "
110+
) == textwrap.dedent(
111+
"""\
112+
Path Param Old New
113+
params.yaml a.b.c 1 2
114+
params.yaml a.d.e 3 4
115+
params.yaml x.b 5 6"""
116+
)
117+
118+
119+
def test_params_diff_markdown_empty():
120+
assert _show_diff({}, markdown=True) == textwrap.dedent(
121+
"""\
122+
| Path | Param | Old | New |
123+
|--------|---------|-------|-------|"""
124+
)
125+
126+
127+
def test_params_diff_markdown():
128+
assert _show_diff(
129+
{
130+
"params.yaml": {
131+
"x.b": {"old": 5, "new": 6},
132+
"a.d.e": {"old": None, "new": 4},
133+
"a.b.c": {"old": 1, "new": None},
134+
}
135+
},
136+
markdown=True,
137+
) == textwrap.dedent(
138+
"""\
139+
| Path | Param | Old | New |
140+
|-------------|---------|-------|-------|
141+
| params.yaml | a.b.c | 1 | None |
142+
| params.yaml | a.d.e | None | 4 |
143+
| params.yaml | x.b | 5 | 6 |"""
102144
)

0 commit comments

Comments
 (0)