#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
runtests - Run the project tests

Usage:
  runtests --travis
  runtests (--all | --unit | --integration)
  runtests --help
  runtests --version

Options:
  --repeat-url           Use the same url concurrently.
  -v --version           Show version.
"""

import os
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "recommendation.default_settings")
os.environ.setdefault("FRAPPE_TEST", "1")
import django
django.setup()
from django.core.management import call_command
from docopt import docopt

UNIT_OPTS = [
    "tests/unit/",
    "--with-doctest",
    "recommendation/util.py",
    "recommendation/core.py",
    "recommendation/models.py"
]
INTEGRATION_OPTS = ["tests/integration/"]


def main():
    opts = docopt(__doc__, version="Frappé tests 1.0")
    if opts["--travis"]:
        os.environ.setdefault("REUSE_DB", "1")
        if os.environ["TRAVIS_BRANCH"] == "master":
            opts["--all"] = True
        else:
            opts["--unit"] = True
    sys.argv = sys.argv[:1]
    if opts["--unit"]:
        call_command(*(["test", "--verbosity=2", "--with-id"] + UNIT_OPTS))
    elif opts["--integration"]:
        call_command(*(["test", "--verbosity=2", "--with-id"] + INTEGRATION_OPTS))
    else:
        call_command(*(["test", "--verbosity=2", "--with-id"] + INTEGRATION_OPTS + UNIT_OPTS))


if __name__ == "__main__":
    main()