Skip to content

Commit b6fa117

Browse files
nik123pmrowla
authored andcommitted
tests: migrate unittest to pytest
1 parent e430693 commit b6fa117

File tree

4 files changed

+189
-203
lines changed

4 files changed

+189
-203
lines changed

tests/basic_env.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,6 @@ def __init__(self, methodName):
155155
TestCase.__init__(self, methodName)
156156

157157

158-
class TestGit(TestGitFixture, TestCase):
159-
def __init__(self, methodName):
160-
TestGitFixture.__init__(self)
161-
TestCase.__init__(self, methodName)
162-
163-
164158
class TestDvc(TestDvcFixture, TestCase):
165159
def __init__(self, methodName):
166160
TestDvcFixture.__init__(self)

tests/func/test_odb.py

Lines changed: 121 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -9,176 +9,168 @@
99
from dvc.hash_info import HashInfo
1010
from dvc.objects.errors import ObjectFormatError
1111
from dvc.utils import relpath
12-
from tests.basic_env import TestDir, TestDvc
13-
14-
15-
class TestCache(TestDvc):
16-
def setUp(self):
17-
super().setUp()
18-
self.cache1_md5 = "123"
19-
self.cache2_md5 = "234"
20-
self.cache1 = os.path.join(
21-
self.dvc.odb.local.cache_dir,
22-
self.cache1_md5[0:2],
23-
self.cache1_md5[2:],
24-
)
25-
self.cache2 = os.path.join(
26-
self.dvc.odb.local.cache_dir,
27-
self.cache2_md5[0:2],
28-
self.cache2_md5[2:],
29-
)
30-
self.create(self.cache1, "1")
31-
self.create(self.cache2, "2")
3212

33-
def test_all(self):
34-
md5_list = list(ODBManager(self.dvc).local.all())
35-
self.assertEqual(len(md5_list), 2)
36-
self.assertIn(self.cache1_md5, md5_list)
37-
self.assertIn(self.cache2_md5, md5_list)
3813

39-
def test_get(self):
40-
cache = ODBManager(self.dvc).local.hash_to_path(self.cache1_md5)
41-
self.assertEqual(os.fspath(cache), self.cache1)
14+
def test_cache(tmp_dir, dvc):
15+
cache1_md5 = "123"
16+
cache2_md5 = "234"
17+
cache1 = os.path.join(
18+
dvc.odb.local.cache_dir,
19+
cache1_md5[0:2],
20+
cache1_md5[2:],
21+
)
22+
cache2 = os.path.join(
23+
dvc.odb.local.cache_dir,
24+
cache2_md5[0:2],
25+
cache2_md5[2:],
26+
)
27+
tmp_dir.gen({cache1: "1", cache2: "2"})
4228

29+
assert os.path.exists(cache1)
30+
assert os.path.exists(cache2)
4331

44-
class TestCacheLoadBadDirCache(TestDvc):
45-
def _do_test(self, ret):
46-
self.assertTrue(isinstance(ret, list))
47-
self.assertEqual(len(ret), 0)
32+
odb = ODBManager(dvc)
4833

49-
def test(self):
50-
from dvc.data import load
34+
md5_list = list(odb.local.all())
35+
assert len(md5_list) == 2
36+
assert cache1_md5 in md5_list
37+
assert cache2_md5 in md5_list
5138

52-
dir_hash = "123.dir"
53-
fname = os.fspath(self.dvc.odb.local.hash_to_path(dir_hash))
54-
self.create(fname, "<clearly>not,json")
55-
with pytest.raises(ObjectFormatError):
56-
load(self.dvc.odb.local, HashInfo("md5", dir_hash))
39+
odb_cache1 = odb.local.hash_to_path(cache1_md5)
40+
odb_cache2 = odb.local.hash_to_path(cache2_md5)
41+
assert os.fspath(odb_cache1) == cache1
42+
assert os.fspath(odb_cache2) == cache2
5743

58-
dir_hash = "234.dir"
59-
fname = os.fspath(self.dvc.odb.local.hash_to_path(dir_hash))
60-
self.create(fname, '{"a": "b"}')
61-
with pytest.raises(ObjectFormatError):
62-
load(self.dvc.odb.local, HashInfo("md5", dir_hash))
6344

45+
def test_cache_load_bad_dir_cache(tmp_dir, dvc):
46+
from dvc.data import load
6447

65-
class TestExternalCacheDir(TestDvc):
66-
def test(self):
67-
cache_dir = TestDvc.mkdtemp()
48+
dir_hash = "123.dir"
49+
fname = os.fspath(dvc.odb.local.hash_to_path(dir_hash))
50+
tmp_dir.gen({fname: "<clearly>not,json"})
51+
with pytest.raises(ObjectFormatError):
52+
load(dvc.odb.local, HashInfo("md5", dir_hash))
6853

