forked from spotify/luigi
/
target_test.py
245 lines (185 loc) · 6.32 KB
/
target_test.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# -*- coding: utf-8 -*-
#
# Copyright 2012-2015 Spotify AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import print_function
from helpers import unittest
import luigi.target
import luigi.format
class TestException(Exception):
pass
class TargetTest(unittest.TestCase):
def test_cannot_instantiate(self):
def instantiate_target():
luigi.target.Target()
self.assertRaises(TypeError, instantiate_target)
def test_abstract_subclass(self):
class ExistsLessTarget(luigi.target.Target):
pass
def instantiate_target():
ExistsLessTarget()
self.assertRaises(TypeError, instantiate_target)
def test_instantiate_subclass(self):
class GoodTarget(luigi.target.Target):
def exists(self):
return True
def open(self, mode):
return None
GoodTarget()
class FileSystemTargetTestMixin(object):
"""All Target that take bytes (python2: str) should pass those
tests. In addition, a test to verify the method `exists`should be added
"""
def create_target(self, format=None):
raise NotImplementedError()
def assertCleanUp(self, tmp_path=''):
pass
def test_atomicity(self):
target = self.create_target()
fobj = target.open("w")
self.assertFalse(target.exists())
fobj.close()
self.assertTrue(target.exists())
def test_readback(self):
target = self.create_target()
origdata = 'lol\n'
fobj = target.open("w")
fobj.write(origdata)
fobj.close()
fobj = target.open('r')
data = fobj.read()
self.assertEqual(origdata, data)
def test_unicode_obj(self):
target = self.create_target()
origdata = u'lol\n'
fobj = target.open("w")
fobj.write(origdata)
fobj.close()
fobj = target.open('r')
data = fobj.read()
self.assertEqual(origdata, data)
def test_with_close(self):
target = self.create_target()
with target.open('w') as fobj:
tp = getattr(fobj, 'tmp_path', '')
fobj.write('hej\n')
self.assertCleanUp(tp)
self.assertTrue(target.exists())
def test_with_exception(self):
target = self.create_target()
a = {}
def foo():
with target.open('w') as fobj:
fobj.write('hej\n')
a['tp'] = getattr(fobj, 'tmp_path', '')
raise TestException('Test triggered exception')
self.assertRaises(TestException, foo)
self.assertCleanUp(a['tp'])
self.assertFalse(target.exists())
def test_del(self):
t = self.create_target()
p = t.open('w')
print('test', file=p)
tp = getattr(p, 'tmp_path', '')
del p
self.assertCleanUp(tp)
self.assertFalse(t.exists())
def test_write_cleanup_no_close(self):
t = self.create_target()
def context():
f = t.open('w')
f.write('stuff')
return getattr(f, 'tmp_path', '')
tp = context()
import gc
gc.collect() # force garbage collection of f variable
self.assertCleanUp(tp)
self.assertFalse(t.exists())
def test_text(self):
t = self.create_target(luigi.format.UTF8)
a = u'我éçф'
with t.open('w') as f:
f.write(a)
with t.open('r') as f:
b = f.read()
self.assertEqual(a, b)
def test_del_with_Text(self):
t = self.create_target(luigi.format.UTF8)
p = t.open('w')
print(u'test', file=p)
tp = getattr(p, 'tmp_path', '')
del p
self.assertCleanUp(tp)
self.assertFalse(t.exists())
def test_format_injection(self):
class CustomFormat(luigi.format.Format):
def pipe_reader(self, input_pipe):
input_pipe.foo = "custom read property"
return input_pipe
def pipe_writer(self, output_pipe):
output_pipe.foo = "custom write property"
return output_pipe
t = self.create_target(CustomFormat())
with t.open("w") as f:
self.assertEqual(f.foo, "custom write property")
with t.open("r") as f:
self.assertEqual(f.foo, "custom read property")
def test_binary_write(self):
t = self.create_target(luigi.format.Nop)
with t.open('w') as f:
f.write(b'a\xf2\xf3\r\nfd')
with t.open('r') as f:
c = f.read()
self.assertEqual(c, b'a\xf2\xf3\r\nfd')
def test_writelines(self):
t = self.create_target()
with t.open('w') as f:
f.writelines([
'a\n',
'b\n',
'c\n',
])
with t.open('r') as f:
c = f.read()
self.assertEqual(c, 'a\nb\nc\n')
def test_read_iterator(self):
t = self.create_target()
with t.open('w') as f:
f.write('a\nb\nc\n')
c = []
with t.open('r') as f:
for x in f:
c.append(x)
self.assertEqual(c, ['a\n', 'b\n', 'c\n'])
def test_gzip(self):
t = self.create_target(luigi.format.Gzip)
p = t.open('w')
test_data = b'test'
p.write(test_data)
tp = getattr(p, 'tmp_path', '')
self.assertFalse(t.exists())
p.close()
self.assertCleanUp(tp)
self.assertTrue(t.exists())
def test_gzip_works_and_cleans_up(self):
t = self.create_target(luigi.format.Gzip)
test_data = b'123testing'
with t.open('w') as f:
tp = getattr(f, 'tmp_path', '')
f.write(test_data)
self.assertCleanUp(tp)
with t.open() as f:
result = f.read()
self.assertEqual(test_data, result)