Browse Source

Add a new manage command to run unit tests, enable doctests

Gu1 11 years ago
parent
commit
3abbebd315
3 changed files with 41 additions and 2 deletions
  1. 4 0
      ffdnispdb/crawler.py
  2. 19 2
      manage.py
  3. 18 0
      test_ffdnispdb.py

+ 4 - 0
ffdnispdb/crawler.py

@@ -15,6 +15,10 @@ from . import db
 
 
 def get_encoding(content_type):
+    """
+    >>> get_encoding('wat/ever; charset=hey')
+    'hey'
+    """
     content_type, params = cgi.parse_header(content_type)
 
     if 'charset' in params:

+ 19 - 2
manage.py

@@ -7,7 +7,8 @@ import werkzeug.serving
 from werkzeug.debug import DebuggedApplication
 
 import os; os.environ.setdefault('FFDNISPDB_SETTINGS', '../settings_dev.py')
-from flask.ext.script import Shell, Server, Manager
+from flask.ext.script import (Shell, Server, Manager, Command, Option,
+                              prompt, prompt_bool, prompt_pass)
 import ffdnispdb
 
 
@@ -23,7 +24,8 @@ def create():
 @database_manager.command
 def drop():
     "Drop existing tables"
-    ffdnispdb.db.drop_all()
+    if prompt_bool("Are you sure you wish to destroy existing database"):
+        ffdnispdb.db.drop_all()
 
 
 index_manager = Manager(usage=u'Manage the Whoosh index')
@@ -59,11 +61,26 @@ def shell_context():
     return ffdnispdb.__dict__
 
 
+class RunTests(Command):
+    "Run unit-tests"
+    option_list = (
+        Option('--verbose', '-v', action='store_true', default=False),
+        Option('--failfast', '-f', action='store_true', default=False),
+    )
+
+    def run(self, verbose, failfast):
+        import unittest
+        import test_ffdnispdb
+        test=unittest.defaultTestLoader.loadTestsFromModule(test_ffdnispdb)
+        unittest.TextTestRunner(verbosity=2 if verbose else 1, failfast=bool(failfast)).run(test)
+
+
 manager = Manager(ffdnispdb.create_app)
 manager.add_command("runserver", Server())
 manager.add_command("shell", Shell(make_context=shell_context))
 manager.add_command("db", database_manager)
 manager.add_command("index", index_manager)
+manager.add_command("test", RunTests())
 
 if __name__ == "__main__":
     manager.run()

+ 18 - 0
test_ffdnispdb.py

@@ -4,6 +4,7 @@ from ffdnispdb.models import ISP
 from flask import Flask
 from flask.ext.sqlalchemy import SQLAlchemy
 import unittest
+import doctest
 import os
 
 
@@ -31,9 +32,16 @@ class TestCase(unittest.TestCase):
         db.drop_all()
         self._ctx.pop()
 
+    def assertStatus(self, response, status_code):
+        self.assertEqual(response.status_code, status_code)
+
 
 class TestForm(TestCase):
 
+    def test_index(self):
+        self.assertStatus(self.client.get('/'), 200)
+        self.assertStatus(self.client.get('/isp/map_data.json'), 200)
+
     def test_projectform(self):
         resp = self.client.post('/isp/create/form', data={
             'tech_email': 'admin@isp.com',
@@ -47,5 +55,15 @@ class TestForm(TestCase):
         self.assertEqual(ISP.query.filter_by(name='Test').count(), 1)
 
 
+def load_tests(loader, tests, ignore):
+    from ffdnispdb import views, models, utils, forms, crawler, sessions
+    tests.addTests(doctest.DocTestSuite(views))
+    tests.addTests(doctest.DocTestSuite(models))
+    tests.addTests(doctest.DocTestSuite(utils))
+    tests.addTests(doctest.DocTestSuite(forms))
+    tests.addTests(doctest.DocTestSuite(crawler))
+    return tests
+
+
 if __name__ == '__main__':
     unittest.main()