Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 410 lines (349 sloc) 14.722 kB
6c6d752 @lakshmivyas Slowly refactoring sitemap inorder to support proper continuous gener…
lakshmivyas authored
1 """
2
3 uses py.test
4
2c341c8 @lakshmivyas Change monitoring complete. Can stop monitor as well
lakshmivyas authored
5 sudo easy_install py
6c6d752 @lakshmivyas Slowly refactoring sitemap inorder to support proper continuous gener…
lakshmivyas authored
6
7 http://codespeak.net/py/dist/test.html
8
9 """
f246313 @lakshmivyas Added siteinfo initial imp and tests
lakshmivyas authored
10 import os
11 import sys
61aaca5 @lakshmivyas test for YAML content processor added
lakshmivyas authored
12 from datetime import datetime, timedelta
f246313 @lakshmivyas Added siteinfo initial imp and tests
lakshmivyas authored
13 import unittest
2c341c8 @lakshmivyas Change monitoring complete. Can stop monitor as well
lakshmivyas authored
14 from threading import Thread
15 from Queue import Queue
16 from Queue import Empty
17
6b691c6 @lakshmivyas Folder support added
lakshmivyas authored
18 from django.conf import settings
dfb9939 @lakshmivyas Added test for page rendering
lakshmivyas authored
19 from django.utils.html import strip_spaces_between_tags
6b691c6 @lakshmivyas Folder support added
lakshmivyas authored
20
f246313 @lakshmivyas Added siteinfo initial imp and tests
lakshmivyas authored
21 TEST_ROOT = os.path.dirname(os.path.abspath(__file__))
22 ROOT = os.path.abspath(TEST_ROOT + "/..")
23
24 sys.path = [ROOT] + sys.path
25
26 from hydeengine.file_system import File, Folder
188f8d8 @lakshmivyas Integrated processing logic
lakshmivyas authored
27 from hydeengine import url, Initializer, Generator, setup_env
9ba007c @lakshmivyas Added test to make sure variables are propogated to all instances of …
lakshmivyas authored
28 from hydeengine.siteinfo import SiteNode, SiteInfo, Page
f246313 @lakshmivyas Added siteinfo initial imp and tests
lakshmivyas authored
29
188f8d8 @lakshmivyas Integrated processing logic
lakshmivyas authored
30 TEST_ROOT = Folder(TEST_ROOT)
31 TEST_SITE = TEST_ROOT.child_folder("test_site")
f246313 @lakshmivyas Added siteinfo initial imp and tests
lakshmivyas authored
32
6b691c6 @lakshmivyas Folder support added
lakshmivyas authored
33 def setup_module(module):
f246313 @lakshmivyas Added siteinfo initial imp and tests
lakshmivyas authored
34 Initializer(TEST_SITE.path).initialize(ROOT, force=True)
35 setup_env(TEST_SITE.path)
36
6b691c6 @lakshmivyas Folder support added
lakshmivyas authored
37 def teardown_module(module):
f246313 @lakshmivyas Added siteinfo initial imp and tests
lakshmivyas authored
38 TEST_SITE.delete()
61aaca5 @lakshmivyas test for YAML content processor added
lakshmivyas authored
39
9bdb1c6 @lakshmivyas Added filters
lakshmivyas authored
40 class TestFilters:
41
42 def setup_method(self, method):
43 self.files = []
44
45 def teardown_method(self, method):
46 for f in self.files:
47 f.delete()
48
49 def test_filters(self):
50 site = SiteInfo(settings, TEST_SITE.path)
51 for name in [".banjo", ".hidden", "junk.abc~"]:
52 f = File(site.content_folder.child(name))
53 self.files.append(f)
54 f.write("junk")
55
56 original_exclude = settings.FILTER['exclude']
57 original_include = settings.FILTER['include']
58 settings.FILTER = {}
59 site = SiteInfo(settings, TEST_SITE.path)
60 site.refresh()
61
62 for f in self.files:
63 assert site.find_resource(f)
64
65 settings.FILTER = {
66 'include': (),
67 'exclude': (".*","*~")
68 }
69
70 site = SiteInfo(settings, TEST_SITE.path)
71 site.refresh()
72
73 for f in self.files:
74 assert not site.find_resource(f)
75
76 settings.FILTER = {
77 'include': (".banjo",),
78 'exclude': (".*","*~")
79 }
80
81 site = SiteInfo(settings, TEST_SITE.path)
82 site.refresh()
83
84 for f in self.files:
85 if f.name == ".banjo":
86 assert site.find_resource(f)
87 else:
88 assert not site.find_resource(f)
89
90 settings.FILTER['exclude'] = original_exclude
91 settings.FILTER['include'] = original_include
92
93
6b691c6 @lakshmivyas Folder support added
lakshmivyas authored
94 class TestSiteInfo:
95
96 def setup_method(self, method):
188f8d8 @lakshmivyas Integrated processing logic
lakshmivyas authored
97 self.site = SiteInfo(settings, TEST_SITE.path)
98 self.site.refresh()
f246313 @lakshmivyas Added siteinfo initial imp and tests
lakshmivyas authored
99
6b691c6 @lakshmivyas Folder support added
lakshmivyas authored
100 def assert_node_complete(self, node, folder):
101 assert node.folder.path == folder.path
102 test_case = self
103 class Visitor(object):
104 def visit_folder(self, folder):
105 child = node.find_child(folder)
106 assert child
107 test_case.assert_node_complete(child, folder)
108
109 def visit_file(self, a_file):
110 assert node.find_resource(a_file)
111 folder.list(Visitor())
112
113 def test_population(self):
114 assert self.site.name == "test_site"
115 self.assert_node_complete(self.site, TEST_SITE)
116
117 def test_type(self):
118 def assert_node_type(node_dir, type):
119 node = self.site.find_child(Folder(node_dir))
120 assert node
121 assert Folder(node_dir).same_as(node.folder)
122 for child in node.walk():
123 assert child.type == type
124 assert_node_type(settings.CONTENT_DIR, "content")
125 assert_node_type(settings.MEDIA_DIR, "media")
126 assert_node_type(settings.LAYOUT_DIR, "layout")
f246313 @lakshmivyas Added siteinfo initial imp and tests
lakshmivyas authored
127
6c6d752 @lakshmivyas Slowly refactoring sitemap inorder to support proper continuous gener…
lakshmivyas authored
128 def test_attributes(self):
6b691c6 @lakshmivyas Folder support added
lakshmivyas authored
129 for node in self.site.walk():
6c6d752 @lakshmivyas Slowly refactoring sitemap inorder to support proper continuous gener…
lakshmivyas authored
130 self.assert_node_attributes(node)
131 for resource in node.resources:
132 self.assert_resource_attributes(resource)
133
134 def assert_node_attributes(self, node):
135 fragment = self.get_node_fragment(node)
136 if node.type == "content":
137 fragment = node.folder.get_fragment(self.site.content_folder)
138 elif node.type == "media":
139 fragment = node.folder.get_fragment(self.site.folder)
140 if node.type in ("content", "media"):
188f8d8 @lakshmivyas Integrated processing logic
lakshmivyas authored
141 fragment = ("/" + fragment.strip("/")).rstrip("/")
142 assert fragment == node.url
143 assert settings.SITE_WWW_URL + fragment == node.full_url
6c6d752 @lakshmivyas Slowly refactoring sitemap inorder to support proper continuous gener…
lakshmivyas authored
144 else:
145 assert not node.url
146 assert not node.full_url
6b691c6 @lakshmivyas Folder support added
lakshmivyas authored
147
6c6d752 @lakshmivyas Slowly refactoring sitemap inorder to support proper continuous gener…
lakshmivyas authored
148 assert node.source_folder == node.folder
149 if not node == self.site and node.type not in ("content", "media"):
150 assert not node.target_folder
151 assert not node.temp_folder
152 else:
153 assert node.target_folder.same_as(Folder(
154 os.path.join(settings.DEPLOY_DIR,
155 fragment.lstrip("/"))))
156 assert node.temp_folder.same_as(Folder(
157 os.path.join(settings.TMP_DIR,
158 fragment.lstrip("/"))))
159
160 def assert_resource_attributes(self, resource):
161 node = resource.node
162 fragment = self.get_node_fragment(node)
163 if resource.node.type in ("content", "media"):
164 assert (resource.url ==
61aaca5 @lakshmivyas test for YAML content processor added
lakshmivyas authored
165 url.join(node.url, resource.file.name))
6c6d752 @lakshmivyas Slowly refactoring sitemap inorder to support proper continuous gener…
lakshmivyas authored
166 assert (resource.full_url ==
61aaca5 @lakshmivyas test for YAML content processor added
lakshmivyas authored
167 url.join(node.full_url, resource.file.name))
6c6d752 @lakshmivyas Slowly refactoring sitemap inorder to support proper continuous gener…
lakshmivyas authored
168 assert resource.target_file.same_as(
169 File(node.target_folder.child(
61aaca5 @lakshmivyas test for YAML content processor added
lakshmivyas authored
170 resource.file.name)))
6c6d752 @lakshmivyas Slowly refactoring sitemap inorder to support proper continuous gener…
lakshmivyas authored
171 assert resource.temp_file.same_as(
61aaca5 @lakshmivyas test for YAML content processor added
lakshmivyas authored
172 File(node.temp_folder.child(resource.file.name)))
6c6d752 @lakshmivyas Slowly refactoring sitemap inorder to support proper continuous gener…
lakshmivyas authored
173 else:
174 assert not resource.url
175 assert not resource.full_url
176
177 assert resource.source_file.parent.same_as(node.folder)
61aaca5 @lakshmivyas test for YAML content processor added
lakshmivyas authored
178 assert resource.source_file.name == resource.file.name
6c6d752 @lakshmivyas Slowly refactoring sitemap inorder to support proper continuous gener…
lakshmivyas authored
179
180 def get_node_fragment(self, node):
181 fragment = ''
182 if node.type == "content":
183 fragment = node.folder.get_fragment(self.site.content_folder)
184 elif node.type == "media":
185 fragment = node.folder.get_fragment(self.site.folder)
a0482f7 @lakshmivyas Added monitoring support. Uses Queue for delivering changes.
lakshmivyas authored
186 return fragment
6c6d752 @lakshmivyas Slowly refactoring sitemap inorder to support proper continuous gener…
lakshmivyas authored
187
2f8bda1 @lakshmivyas Removed Init method
lakshmivyas authored
188
188f8d8 @lakshmivyas Integrated processing logic
lakshmivyas authored
189 class MonitorTests(object):
190 def clean_queue(self):
191 while not self.queue.empty():
192 try:
193 self.queue.get()
194 self.queue.task_done()
195 except Empty:
196 break
197
2f8bda1 @lakshmivyas Removed Init method
lakshmivyas authored
198 def setup_class(cls):
199 cls.site = None
188f8d8 @lakshmivyas Integrated processing logic
lakshmivyas authored
200 cls.queue = Queue()
2f8bda1 @lakshmivyas Removed Init method
lakshmivyas authored
201
202 def teardown_class(cls):
203 if cls.site:
204 cls.site.dont_monitor()
205
a0482f7 @lakshmivyas Added monitoring support. Uses Queue for delivering changes.
lakshmivyas authored
206 def setup_method(self, method):
207 self.site = SiteInfo(settings, TEST_SITE.path)
188f8d8 @lakshmivyas Integrated processing logic
lakshmivyas authored
208 self.site.refresh()
2c341c8 @lakshmivyas Change monitoring complete. Can stop monitor as well
lakshmivyas authored
209 self.exception_queue = Queue()
188f8d8 @lakshmivyas Integrated processing logic
lakshmivyas authored
210 self.clean_queue()
2c341c8 @lakshmivyas Change monitoring complete. Can stop monitor as well
lakshmivyas authored
211
2f8bda1 @lakshmivyas Removed Init method
lakshmivyas authored
212 class TestSiteInfoMonitoring(MonitorTests):
213
2c341c8 @lakshmivyas Change monitoring complete. Can stop monitor as well
lakshmivyas authored
214 def change_checker(self, change, path):
a0482f7 @lakshmivyas Added monitoring support. Uses Queue for delivering changes.
lakshmivyas authored
215 try:
188f8d8 @lakshmivyas Integrated processing logic
lakshmivyas authored
216 changes = self.queue.get(block=True, timeout=20)
217 self.queue.task_done()
a0482f7 @lakshmivyas Added monitoring support. Uses Queue for delivering changes.
lakshmivyas authored
218 assert changes
2c341c8 @lakshmivyas Change monitoring complete. Can stop monitor as well
lakshmivyas authored
219 assert not changes['exception']
220 assert changes['change'] == change
a0482f7 @lakshmivyas Added monitoring support. Uses Queue for delivering changes.
lakshmivyas authored
221 assert changes['resource']
61aaca5 @lakshmivyas test for YAML content processor added
lakshmivyas authored
222 assert changes['resource'].file.path == path
2c341c8 @lakshmivyas Change monitoring complete. Can stop monitor as well
lakshmivyas authored
223 except:
224 self.exception_queue.put(sys.exc_info())
225 raise
a0482f7 @lakshmivyas Added monitoring support. Uses Queue for delivering changes.
lakshmivyas authored
226
2c341c8 @lakshmivyas Change monitoring complete. Can stop monitor as well
lakshmivyas authored
227 def test_monitor_stop(self):
228 m = self.site.monitor()
229 self.site.dont_monitor()
230 assert not m.isAlive()
231
232 def test_modify(self):
188f8d8 @lakshmivyas Integrated processing logic
lakshmivyas authored
233 self.site.monitor(self.queue)
2c341c8 @lakshmivyas Change monitoring complete. Can stop monitor as well
lakshmivyas authored
234 path = self.site.media_folder.child("css/base.css")
235 t = Thread(target=self.change_checker,
236 kwargs={"change":"Modified", "path":path})
a0482f7 @lakshmivyas Added monitoring support. Uses Queue for delivering changes.
lakshmivyas authored
237 t.start()
2c341c8 @lakshmivyas Change monitoring complete. Can stop monitor as well
lakshmivyas authored
238 os.utime(path, None)
239 t.join()
240 assert self.exception_queue.empty()
241
242 def test_add(self, direct=False):
188f8d8 @lakshmivyas Integrated processing logic
lakshmivyas authored
243 self.site.monitor(self.queue)
2c341c8 @lakshmivyas Change monitoring complete. Can stop monitor as well
lakshmivyas authored
244 path = self.site.layout_folder.child("test.ggg")
245 t = Thread(target=self.change_checker,
246 kwargs={"change":"Added", "path":path})
247 t.start()
248 f = File(path)
249 f.write("test")
250 t.join()
251 if not direct:
252 f.delete()
188f8d8 @lakshmivyas Integrated processing logic
lakshmivyas authored
253 assert self.exception_queue.empty()
2c341c8 @lakshmivyas Change monitoring complete. Can stop monitor as well
lakshmivyas authored
254
255 def test_delete(self):
256 path = self.site.layout_folder.child("test.ggg")
257 self.test_add(direct=True)
258 t = Thread(target=self.change_checker,
259 kwargs={"change":"Deleted", "path":path})
260 t.start()
261 File(path).delete()
262 t.join()
263 assert self.exception_queue.empty()
264
2f8bda1 @lakshmivyas Removed Init method
lakshmivyas authored
265 class TestYAMLProcessor(MonitorTests):
266
61aaca5 @lakshmivyas test for YAML content processor added
lakshmivyas authored
267 def yaml_checker(self, path, vars):
268 try:
188f8d8 @lakshmivyas Integrated processing logic
lakshmivyas authored
269 changes = self.queue.get(block=True, timeout=5)
270 self.queue.task_done()
61aaca5 @lakshmivyas test for YAML content processor added
lakshmivyas authored
271 assert changes
272 assert not changes['exception']
273 resource = changes['resource']
274 assert resource
275 assert resource.file.path == path
e950ba6 @lakshmivyas Integrated YAMLContentProcessor to Page
lakshmivyas authored
276 # from hydeengine.content_processors import YAMLContentProcessor
277 # YAMLContentProcessor.process(resource)
61aaca5 @lakshmivyas test for YAML content processor added
lakshmivyas authored
278 for key, value in vars.iteritems():
279 assert hasattr(resource, key)
280 assert getattr(resource, key) == value
281 except:
282 self.exception_queue.put(sys.exc_info())
283 raise
284
285 def test_variables_are_added(self):
286 vars = {}
287 vars["title"] = "Test Title"
288 vars["created"] = datetime.now()
289 vars["updated"] = datetime.now() + timedelta(hours=1)
290 content = "{%hyde\n"
291 for key, value in vars.iteritems():
292 content += " %s: %s\n" % (key, value)
293 content += "%}"
294 out = File(self.site.content_folder.child("test_yaml.html"))
188f8d8 @lakshmivyas Integrated processing logic
lakshmivyas authored
295 self.site.monitor(self.queue)
61aaca5 @lakshmivyas test for YAML content processor added
lakshmivyas authored
296 t = Thread(target=self.yaml_checker,
297 kwargs={"path":out.path, "vars":vars})
298 t.start()
299 out.write(content)
300 t.join()
301 assert self.exception_queue.empty()
9ba007c @lakshmivyas Added test to make sure variables are propogated to all instances of …
lakshmivyas authored
302 # Ensure default values are added for all pages
303 #
e950ba6 @lakshmivyas Integrated YAMLContentProcessor to Page
lakshmivyas authored
304 temp = File(self.site.content_folder.child("test.html"))
305 temp.write('text')
306 page = Page(temp, self.site)
9ba007c @lakshmivyas Added test to make sure variables are propogated to all instances of …
lakshmivyas authored
307 for key, value in vars.iteritems():
308 assert hasattr(page, key)
309 assert not getattr(page, key)
e950ba6 @lakshmivyas Integrated YAMLContentProcessor to Page
lakshmivyas authored
310 temp.delete()
311 out.delete()
188f8d8 @lakshmivyas Integrated processing logic
lakshmivyas authored
312
313 class TestProcessing(MonitorTests):
314 def checker(self, asserter):
315 try:
316 changes = self.queue.get(block=True, timeout=5)
317 self.queue.task_done()
318 assert changes
319 assert not changes['exception']
320 resource = changes['resource']
321 assert resource
322 asserter(resource)
323 except:
324 self.exception_queue.put(sys.exc_info())
325 raise
326
327 def assert_valid_css(self, actual_css_resource):
328 expected_text = File(
329 TEST_ROOT.child("test_dest.css")).read_all()
4155c21 @lakshmivyas Tweaked processing to perform tmp file processing
lakshmivyas authored
330 self.generator.process(actual_css_resource)
331
332 # Ensure source file is not changed
333 # The source should be copied to tmp and then
334 # the processor should do its thing.
335 original_source = File(
336 TEST_ROOT.child("test_src.css")).read_all()
337 source_text = actual_css_resource.file.read_all()
338 assert original_source == source_text
339 actual_text = actual_css_resource.temp_file.read_all()
188f8d8 @lakshmivyas Integrated processing logic
lakshmivyas authored
340 assert expected_text == actual_text
61aaca5 @lakshmivyas test for YAML content processor added
lakshmivyas authored
341
188f8d8 @lakshmivyas Integrated processing logic
lakshmivyas authored
342 def test_process_css_with_templates(self):
343 original_MP = settings.MEDIA_PROCESSORS
344 original_site = settings.SITE_ROOT
345 settings.MEDIA_PROCESSORS = {"*":{".css":
346 ('hydeengine.media_processors.TemplateProcessor',)}}
347 settings.SITE_ROOT = "www.hyde-test.bogus/"
348 self.generator = Generator(TEST_SITE.path)
349 self.generator.build_siteinfo()
350 source = File(TEST_ROOT.child("test_src.css"))
4155c21 @lakshmivyas Tweaked processing to perform tmp file processing
lakshmivyas authored
351 self.site.refresh()
188f8d8 @lakshmivyas Integrated processing logic
lakshmivyas authored
352 self.site.monitor(self.queue)
353 t = Thread(target=self.checker,
354 kwargs={"asserter":self.assert_valid_css})
355 t.start()
356 source.copy_to(self.site.media_folder.child("test.css"))
357 t.join()
358 settings.MEDIA_PROCESSORS = original_MP
359 settings.SITE_ROOT = original_site
dfb9939 @lakshmivyas Added test for page rendering
lakshmivyas authored
360 assert self.exception_queue.empty()
361
362 def assert_html_equals(self, expected, actual):
363 expected = strip_spaces_between_tags(expected.strip())
364 actual = strip_spaces_between_tags(actual.strip())
365 assert expected == actual
366
367 def assert_valid_html(self, actual_html_resource):
368 expected_text = File(
369 TEST_ROOT.child("test_dest.html")).read_all()
370 self.generator.process(actual_html_resource)
371
372 # Ensure source file is not changed
373 # The source should be copied to tmp and then
374 # the processor should do its thing.
375 original_source = File(
376 TEST_ROOT.child("test_src.html")).read_all()
377 source_text = actual_html_resource.file.read_all()
378 assert original_source == source_text
379 actual_text = actual_html_resource.temp_file.read_all()
380 self.assert_html_equals(expected_text, actual_text)
381
382 def test_process_page_rendering(self):
383 self.generator = Generator(TEST_SITE.path)
384 self.generator.build_siteinfo()
385 source = File(TEST_ROOT.child("test_src.html"))
386 self.site.refresh()
387 self.site.monitor(self.queue)
388 t = Thread(target=self.checker,
389 kwargs={"asserter":self.assert_valid_html})
390 t.start()
391 source.copy_to(self.site.content_folder.child("test.html"))
392 t.join()
393 assert self.exception_queue.empty()
ba5d8db @lakshmivyas Added check for layout files in content and media(files starting with _)
lakshmivyas authored
394
395 def assert_layout_not_rendered(self, actual_html_resource):
396 self.generator.process(actual_html_resource)
397 assert not actual_html_resource.temp_file.exists
398
399 def test_underscored_pages_are_not_rendered(self):
400 self.generator = Generator(TEST_SITE.path)
401 self.generator.build_siteinfo()
402 source = File(TEST_ROOT.child("test_src.html"))
403 self.site.refresh()
404 self.site.monitor(self.queue)
405 t = Thread(target=self.checker,
406 kwargs={"asserter":self.assert_layout_not_rendered})
407 t.start()
408 source.copy_to(self.site.content_folder.child("_test.html"))
409 t.join()
410 assert self.exception_queue.empty()
Something went wrong with that request. Please try again.