def test_other_html_renderer(self): pages = FlatPages(Flask(__name__)) pages.app.config['FLATPAGES_HTML_RENDERER'] = unicode.upper hello = pages.get('hello') self.assertEquals(hello.body, u'Hello, *世界*!\n') # Markdow self.assertEquals(hello.html, u'HELLO, *世界*!\n')
def test_other_html_renderer(self): for renderer in (unicode.upper, 'string.upper'): pages = FlatPages(Flask(__name__)) pages.app.config['FLATPAGES_HTML_RENDERER'] = renderer hello = pages.get('hello') self.assertEquals(hello.body, u'Hello, *世界*!\n') # Upper-case, markdown not interpreted self.assertEquals(hello.html, u'HELLO, *世界*!\n')
def test_yaml_meta(self): pages = FlatPages(Flask(__name__)) foo = pages.get('foo') self.assertEquals(foo.meta, { 'title': 'Foo > bar', 'created': datetime.date(2010, 12, 11), 'versions': [3.14, 42] }) self.assertEquals(foo['title'], 'Foo > bar') self.assertEquals(foo['created'], datetime.date(2010, 12, 11)) self.assertEquals(foo['versions'], [3.14, 42]) self.assertRaises(KeyError, lambda: foo['nonexistent'])
def test_other_extension(self): app = Flask(__name__) app.config['FLATPAGES_EXTENSION'] = '.txt' pages = FlatPages(app) self.assertEquals( set(page.path for page in pages), set(['not_a_page', 'foo/42/not_a_page']) )
def temp_pages(app=None): """This context manager gives a FlatPages object configured in a temporary directory with a copy of the test pages. Using a temporary copy allows us to safely write and remove stuff without worrying about undoing our changes. """ with temp_directory() as temp: source = os.path.join(os.path.dirname(__file__), 'pages') # Remove the destination dir as copytree wants it not to exist. # Doing so kind of defeats the purpose of tempfile as it introduces # a race condition, but should be good enough for our purpose. os.rmdir(temp) shutil.copytree(source, temp) app = app or Flask(__name__) app.config['FLATPAGES_ROOT'] = temp yield FlatPages(app)
STATIC_DIR = os.path.join(SOURCES_DIR, 'static') TEMPLATES_DIR = os.path.join(SOURCES_DIR, 'templates') ASSETS_DEBUG = DEBUG FLATPAGES_AUTO_RELOAD = True FLATPAGES_EXTENSION = '.md' FLATPAGES_ROOT = os.path.join(SOURCES_DIR, 'pages') FREEZER_DESTINATION = ROOT_DIR FREEZER_REMOVE_EXTRA_FILES = False # to keep git related stuff in there # App configuration FEED_MAX_LINKS = 25 SECTION_MAX_LINKS = 12 app = Flask(__name__, template_folder=TEMPLATES_DIR, static_folder=STATIC_DIR) app.config.from_object(__name__) pages = FlatPages(app) freezer = Freezer(app) markdown_manager = Markdown(app, output_format='html5', extensions=['tables', 'fenced_code', 'codehilite']) asset_manager = AssetManager(app) @app.template_filter() def to_rfc2822(dt): if not dt: return current_locale = locale.getlocale(locale.LC_TIME) locale.setlocale(locale.LC_TIME, "en_US") formatted = dt.strftime("%a, %d %b %Y %H:%M:%S +0000") locale.setlocale(locale.LC_TIME, current_locale)
def test_get_or_404(self): pages = FlatPages(Flask(__name__)) self.assertEquals(pages.get_or_404('foo/bar').path, 'foo/bar') self.assertRaises(NotFound, pages.get_or_404, 'nonexistent')
def test_get(self): pages = FlatPages(Flask(__name__)) self.assertEquals(pages.get('foo/bar').path, 'foo/bar') self.assertEquals(pages.get('nonexistent'), None) self.assertEquals(pages.get('nonexistent', 42), 42)
def test_iter(self): pages = FlatPages(Flask(__name__)) self.assertEquals( set(page.path for page in pages), set(['foo', 'foo/bar', 'foo/lorem/ipsum', 'hello']) )
def test_other_encoding(self): app = Flask(__name__) app.config['FLATPAGES_ENCODING'] = 'shift_jis' app.config['FLATPAGES_ROOT'] = 'pages_shift_jis' pages = FlatPages(app) self._unicode(pages)
def test_unicode(self): pages = FlatPages(Flask(__name__)) self._unicode(pages)
def test_markdown(self): pages = FlatPages(Flask(__name__)) foo = pages.get('foo') self.assertEquals(foo.body, 'Foo *bar*\n') self.assertEquals(foo.html, '<p>Foo <em>bar</em></p>')
def test_consistency(self): pages = FlatPages(Flask(__name__)) for page in pages: assert pages.get(page.path) is page