forked from iterative/dvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_data_file_obj.py
151 lines (107 loc) · 5.28 KB
/
test_data_file_obj.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from unittest import TestCase
import os
import shutil
from neatlynx.config import ConfigI
from neatlynx.data_file_obj import DataFileObj, NotInDataDirError, DataFilePathError
from neatlynx.git_wrapper import GitWrapperI
class BasicDataDirTest(TestCase):
def setUp(self, test_dir, curr_dir=None):
self._test_dir = os.path.realpath(test_dir)
self._test_git_dir = os.path.join(self._test_dir, 'proj')
self._old_curr_dir_abs = os.path.realpath(os.curdir)
shutil.rmtree(self._test_dir, ignore_errors=True)
if curr_dir:
self._curr_dir = os.path.realpath(curr_dir)
else:
self._curr_dir = self._test_git_dir
if not os.path.exists(self._curr_dir):
os.makedirs(self._curr_dir)
if not os.path.isdir(self._test_git_dir):
os.makedirs(self._test_git_dir)
os.chdir(self._curr_dir)
def tearDown(self):
os.chdir(self._old_curr_dir_abs)
shutil.rmtree(self._test_dir, ignore_errors=True)
class TestDataFileObjBasic(BasicDataDirTest):
def setUp(self):
BasicDataDirTest.setUp(self, test_dir=os.path.join(os.path.sep, 'tmp', 'ntx_unit_test'))
git = GitWrapperI(git_dir=self._test_git_dir, commit='ad45ba8')
config = ConfigI('data', 'cache', 'state')
self._dobj = DataFileObj(os.path.join('data', 'file.txt'), git, config)
pass
def test_data_file(self):
self.assertEqual(self._dobj.data_file_abs, os.path.join(self._test_git_dir, 'data', 'file.txt'))
def test_cache_file(self):
self.assertEqual(self._dobj.cache_file_abs, os.path.join(self._test_git_dir, 'cache', 'file.txt_ad45ba8'))
def test_state_file(self):
self.assertEqual(self._dobj.state_file_abs, os.path.join(self._test_git_dir, 'state', 'file.txt.state'))
class TestNlxNames(BasicDataDirTest):
def setUp(self):
BasicDataDirTest.setUp(self, test_dir=os.path.join(os.path.sep, 'tmp', 'ntx_unit_test'))
git = GitWrapperI(git_dir=self._test_git_dir, commit='ad45ba8')
config = ConfigI('data', 'cache', 'state')
self._dobj = DataFileObj(os.path.join('data', 'file.txt'), git, config)
pass
def test_data_nlx(self):
self.assertEqual(self._dobj.data_file_nlx, 'file.txt')
pass
class TestDataFileObjInDataDir(BasicDataDirTest):
def setUp(self):
BasicDataDirTest.setUp(self, os.path.join(os.path.sep, 'tmp', 'ntx_unit_test'))
git = GitWrapperI(git_dir=self._test_git_dir, commit='eeeff8f')
config = ConfigI('da', 'ca', 'st')
self._dobj = DataFileObj(os.path.join('da', 'dir1', 'd2', 'file.txt'), git, config)
pass
def test_data_file(self):
target = os.path.join(self._test_git_dir, 'da', 'dir1', 'd2', 'file.txt')
self.assertEqual(self._dobj.data_file_abs, target)
def test_cache_file(self):
target = os.path.join(self._test_git_dir, 'ca', 'dir1', 'd2', 'file.txt_eeeff8f')
self.assertEqual(self._dobj.cache_file_abs, target)
def test_state_file(self):
target = os.path.join(self._test_git_dir, 'st', 'dir1', 'd2', 'file.txt.state')
self.assertEqual(self._dobj.state_file_abs, target)
class TestDataFileObjLongPath(BasicDataDirTest):
def setUp(self):
curr_dir = os.path.join(os.path.sep, 'tmp', 'ntx_unit_test', 'proj', 'mydir', 'dd3')
BasicDataDirTest.setUp(self,
os.path.join(os.path.sep, 'tmp', 'ntx_unit_test'),
curr_dir)
self._git = GitWrapperI(git_dir=self._test_git_dir, commit='123ed8')
self._config = ConfigI('data', 'cache', 'state')
self._dobj = DataFileObj(os.path.join('..', '..', 'data', 'file1.txt'),
self._git, self._config)
pass
def test_data_file(self):
target = os.path.join(self._test_git_dir, 'data', 'file1.txt')
self.assertEqual(self._dobj.data_file_abs, target)
def test_cache_file(self):
target = os.path.join(self._test_git_dir, 'cache', 'file1.txt_123ed8')
self.assertEqual(self._dobj.cache_file_abs, target)
def test_state_file(self):
target = os.path.join(self._test_git_dir, 'state', 'file1.txt.state')
self.assertEqual(self._dobj.state_file_abs, target)
def test_file_name_only(self):
with self.assertRaises(NotInDataDirError):
DataFileObj('file.txt', self._git, self._config)
pass
def test_relative_git_dir(self):
with self.assertRaises(NotInDataDirError):
DataFileObj('data/file.txt', self._git, self._config)
pass
def test_relative_path_error(self):
with self.assertRaises(NotInDataDirError):
DataFileObj('../data/file.txt', self._git, self._config)
pass
class RunOutsideGitRepoTest(BasicDataDirTest):
def setUp(self):
curr_dir = os.path.join(os.path.sep, 'tmp')
BasicDataDirTest.setUp(self,
os.path.join(os.path.sep, 'tmp', 'ntx_unit_test'),
curr_dir)
def test_outside_git_dir(self):
git = GitWrapperI(git_dir=self._test_git_dir, commit='123ed8')
config = ConfigI('data', 'cache', 'state')
with self.assertRaises(NotInDataDirError):
DataFileObj('file.txt', git, config)
pass