Permalink
Browse files

Add tests for `Entity`.

  • Loading branch information...
Lee Heung-sub
Lee Heung-sub committed Sep 9, 2010
1 parent 7392e5c commit f1888c36fcd3384daa46a783f6ba2caa8a5bd9fa
Showing with 179 additions and 16 deletions.
  1. +179 −16 tests/__init__.py
View
@@ -1,47 +1,207 @@
import os.path
+import mimetypes
import unittest
from flask import *
-from flaskext.autoindex import AutoIndex
+from flaskext.autoindex import *
+__file__ = __file__.replace(".pyc", ".py")
browse_root = os.path.abspath(os.path.dirname(__file__))
+class RootDirectoryTestCase(unittest.TestCase):
+
+ def setUp(self):
+ self.rootdir = RootDirectory(browse_root)
+
+ def test_root_dir(self):
+ assert isinstance(self.rootdir, RootDirectory)
+ assert self.rootdir.path == "."
+ assert os.path.samefile(self.rootdir.abspath, browse_root)
+ assert os.path.isdir(self.rootdir.abspath)
+
+ def test_init(self):
+ assert isinstance(Directory(browse_root), RootDirectory)
+ assert isinstance(Directory(".", self.rootdir), RootDirectory)
+ assert isinstance(Entry(browse_root), RootDirectory)
+ assert isinstance(Entry(".", self.rootdir), RootDirectory)
+
+ def test_same_object(self):
+ assert self.rootdir is RootDirectory(browse_root)
+ assert self.rootdir is Directory(browse_root)
+ assert self.rootdir is Directory(".", self.rootdir)
+ assert self.rootdir is Entry(browse_root)
+ assert self.rootdir is Entry(".", self.rootdir)
+ assert RootDirectory(browse_root) is Directory(browse_root)
+ assert RootDirectory(browse_root) is Entry(".", self.rootdir)
+ assert Entry(browse_root) is Directory(".", self.rootdir)
+
+ def test_get_child_file(self):
+ file = self.rootdir.get_child("__init__.py")
+ assert isinstance(file, File)
+ assert file.ext == "py"
+ assert file.path == "__init__.py"
+ assert file.rootdir is self.rootdir
+
+ def test_get_child_dir(self):
+ dir = self.rootdir.get_child("static")
+ assert isinstance(dir, Directory)
+ assert dir.path == "static"
+ assert dir.rootdir is self.rootdir
+
+ def test_contain(self):
+ assert "static" in self.rootdir
+ assert "__init__.py" in self.rootdir
+ assert Entry("static", self.rootdir) in self.rootdir
+ assert Entry("__init__.py", self.rootdir) in self.rootdir
+ assert "^_^" not in self.rootdir
+
+
+class DirectoryTestCase(unittest.TestCase):
+
+ def setUp(self):
+ self.rootdir = RootDirectory(browse_root)
+ self.static = Directory("static", self.rootdir)
+
+ def test_dir(self):
+ assert isinstance(self.static, Directory)
+ assert self.static.path == "static"
+ assert os.path.samefile(self.static.abspath,
+ os.path.join(browse_root, "static"))
+ assert os.path.isdir(self.static.abspath)
+
+ def test_init(self):
+ assert isinstance(Directory("static", self.rootdir), Directory)
+ assert isinstance(Entry("static", self.rootdir), Directory)
+
+ def test_same_object(self):
+ assert self.static is Directory("static", self.rootdir)
+ assert self.static is Entry("static", self.rootdir)
+ assert Directory("static", self.rootdir) is \
+ Entry("static", self.rootdir)
+ assert self.static.parent is self.rootdir
+
+ def test_get_child_file(self):
+ file = self.static.get_child("test.txt")
+ assert isinstance(file, File)
+ assert file.ext == "txt"
+ assert file.path == "static/test.txt"
+ assert file.rootdir is self.rootdir
+
+ def test_contain(self):
+ assert "test.py" in self.static
+ assert Entry("static/test.py", self.rootdir) in self.static
+ assert "^_^" not in self.static
+
+
+class FileTestCase(unittest.TestCase):
+
+ def setUp(self):
+ self.rootdir = RootDirectory(browse_root)
+ self.itself = File("__init__.py", self.rootdir)
+
+ def test_file(self):
+ assert isinstance(self.itself, File)
+ assert self.itself.path == "__init__.py"
+ assert os.path.samefile(self.itself.abspath,
+ os.path.join(browse_root, "__init__.py"))
+ assert os.path.isfile(self.itself.abspath)
+
+ def test_init(self):
+ assert isinstance(File("__init__.py", self.rootdir), File)
+ assert isinstance(Entry("__init__.py", self.rootdir), File)
+ assert isinstance(File("static/test.txt", self.rootdir), File)
+ assert isinstance(Entry("static/test.txt", self.rootdir), File)
+
+ def test_same_object(self):
+ assert self.itself is File("__init__.py", self.rootdir)
+ assert self.itself is Entry("__init__.py", self.rootdir)
+ assert File("__init__.py", self.rootdir) is \
+ Entry("__init__.py", self.rootdir)
+
+ def test_properties(self):
+ source = "".join(file(__file__).readlines())
+ assert self.itself.data.strip() == source.strip()
+ assert self.itself.size == len(source)
+ assert self.itself.ext == "py"
+ assert self.itself.mimetype == mimetypes.guess_type(__file__)
+
+
class ApplicationTestCase(unittest.TestCase):
def setUp(self):
- app = Flask(__name__)
- AutoIndex(app, browse_root)
- self.app = app
+ self.app = Flask(__name__)
+ self.app2 = Flask(__name__ + "2")
+ self.idx = AutoIndex(self.app, browse_root, add_url_rules=True)
+ self.idx2 = AutoIndex(self.app2)
+ #self.idx3 = AutoIndex()
+ @self.app2.route("/")
+ @self.app2.route("/<path:path>")
+ def autoindex(path="."):
+ return self.idx2.render_autoindex(path, browse_root)
+ #def create_app(__name__):
+ # app = Flask(__name__)
+ # self.idx3.init_app(app)
+ # return app
+ #self.app3 = create_app(__name__ + "3")
def get(self, path):
return self.app.test_client().get(path)
+ def get2(self, path):
+ return self.app2.test_client().get(path)
+
def test_css(self):
- rv = self.get("/static/autoindex.css")
- assert 200 == rv.status_code, "could not found preloaded css file."
+ for get in self.get, self.get2:
+ rv = get("/static/autoindex.css")
+ assert 200 == rv.status_code
def test_icon(self):
- rv = self.get("/icons/page_white.png")
- assert 294 == len(rv.data), "could not found preloaded icon file."
+ for get in self.get, self.get2:
+ rv = get("/icons/page_white.png")
+ assert 294 == len(rv.data)
- def test_browse(self):
- rv = self.get("/")
- assert "Index of /" in rv.data
- assert "__init__.py" in rv.data
+ def test_autoindex(self):
+ for get in self.get, self.get2:
+ rv = get("/")
+ assert "__init__.py" in rv.data
def test_own_static_file(self):
rv = self.get("/static/helloworld.txt")
assert "Hello, world!" == rv.data.strip()
def test_own_page(self):
- rv = self.get("/test")
- assert not "foo bar foo bar" == rv.data.strip()
+ for get in self.get, self.get2:
+ rv = get("/test")
+ assert not "foo bar foo bar" == rv.data.strip()
@self.app.route("/test")
def sublee():
return "foo bar foo bar", 200
- rv = self.get("/test")
- assert "foo bar foo bar" == rv.data.strip()
+ @self.app2.route("/test")
+ def sublee():
+ return "foo bar foo bar", 200
+ for get in self.get, self.get2:
+ rv = get("/test")
+ assert "foo bar foo bar" == rv.data.strip()
+
+ def test_builtin_icon_rule(self):
+ testset = {"7z": "page_white_zip.png",
+ "avi": "film.png",
+ "cer": "key.png",
+ "html": "page_white_code.png",
+ "iso": "cd.png",
+ "rss": "feed.png"}
+ for ext, icon in testset.iteritems():
+ file = self.idx.rootdir.get_child("static/test." + ext)
+ assert file.guess_icon().endswith(icon)
+
+ def test_custom_icon_rule(self):
+ file = self.idx.rootdir.get_child("__init__.py")
+ original_icon_url = file.guess_icon()
+ self.idx.add_icon_rule("table.png", ext="py")
+ customized_icon_url = file.guess_icon()
+ assert original_icon_url.endswith("page_white_python.png")
+ assert customized_icon_url.endswith("table.png")
class SubdomainTestCase(unittest.TestCase):
@@ -103,6 +263,9 @@ def test_browse(self):
def suite():
suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(RootDirectoryTestCase))
+ suite.addTest(unittest.makeSuite(DirectoryTestCase))
+ suite.addTest(unittest.makeSuite(FileTestCase))
suite.addTest(unittest.makeSuite(ApplicationTestCase))
# These cases will be passed on Flask next generation.
# suite.addTest(unittest.makeSuite(SubdomainTestCase))

0 comments on commit f1888c3

Please sign in to comment.