|
0 |
import os
|
|
1 |
import pytest
|
|
2 |
import pyct.cmd
|
0 | 3 |
from pyct.cmd import fetch_data, clean_data, copy_examples, examples
|
1 | |
import pytest
|
2 | 4 |
|
3 | 5 |
# Same as in pyct/examples/datasets.yml
|
4 | 6 |
DATASETS_CONTENT = u"""
|
|
27 | 29 |
Frank,75,9
|
28 | 30 |
"""
|
29 | 31 |
|
30 | |
FAKE_EXAMPLE_CONTENT = u"""
|
31 | |
import numpy as np
|
32 | |
|
33 | |
a = np.arange(10)
|
34 | |
"""
|
35 | |
|
|
32 |
EXAMPLE_CONTENT = u"""{
|
|
33 |
"cells": [
|
|
34 |
{
|
|
35 |
"cell_type": "markdown",
|
|
36 |
"metadata": {},
|
|
37 |
"source": [
|
|
38 |
"**NOTE:** This is a temporary notebook that gets created for tests."
|
|
39 |
]
|
|
40 |
},
|
|
41 |
],
|
|
42 |
"metadata": {
|
|
43 |
"language_info": {
|
|
44 |
"name": "python",
|
|
45 |
"pygments_lexer": "ipython3"
|
|
46 |
}
|
|
47 |
},
|
|
48 |
"nbformat": 4,
|
|
49 |
"nbformat_minor": 2
|
|
50 |
}
|
|
51 |
"""
|
|
52 |
|
|
53 |
|
|
54 |
@pytest.fixture(autouse=True)
|
|
55 |
def tmp_module(tmp_path):
|
|
56 |
"""This sets up a temporary directory structure meant to mimic a module
|
|
57 |
"""
|
|
58 |
project = tmp_path / "static_module"
|
|
59 |
project.mkdir()
|
|
60 |
examples = project / "examples"
|
|
61 |
examples.mkdir()
|
|
62 |
(examples / "Test_Example_Notebook.ipynb").write_text(EXAMPLE_CONTENT)
|
|
63 |
(examples / "datasets.yml").write_text(DATASETS_CONTENT)
|
|
64 |
(examples / "data").mkdir()
|
|
65 |
(examples / "data" / ".data_stubs").mkdir()
|
|
66 |
(examples / "data" / ".data_stubs" / "test_data.csv").write_text(TEST_FILE_CONTENT)
|
|
67 |
return project
|
|
68 |
|
|
69 |
@pytest.fixture(autouse=True)
|
|
70 |
def monkeypatch_find_examples(monkeypatch, tmp_module):
|
|
71 |
"""Monkeypatching find examples to use a tmp examples.
|
|
72 |
"""
|
|
73 |
def _find_examples(name):
|
|
74 |
return os.path.join(str(tmp_module), "examples")
|
|
75 |
monkeypatch.setattr(pyct.cmd, '_find_examples', _find_examples)
|
36 | 76 |
|
37 | 77 |
@pytest.fixture(scope='function')
|
38 | 78 |
def tmp_project(tmp_path):
|
|
49 | 89 |
datasets.write_text(DATASETS_CONTENT)
|
50 | 90 |
(examples / "data").mkdir()
|
51 | 91 |
example = examples / "Test_Example_Notebook.ipynb"
|
52 | |
example.write_text(FAKE_EXAMPLE_CONTENT)
|
|
92 |
example.write_text(u"Fake notebook contents")
|
53 | 93 |
return project
|
54 | 94 |
|
55 | 95 |
@pytest.fixture(scope='function')
|
|
82 | 122 |
with pytest.raises(ValueError):
|
83 | 123 |
examples(name="pyct", path=path, use_test_data=True)
|
84 | 124 |
assert (project / "examples" / "Test_Example_Notebook.ipynb").is_file()
|
85 | |
assert (project / "examples" / "Test_Example_Notebook.ipynb").read_text() == FAKE_EXAMPLE_CONTENT
|
|
125 |
assert (project / "examples" / "Test_Example_Notebook.ipynb").read_text() != EXAMPLE_CONTENT
|
86 | 126 |
assert (project / "examples" / "data" / "test_data.csv").is_file()
|
87 | 127 |
assert (project / "examples" / "data" / "test_data.csv").read_text() == REAL_FILE_CONTENT
|
88 | 128 |
|
|
93 | 133 |
data.write_text(REAL_FILE_CONTENT)
|
94 | 134 |
examples(name="pyct", path=path, use_test_data=True, force=True)
|
95 | 135 |
assert (project / "examples" / "Test_Example_Notebook.ipynb").is_file()
|
96 | |
assert (project / "examples" / "Test_Example_Notebook.ipynb").read_text() != FAKE_EXAMPLE_CONTENT
|
97 | |
assert (project / "examples" / "data" / "test_data.csv").is_file()
|
98 | |
assert (project / "examples" / "data" / "test_data.csv").read_text() != REAL_FILE_CONTENT
|
|
136 |
assert (project / "examples" / "Test_Example_Notebook.ipynb").read_text() == EXAMPLE_CONTENT
|
|
137 |
assert (project / "examples" / "data" / "test_data.csv").is_file()
|
|
138 |
assert (project / "examples" / "data" / "test_data.csv").read_text() == TEST_FILE_CONTENT
|
99 | 139 |
|
100 | 140 |
def test_copy_examples(tmp_project):
|
101 | 141 |
project = tmp_project
|
|
109 | 149 |
with pytest.raises(ValueError):
|
110 | 150 |
copy_examples(name="pyct", path=path)
|
111 | 151 |
assert (project / "examples" / "Test_Example_Notebook.ipynb").is_file()
|
112 | |
assert (project / "examples" / "Test_Example_Notebook.ipynb").read_text() == FAKE_EXAMPLE_CONTENT
|
|
152 |
assert (project / "examples" / "Test_Example_Notebook.ipynb").read_text() != EXAMPLE_CONTENT
|
113 | 153 |
|
114 | 154 |
def test_copy_examples_using_force_with_prexisting_content_in_target(tmp_project_with_examples):
|
115 | 155 |
project = tmp_project_with_examples
|
116 | 156 |
path = str(project / "examples")
|
117 | 157 |
copy_examples(name="pyct", path=path, force=True)
|
118 | 158 |
assert (project / "examples" / "Test_Example_Notebook.ipynb").is_file()
|
119 | |
assert (project / "examples" / "Test_Example_Notebook.ipynb").read_text() != FAKE_EXAMPLE_CONTENT
|
|
159 |
assert (project / "examples" / "Test_Example_Notebook.ipynb").read_text() == EXAMPLE_CONTENT
|
120 | 160 |
|
121 | 161 |
def test_fetch_data_using_test_data_with_no_file_in_data_copies_from_stubs(tmp_project_with_test_file):
|
122 | 162 |
project = tmp_project_with_test_file
|
123 | |
name = 'pyct'
|
124 | |
path = str(project / "examples")
|
125 | |
fetch_data(name=name, path=path, use_test_data=True)
|
|
163 |
path = str(project / "examples")
|
|
164 |
fetch_data(name="pyct", path=path, use_test_data=True)
|
126 | 165 |
assert (project / "examples" / "data" / "test_data.csv").is_file()
|
127 | 166 |
assert (project / "examples" / "data" / "test_data.csv").read_text() == TEST_FILE_CONTENT
|
128 | 167 |
|
129 | 168 |
def test_fetch_data_using_test_data_with_file_in_data_skips(tmp_project_with_test_file):
|
130 | 169 |
project = tmp_project_with_test_file
|
131 | |
name = 'pyct'
|
132 | |
path = str(project / "examples")
|
133 | |
data = project / "examples" / "data" / "test_data.csv"
|
134 | |
data.write_text(REAL_FILE_CONTENT)
|
135 | |
fetch_data(name=name, path=path, use_test_data=True)
|
|
170 |
path = str(project / "examples")
|
|
171 |
data = project / "examples" / "data" / "test_data.csv"
|
|
172 |
data.write_text(REAL_FILE_CONTENT)
|
|
173 |
fetch_data(name="pyct", path=path, use_test_data=True)
|
136 | 174 |
assert (project / "examples" / "data" / "test_data.csv").is_file()
|
137 | 175 |
assert (project / "examples" / "data" / "test_data.csv").read_text() == REAL_FILE_CONTENT
|
138 | 176 |
|
139 | 177 |
def test_fetch_data_using_test_data_and_force_with_file_in_data_over_writes(tmp_project_with_test_file):
|
140 | 178 |
project = tmp_project_with_test_file
|
141 | |
name = 'pyct'
|
142 | |
path = str(project / "examples")
|
143 | |
data = project / "examples" / "data" / "test_data.csv"
|
144 | |
data.write_text(REAL_FILE_CONTENT)
|
145 | |
fetch_data(name=name, path=path, use_test_data=True, force=True)
|
|
179 |
path = str(project / "examples")
|
|
180 |
data = project / "examples" / "data" / "test_data.csv"
|
|
181 |
data.write_text(REAL_FILE_CONTENT)
|
|
182 |
fetch_data(name="pyct", path=path, use_test_data=True, force=True)
|
146 | 183 |
assert (project / "examples" / "data" / "test_data.csv").is_file()
|
147 | 184 |
assert (project / "examples" / "data" / "test_data.csv").read_text() == TEST_FILE_CONTENT
|
148 | 185 |
|
149 | 186 |
def test_clean_data_when_data_file_is_real_does_nothing(tmp_project_with_test_file):
|
150 | 187 |
project = tmp_project_with_test_file
|
151 | |
name = 'pyct'
|
152 | |
path = str(project / "examples")
|
153 | |
data = project / "examples" / "data" / "test_data.csv"
|
154 | |
data.write_text(REAL_FILE_CONTENT)
|
155 | |
clean_data(name=name, path=path)
|
|
188 |
path = str(project / "examples")
|
|
189 |
data = project / "examples" / "data" / "test_data.csv"
|
|
190 |
data.write_text(REAL_FILE_CONTENT)
|
|
191 |
clean_data(name="pyct", path=path)
|
156 | 192 |
assert (project / "examples" / "data" / "test_data.csv").is_file()
|
157 | 193 |
assert (project / "examples" / "data" / "test_data.csv").read_text() == REAL_FILE_CONTENT
|
158 | 194 |
|
159 | 195 |
def test_clean_data_when_data_file_is_from_stubs_removes_file_from_data(tmp_project_with_test_file):
|
160 | 196 |
project = tmp_project_with_test_file
|
161 | |
name = 'pyct'
|
162 | 197 |
path = str(project / "examples")
|
163 | 198 |
data = project / "examples" / "data" / "test_data.csv"
|
164 | 199 |
data.write_text(TEST_FILE_CONTENT)
|
165 | |
clean_data(name=name, path=path)
|
|
200 |
clean_data(name="pyct", path=path)
|
166 | 201 |
assert not (project / "examples" / "data" / "test_data.csv").is_file()
|
167 | 202 |
assert (project / "examples" / "data" / ".data_stubs" / "test_data.csv").is_file()
|
168 | 203 |
assert (project / "examples" / "data" / ".data_stubs" / "test_data.csv").read_text() == TEST_FILE_CONTENT
|
169 | 204 |
|
170 | 205 |
def test_clean_data_when_file_not_in_data_does_nothing(tmp_project_with_test_file):
|
171 | 206 |
project = tmp_project_with_test_file
|
172 | |
name = 'pyct'
|
173 | |
path = str(project / "examples")
|
174 | |
clean_data(name=name, path=path)
|
|
207 |
path = str(project / "examples")
|
|
208 |
clean_data(name="pyct", path=path)
|
175 | 209 |
assert not (project / "examples" / "data" / "test_data.csv").is_file()
|
176 | 210 |
assert (project / "examples" / "data" / ".data_stubs" / "test_data.csv").is_file()
|
177 | 211 |
assert (project / "examples" / "data" / ".data_stubs" / "test_data.csv").read_text() == TEST_FILE_CONTENT
|
178 | 212 |
|
179 | 213 |
def test_clean_data_when_stubs_is_empty_does_nothing(tmp_project_with_stubs):
|
180 | 214 |
project = tmp_project_with_stubs
|
181 | |
name = 'pyct'
|
182 | |
path = str(project / "examples")
|
183 | |
data = project / "examples" / "data" / "test_data.csv"
|
184 | |
data.write_text(REAL_FILE_CONTENT)
|
185 | |
clean_data(name=name, path=path)
|
|
215 |
path = str(project / "examples")
|
|
216 |
data = project / "examples" / "data" / "test_data.csv"
|
|
217 |
data.write_text(REAL_FILE_CONTENT)
|
|
218 |
clean_data(name="pyct", path=path)
|
186 | 219 |
assert (project / "examples" / "data" / "test_data.csv").is_file()
|
187 | 220 |
assert not (project / "examples" / "data" / ".data_stubs" / "test_data.csv").is_file()
|
188 | 221 |
|
189 | 222 |
def test_clean_data_when_no_stubs_dir_does_nothing(tmp_project_with_examples):
|
190 | 223 |
project = tmp_project_with_examples
|
191 | |
name = 'pyct'
|
192 | |
path = str(project / "examples")
|
193 | |
data = project / "examples" / "data" / "test_data.csv"
|
194 | |
data.write_text(REAL_FILE_CONTENT)
|
195 | |
clean_data(name=name, path=path)
|
196 | |
assert (project / "examples" / "data" / "test_data.csv").is_file()
|
|
224 |
path = str(project / "examples")
|
|
225 |
data = project / "examples" / "data" / "test_data.csv"
|
|
226 |
data.write_text(REAL_FILE_CONTENT)
|
|
227 |
clean_data(name="pyct", path=path)
|
|
228 |
assert (project / "examples" / "data" / "test_data.csv").is_file()
|