import os
import unittest
import sys
sys.path = ['..'] + sys.path

import db

db.create()

import web

def input_hack():
    return dict()
web.input = input_hack
web.ctx.environ = dict()

# TODO: the following import triggers an exception for useexisting=True I cannot resolve
import admin

web.ctx.path = ""
admin.session_hook()
web.ctx.orm = db.connect()

import web.webapi

web.webapi.ctx.path = ""
web.webapi.ctx.home = ""
web.webapi.ctx.headers = []

suite = unittest.TestSuite()

def find_tests(value):
    t = ''
    if len(sys.argv) >= 2:
        t = sys.argv[1:]

    if t:
        return value in t or value[:-3] in t
    else:
        return value.startswith('test') and value.endswith('.py')

tests = filter(find_tests, os.listdir(os.path.dirname(__file__)))
for test in tests:
    tm = __import__(test[:-3])
    suite.addTest(tm.suite())

unittest.TextTestRunner(verbosity=2).run(suite)
