Parent: [eb3bea] (diff)

Child: [ddf08c] (diff)

Download this file

test_commands.py    187 lines (158 with data), 6.3 kB

  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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from datetime import datetime, timedelta
from pylons import tmpl_context as c, app_globals as g
from datadiff.tools import assert_equal
from IPython.testing.decorators import module_not_available, skipif
from ming.orm.ormsession import ThreadLocalORMSession
from alluratest.controller import setup_basic_test, setup_global_objects
from allura import model as M
from allura.lib import security
from allura.lib import helpers as h
from forgeblog import model as BM
import mock
import feedparser
test_config = 'test.ini#main'
def setUp():
setup_basic_test()
setup_global_objects()
def _mock_feed(*entries):
class attrdict(dict):
def __getattr__(self, name):
return self[name]
feed = mock.Mock()
feed.bozo = False
feed.entries = []
for e in entries:
_mock_feed.i += 1
entry = attrdict(
content_type='text/plain',
title='Default Title %d' % _mock_feed.i,
subtitle='',
summary='',
link='http://example.com/',
updated=datetime.utcnow()+timedelta(days=_mock_feed.i - 100))
entry.update(e)
entry['updated_parsed'] = entry['updated'].timetuple()
if 'content' in entry:
entry['content'] = [attrdict(type=entry['content_type'], value=entry['content'])]
feed.entries.append(entry)
return feed
_mock_feed.i = 0
@skipif(module_not_available('html2text'))
@mock.patch.object(feedparser, 'parse')
def test_pull_rss_feeds(parsefeed):
parsefeed.return_value = _mock_feed(
dict(title='Test', subtitle='test', summary='This is a test'),
dict(content_type='text/plain', content='Test feed'),
dict(content_type='text/html', content=
"<p>1. foo</p>\n"
"\n"
"<p>\n"
"#foo bar <a href='baz'>baz</a>\n"
"foo bar\n"
"</p>\n"
"\n"
"<p>#foo bar <a href='baz'>\n"
"baz\n"
"</a></p>\n"
),
)
base_app = M.AppConfig.query.find().all()[0]
tmp_app = M.AppConfig(tool_name=u'Blog', discussion_id=base_app.discussion_id,
project_id=base_app.project_id,
options={u'ordinal': 0, u'show_right_bar': True,
u'project_name': base_app.project.name,
u'mount_point': u'blog',
u'mount_label': u'Blog'})
new_external_feeds = ['http://example.com/news/feed/']
BM.Globals(app_config_id=tmp_app._id, external_feeds=new_external_feeds)
ThreadLocalORMSession.flush_all()
from forgeblog.command import rssfeeds
cmd = rssfeeds.RssFeedsCommand('pull-rss-feeds')
cmd.run([test_config, '-a', tmp_app._id])
cmd.command()
parsefeed.assert_called_with('http://example.com/news/feed/')
posts = BM.BlogPost.query.find({'app_config_id': tmp_app._id}).sort('timestamp', 1)
assert_equal(posts.count(), 3)
posts = posts.all()
assert_equal(posts[0].title, 'Test')
assert_equal(posts[0].text, 'This is a test [link](http://example.com/)')
assert_equal(posts[1].title, 'Default Title 2')
assert_equal(posts[1].text, 'Test feed [link](http://example.com/)')
assert_equal(posts[2].title, 'Default Title 3')
assert_equal(posts[2].text, "\n".join([
r"1\. foo",
"",
r"\#foo bar [baz](baz) foo bar ",
"",
r"\#foo bar [ baz ](baz)",
" [link](http://example.com/)",
]))
@skipif(module_not_available('html2text'))
def test_plaintext_preprocessor():
from html2text import html2text
text = html2text(
"[plain]1. foo[/plain]\n"
"\n"
"[plain]#foo bar [/plain]<a href='baz'>[plain]baz[/plain]</a>\n"
"[plain]foo bar[/plain]\n"
"\n"
"[plain]#foo bar [/plain]<a href='baz'>\n"
"[plain]baz[/plain]\n"
"</a>\n"
)
html = g.markdown.convert(text)
assert_equal(html,
'<div class="markdown_content"><p>1. foo '
'#foo bar <a class="" href="../baz">baz</a> foo bar '
'#foo bar <a class="" href="../baz"> baz </a></p></div>'
)
@skipif(module_not_available('html2text'))
def test_plaintext_preprocessor_wrapped():
from html2text import html2text
text = html2text(
"<p>[plain]1. foo[/plain]</p>\n"
"\n"
"<p>\n"
"[plain]#foo bar [/plain]<a href='baz'>[plain]baz[/plain]</a>\n"
"[plain]foo bar[/plain]\n"
"</p>\n"
"\n"
"<p>[plain]#foo bar [/plain]<a href='baz'>\n"
"[plain]baz[/plain]\n"
"</a></p>\n"
)
html = g.markdown.convert(text)
assert_equal(html,
'<div class="markdown_content"><p>1. foo</p>\n'
'<p>#foo bar <a class="" href="../baz">baz</a> foo bar </p>\n'
'<p>#foo bar <a class="" href="../baz"> baz </a></p></div>'
)
@skipif(module_not_available('html2text'))
def test_plain2markdown():
text = '''paragraph
4 spaces before this
*blah*
here's a <tag> that should be <b>preserved</b>
Literal &gt; &Ograve; &frac14; &amp; &#38; &#x123F;
M & Ms - doesn't get escaped
http://blah.com/?x=y&a=b - not escaped either
'''
expected = '''paragraph
4 spaces before this
\*blah\*
here's a &lt;tag&gt; that should be &lt;b&gt;preserved&lt;/b&gt;
Literal &amp;gt; &amp;Ograve; &amp;frac14; &amp;amp; &amp;\#38; &amp;\#x123F;
M & Ms - doesn't get escaped
http://blah.com/?x=y&a=b - not escaped either
'''
# note: the \# isn't necessary it could be just # but that's the way
# html2text escapes all #s currently. The extra escaping of \# ends up
# being ok though when rendered
from forgeblog.command import rssfeeds
assert_equal(rssfeeds.plain2markdown(text), expected)
assert_equal(rssfeeds.plain2markdown('a foo bar\n\n code here?', preserve_multiple_spaces=True),
'a foo&nbsp; bar\n\n&nbsp;&nbsp;&nbsp; code here?')
assert_equal(rssfeeds.plain2markdown('\ttab before (stuff)', preserve_multiple_spaces=True),
'&nbsp;&nbsp;&nbsp; tab before \(stuff\)')
assert_equal(rssfeeds.plain2markdown('\ttab before (stuff)', preserve_multiple_spaces=False),
'tab before \(stuff\)')