Codebase list pyct / a58d62d
Making all test files temporary (#70) * Adding init file to tests * Adding example copying to pkg * Adding pkg command to tox properly * Monkeypatching _find_examples, moving examples into tests, no more pkg command * Adding init files * Made all test files temporary * Making python2 friendly Julia Signell authored 5 years ago GitHub committed 5 years ago
6 changed file(s) with 81 addition(s) and 96 deletion(s). Raw diff Collapse all Expand all
+0
-38
examples/Test_Example_Notebook.ipynb less more
0 {
1 "cells": [
2 {
3 "cell_type": "markdown",
4 "metadata": {},
5 "source": [
6 "**NOTE:** This notebook is used in the tests and should not be deleted."
7 ]
8 },
9 {
10 "cell_type": "code",
11 "execution_count": null,
12 "metadata": {},
13 "outputs": [],
14 "source": [
15 "import pandas as pd"
16 ]
17 },
18 {
19 "cell_type": "code",
20 "execution_count": null,
21 "metadata": {},
22 "outputs": [],
23 "source": [
24 "df = pd.read_csv('../data/test_data.csv')\n",
25 "df.head()"
26 ]
27 }
28 ],
29 "metadata": {
30 "language_info": {
31 "name": "python",
32 "pygments_lexer": "ipython3"
33 }
34 },
35 "nbformat": 4,
36 "nbformat_minor": 2
37 }
+0
-4
examples/data/.data_stubs/test_data.csv less more
0 name,score,rank
1 Alice,100.5,1
2 Bob,50.3,2
3 Charlie,25,3
+0
-5
examples/datasets.yml less more
0 data:
1 - url: this_should_never_be_used
2 title: 'Test Data'
3 files:
4 - test_data.csv
(New empty file)
0 import os
1 import pytest
2 import pyct.cmd
03 from pyct.cmd import fetch_data, clean_data, copy_examples, examples
1 import pytest
24
35 # Same as in pyct/examples/datasets.yml
46 DATASETS_CONTENT = u"""
2729 Frank,75,9
2830 """
2931
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)
3676
3777 @pytest.fixture(scope='function')
3878 def tmp_project(tmp_path):
4989 datasets.write_text(DATASETS_CONTENT)
5090 (examples / "data").mkdir()
5191 example = examples / "Test_Example_Notebook.ipynb"
52 example.write_text(FAKE_EXAMPLE_CONTENT)
92 example.write_text(u"Fake notebook contents")
5393 return project
5494
5595 @pytest.fixture(scope='function')
82122 with pytest.raises(ValueError):
83123 examples(name="pyct", path=path, use_test_data=True)
84124 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
86126 assert (project / "examples" / "data" / "test_data.csv").is_file()
87127 assert (project / "examples" / "data" / "test_data.csv").read_text() == REAL_FILE_CONTENT
88128
93133 data.write_text(REAL_FILE_CONTENT)
94134 examples(name="pyct", path=path, use_test_data=True, force=True)
95135 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
99139
100140 def test_copy_examples(tmp_project):
101141 project = tmp_project
109149 with pytest.raises(ValueError):
110150 copy_examples(name="pyct", path=path)
111151 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
113153
114154 def test_copy_examples_using_force_with_prexisting_content_in_target(tmp_project_with_examples):
115155 project = tmp_project_with_examples
116156 path = str(project / "examples")
117157 copy_examples(name="pyct", path=path, force=True)
118158 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
120160
121161 def test_fetch_data_using_test_data_with_no_file_in_data_copies_from_stubs(tmp_project_with_test_file):
122162 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)
126165 assert (project / "examples" / "data" / "test_data.csv").is_file()
127166 assert (project / "examples" / "data" / "test_data.csv").read_text() == TEST_FILE_CONTENT
128167
129168 def test_fetch_data_using_test_data_with_file_in_data_skips(tmp_project_with_test_file):
130169 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)
136174 assert (project / "examples" / "data" / "test_data.csv").is_file()
137175 assert (project / "examples" / "data" / "test_data.csv").read_text() == REAL_FILE_CONTENT
138176
139177 def test_fetch_data_using_test_data_and_force_with_file_in_data_over_writes(tmp_project_with_test_file):
140178 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)
146183 assert (project / "examples" / "data" / "test_data.csv").is_file()
147184 assert (project / "examples" / "data" / "test_data.csv").read_text() == TEST_FILE_CONTENT
148185
149186 def test_clean_data_when_data_file_is_real_does_nothing(tmp_project_with_test_file):
150187 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)
156192 assert (project / "examples" / "data" / "test_data.csv").is_file()
157193 assert (project / "examples" / "data" / "test_data.csv").read_text() == REAL_FILE_CONTENT
158194
159195 def test_clean_data_when_data_file_is_from_stubs_removes_file_from_data(tmp_project_with_test_file):
160196 project = tmp_project_with_test_file
161 name = 'pyct'
162197 path = str(project / "examples")
163198 data = project / "examples" / "data" / "test_data.csv"
164199 data.write_text(TEST_FILE_CONTENT)
165 clean_data(name=name, path=path)
200 clean_data(name="pyct", path=path)
166201 assert not (project / "examples" / "data" / "test_data.csv").is_file()
167202 assert (project / "examples" / "data" / ".data_stubs" / "test_data.csv").is_file()
168203 assert (project / "examples" / "data" / ".data_stubs" / "test_data.csv").read_text() == TEST_FILE_CONTENT
169204
170205 def test_clean_data_when_file_not_in_data_does_nothing(tmp_project_with_test_file):
171206 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)
175209 assert not (project / "examples" / "data" / "test_data.csv").is_file()
176210 assert (project / "examples" / "data" / ".data_stubs" / "test_data.csv").is_file()
177211 assert (project / "examples" / "data" / ".data_stubs" / "test_data.csv").read_text() == TEST_FILE_CONTENT
178212
179213 def test_clean_data_when_stubs_is_empty_does_nothing(tmp_project_with_stubs):
180214 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)
186219 assert (project / "examples" / "data" / "test_data.csv").is_file()
187220 assert not (project / "examples" / "data" / ".data_stubs" / "test_data.csv").is_file()
188221
189222 def test_clean_data_when_no_stubs_dir_does_nothing(tmp_project_with_examples):
190223 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()
3535
3636 [pytest]
3737 addopts = -v --pyargs
38 norecursedirs = .git
38 norecursedirs = doc .git dist build _build .ipynb_checkpoints apps
3939
4040 [flake8]
4141 ignore = E,W