forked from coursera-dl/edx-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_utils.py
171 lines (137 loc) · 5.45 KB
/
test_utils.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import subprocess
import pytest
import six
from edx_dl import utils
def test_clean_filename():
strings = {
'(23:90)': '23-90',
'(:': '-',
'a téest &and a@noòtheèr': 'a_test_and_another',
'Lecture 2.7 - Evaluation and Operators (16:25)':
'Lecture_2.7_-_Evaluation_and_Operators_16-25',
'Week 3: Data and Abstraction':
'Week_3-_Data_and_Abstraction',
' (Week 1) BRANDING: Marketing Strategy and Brand Positioning':
'Week_1_BRANDING-__Marketing_Strategy_and_Brand_Positioning',
'test & " adfas': 'test___adfas',
' ': ''
}
for k, v in six.iteritems(strings):
actual_res = utils.clean_filename(k)
assert actual_res == v, actual_res
def test_clean_filename_minimal_change():
strings = {
'(23:90)': '(23-90)',
'(:': '(-',
'a téest &and a@noòtheèr': 'a téest &and a@noòtheèr',
'Lecture 2.7 - Evaluation and Operators (16:25)':
'Lecture 2.7 - Evaluation and Operators (16-25)',
'Week 3: Data and Abstraction':
'Week 3- Data and Abstraction',
' (Week 1) BRANDING: Marketing Strategy and Brand Positioning':
' (Week 1) BRANDING- Marketing Strategy and Brand Positioning',
'test & " adfas': 'test & " adfas',
' ': u'\xa0'
}
for k, v in six.iteritems(strings):
actual_res = utils.clean_filename(k, minimal_change=True)
assert actual_res == v, actual_res
@pytest.mark.skipif(True,
reason="Needs change in interface")
def test_execute_command_should_succeed():
actual_res = utils.execute_command(['ls', '--help'])
assert actual_res == 0, actual_res
@pytest.mark.skipif(True,
reason="Needs change in interface")
def test_execute_command_should_fail():
try:
actual_res = utils.execute_command(['ls', '--help-does-not-exist'])
except subprocess.CalledProcessError as e:
assert True, "Expected exception thrown."
else:
assert False, "Unexpected exception (or no exception) thrown"
# For the future
# actual_res == 2, actual_res
def test_get_filename_from_prefix():
target_dir = '.'
cases = {
'requirements.txt': 'requirements',
'does-not-exist': None,
# 'requirements': 'requirements-dev', # depends on filesystem!
}
for k, v in six.iteritems(cases):
actual_res = utils.get_filename_from_prefix(target_dir, k)
assert actual_res == v, actual_res
def test_remove_duplicates_without_seen():
empty_set = set()
lists = [
([], [], empty_set),
([1], [1], {1}),
([1, 1], [1], {1}),
([None], [None], {None}),
([None, None], [None], {None}),
([1, None], [1, None], {1, None}),
(['a'], ['a'], {'a'}),
(['a', 'a'], ['a'], {'a'}),
(['a', 'b'], ['a', 'b'], {'a', 'b'}),
(['a', 'b', 'a'], ['a', 'b'], {'a', 'b'}),
(['a', 'a', 'b'], ['a', 'b'], {'a', 'b'}),
(['b', 'a', 'b'], ['b', 'a'], {'a', 'b'}),
(['b', 'a', 'a'], ['b', 'a'], {'a', 'b'}),
([1, 2, 1, 2], [1, 2], {1, 2}),
]
for l, reduced_l, seen in lists:
actual_res = utils.remove_duplicates(l)
assert actual_res == (reduced_l, seen), actual_res
def test_remove_duplicates_with_seen():
empty_set = set()
lists = [
([], empty_set, [], empty_set),
([], {None}, [], {None}),
([], {1}, [], {1}),
([], {1, 2}, [], {1, 2}),
([1], empty_set, [1], {1}),
([1], {1}, [], {1}),
([1, 1], empty_set, [1], {1}),
([1, 1], {1}, [], {1}),
([1, 1], {None}, [1], {1, None}),
([1, 1], {2}, [1], {1, 2}),
([1, 1], {1, 2}, [], {1, 2}),
([None], empty_set, [None], {None}),
([None], {1}, [None], {1, None}),
([None], {1, 2}, [None], {1, 2, None}),
([None], {1, 2}, [None], {2, 1, None}),
([None], {1, 2}, [None], {None, 2, 1}),
([None], {1, 2}, [None], {2, None, 1}),
([None], {1, 2, None}, [], {1, 2, None}),
([1, None], empty_set, [1, None], {1, None}),
([1, None], {1}, [None], {1, None}),
([1, None], {None}, [1], {1, None}),
([1, None], {1, None}, [], {1, None}),
([1, None], {1, None, 2}, [], {1, None, 2}),
([None, 1], empty_set, [None, 1], {1, None}),
([None, 1], {1}, [None], {1, None}),
([None, 1], {None}, [1], {1, None}),
([None, 1], {1, None}, [], {1, None}),
([None, 1], {1, None, 2}, [], {1, None, 2}),
(['a'], empty_set, ['a'], {'a'}),
(['a'], {'a'}, [], {'a'}),
(['a'], {None}, ['a'], {'a', None}),
(['a'], {'b'}, ['a'], {'a', 'b'}),
(['a'], {'a', 'b'}, [], {'a', 'b'}),
(['a'], {'a', 'b', tuple()}, [], {'a', 'b', tuple()}),
# (['a', 'a'], ['a'], {'a'}),
# (['a', 'b'], ['a', 'b'], {'a', 'b'}),
# (['a', 'b', 'a'], ['a', 'b'], {'a', 'b'}),
# (['a', 'a', 'b'], ['a', 'b'], {'a', 'b'}),
# (['b', 'a', 'b'], ['b', 'a'], {'a', 'b'}),
# (['b', 'a', 'a'], ['b', 'a'], {'a', 'b'}),
# ([1, 2, 1, 2], [1, 2], {1, 2}),
# ([1, 2, 1, 2], [1, 2], {1, 2}),
]
for l, seen_before, reduced_l, seen_after in lists:
actual_res = utils.remove_duplicates(l, seen_before)
assert actual_res == (reduced_l, seen_after), actual_res