69-
ret = main(["config", "cache.dir", cache_dir])
70-
self.assertEqual(ret, 0)
54+
dir_hash = "234.dir"
55+
fname = os.fspath(dvc.odb.local.hash_to_path(dir_hash))
56+
tmp_dir.gen({fname: '{"a": "b"}'})
57+
with pytest.raises(ObjectFormatError):
58+
load(dvc.odb.local, HashInfo("md5", dir_hash))
7159

72-
self.assertFalse(os.path.exists(self.dvc.odb.local.cache_dir))
7360

74-
ret = main(["add", self.FOO])
75-
self.assertEqual(ret, 0)
61+
def test_external_cache_dir(tmp_dir, dvc, make_tmp_dir):
62+
cache_dir = make_tmp_dir("cache")
7663

77-
ret = main(["add", self.DATA_DIR])
78-
self.assertEqual(ret, 0)
64+
with dvc.config.edit() as conf:
65+
conf["cache"]["dir"] = cache_dir.fs_path
66+
assert not os.path.exists(dvc.odb.local.cache_dir)
67+
dvc.odb = ODBManager(dvc)
7968

80-
self.assertFalse(os.path.exists(".dvc/cache"))
81-
self.assertNotEqual(len(os.listdir(cache_dir)), 0)
69+
tmp_dir.dvc_gen({"foo": "foo"})
8270

83-
def test_remote_references(self):
84-
ssh_url = "ssh://user@localhost:23"
85-
assert main(["remote", "add", "storage", ssh_url]) == 0
86-
assert main(["remote", "add", "cache", "remote://storage/tmp"]) == 0
87-
assert main(["config", "cache.ssh", "cache"]) == 0
71+
tmp_dir.dvc_gen(
72+
{
73+
"data_dir": {
74+
"data": "data_dir/data",
75+
"data_sub_dir": {"data_sub": "data_dir/data_sub_dir/data_sub"},
76+
}
77+
}
78+
)
79+
80+
assert not os.path.exists(".dvc/cache")
81+
assert len(os.listdir(cache_dir)) != 0
8882

89-
self.dvc.__init__()
9083

91-
assert self.dvc.odb.ssh.fs_path == "/tmp"
84+
def test_remote_cache_references(tmp_dir, dvc):
85+
with dvc.config.edit() as conf:
86+
conf["remote"]["storage"] = {"url": "ssh://user@localhost:23"}
87+
conf["remote"]["cache"] = {"url": "remote://storage/tmp"}
88+
conf["cache"]["ssh"] = "cache"
9289

90+
dvc.odb = ODBManager(dvc)
9391

94-
class TestSharedCacheDir(TestDir):
95-
def test(self):
96-
cache_dir = os.path.abspath(os.path.join(os.curdir, "cache"))
97-
for d in ["dir1", "dir2"]:
98-
os.mkdir(d)
99-
os.chdir(d)
92+
assert dvc.odb.ssh.fs_path == "/tmp"
10093

94+
95+
def test_shared_cache_dir(tmp_dir):
96+
cache_dir = os.path.abspath(os.path.join(os.curdir, "cache"))
97+
for d in ["dir1", "dir2"]:
98+
os.mkdir(d)
99+
with (tmp_dir / d).chdir():
101100
ret = main(["init", "--no-scm"])
102-
self.assertEqual(ret, 0)
101+
assert ret == 0
103102

104103
ret = main(["config", "cache.dir", cache_dir])
105-
self.assertEqual(ret, 0)
106-
107-
self.assertFalse(os.path.exists(os.path.join(".dvc", "cache")))
104+
assert ret == 0
108105

109-
with open("common", "w+", encoding="utf-8") as fd:
110-
fd.write("common")
106+
assert not os.path.exists(os.path.join(".dvc", "cache"))
111107

112-
with open("unique", "w+", encoding="utf-8") as fd:
113-
fd.write(d)
108+
(tmp_dir / d).gen({"common": "common", "unique": d})
114109

115110
ret = main(["add", "common", "unique"])
116-
self.assertEqual(ret, 0)
111+
assert ret == 0
117112

118-
os.chdir("..")
113+
assert not os.path.exists(os.path.join("dir1", ".dvc", "cache"))
114+
assert not os.path.exists(os.path.join("dir2", ".dvc", "cache"))
119115

