-
Notifications
You must be signed in to change notification settings - Fork 232
/
test_metaproduct.py
135 lines (95 loc) · 2.85 KB
/
test_metaproduct.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
from unittest.mock import Mock
from pathlib import Path
import pytest
from ploomber import DAG
from ploomber.tasks import PythonCallable
from ploomber.products import File, MetaProduct
def touch_all(product):
for p in product:
Path(str(p)).touch()
def touch_all_upstream(product, upstream):
for p in product:
Path(str(p)).touch()
def test_get():
a = File('1.txt')
m = MetaProduct({'a': a})
assert m.get('a') is a
assert m.get('b') is None
def test_delete_metadata(tmp_directory):
Path('.a.txt.metadata').touch()
Path('.b.txt.metadata').touch()
a = File('a.txt')
b = File('b.txt')
m = MetaProduct({'a': a, 'b': b})
m.metadata.delete()
assert not Path('.a.txt.metadata').exists()
assert not Path('.b.txt.metadata').exists()
def test_can_iterate_over_products():
p1 = File('1.txt')
p2 = File('2.txt')
m = MetaProduct([p1, p2])
assert set(m) == {p1, p2}
def test_can_iterate_when_initialized_with_dictionary():
p1 = File('1.txt')
p2 = File('2.txt')
m = MetaProduct({'a': p1, 'b': p2})
assert set(m) == {p1, p2}
def test_can_create_task_with_more_than_one_product(tmp_directory):
dag = DAG()
fa = Path('a.txt')
fb = Path('b.txt')
fc = Path('c.txt')
fd = Path('d.txt')
ta = PythonCallable(touch_all, (File(fa), File(fb)), dag, 'ta')
tc = PythonCallable(touch_all_upstream, (File(fc), File(fd)), dag, 'tc')
ta >> tc
dag.build()
assert fa.exists()
assert fb.exists()
assert fc.exists()
assert fd.exists()
def test_download():
p1, p2 = Mock(), Mock()
m = MetaProduct({'a': p1, 'b': p2})
m.download()
p1.download.assert_called_once_with()
p2.download.assert_called_once_with()
def test_upload():
p1, p2 = Mock(), Mock()
m = MetaProduct({'a': p1, 'b': p2})
m.upload()
p1.upload.assert_called_once_with()
p2.upload.assert_called_once_with()
def test_error_if_metaproduct_initialized_with_non_products():
def do_stuff():
pass
with pytest.raises(AttributeError) as excinfo:
PythonCallable(do_stuff, {'a': 'not-a-product'}, dag=DAG())
assert 'Expected MetaProduct to initialize' in str(excinfo.value)
assert "'not-a-product'" in str(excinfo.value)
assert "<class 'str'>" in str(excinfo.value)
assert "File('not-a-product')" in str(excinfo.value)
@pytest.mark.parametrize(
'arg, expected',
[
[
{
'a': 1,
'b': 2
},
"MetaProduct({'a': 1, 'b': 2})",
],
[
{
'a': 1,
'b': 2,
'c': 3,
'd': 4,
'e': 5
},
"MetaProduct({'a': 1, 'b': 2, 'c': 3, 'd': 4, ...})",
],
],
)
def test_repr(arg, expected):
assert repr(MetaProduct(arg)) == expected