120-
self.assertFalse(os.path.exists(os.path.join("dir1", ".dvc", "cache")))
121-
self.assertFalse(os.path.exists(os.path.join("dir2", ".dvc", "cache")))
122-
123-
subdirs = list(
124-
filter(
125-
lambda x: os.path.isdir(os.path.join(cache_dir, x)),
126-
os.listdir(cache_dir),
127-
)
128-
)
129-
self.assertEqual(len(subdirs), 3)
130-
self.assertEqual(
131-
len(os.listdir(os.path.join(cache_dir, subdirs[0]))), 1
132-
)
133-
self.assertEqual(
134-
len(os.listdir(os.path.join(cache_dir, subdirs[1]))), 1
135-
)
136-
self.assertEqual(
137-
len(os.listdir(os.path.join(cache_dir, subdirs[2]))), 1
116+
subdirs = list(
117+
filter(
118+
lambda x: os.path.isdir(os.path.join(cache_dir, x)),
119+
os.listdir(cache_dir),
138120
)
121+
)
122+
assert len(subdirs) == 3
123+
assert len(os.listdir(os.path.join(cache_dir, subdirs[0]))) == 1
139124

125+
assert len(os.listdir(os.path.join(cache_dir, subdirs[1]))) == 1
126+
assert len(os.listdir(os.path.join(cache_dir, subdirs[2]))) == 1
140127

141-
class TestCacheLinkType(TestDvc):
142-
def test(self):
143-
ret = main(["config", "cache.type", "reflink,copy"])
144-
self.assertEqual(ret, 0)
145128

146-
ret = main(["add", self.FOO])
147-
self.assertEqual(ret, 0)
129+
def test_cache_link_type(tmp_dir, scm, dvc):
130+
with dvc.config.edit() as conf:
131+
conf["cache"]["type"] = "reflink,copy"
132+
dvc.odb = ODBManager(dvc)
148133

134+
stages = tmp_dir.dvc_gen({"foo": "foo"})
135+
assert len(stages) == 1
136+
assert (tmp_dir / "foo").read_text().strip() == "foo"
149137

150-
class TestCmdCacheDir(TestDvc):
151-
def test(self):
152-
ret = main(["cache", "dir"])
153-
self.assertEqual(ret, 0)
154138

155-
def test_abs_path(self):
156-
dname = os.path.join(os.path.dirname(self._root_dir), "dir")
157-
ret = main(["cache", "dir", dname])
158-
self.assertEqual(ret, 0)
139+
def test_cmd_cache_dir(tmp_dir, scm, dvc):
140+
ret = main(["cache", "dir"])
141+
assert ret == 0
159142

160-
config = configobj.ConfigObj(self.dvc.config.files["repo"])
161-
self.assertEqual(config["cache"]["dir"], dname)
162143

163-
def test_relative_path(self):
164-
tmpdir = os.path.realpath(self.mkdtemp())
165-
dname = relpath(tmpdir)
166-
ret = main(["cache", "dir", dname])
167-
self.assertEqual(ret, 0)
144+
def test_cmd_cache_abs_path(tmp_dir, scm, dvc, make_tmp_dir):
145+
cache_dir = make_tmp_dir("cache")
146+
ret = main(["cache", "dir", cache_dir.fs_path])
147+
assert ret == 0
148+
149+
config = configobj.ConfigObj(dvc.config.files["repo"])
150+
assert config["cache"]["dir"] == cache_dir.fs_path
151+
152+
153+
def test_cmd_cache_relative_path(tmp_dir, scm, dvc, make_tmp_dir):
154+
cache_dir = make_tmp_dir("cache")
155+
dname = relpath(cache_dir)
156+
ret = main(["cache", "dir", dname])
157+
assert ret == 0
158+
159+
dvc.config.load()
160+
dvc.odb = ODBManager(dvc)
168161

169-
# NOTE: we are in the repo's root and config is in .dvc/, so
170-
# dir path written to config should be just one level above.
171-
rel = os.path.join("..", dname)
172-
config = configobj.ConfigObj(self.dvc.config.files["repo"])
173-
self.assertEqual(config["cache"]["dir"], rel.replace("\\", "/"))
162+
# NOTE: we are in the repo's root and config is in .dvc/, so
163+
# dir path written to config should be just one level above.
164+
rel = os.path.join("..", dname)
165+
config = configobj.ConfigObj(dvc.config.files["repo"])
166+
assert config["cache"]["dir"] == rel.replace("\\", "/")
174167

175-
ret = main(["add", self.FOO])
176-
self.assertEqual(ret, 0)
168+
tmp_dir.dvc_gen({"foo": "foo"})
177169

178-
subdirs = os.listdir(tmpdir)
179-
self.assertEqual(len(subdirs), 1)
180-
files = os.listdir(os.path.join(tmpdir, subdirs[0]))
181-
self.assertEqual(len(files), 1)
170+
subdirs = os.listdir(cache_dir)
171+
assert len(subdirs) == 1
172+
files = os.listdir(os.path.join(cache_dir, subdirs[0]))
173+
assert len(files) == 1
182174

183175

184176
def test_default_cache_type(dvc):

tests/unit/test_context.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from dataclasses import asdict
22
from math import pi
3-
from unittest.mock import mock_open
43

54
import pytest
65

@@ -219,7 +218,7 @@ def test_overwrite_with_setitem():
219218
def test_load_from(mocker):
220219
d = {"x": {"y": {"z": 5}, "lst": [1, 2, 3]}, "foo": "foo"}
221220
fs = mocker.Mock(
222-
open=mock_open(read_data=dumps_yaml(d)),
221+
open=mocker.mock_open(read_data=dumps_yaml(d)),
223222
**{"exists.return_value": True, "isdir.return_value": False},
224223
)
225224
file = "params.yaml"

0 commit comments

Comments
 (0)