<commit_before>#!/usr/bin/env python
#
# Test major functions.
#
# Authors: Julien Cohen-Adad, Benjamin De Leener, Augustin Roux
# Updated: 2014-10-06

# TODO: list functions to test in help (do a search in testing folder)

import os
import getopt
import sys
import time
import commands
# get path of the toolbox
status, path_sct = commands.getstatusoutput('echo $SCT_DIR')
# append path that contains scripts, to be able to load modules
sys.path.append(path_sct + '/scripts')
sys.path.append(path_sct + '/testing')
import sct_utils as sct
from os import listdir
from os.path import isfile, join
import importlib

# define nice colors
class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'

# get path of testing data
status, path_sct_testing = commands.getstatusoutput('echo $SCT_TESTING_DATA_DIR')

class param:
    def __init__(self):
        self.download = 0
        self.path_data = sct.slash_at_the_end(path_sct_testing, 1)
        self.function_to_test = None
        # self.function_to_avoid = None
        self.remove_tmp_file = 0
        self.verbose = 1
        self.url_git = 'https://github.com/neuropoly/sct_testing_data.git'
        self.path_tmp = ""

# START MAIN
# ==========================================================================================
def main():
    path_data = param.path_data
    function_to_test = param.function_to_test
    # function_to_avoid = param.function_to_avoid
    remove_tmp_file = param.remove_tmp_file

    # Check input parameters
    try:
        opts, args = getopt.getopt(sys.argv[1:],'h:d:p:f:r:a:')
    except getopt.GetoptError:
        usage()
    for opt, arg in opts:
        if opt == '-h':
            usage()
            sys.exit(0)
        if opt == '-d':
            param.download = int(arg)
        if opt == '-p':
            param.path_data = arg
        if opt == '-f':
            function_to_test = arg
        # if opt == '-a':
        #     function_to_avoid = arg
        if opt == '-r':
            remove_tmp_file = int(arg)

    start_time = time.time()

    # if function_to_avoid:
    #     try:
    #         functions.remove(function_to_avoid)
    #     except ValueError:
    #         print 'The function you want to avoid does not figure in the functions to test list'

    # download data
    if param.download:
        downloaddata()

    param.path_data = 'sct_testing_data/data'
    # get absolute path and add slash at the end
    param.path_data = sct.slash_at_the_end(os.path.abspath(param.path_data), 1)

    # check existence of testing data folder
    if not sct.return_folder_exist(param.path_data):
        downloaddata()

    # display path to data
    sct.printv('\nCheck path to testing data: \n\tOK... '+param.path_data, param.verbose)

    # create temp folder that will have all results and go in it
    param.path_tmp = sct.slash_at_the_end('tmp.'+time.strftime("%y%m%d%H%M%S"), 1)
    sct.create_folder(param.path_tmp)
    os.chdir(param.path_tmp)

    # get list of all scripts to test
    functions = fill_functions()

    # loop across all functions and test them
    status = []
    [status.append(test_function(f)) for f in functions if function_to_test == f]
    if not status:
        for f in functions:
            status.append(test_function(f))
    print 'status: '+str(status)

    # display elapsed time
    elapsed_time = time.time() - start_time
    print 'Finished! Elapsed time: '+str(int(round(elapsed_time)))+'s\n'

    # remove temp files
    if param.remove_tmp_file:
        sct.printv('\nRemove temporary files...', param.verbose)
        sct.run('rm -rf '+param.path_tmp, param.verbose)

    e = 0
    if sum(status) != 0:
        e = 1
    print e

    sys.exit(e)

def downloaddata():
    sct.printv('\nDownloading testing data...', param.verbose)
    # remove data folder if exist
    if os.path.exists('sct_testing_data'):
        sct.printv('WARNING: sct_testing_data already exists. Removing it...', param.verbose, 'warning')
        sct.run('rm -rf sct_testing_data')
    # clone git repos
    sct.run('git clone '+param.url_git)
    # update path_data field

# list of all functions to test
# ==========================================================================================
def fill_functions():
    functions = []
    #functions.append('test_debug')  --> removed by jcohenadad. No more use for it now.
    functions.append('sct_apply_transfo')
    functions.append('sct_check_atlas_integrity')
    functions.append('sct_compute_mtr')
    functions.append('sct_concat_transfo')
    #functions.append('sct_convert')
    #functions.append('sct_convert_binary_to_trilinear')  # not useful
    functions.append('sct_create_mask')
    functions.append('sct_crop_image')
    functions.append('sct_dmri_get_bvalue')
    functions.append('sct_dmri_transpose_bvecs')
    functions.append('sct_dmri_moco')
    functions.append('sct_dmri_separate_b0_and_dwi')
    functions.append('sct_extract_metric')
    functions.append('sct_flatten_sagittal')
    functions.append('sct_fmri_compute_tsnr')
    functions.append('sct_fmri_moco')
    functions.append('sct_get_centerline_automatic')
    functions.append('sct_get_centerline_from_labels')
    functions.append('sct_label_utils')
    functions.append('sct_orientation')
    functions.append('sct_process_segmentation')
    functions.append('sct_propseg')
    functions.append('sct_register_multimodal')
    functions.append('sct_register_to_template')
    functions.append('sct_resample')
    functions.append('sct_smooth_spinalcord')
    functions.append('sct_straighten_spinalcord')
    functions.append('sct_warp_template')
    return functions

# print without carriage return
# ==========================================================================================
def print_line(string):
    import sys
    sys.stdout.write(string + make_dot_lines(string))
    sys.stdout.flush()

# fill line with dots
# ==========================================================================================
def make_dot_lines(string):
    if len(string) < 52:
        dot_lines = '.'*(52 - len(string))
        return dot_lines
    else: return ''

# print in color
# ==========================================================================================
def print_ok():
    print "[" + bcolors.OKGREEN + "OK" + bcolors.ENDC + "]"

def print_warning():
    print "[" + bcolors.WARNING + "WARNING" + bcolors.ENDC + "]"

def print_fail():
    print "[" + bcolors.FAIL + "FAIL" + bcolors.ENDC + "]"

# write to log file
# ==========================================================================================
def write_to_log_file(fname_log, string, mode='w'):

    '''
    status, output = sct.run('echo $SCT_DIR', 0)
    path_logs_dir = output + '/testing/logs'

    if not os.path.isdir(path_logs_dir):
        os.makedirs(path_logs_dir)
    '''

    string = "test ran at "+time.strftime("%y%m%d%H%M%S")+"\n" \
             + fname_log \
             + string

    f = open('../' + fname_log, mode)
    f.write(string+'\n')
    f.close()

# test function
# ==========================================================================================
def test_function(script_name):
    if script_name == 'test_debug':
        return test_debug()  # JULIEN
    else:
        # build script name
        fname_log = script_name + ".log"
        tmp_script_name = script_name
        result_folder = "results_"+script_name
        script_name = "test_"+script_name

        sct.create_folder(result_folder)
        os.chdir(result_folder)

        # display script name
        print_line('Checking '+script_name)
        # import function as a module
        script_tested = importlib.import_module(script_name)
        # test function
        status, output = script_tested.test(param.path_data)
        # manage status
        if status == 0:
            print_ok()
        else:
            if status == 5:
                print_warning()
            else:
                print_fail()
            print output
            # log file
            write_to_log_file(fname_log, output, 'w')

            # go back to parent folder
            os.chdir('..')

        # return
        return status

# def old_test_function(folder_test):
#     fname_log = folder_test + ".log"
#     print_line('Checking '+folder_test)
#     os.chdir(folder_test)
#     status, output = commands.getstatusoutput('./test_'+folder_test+'.sh')
#     if status == 0:
#         print_ok()
#     else:
#         print_fail()
#     shutil.rmtree('./results')
#     os.chdir('../')
#     write_to_log_file(fname_log,output)
#     return status

def test_debug():
    print_line ('Checking if debug mode is on .......................')
    path_sct_testing = path_sct + '/testing'
    path_sct_scripts = path_sct + '/scripts'
    debug = []
    files = [f for f in listdir(path_sct_scripts) if isfile(join(path_sct_scripts, f))]
    #print(files)
    for file in files:
        file_fname, ext_fname = os.path.splitext(file)
        if ext_fname == '.py':
            cmd = 'python ' + path_sct_testing + '/test_debug_off.py -i ' + file_fname
            status, output = commands.getstatusoutput(cmd)
            if status != 0:
                print cmd
                debug.append(output)
    if debug == []:
        print_ok()
        return 0  # JULIEN
    else:
        print_fail()
        for string in debug: print string
        return 1  # JULIEN

# Print usage
# ==========================================================================================
def usage():
    print """
"""+os.path.basename(__file__)+"""
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Part of the Spinal Cord Toolbox <https://sourceforge.net/projects/spinalcordtoolbox>

DESCRIPTION
  Crash test for functions of the Spinal Cord Toolbox.

USAGE
  python """+os.path.basename(__file__)+"""

OPTIONAL ARGUMENTS
  -f <script_name>      test this specific script (do not add extension).
  -d {0,1}              download testing data. Default="""+str(param.download)+"""
  -p <path_data>        path to testing data. Default="""+str(param.path_data)+"""
                        NB: no need to set if using "-d 1"
  -r {0,1}              remove temp files. Default="""+str(param.remove_tmp_file)+"""
  -h                    help. Show this message

EXAMPLE
  python """+os.path.basename(__file__)+""" \n"""

    # exit program
    sys.exit(2)

if __name__ == "__main__":
    # initialize parameters
    param = param()
    # call main function
    main()<commit_msg>TEST: minor display changes<commit_after>#!/usr/bin/env python
#
# Test major functions.
#
# Authors: Julien Cohen-Adad, Benjamin De Leener, Augustin Roux
# Updated: 2014-10-06

# TODO: list functions to test in help (do a search in testing folder)

import os
import getopt
import sys
import time
import commands
# get path of the toolbox
status, path_sct = commands.getstatusoutput('echo $SCT_DIR')
# append path that contains scripts, to be able to load modules
sys.path.append(path_sct + '/scripts')
sys.path.append(path_sct + '/testing')
import sct_utils as sct
from os import listdir
from os.path import isfile, join
import importlib

# define nice colors
class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'

# get path of testing data
status, path_sct_testing = commands.getstatusoutput('echo $SCT_TESTING_DATA_DIR')

class param:
    def __init__(self):
        self.download = 0
        self.path_data = sct.slash_at_the_end(path_sct_testing, 1)
        self.function_to_test = None
        # self.function_to_avoid = None
        self.remove_tmp_file = 0
        self.verbose = 1
        self.url_git = 'https://github.com/neuropoly/sct_testing_data.git'
        self.path_tmp = ""

# START MAIN
# ==========================================================================================
def main():
    path_data = param.path_data
    function_to_test = param.function_to_test
    # function_to_avoid = param.function_to_avoid
    remove_tmp_file = param.remove_tmp_file

    # Check input parameters
    try:
        opts, args = getopt.getopt(sys.argv[1:],'h:d:p:f:r:a:')
    except getopt.GetoptError:
        usage()
    for opt, arg in opts:
        if opt == '-h':
            usage()
            sys.exit(0)
        if opt == '-d':
            param.download = int(arg)
        if opt == '-p':
            param.path_data = arg
        if opt == '-f':
            function_to_test = arg
        # if opt == '-a':
        #     function_to_avoid = arg
        if opt == '-r':
            remove_tmp_file = int(arg)

    start_time = time.time()

    # if function_to_avoid:
    #     try:
    #         functions.remove(function_to_avoid)
    #     except ValueError:
    #         print 'The function you want to avoid does not figure in the functions to test list'

    # download data
    if param.download:
        downloaddata()

    param.path_data = 'sct_testing_data/data'
    # get absolute path and add slash at the end
    param.path_data = sct.slash_at_the_end(os.path.abspath(param.path_data), 1)

    # check existence of testing data folder
    if not sct.return_folder_exist(param.path_data):
        downloaddata()

    # display path to data
    sct.printv('\nPath to testing data:\n.. '+param.path_data, param.verbose)

    # create temp folder that will have all results and go in it
    param.path_tmp = sct.slash_at_the_end('tmp.'+time.strftime("%y%m%d%H%M%S"), 1)
    sct.create_folder(param.path_tmp)
    os.chdir(param.path_tmp)

    # get list of all scripts to test
    functions = fill_functions()

    # loop across all functions and test them
    status = []
    [status.append(test_function(f)) for f in functions if function_to_test == f]
    if not status:
        for f in functions:
            status.append(test_function(f))
    print 'status: '+str(status)

    # display elapsed time
    elapsed_time = time.time() - start_time
    print 'Finished! Elapsed time: '+str(int(round(elapsed_time)))+'s\n'

    # remove temp files
    if param.remove_tmp_file:
        sct.printv('\nRemove temporary files...', param.verbose)
        sct.run('rm -rf '+param.path_tmp, param.verbose)

    e = 0
    if sum(status) != 0:
        e = 1
    print e

    sys.exit(e)

def downloaddata():
    sct.printv('\nDownloading testing data...', param.verbose)
    # remove data folder if exist
    if os.path.exists('sct_testing_data'):
        sct.printv('WARNING: sct_testing_data already exists. Removing it...', param.verbose, 'warning')
        sct.run('rm -rf sct_testing_data')
    # clone git repos
    sct.run('git clone '+param.url_git)
    # update path_data field

# list of all functions to test
# ==========================================================================================
def fill_functions():
    functions = []
    #functions.append('test_debug')  --> removed by jcohenadad. No more use for it now.
    functions.append('sct_apply_transfo')
    functions.append('sct_check_atlas_integrity')
    functions.append('sct_compute_mtr')
    functions.append('sct_concat_transfo')
    #functions.append('sct_convert')
    #functions.append('sct_convert_binary_to_trilinear')  # not useful
    functions.append('sct_create_mask')
    functions.append('sct_crop_image')
    functions.append('sct_dmri_get_bvalue')
    functions.append('sct_dmri_transpose_bvecs')
    functions.append('sct_dmri_moco')
    functions.append('sct_dmri_separate_b0_and_dwi')
    functions.append('sct_extract_metric')
    functions.append('sct_flatten_sagittal')
    functions.append('sct_fmri_compute_tsnr')
    functions.append('sct_fmri_moco')
    functions.append('sct_get_centerline_automatic')
    functions.append('sct_get_centerline_from_labels')
    functions.append('sct_label_utils')
    functions.append('sct_orientation')
    functions.append('sct_process_segmentation')
    functions.append('sct_propseg')
    functions.append('sct_register_multimodal')
    functions.append('sct_register_to_template')
    functions.append('sct_resample')
    functions.append('sct_smooth_spinalcord')
    functions.append('sct_straighten_spinalcord')
    functions.append('sct_warp_template')
    return functions

# print without carriage return
# ==========================================================================================
def print_line(string):
    import sys
    sys.stdout.write(string + make_dot_lines(string))
    sys.stdout.flush()

# fill line with dots
# ==========================================================================================
def make_dot_lines(string):
    if len(string) < 52:
        dot_lines = '.'*(52 - len(string))
        return dot_lines
    else: return ''

# print in color
# ==========================================================================================
def print_ok():
    print "[" + bcolors.OKGREEN + "OK" + bcolors.ENDC + "]"

def print_warning():
    print "[" + bcolors.WARNING + "WARNING" + bcolors.ENDC + "]"

def print_fail():
    print "[" + bcolors.FAIL + "FAIL" + bcolors.ENDC + "]"

# write to log file
# ==========================================================================================
def write_to_log_file(fname_log, string, mode='w'):

    '''
    status, output = sct.run('echo $SCT_DIR', 0)
    path_logs_dir = output + '/testing/logs'

    if not os.path.isdir(path_logs_dir):
        os.makedirs(path_logs_dir)
    '''

    string = "test ran at "+time.strftime("%y%m%d%H%M%S")+"\n" \
             + fname_log \
             + string

    f = open('../' + fname_log, mode)
    f.write(string+'\n')
    f.close()

# test function
# ==========================================================================================
def test_function(script_name):
    if script_name == 'test_debug':
        return test_debug()  # JULIEN
    else:
        # build script name
        fname_log = script_name + ".log"
        tmp_script_name = script_name
        result_folder = "results_"+script_name
        script_name = "test_"+script_name

        sct.create_folder(result_folder)
        os.chdir(result_folder)

        # display script name
        print_line('Checking '+script_name)
        # import function as a module
        script_tested = importlib.import_module(script_name)
        # test function
        status, output = script_tested.test(param.path_data)
        # manage status
        if status == 0:
            print_ok()
        else:
            if status == 5:
                print_warning()
            else:
                print_fail()
            print output
            # log file
            write_to_log_file(fname_log, output, 'w')

            # go back to parent folder
            os.chdir('..')

        # return
        return status

# def old_test_function(folder_test):
#     fname_log = folder_test + ".log"
#     print_line('Checking '+folder_test)
#     os.chdir(folder_test)
#     status, output = commands.getstatusoutput('./test_'+folder_test+'.sh')
#     if status == 0:
#         print_ok()
#     else:
#         print_fail()
#     shutil.rmtree('./results')
#     os.chdir('../')
#     write_to_log_file(fname_log,output)
#     return status

def test_debug():
    print_line ('Checking if debug mode is on .......................')
    path_sct_testing = path_sct + '/testing'
    path_sct_scripts = path_sct + '/scripts'
    debug = []
    files = [f for f in listdir(path_sct_scripts) if isfile(join(path_sct_scripts, f))]
    #print(files)
    for file in files:
        file_fname, ext_fname = os.path.splitext(file)
        if ext_fname == '.py':
            cmd = 'python ' + path_sct_testing + '/test_debug_off.py -i ' + file_fname
            status, output = commands.getstatusoutput(cmd)
            if status != 0:
                print cmd
                debug.append(output)
    if debug == []:
        print_ok()
        return 0  # JULIEN
    else:
        print_fail()
        for string in debug: print string
        return 1  # JULIEN

# Print usage
# ==========================================================================================
def usage():
    print """
"""+os.path.basename(__file__)+"""
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Part of the Spinal Cord Toolbox <https://sourceforge.net/projects/spinalcordtoolbox>

DESCRIPTION
  Crash test for functions of the Spinal Cord Toolbox.

USAGE
  python """+os.path.basename(__file__)+"""

OPTIONAL ARGUMENTS
  -f <script_name>      test this specific script (do not add extension).
  -d {0,1}              download testing data. Default="""+str(param.download)+"""
  -p <path_data>        path to testing data. Default="""+str(param.path_data)+"""
                        NB: no need to set if using "-d 1"
  -r {0,1}              remove temp files. Default="""+str(param.remove_tmp_file)+"""
  -h                    help. Show this message

EXAMPLE
  python """+os.path.basename(__file__)+""" \n"""

    # exit program
    sys.exit(2)

if __name__ == "__main__":
    # initialize parameters
    param = param()
    # call main function
    main()<|endoftext|>


<commit_before>from __future__ import unicode_literals

import os
import sys
from unittest import skipUnless
import warnings

from django.apps import apps, AppConfig
from django.apps.registry import Apps
from django.contrib.admin.models import LogEntry
from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.test import TestCase, override_settings
from django.test.utils import extend_sys_path
from django.utils._os import upath
from django.utils import six

from .default_config_app.apps import CustomConfig
from .models import TotallyNormal, SoAlternative, new_apps

# Small list with a variety of cases for tests that iterate on installed apps.
# Intentionally not in alphabetical order to check if the order is preserved.

SOME_INSTALLED_APPS = [
    'apps.apps.MyAdmin',
    'apps.apps.MyAuth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

SOME_INSTALLED_APPS_NAMES = [
    'django.contrib.admin',
    'django.contrib.auth',
] + SOME_INSTALLED_APPS[2:]

HERE = os.path.dirname(__file__)

class AppsTests(TestCase):

    def test_singleton_master(self):
        """
        Ensures that only one master registry can exist.
        """
        with self.assertRaises(RuntimeError):
            Apps(installed_apps=None)

    def test_ready(self):
        """
        Tests the ready property of the master registry.
        """
        # The master app registry is always ready when the tests run.
        self.assertTrue(apps.ready)
        # Non-master app registries are populated in __init__.
        self.assertTrue(Apps().ready)

    def test_bad_app_config(self):
        """
        Tests when INSTALLED_APPS contains an incorrect app config.
        """
        with self.assertRaises(ImproperlyConfigured):
            with self.settings(INSTALLED_APPS=['apps.apps.BadConfig']):
                pass

    def test_not_an_app_config(self):
        """
        Tests when INSTALLED_APPS contains a class that isn't an app config.
        """
        with self.assertRaises(ImproperlyConfigured):
            with self.settings(INSTALLED_APPS=['apps.apps.NotAConfig']):
                pass

    def test_no_such_app(self):
        """
        Tests when INSTALLED_APPS contains an app that doesn't exist, either
        directly or via an app config.
        """
        with self.assertRaises(ImportError):
            with self.settings(INSTALLED_APPS=['there is no such app']):
                pass
        with self.assertRaises(ImportError):
            with self.settings(INSTALLED_APPS=['apps.apps.NoSuchApp']):
                pass

    def test_no_such_app_config(self):
        """
        Tests when INSTALLED_APPS contains an entry that doesn't exist.
        """
        with self.assertRaises(ImportError):
            with self.settings(INSTALLED_APPS=['apps.apps.NoSuchConfig']):
                pass

    def test_default_app_config(self):
        with self.settings(INSTALLED_APPS=['apps.default_config_app']):
            config = apps.get_app_config('default_config_app')
        self.assertIsInstance(config, CustomConfig)

    @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
    def test_get_app_configs(self):
        """
        Tests apps.get_app_configs().
        """
        app_configs = apps.get_app_configs()
        self.assertListEqual(
            [app_config.name for app_config in app_configs],
            SOME_INSTALLED_APPS_NAMES)

    @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
    def test_get_app_config(self):
        """
        Tests apps.get_app_config().
        """
        app_config = apps.get_app_config('admin')
        self.assertEqual(app_config.name, 'django.contrib.admin')

        app_config = apps.get_app_config('staticfiles')
        self.assertEqual(app_config.name, 'django.contrib.staticfiles')

        with self.assertRaises(LookupError):
            apps.get_app_config('webdesign')

    @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
    def test_is_installed(self):
        """
        Tests apps.is_installed().
        """
        self.assertTrue(apps.is_installed('django.contrib.admin'))
        self.assertTrue(apps.is_installed('django.contrib.auth'))
        self.assertTrue(apps.is_installed('django.contrib.staticfiles'))
        self.assertFalse(apps.is_installed('django.contrib.webdesign'))

    @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
    def test_get_model(self):
        """
        Tests apps.get_model().
        """
        self.assertEqual(apps.get_model('admin', 'LogEntry'), LogEntry)
        with self.assertRaises(LookupError):
            apps.get_model('admin', 'LogExit')

        # App label is case-sensitive, Model name is case-insensitive.
        self.assertEqual(apps.get_model('admin', 'loGentrY'), LogEntry)
        with self.assertRaises(LookupError):
            apps.get_model('Admin', 'LogEntry')

        # A single argument is accepted.
        self.assertEqual(apps.get_model('admin.LogEntry'), LogEntry)
        with self.assertRaises(LookupError):
            apps.get_model('admin.LogExit')
        with self.assertRaises(ValueError):
            apps.get_model('admin_LogEntry')

    @override_settings(INSTALLED_APPS=['apps.apps.RelabeledAppsConfig'])
    def test_relabeling(self):
        self.assertEqual(apps.get_app_config('relabeled').name, 'apps')

    def test_duplicate_labels(self):
        with six.assertRaisesRegex(self, ImproperlyConfigured, "Application labels aren't unique"):
            with self.settings(INSTALLED_APPS=['apps.apps.PlainAppsConfig', 'apps']):
                pass

    def test_duplicate_names(self):
        with six.assertRaisesRegex(self, ImproperlyConfigured, "Application names aren't unique"):
            with self.settings(INSTALLED_APPS=['apps.apps.RelabeledAppsConfig', 'apps']):
                pass

    def test_import_exception_is_not_masked(self):
        """
        App discovery should preserve stack traces. Regression test for #22920.
        """
        with six.assertRaisesRegex(self, ImportError, "Oops"):
            with self.settings(INSTALLED_APPS=['apps.failing_app']):
                pass

    def test_models_py(self):
        """
        Tests that the models in the models.py file were loaded correctly.
        """
        self.assertEqual(apps.get_model("apps", "TotallyNormal"), TotallyNormal)
        with self.assertRaises(LookupError):
            apps.get_model("apps", "SoAlternative")

        with self.assertRaises(LookupError):
            new_apps.get_model("apps", "TotallyNormal")
        self.assertEqual(new_apps.get_model("apps", "SoAlternative"), SoAlternative)

    def test_dynamic_load(self):
        """
        Makes a new model at runtime and ensures it goes into the right place.
        """
        old_models = list(apps.get_app_config("apps").get_models())
        # Construct a new model in a new app registry
        body = {}
        new_apps = Apps(["apps"])
        meta_contents = {
            'app_label': "apps",
            'apps': new_apps,
        }
        meta = type(str("Meta"), tuple(), meta_contents)
        body['Meta'] = meta
        body['__module__'] = TotallyNormal.__module__
        temp_model = type(str("SouthPonies"), (models.Model,), body)
        # Make sure it appeared in the right place!
        self.assertListEqual(list(apps.get_app_config("apps").get_models()), old_models)
        with self.assertRaises(LookupError):
            apps.get_model("apps", "SouthPonies")
        self.assertEqual(new_apps.get_model("apps", "SouthPonies"), temp_model)

    def test_model_clash(self):
        """
        Test for behavior when two models clash in the app registry.
        """
        new_apps = Apps(["apps"])
        meta_contents = {
            'app_label': "apps",
            'apps': new_apps,
        }

        body = {}
        body['Meta'] = type(str("Meta"), tuple(), meta_contents)
        body['__module__'] = TotallyNormal.__module__
        type(str("SouthPonies"), (models.Model,), body)

        # When __name__ and __module__ match we assume the module
        # was reloaded and issue a warning. This use-case is
        # useful for REPL. Refs #23621.
        body = {}
        body['Meta'] = type(str("Meta"), tuple(), meta_contents)
        body['__module__'] = TotallyNormal.__module__
        with warnings.catch_warnings(record=True) as w:
            type(str("SouthPonies"), (models.Model,), body)
            self.assertEqual(len(w), 1)
            self.assertTrue(issubclass(w[-1].category, RuntimeWarning))
            self.assertEqual(str(w[-1].message),
                 "Model 'southponies.apps' was already registered. "
                 "Reloading models is not advised as it can lead to inconsistencies, "
                 "most notably with related models.")

        # If it doesn't appear to be a reloaded module then we expect
        # a RuntimeError.
        body = {}
        body['Meta'] = type(str("Meta"), tuple(), meta_contents)
        body['__module__'] = TotallyNormal.__module__ + '.whatever'
        with six.assertRaisesRegex(self, RuntimeError,
                "Conflicting 'southponies' models in application 'apps':.*"):
            type(str("SouthPonies"), (models.Model,), body)

class Stub(object):
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

class AppConfigTests(TestCase):
    """Unit tests for AppConfig class."""
    def test_path_set_explicitly(self):
        """If subclass sets path as class attr, no module attributes needed."""
        class MyAppConfig(AppConfig):
            path = 'foo'

        ac = MyAppConfig('label', Stub())

        self.assertEqual(ac.path, 'foo')

    def test_explicit_path_overrides(self):
        """If path set as class attr, overrides __path__ and __file__."""
        class MyAppConfig(AppConfig):
            path = 'foo'

        ac = MyAppConfig('label', Stub(__path__=['a'], __file__='b/__init__.py'))

        self.assertEqual(ac.path, 'foo')

    def test_dunder_path(self):
        """If single element in __path__, use it (in preference to __file__)."""
        ac = AppConfig('label', Stub(__path__=['a'], __file__='b/__init__.py'))

        self.assertEqual(ac.path, 'a')

    def test_no_dunder_path_fallback_to_dunder_file(self):
        """If there is no __path__ attr, use __file__."""
        ac = AppConfig('label', Stub(__file__='b/__init__.py'))

        self.assertEqual(ac.path, 'b')

    def test_empty_dunder_path_fallback_to_dunder_file(self):
        """If the __path__ attr is empty, use __file__ if set."""
        ac = AppConfig('label', Stub(__path__=[], __file__='b/__init__.py'))

        self.assertEqual(ac.path, 'b')

    def test_multiple_dunder_path_fallback_to_dunder_file(self):
        """If the __path__ attr is length>1, use __file__ if set."""
        ac = AppConfig('label', Stub(__path__=['a', 'b'], __file__='c/__init__.py'))

        self.assertEqual(ac.path, 'c')

    def test_no_dunder_path_or_dunder_file(self):
        """If there is no __path__ or __file__, raise ImproperlyConfigured."""
        with self.assertRaises(ImproperlyConfigured):
            AppConfig('label', Stub())

    def test_empty_dunder_path_no_dunder_file(self):
        """If the __path__ attr is empty and there is no __file__, raise."""
        with self.assertRaises(ImproperlyConfigured):
            AppConfig('label', Stub(__path__=[]))

    def test_multiple_dunder_path_no_dunder_file(self):
        """If the __path__ attr is length>1 and there is no __file__, raise."""
        with self.assertRaises(ImproperlyConfigured):
            AppConfig('label', Stub(__path__=['a', 'b']))

@skipUnless(
    sys.version_info > (3, 3, 0),
    "Namespace packages sans __init__.py were added in Python 3.3")
class NamespacePackageAppTests(TestCase):
    # We need nsapp to be top-level so our multiple-paths tests can add another
    # location for it (if its inside a normal package with an __init__.py that
    # isn't possible). In order to avoid cluttering the already-full tests/ dir
    # (which is on sys.path), we add these new entries to sys.path temporarily.
    base_location = os.path.join(HERE, 'namespace_package_base')
    other_location = os.path.join(HERE, 'namespace_package_other_base')
    app_path = os.path.join(base_location, 'nsapp')

    def test_single_path(self):
        """
        A Py3.3+ namespace package can be an app if it has only one path.
        """
        with extend_sys_path(self.base_location):
            with self.settings(INSTALLED_APPS=['nsapp']):
                app_config = apps.get_app_config('nsapp')
                self.assertEqual(app_config.path, upath(self.app_path))

    def test_multiple_paths(self):
        """
        A Py3.3+ namespace package with multiple locations cannot be an app.

        (Because then we wouldn't know where to load its templates, static
        assets, etc from.)

        """
        # Temporarily add two directories to sys.path that both contain
        # components of the "nsapp" package.
        with extend_sys_path(self.base_location, self.other_location):
            with self.assertRaises(ImproperlyConfigured):
                with self.settings(INSTALLED_APPS=['nsapp']):
                    pass

    def test_multiple_paths_explicit_path(self):
        """
        Multiple locations are ok only if app-config has explicit path.
        """
        # Temporarily add two directories to sys.path that both contain
        # components of the "nsapp" package.
        with extend_sys_path(self.base_location, self.other_location):
            with self.settings(INSTALLED_APPS=['nsapp.apps.NSAppConfig']):
                app_config = apps.get_app_config('nsapp')
                self.assertEqual(app_config.path, upath(self.app_path))
<commit_msg>[1.7.x] Fixed flake8 warning.<commit_after>from __future__ import unicode_literals

import os
import sys
from unittest import skipUnless
import warnings

from django.apps import apps, AppConfig
from django.apps.registry import Apps
from django.contrib.admin.models import LogEntry
from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.test import TestCase, override_settings
from django.test.utils import extend_sys_path
from django.utils._os import upath
from django.utils import six

from .default_config_app.apps import CustomConfig
from .models import TotallyNormal, SoAlternative, new_apps

# Small list with a variety of cases for tests that iterate on installed apps.
# Intentionally not in alphabetical order to check if the order is preserved.

SOME_INSTALLED_APPS = [
    'apps.apps.MyAdmin',
    'apps.apps.MyAuth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

SOME_INSTALLED_APPS_NAMES = [
    'django.contrib.admin',
    'django.contrib.auth',
] + SOME_INSTALLED_APPS[2:]

HERE = os.path.dirname(__file__)

class AppsTests(TestCase):

    def test_singleton_master(self):
        """
        Ensures that only one master registry can exist.
        """
        with self.assertRaises(RuntimeError):
            Apps(installed_apps=None)

    def test_ready(self):
        """
        Tests the ready property of the master registry.
        """
        # The master app registry is always ready when the tests run.
        self.assertTrue(apps.ready)
        # Non-master app registries are populated in __init__.
        self.assertTrue(Apps().ready)

    def test_bad_app_config(self):
        """
        Tests when INSTALLED_APPS contains an incorrect app config.
        """
        with self.assertRaises(ImproperlyConfigured):
            with self.settings(INSTALLED_APPS=['apps.apps.BadConfig']):
                pass

    def test_not_an_app_config(self):
        """
        Tests when INSTALLED_APPS contains a class that isn't an app config.
        """
        with self.assertRaises(ImproperlyConfigured):
            with self.settings(INSTALLED_APPS=['apps.apps.NotAConfig']):
                pass

    def test_no_such_app(self):
        """
        Tests when INSTALLED_APPS contains an app that doesn't exist, either
        directly or via an app config.
        """
        with self.assertRaises(ImportError):
            with self.settings(INSTALLED_APPS=['there is no such app']):
                pass
        with self.assertRaises(ImportError):
            with self.settings(INSTALLED_APPS=['apps.apps.NoSuchApp']):
                pass

    def test_no_such_app_config(self):
        """
        Tests when INSTALLED_APPS contains an entry that doesn't exist.
        """
        with self.assertRaises(ImportError):
            with self.settings(INSTALLED_APPS=['apps.apps.NoSuchConfig']):
                pass

    def test_default_app_config(self):
        with self.settings(INSTALLED_APPS=['apps.default_config_app']):
            config = apps.get_app_config('default_config_app')
        self.assertIsInstance(config, CustomConfig)

    @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
    def test_get_app_configs(self):
        """
        Tests apps.get_app_configs().
        """
        app_configs = apps.get_app_configs()
        self.assertListEqual(
            [app_config.name for app_config in app_configs],
            SOME_INSTALLED_APPS_NAMES)

    @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
    def test_get_app_config(self):
        """
        Tests apps.get_app_config().
        """
        app_config = apps.get_app_config('admin')
        self.assertEqual(app_config.name, 'django.contrib.admin')

        app_config = apps.get_app_config('staticfiles')
        self.assertEqual(app_config.name, 'django.contrib.staticfiles')

        with self.assertRaises(LookupError):
            apps.get_app_config('webdesign')

    @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
    def test_is_installed(self):
        """
        Tests apps.is_installed().
        """
        self.assertTrue(apps.is_installed('django.contrib.admin'))
        self.assertTrue(apps.is_installed('django.contrib.auth'))
        self.assertTrue(apps.is_installed('django.contrib.staticfiles'))
        self.assertFalse(apps.is_installed('django.contrib.webdesign'))

    @override_settings(INSTALLED_APPS=SOME_INSTALLED_APPS)
    def test_get_model(self):
        """
        Tests apps.get_model().
        """
        self.assertEqual(apps.get_model('admin', 'LogEntry'), LogEntry)
        with self.assertRaises(LookupError):
            apps.get_model('admin', 'LogExit')

        # App label is case-sensitive, Model name is case-insensitive.
        self.assertEqual(apps.get_model('admin', 'loGentrY'), LogEntry)
        with self.assertRaises(LookupError):
            apps.get_model('Admin', 'LogEntry')

        # A single argument is accepted.
        self.assertEqual(apps.get_model('admin.LogEntry'), LogEntry)
        with self.assertRaises(LookupError):
            apps.get_model('admin.LogExit')
        with self.assertRaises(ValueError):
            apps.get_model('admin_LogEntry')

    @override_settings(INSTALLED_APPS=['apps.apps.RelabeledAppsConfig'])
    def test_relabeling(self):
        self.assertEqual(apps.get_app_config('relabeled').name, 'apps')

    def test_duplicate_labels(self):
        with six.assertRaisesRegex(self, ImproperlyConfigured, "Application labels aren't unique"):
            with self.settings(INSTALLED_APPS=['apps.apps.PlainAppsConfig', 'apps']):
                pass

    def test_duplicate_names(self):
        with six.assertRaisesRegex(self, ImproperlyConfigured, "Application names aren't unique"):
            with self.settings(INSTALLED_APPS=['apps.apps.RelabeledAppsConfig', 'apps']):
                pass

    def test_import_exception_is_not_masked(self):
        """
        App discovery should preserve stack traces. Regression test for #22920.
        """
        with six.assertRaisesRegex(self, ImportError, "Oops"):
            with self.settings(INSTALLED_APPS=['apps.failing_app']):
                pass

    def test_models_py(self):
        """
        Tests that the models in the models.py file were loaded correctly.
        """
        self.assertEqual(apps.get_model("apps", "TotallyNormal"), TotallyNormal)
        with self.assertRaises(LookupError):
            apps.get_model("apps", "SoAlternative")

        with self.assertRaises(LookupError):
            new_apps.get_model("apps", "TotallyNormal")
        self.assertEqual(new_apps.get_model("apps", "SoAlternative"), SoAlternative)

    def test_dynamic_load(self):
        """
        Makes a new model at runtime and ensures it goes into the right place.
        """
        old_models = list(apps.get_app_config("apps").get_models())
        # Construct a new model in a new app registry
        body = {}
        new_apps = Apps(["apps"])
        meta_contents = {
            'app_label': "apps",
            'apps': new_apps,
        }
        meta = type(str("Meta"), tuple(), meta_contents)
        body['Meta'] = meta
        body['__module__'] = TotallyNormal.__module__
        temp_model = type(str("SouthPonies"), (models.Model,), body)
        # Make sure it appeared in the right place!
        self.assertListEqual(list(apps.get_app_config("apps").get_models()), old_models)
        with self.assertRaises(LookupError):
            apps.get_model("apps", "SouthPonies")
        self.assertEqual(new_apps.get_model("apps", "SouthPonies"), temp_model)

    def test_model_clash(self):
        """
        Test for behavior when two models clash in the app registry.
        """
        new_apps = Apps(["apps"])
        meta_contents = {
            'app_label': "apps",
            'apps': new_apps,
        }

        body = {}
        body['Meta'] = type(str("Meta"), tuple(), meta_contents)
        body['__module__'] = TotallyNormal.__module__
        type(str("SouthPonies"), (models.Model,), body)

        # When __name__ and __module__ match we assume the module
        # was reloaded and issue a warning. This use-case is
        # useful for REPL. Refs #23621.
        body = {}
        body['Meta'] = type(str("Meta"), tuple(), meta_contents)
        body['__module__'] = TotallyNormal.__module__
        with warnings.catch_warnings(record=True) as w:
            type(str("SouthPonies"), (models.Model,), body)
            self.assertEqual(len(w), 1)
            self.assertTrue(issubclass(w[-1].category, RuntimeWarning))
            self.assertEqual(str(w[-1].message),
                 "Model 'southponies.apps' was already registered. "
                 "Reloading models is not advised as it can lead to inconsistencies, "
                 "most notably with related models.")

        # If it doesn't appear to be a reloaded module then we expect
        # a RuntimeError.
        body = {}
        body['Meta'] = type(str("Meta"), tuple(), meta_contents)
        body['__module__'] = TotallyNormal.__module__ + '.whatever'
        with six.assertRaisesRegex(self, RuntimeError,
                "Conflicting 'southponies' models in application 'apps':.*"):
            type(str("SouthPonies"), (models.Model,), body)

class Stub(object):
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

class AppConfigTests(TestCase):
    """Unit tests for AppConfig class."""
    def test_path_set_explicitly(self):
        """If subclass sets path as class attr, no module attributes needed."""
        class MyAppConfig(AppConfig):
            path = 'foo'

        ac = MyAppConfig('label', Stub())

        self.assertEqual(ac.path, 'foo')

    def test_explicit_path_overrides(self):
        """If path set as class attr, overrides __path__ and __file__."""
        class MyAppConfig(AppConfig):
            path = 'foo'

        ac = MyAppConfig('label', Stub(__path__=['a'], __file__='b/__init__.py'))

        self.assertEqual(ac.path, 'foo')

    def test_dunder_path(self):
        """If single element in __path__, use it (in preference to __file__)."""
        ac = AppConfig('label', Stub(__path__=['a'], __file__='b/__init__.py'))

        self.assertEqual(ac.path, 'a')

    def test_no_dunder_path_fallback_to_dunder_file(self):
        """If there is no __path__ attr, use __file__."""
        ac = AppConfig('label', Stub(__file__='b/__init__.py'))

        self.assertEqual(ac.path, 'b')

    def test_empty_dunder_path_fallback_to_dunder_file(self):
        """If the __path__ attr is empty, use __file__ if set."""
        ac = AppConfig('label', Stub(__path__=[], __file__='b/__init__.py'))

        self.assertEqual(ac.path, 'b')

    def test_multiple_dunder_path_fallback_to_dunder_file(self):
        """If the __path__ attr is length>1, use __file__ if set."""
        ac = AppConfig('label', Stub(__path__=['a', 'b'], __file__='c/__init__.py'))

        self.assertEqual(ac.path, 'c')

    def test_no_dunder_path_or_dunder_file(self):
        """If there is no __path__ or __file__, raise ImproperlyConfigured."""
        with self.assertRaises(ImproperlyConfigured):
            AppConfig('label', Stub())

    def test_empty_dunder_path_no_dunder_file(self):
        """If the __path__ attr is empty and there is no __file__, raise."""
        with self.assertRaises(ImproperlyConfigured):
            AppConfig('label', Stub(__path__=[]))

    def test_multiple_dunder_path_no_dunder_file(self):
        """If the __path__ attr is length>1 and there is no __file__, raise."""
        with self.assertRaises(ImproperlyConfigured):
            AppConfig('label', Stub(__path__=['a', 'b']))

@skipUnless(
    sys.version_info > (3, 3, 0),
    "Namespace packages sans __init__.py were added in Python 3.3")
class NamespacePackageAppTests(TestCase):
    # We need nsapp to be top-level so our multiple-paths tests can add another
    # location for it (if its inside a normal package with an __init__.py that
    # isn't possible). In order to avoid cluttering the already-full tests/ dir
    # (which is on sys.path), we add these new entries to sys.path temporarily.
    base_location = os.path.join(HERE, 'namespace_package_base')
    other_location = os.path.join(HERE, 'namespace_package_other_base')
    app_path = os.path.join(base_location, 'nsapp')

    def test_single_path(self):
        """
        A Py3.3+ namespace package can be an app if it has only one path.
        """
        with extend_sys_path(self.base_location):
            with self.settings(INSTALLED_APPS=['nsapp']):
                app_config = apps.get_app_config('nsapp')
                self.assertEqual(app_config.path, upath(self.app_path))

    def test_multiple_paths(self):
        """
        A Py3.3+ namespace package with multiple locations cannot be an app.

        (Because then we wouldn't know where to load its templates, static
        assets, etc from.)

        """
        # Temporarily add two directories to sys.path that both contain
        # components of the "nsapp" package.
        with extend_sys_path(self.base_location, self.other_location):
            with self.assertRaises(ImproperlyConfigured):
                with self.settings(INSTALLED_APPS=['nsapp']):
                    pass

    def test_multiple_paths_explicit_path(self):
        """
        Multiple locations are ok only if app-config has explicit path.
        """
        # Temporarily add two directories to sys.path that both contain
        # components of the "nsapp" package.
        with extend_sys_path(self.base_location, self.other_location):
            with self.settings(INSTALLED_APPS=['nsapp.apps.NSAppConfig']):
                app_config = apps.get_app_config('nsapp')
                self.assertEqual(app_config.path, upath(self.app_path))
<|endoftext|>


<commit_before>from cement.utils import test
from common.testutils import file_len, decallmethods
from requests.exceptions import ConnectionError
from tests import BaseTest
from plugins.drupal import Drupal
import responses

@decallmethods(responses.activate)
class BaseTests(BaseTest):
    """
        Basic tests, and generic tests.
    """
    @test.raises(SystemExit)
    def test_errors_when_no_url(self):
        self.add_argv(["drupal"])
        self.app.run()

    @test.raises(RuntimeError)
    def test_errors_when_invalid_url(self):
        self.add_argv(["drupal"])
        self.add_argv(self.param_plugins)
        self.add_argv(["--url", "invalidurl"])
        self.app.run()

    @test.raises(SystemExit)
    def test_errors_when_invalid_enumerate(self):
        self.add_argv(["drupal"])
        self.add_argv(self.param_base + ["-e", 'z'])
        self.app.run()

    @test.raises(SystemExit)
    def test_errors_when_invalid_method(self):
        self.add_argv(["drupal"])
        self.add_argv(self.param_plugins + ["--method", "derpo"])
        self.app.run()

    @test.raises(ConnectionError)
    def test_calls_plugin(self):
        self.add_argv(["drupal"])
        self.add_argv(self.param_plugins)
        self.add_argv(["--method", "forbidden"])

        self.app.run()

    @test.raises(ConnectionError)
    def test_calls_theme(self):
        self.add_argv(["drupal"])
        self.add_argv(self.param_themes)

        self.add_argv(["--method", "forbidden"])

        self.app.run()

    def test_calls_all(self):
        self.add_argv(["drupal"])
        self.add_argv(self.param_all)
        self.add_argv(["--method", "forbidden"])

        all_mocks = self.mock_all_enumerate('drupal')

        self.app.run()

        for m in all_mocks:
            assert m .called, "module %s" % m

    def test_doesnt_crash_on_runtimeerror(self):
        self.add_argv(["drupal"])
        self.add_argv(self.param_all)
        self.add_argv(["--method", "forbidden"])

        all_mocks = self.mock_all_enumerate('drupal', side_effect_on_one=True)

        self.app.run()

    def test_fix_dereference_bug(self):
        """
            test for annoying dereference that made the app fail even though
            all tests were passing.
        """

        plugins_base_url = "plugins_base_url"
        themes_base_url = "themes_base_url"
        opts_p = {
            'url': self.base_url,
            'plugins_base_url': plugins_base_url,
            'themes_base_url': themes_base_url,
            'scanning_method': 'a',
            'number': 'a',
            'threads': 'a',
            'verb': 'a',
            'enumerate': 'p',
        }
        opts_t = dict(opts_p)
        opts_t['enumerate'] = 't'

        drupal = Drupal()
        kwargs_p = drupal._functionality(opts_p)['plugins']['kwargs']
        kwargs_t = drupal._functionality(opts_t)['themes']['kwargs']

        # these should not be equal
        assert not kwargs_p == kwargs_t

<commit_msg>made tests silent again<commit_after>from cement.utils import test
from common.testutils import file_len, decallmethods
from contextlib import contextmanager
from plugins.drupal import Drupal
from requests.exceptions import ConnectionError
from StringIO import StringIO
from tests import BaseTest
import responses
import sys

@contextmanager
def capture_sys_output():
    caputure_out, capture_err = StringIO(), StringIO()
    current_out, current_err = sys.stdout, sys.stderr
    try:
        sys.stdout, sys.stderr = caputure_out, capture_err
        yield caputure_out, capture_err
    finally:
        sys.stdout, sys.stderr = current_out, current_err

@decallmethods(responses.activate)
class BaseTests(BaseTest):
    """
        Basic tests, and generic tests.
    """
    @test.raises(SystemExit)
    def test_errors_when_no_url(self):
        with capture_sys_output() as (stdout, stderr):
            self.add_argv(["drupal"])
            self.app.run()

    @test.raises(RuntimeError)
    def test_errors_when_invalid_url(self):
        self.add_argv(["drupal"])
        self.add_argv(self.param_plugins)
        self.add_argv(["--url", "invalidurl"])
        self.app.run()

    @test.raises(SystemExit)
    def test_errors_when_invalid_enumerate(self):
        with capture_sys_output() as (stdout, stderr):
            self.add_argv(["drupal"])
            self.add_argv(self.param_base + ["-e", 'z'])
            self.app.run()

    @test.raises(SystemExit)
    def test_errors_when_invalid_method(self):
        with capture_sys_output() as (stdout, stderr):
            self.add_argv(["drupal"])
            self.add_argv(self.param_plugins + ["--method", "derpo"])
            self.app.run()

    @test.raises(ConnectionError)
    def test_calls_plugin(self):
        self.add_argv(["drupal"])
        self.add_argv(self.param_plugins)
        self.add_argv(["--method", "forbidden"])

        self.app.run()

    @test.raises(ConnectionError)
    def test_calls_theme(self):
        self.add_argv(["drupal"])
        self.add_argv(self.param_themes)

        self.add_argv(["--method", "forbidden"])

        self.app.run()

    def test_calls_all(self):
        self.add_argv(["drupal"])
        self.add_argv(self.param_all)
        self.add_argv(["--method", "forbidden"])

        all_mocks = self.mock_all_enumerate('drupal')

        self.app.run()

        for m in all_mocks:
            assert m .called, "module %s" % m

    def test_doesnt_crash_on_runtimeerror(self):
        self.add_argv(["drupal"])
        self.add_argv(self.param_all)
        self.add_argv(["--method", "forbidden"])

        all_mocks = self.mock_all_enumerate('drupal', side_effect_on_one=True)

        self.app.run()

    def test_fix_dereference_bug(self):
        """
            test for annoying dereference that made the app fail even though
            all tests were passing.
        """

        plugins_base_url = "plugins_base_url"
        themes_base_url = "themes_base_url"
        opts_p = {
            'url': self.base_url,
            'plugins_base_url': plugins_base_url,
            'themes_base_url': themes_base_url,
            'scanning_method': 'a',
            'number': 'a',
            'threads': 'a',
            'verb': 'a',
            'enumerate': 'p',
        }
        opts_t = dict(opts_p)
        opts_t['enumerate'] = 't'

        drupal = Drupal()
        kwargs_p = drupal._functionality(opts_p)['plugins']['kwargs']
        kwargs_t = drupal._functionality(opts_t)['themes']['kwargs']

        # these should not be equal
        assert not kwargs_p == kwargs_t

<|endoftext|>


<commit_before><commit_msg>validation: fix creation of g_%s_info_map entries<commit_after><|endoftext|>


<commit_before><commit_msg>validation: Remove unused variables<commit_after><|endoftext|>


<commit_before># import os
from __future__ import print_function
import sys
from cloudmesh_common.logger import LOGGER
from cloudmesh.config.cm_config import cm_config
from cloudmesh.user.cm_user import cm_user
from cloudmesh.cm_mongo import cm_mongo
from cloudmesh_common.tables import row_table

from cmd3.shell import command

log = LOGGER(__file__)

class cm_shell_limits:

    """opt_example class"""
    _id = "limits"  # id for usage in cm_mongo

    def activate_cm_shell_limits(self):
        self.cm_mongo = cm_mongo()
        self.cm_config = cm_config()
        self.cm_user = cm_user()
        self.register_command_topic('cloud', 'usage')
        pass

    def get_cloud_name(self, cm_user_id):
        """Returns a default cloud name if exists
        """
        try:
            return self.cm_user.get_defaults(cm_user_id)['cloud']
        except KeyError:
            log.error('set a default cloud with openstack. "stack" works on'
                      ' openstack platform only')
            return None

    @command
    def do_limits(self, args, arguments):
        """
        Usage:
            limits [CLOUD]
            limits help | -h

        Current usage data with limits on a selected project (tenant)

        Arguments:

          CLOUD          Cloud name to see the usage
          help           Prints this message

        Options:

           -v       verbose mode

        """

        if arguments["help"] or arguments["-h"]:
            print (self.do_limits.__doc__)
        else:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            self.cm_mongo.activate(userid)
            usage_with_limits = self.cm_mongo.usage_with_limits(def_cloud, userid)

            print(row_table(usage_with_limits, order=None, labels=["Variable",
                                                                   "(Used/Max)"]))

            return usage_with_limits
<commit_msg>title<commit_after># import os
from __future__ import print_function
import sys
from cloudmesh_common.logger import LOGGER
from cloudmesh.config.cm_config import cm_config
from cloudmesh.user.cm_user import cm_user
from cloudmesh.cm_mongo import cm_mongo
from cloudmesh_common.tables import row_table

from cmd3.shell import command

log = LOGGER(__file__)

class cm_shell_limits:

    """opt_example class"""
    _id = "limits"  # id for usage in cm_mongo

    def activate_cm_shell_limits(self):
        self.cm_mongo = cm_mongo()
        self.cm_config = cm_config()
        self.cm_user = cm_user()
        self.register_command_topic('cloud', 'usage')
        pass

    def get_cloud_name(self, cm_user_id):
        """Returns a default cloud name if exists
        """
        try:
            return self.cm_user.get_defaults(cm_user_id)['cloud']
        except KeyError:
            log.error('set a default cloud with openstack. "stack" works on'
                      ' openstack platform only')
            return None

    @command
    def do_limits(self, args, arguments):
        """
        Usage:
            limits [CLOUD]
            limits help | -h

        Current usage data with limits on a selected project (tenant)

        Arguments:

          CLOUD          Cloud name to see the usage
          help           Prints this message

        Options:

           -v       verbose mode

        """

        if arguments["help"] or arguments["-h"]:
            print (self.do_limits.__doc__)
        else:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            self.cm_mongo.activate(userid)
            usage_with_limits = self.cm_mongo.usage_with_limits(def_cloud, userid)

            print(row_table(usage_with_limits, order=None, labels=["Limits",
                                                                   "(Used/Max)"]))

            return usage_with_limits
<|endoftext|>


<commit_before><commit_msg>Extended description of tfds.as_numpy<commit_after><|endoftext|>


<commit_before># coding=utf-8
import logging
import random
import re
import requests
import time

from data_extraction.params import REQUEST_HEADERS

log = logging.getLogger('app')

change_email_settings_tips = [
    'list-manage',
    'unsubscribe',
    'edit_subscription',
    'edit-subscription',
    'confirm',
    'click.e.mozilla.org',
    '/leave/',
    '/newsletter/existing/',
]

no_redir_tips = [
    'list-manage',
    'unsubscribe',
    'edit_subscription',
    'edit-subscription',
    'confirm',
    '/leave/',
    '/newsletter/existing/',
    'http://www.w3.org/1999/xhtml',
]

url_pattern = re.compile(
    r'(?:https?:)?//(?:(?:[\w-]+\.)+[\w/#@~.-]*)(?:\?(?:[\w&=.!,;$#%-]+)?)?',
    re.UNICODE | re.IGNORECASE
)

garbage_params_pattern = re.compile(r'(?:&?utm_[^&]+)|(?:&?from=rss)|(?:&?source=rss)')
double_amp_pattern = re.compile(r'&&')
leading_amp_pattern = re.compile(r'\?&')

def extract_all_links(html):
    return url_pattern.findall(html)

def replace_redirects(text):
    return url_pattern.sub(_redirect_replacer, text)

def _redirect_replacer(match):
    url = match.group(0)

    if is_local_link(url):
        return url

    for tip in no_redir_tips:
        if tip in url:
            return url

    timeout = random.randint(10, 50) / 1000.0
    time.sleep(timeout)

    res = requests.head(url, headers=REQUEST_HEADERS, allow_redirects=True)

    if res.status_code == 200 and res.url != url:
        log.debug('Resolve redirect: %s -> %s', url, res.url)
        return res.url
    elif res.status_code != 200:
        log.warn('Got code %d from redirect request: %s', res.status_code, url)

    return url

def is_local_link(url):
    return url.startswith('/') and not url.startswith('//')

def replace_email_settings_links(html):
    return url_pattern.sub(_email_settings_replacer, html)

def _email_settings_replacer(match):
    url = match.group(0)
    if any(tip in url for tip in change_email_settings_tips):
        return 'http://natribu.org'
    return url

def highlight_urls(html):
    return url_pattern.sub(_url_highlighter, html)

def _url_highlighter(match):
    url = match.group(0)
    return '{url}'.format(url=url)

def clean_url(url):
    url = garbage_params_pattern.sub('', url)
    url = leading_amp_pattern.sub('?', url)
    url = double_amp_pattern.sub('&', url)

    if url.endswith('?'):
        url = url[:-1]

    return url
<commit_msg>Localize errors for redirect resolving<commit_after># coding=utf-8
import logging
import random
import re
import requests
import time

from data_extraction.params import REQUEST_HEADERS

log = logging.getLogger('app')

change_email_settings_tips = [
    'list-manage',
    'unsubscribe',
    'edit_subscription',
    'edit-subscription',
    'confirm',
    'click.e.mozilla.org',
    '/leave/',
    '/newsletter/existing/',
]

no_redir_tips = [
    'list-manage',
    'unsubscribe',
    'edit_subscription',
    'edit-subscription',
    'confirm',
    '/leave/',
    '/newsletter/existing/',
    'http://www.w3.org/1999/xhtml',
]

url_pattern = re.compile(
    r'(?:https?:)?//(?:(?:[\w-]+\.)+[\w/#@~.-]*)(?:\?(?:[\w&=.!,;$#%-]+)?)?',
    re.UNICODE | re.IGNORECASE
)

garbage_params_pattern = re.compile(r'(?:&?utm_[^&]+)|(?:&?from=rss)|(?:&?source=rss)')
double_amp_pattern = re.compile(r'&&')
leading_amp_pattern = re.compile(r'\?&')

def extract_all_links(html):
    return url_pattern.findall(html)

def replace_redirects(text):
    return url_pattern.sub(_redirect_replacer, text)

def _redirect_replacer(match):
    url = match.group(0)

    if is_local_link(url):
        return url

    for tip in no_redir_tips:
        if tip in url:
            return url

    timeout = random.randint(10, 50) / 1000.0
    time.sleep(timeout)

    try:
        res = requests.head(url, headers=REQUEST_HEADERS, allow_redirects=True)
    except Exception as e:
        log.warn('Got error from redirect request: %s', e)
        return url

    if res.status_code == 200 and res.url != url:
        log.debug('Resolve redirect: %s -> %s', url, res.url)
        return res.url
    elif res.status_code != 200:
        log.warn('Got code %d from redirect request: %s', res.status_code, url)

    return url

def is_local_link(url):
    return url.startswith('/') and not url.startswith('//')

def replace_email_settings_links(html):
    return url_pattern.sub(_email_settings_replacer, html)

def _email_settings_replacer(match):
    url = match.group(0)
    if any(tip in url for tip in change_email_settings_tips):
        return 'http://natribu.org'
    return url

def highlight_urls(html):
    return url_pattern.sub(_url_highlighter, html)

def _url_highlighter(match):
    url = match.group(0)
    return '{url}'.format(url=url)

def clean_url(url):
    url = garbage_params_pattern.sub('', url)
    url = leading_amp_pattern.sub('?', url)
    url = double_amp_pattern.sub('&', url)

    if url.endswith('?'):
        url = url[:-1]

    return url
<|endoftext|>


<commit_before><commit_msg>Repeat the setUp/tearDown changes made in r11699 for the tests converted in r11694.  This allows the dateformat tests to pass on Windows.<commit_after><|endoftext|>


<commit_before>from unittest.mock import Mock, patch
from django.test import TestCase
from data_refinery_common import utils

class UtilsTestCase(TestCase):

    @patch('data_refinery_common.utils.requests.get')
    def test_get_instance_id_cloud(self, mock_get):
        """Test that a request is made and the global value is stored"""
        # Ensure utils.INSTANCE_ID hasn't been set yet in case the
        # order the tests are run in ever changes
        utils.INSTANCE_ID = None

        with self.settings(RUNNING_IN_CLOUD=True):
            self.assertEqual(utils.get_instance_id(), "instance_id")

        # Ensure that the second call uses the now-set global value.
        # (By resetting the mocks, calling it again, and checking that
        # the values didn't need to be set again).
        mock_get.reset_mock()
        mock_get_env_variable.reset_mock()
        utils.get_instance_id()
        mock_get.assert_not_called()

    def test_get_instance_id_local(self):
        """Test that local is used for instance id."""
        # Ensure utils.INSTANCE_ID hasn't been set yet in case the
        # order the tests are run in ever changes
        utils.INSTANCE_ID = None

        with self.settings(RUNNING_IN_CLOUD=False):
            self.assertEqual(utils.get_instance_id(), "local")

        # Ensure that the second call uses the now-set global value
        # by changing what settings would tell it.
        with self.settings(RUNNING_IN_CLOUD=True):
            self.assertEqual(utils.get_instance_id(), "local")

    def test_supported_microarray_platforms(self):
        """Test that supported microarray platforms setting is set correctly."""
        supported_microarray_platforms = utils.get_supported_microarray_platforms()

        has_equgene11st = False
        has_A_AFFY_59 = False
        has_GPL23026 = False
        has_AGEOD23026 = False
        for platform in supported_microarray_platforms:
            if platform["platform_accession"] == "equgene11st" and platform["is_brainarray"]:
                has_equgene11st = True

            if platform["external_accession"] == "A-AFFY-59" and not platform["is_brainarray"]:
                has_A_AFFY_59 = True

            if platform["external_accession"] == "GPL23026" and not platform["is_brainarray"]:
                has_GPL23026 = True

            if platform["external_accession"] == "A-GEOD-23026" and not platform["is_brainarray"]:
                has_AGEOD23026 = True

        self.assertTrue(has_equgene11st)
        self.assertTrue(has_A_AFFY_59)
        self.assertTrue(has_GPL23026)
        self.assertTrue(has_AGEOD23026)

    def test_get_internal_microarray_accession(self):
        """Test that supported microarray platforms setting is set correctly."""

        self.assertEqual(utils.get_internal_microarray_accession("hgu133a"), "hgu133a")
        self.assertEqual(utils.get_internal_microarray_accession("A-AFFY-59"), "soybean")
        self.assertEqual(utils.get_internal_microarray_accession("GPL23026"), "Illumina_HumanHT-12_V4.0")

    def test_supported_rnaseq_platforms(self):
        """Test that supported RNASeq platforms setting is set correctly."""
        self.assertTrue("Illumina HiSeq 1000" in utils.get_supported_rnaseq_platforms())

    def test_readable_affymetrix_names(self):
        """Test that the setting for Affymetrix accessions to
        human readable names is set correctly."""
        readable_platform_names = utils.get_readable_affymetrix_names()
        expected_readable_name = "[ChiGene-1_0-st] Affymetrix Chicken Gene 1.0 ST Array"
        self.assertTrue(readable_platform_names["chigene10st"] == expected_readable_name)
        expected_readable_name = "[Xenopus_laevis] Affymetrix Xenopus laevis Genome Array"
        self.assertTrue(readable_platform_names["xenopuslaevis"] == expected_readable_name)

    def test_get_normalized_platform(self):
        """ Test a particular normaization we need to perform """

        self.assertEqual(utils.get_normalized_platform("hugene10stv1"), "hugene10st")
        self.assertEqual(utils.get_normalized_platform("hugene10stv2"), "hugene10st")
        self.assertEqual(utils.get_normalized_platform("stv1hugene10"), "stv1hugene10")

    def test_volume_index(self):
        """Test that supported RNASeq platforms setting is set correctly."""
        self.assertEqual(utils.get_volume_index(), "0")
        with open('/tmp/VOLUME_INDEX', 'wb') as f:
            f.write("123".encode())
        self.assertEqual(utils.get_volume_index(path='/tmp/VOLUME_INDEX'), "123")
<commit_msg>Readd mock get method for common tests.<commit_after>from unittest.mock import Mock, patch
from django.test import TestCase
from data_refinery_common import utils

class UtilsTestCase(TestCase):

    @patch('data_refinery_common.utils.requests.get')
    def test_get_instance_id_cloud(self, mock_get):
        """Test that a request is made and the global value is stored"""
        # Ensure utils.INSTANCE_ID hasn't been set yet in case the
        # order the tests are run in ever changes
        utils.INSTANCE_ID = None
        mock_get.return_value = Mock(ok=True)
        mock_get.return_value.text = "instance_id"

        with self.settings(RUNNING_IN_CLOUD=True):
            self.assertEqual(utils.get_instance_id(), "instance_id")

        # Ensure that the second call uses the now-set global value.
        # (By resetting the mocks, calling it again, and checking that
        # the values didn't need to be set again).
        mock_get.reset_mock()
        mock_get_env_variable.reset_mock()
        utils.get_instance_id()
        mock_get.assert_not_called()

    def test_get_instance_id_local(self):
        """Test that local is used for instance id."""
        # Ensure utils.INSTANCE_ID hasn't been set yet in case the
        # order the tests are run in ever changes
        utils.INSTANCE_ID = None

        with self.settings(RUNNING_IN_CLOUD=False):
            self.assertEqual(utils.get_instance_id(), "local")

        # Ensure that the second call uses the now-set global value
        # by changing what settings would tell it.
        with self.settings(RUNNING_IN_CLOUD=True):
            self.assertEqual(utils.get_instance_id(), "local")

    def test_supported_microarray_platforms(self):
        """Test that supported microarray platforms setting is set correctly."""
        supported_microarray_platforms = utils.get_supported_microarray_platforms()

        has_equgene11st = False
        has_A_AFFY_59 = False
        has_GPL23026 = False
        has_AGEOD23026 = False
        for platform in supported_microarray_platforms:
            if platform["platform_accession"] == "equgene11st" and platform["is_brainarray"]:
                has_equgene11st = True

            if platform["external_accession"] == "A-AFFY-59" and not platform["is_brainarray"]:
                has_A_AFFY_59 = True

            if platform["external_accession"] == "GPL23026" and not platform["is_brainarray"]:
                has_GPL23026 = True

            if platform["external_accession"] == "A-GEOD-23026" and not platform["is_brainarray"]:
                has_AGEOD23026 = True

        self.assertTrue(has_equgene11st)
        self.assertTrue(has_A_AFFY_59)
        self.assertTrue(has_GPL23026)
        self.assertTrue(has_AGEOD23026)

    def test_get_internal_microarray_accession(self):
        """Test that supported microarray platforms setting is set correctly."""

        self.assertEqual(utils.get_internal_microarray_accession("hgu133a"), "hgu133a")
        self.assertEqual(utils.get_internal_microarray_accession("A-AFFY-59"), "soybean")
        self.assertEqual(utils.get_internal_microarray_accession("GPL23026"), "Illumina_HumanHT-12_V4.0")

    def test_supported_rnaseq_platforms(self):
        """Test that supported RNASeq platforms setting is set correctly."""
        self.assertTrue("Illumina HiSeq 1000" in utils.get_supported_rnaseq_platforms())

    def test_readable_affymetrix_names(self):
        """Test that the setting for Affymetrix accessions to
        human readable names is set correctly."""
        readable_platform_names = utils.get_readable_affymetrix_names()
        expected_readable_name = "[ChiGene-1_0-st] Affymetrix Chicken Gene 1.0 ST Array"
        self.assertTrue(readable_platform_names["chigene10st"] == expected_readable_name)
        expected_readable_name = "[Xenopus_laevis] Affymetrix Xenopus laevis Genome Array"
        self.assertTrue(readable_platform_names["xenopuslaevis"] == expected_readable_name)

    def test_get_normalized_platform(self):
        """ Test a particular normaization we need to perform """

        self.assertEqual(utils.get_normalized_platform("hugene10stv1"), "hugene10st")
        self.assertEqual(utils.get_normalized_platform("hugene10stv2"), "hugene10st")
        self.assertEqual(utils.get_normalized_platform("stv1hugene10"), "stv1hugene10")

    def test_volume_index(self):
        """Test that supported RNASeq platforms setting is set correctly."""
        self.assertEqual(utils.get_volume_index(), "0")
        with open('/tmp/VOLUME_INDEX', 'wb') as f:
            f.write("123".encode())
        self.assertEqual(utils.get_volume_index(path='/tmp/VOLUME_INDEX'), "123")
<|endoftext|>


<commit_before>import random
import unittest
import numpy as np
import modAL.uncertainty
import modAL.query
import modAL.models
import modAL.utils.validation
from itertools import chain
from collections import namedtuple
from mock import MockClassifier, MockUtility, MockActiveLearner

Test = namedtuple('Test', ['input', 'output'])

def random_array(shape, n_arrays):
    for _ in range(n_arrays):
        yield np.random.rand(*shape)

class TestUtils(unittest.TestCase):

    def test_check_class_labels(self):
        for n_labels in range(1, 10):
            for n_learners in range(1, 10):
                labels = np.random.randint(10, size=n_labels)
                different_labels = np.random.randint(10, 20, size=np.random.randint(1, 10))
                learner_list_1 = [MockClassifier(classes_=labels) for _ in range(n_learners)]
                learner_list_2 = [MockClassifier(classes_=different_labels) for _ in range(np.random.randint(1, 5))]
                shuffled_learners = random.sample(learner_list_1 + learner_list_2, len(learner_list_1 + learner_list_2))
                self.assertTrue(modAL.utils.validation.check_class_labels(*learner_list_1))
                self.assertFalse(modAL.utils.validation.check_class_labels(*shuffled_learners))

class TestUtilities(unittest.TestCase):

    def test_uncertainty(self):
        test_cases = (Test(p * np.ones(shape=(k, l)), (1 - p) * np.ones(shape=(k, )))
                      for k in range(1, 100) for l in range(1, 10) for p in np.linspace(0, 1, 11))
        for case in test_cases:
            mock_classifier = MockClassifier(predict_proba_return=case.input)
            np.testing.assert_almost_equal(
                modAL.uncertainty.classifier_uncertainty(mock_classifier, np.random.rand(10)),
                case.output
            )

    def test_margin(self):
        test_cases_1 = (Test(p * np.ones(shape=(k, l)), np.zeros(shape=(k,)))
                      for k in range(1, 100) for l in range(1, 10) for p in np.linspace(0, 1, 11))
        test_cases_2 = (Test(p * np.tile(np.asarray(range(k))+1.0, l).reshape(l, k),
                             p * np.ones(shape=(l, ))*int(k!=1))
                        for k in range(1, 10) for l in range(1, 100) for p in np.linspace(0, 1, 11))
        for case in chain(test_cases_1, test_cases_2):
            mock_classifier = MockClassifier(predict_proba_return=case.input)
            np.testing.assert_almost_equal(
                modAL.uncertainty.classifier_margin(mock_classifier, np.random.rand(10)),
                case.output
            )

class TestQueries(unittest.TestCase):

    def test_max_utility(self):
        for n_pool in range(2, 100):
            for n_instances in range(1, n_pool):
                utility = np.zeros(n_pool)
                max_idx = np.random.choice(range(n_pool), size=n_instances, replace=False)
                utility[max_idx] = 1.0
                np.testing.assert_equal(
                    np.sort(modAL.query.max_uncertainty(utility, n_instances)),
                    np.sort(max_idx)
                )

    def test_utility_weighted_random(self):
        for n_pool in range(2, 100):
            for n_instances in range(1, n_pool):
                utility = np.ones(n_pool)
                query_idx = modAL.query.uncertainty_weighted_random(utility, n_instances)
                # testing for correct number of returned indices
                np.testing.assert_equal(len(query_idx), n_instances)
                # testing for uniqueness of each query index
                np.testing.assert_equal(len(query_idx), len(np.unique(query_idx)))

class TestActiveLearner(unittest.TestCase):

    def test_calculate_utility(self):
        test_cases = (Test(array, array) for k in range(1, 10) for l in range(1, 10) for array in random_array((k, l), 100))
        for case in test_cases:
            mock_classifier = MockClassifier()
            learner = modAL.models.ActiveLearner(mock_classifier, MockUtility(case.input))
            np.testing.assert_almost_equal(
                learner.calculate_uncertainty(case.input),
                case.output
            )

    def test_query(self):
        pass

    def test_predict(self):
        pass

    def test_predict_proba(self):
        pass

    def test_score(self):
        test_cases = (np.random.rand() for _ in range(10))
        for score_return in test_cases:
            mock_classifier = MockClassifier(score_return=score_return)
            learner = modAL.models.ActiveLearner(mock_classifier, MockUtility(None))
            np.testing.assert_almost_equal(
                learner.score(np.random.rand(5, 2), np.random.rand(5, )),
                score_return
            )

    def test_keras(self):
        pass

    def test_sklearn(self):
        pass

class TestCommittee(unittest.TestCase):

    def test_calculate_utility(self):
        for n_learners in range(1, 200):
            utility = np.random.rand(100, n_learners)
            committee = modAL.models.Committee(
                learner_list=[MockActiveLearner(
                                    MockClassifier(classes_=np.asarray([0])),
                                    calculate_utility_return=utility[:, learner_idx].reshape(-1)
                              )
                              for learner_idx in range(n_learners)]
            )
            np.testing.assert_almost_equal(
                committee.calculate_uncertainty(np.random.rand(100, 1)),
                utility
            )

    def test_predict(self):
        for n_learners in range(1, 10):
            for n_instances in range(1, 10):
                prediction = np.random.randint(10, size=(n_instances, n_learners))
                committee = modAL.models.Committee(
                    learner_list=[MockActiveLearner(
                                          MockClassifier(classes_=np.asarray([0])),
                                          predict_return=prediction[:, learner_idx]
                                  )
                                  for learner_idx in range(n_learners)]
                )
                np.testing.assert_equal(
                    committee.vote(np.random.rand(n_instances, 5)),
                    prediction
                )

if __name__ == '__main__':
    unittest.main()
<commit_msg>tests/core_tests.py TestUncertainties.test_entropy() implemented<commit_after>import random
import unittest
import numpy as np
import modAL.uncertainty
import modAL.query
import modAL.models
import modAL.utils.validation
from itertools import chain
from collections import namedtuple
from mock import MockClassifier, MockUtility, MockActiveLearner

Test = namedtuple('Test', ['input', 'output'])

def random_array(shape, n_arrays):
    for _ in range(n_arrays):
        yield np.random.rand(*shape)

class TestUtils(unittest.TestCase):

    def test_check_class_labels(self):
        for n_labels in range(1, 10):
            for n_learners in range(1, 10):
                labels = np.random.randint(10, size=n_labels)
                different_labels = np.random.randint(10, 20, size=np.random.randint(1, 10))
                learner_list_1 = [MockClassifier(classes_=labels) for _ in range(n_learners)]
                learner_list_2 = [MockClassifier(classes_=different_labels) for _ in range(np.random.randint(1, 5))]
                shuffled_learners = random.sample(learner_list_1 + learner_list_2, len(learner_list_1 + learner_list_2))
                self.assertTrue(modAL.utils.validation.check_class_labels(*learner_list_1))
                self.assertFalse(modAL.utils.validation.check_class_labels(*shuffled_learners))

class TestUncertainties(unittest.TestCase):

    def test_uncertainty(self):
        test_cases = (Test(p * np.ones(shape=(k, l)), (1 - p) * np.ones(shape=(k, )))
                      for k in range(1, 100) for l in range(1, 10) for p in np.linspace(0, 1, 11))
        for case in test_cases:
            mock_classifier = MockClassifier(predict_proba_return=case.input)
            np.testing.assert_almost_equal(
                modAL.uncertainty.classifier_uncertainty(mock_classifier, np.random.rand(10)),
                case.output
            )

    def test_margin(self):
        test_cases_1 = (Test(p * np.ones(shape=(k, l)), np.zeros(shape=(k,)))
                      for k in range(1, 100) for l in range(1, 10) for p in np.linspace(0, 1, 11))
        test_cases_2 = (Test(p * np.tile(np.asarray(range(k))+1.0, l).reshape(l, k),
                             p * np.ones(shape=(l, ))*int(k!=1))
                        for k in range(1, 10) for l in range(1, 100) for p in np.linspace(0, 1, 11))
        for case in chain(test_cases_1, test_cases_2):
            mock_classifier = MockClassifier(predict_proba_return=case.input)
            np.testing.assert_almost_equal(
                modAL.uncertainty.classifier_margin(mock_classifier, np.random.rand(10)),
                case.output
            )

    def test_entropy(self):
        for n_samples in range(1, 100):
            for n_classes in range(1, 20):
                proba = np.zeros(shape=(n_samples, n_classes))
                for sample_idx in range(n_samples):
                    proba[sample_idx, np.random.choice(range(n_classes))] = 1.0

                classifier = MockClassifier(predict_proba_return=proba)
                np.testing.assert_equal(
                    modAL.uncertainty.classifier_entropy(classifier, np.random.rand(n_samples, 1)),
                    np.zeros(shape=(n_samples, ))
                )

class TestQueries(unittest.TestCase):

    def test_max_utility(self):
        for n_pool in range(2, 100):
            for n_instances in range(1, n_pool):
                utility = np.zeros(n_pool)
                max_idx = np.random.choice(range(n_pool), size=n_instances, replace=False)
                utility[max_idx] = 1.0
                np.testing.assert_equal(
                    np.sort(modAL.query.max_uncertainty(utility, n_instances)),
                    np.sort(max_idx)
                )

    def test_utility_weighted_random(self):
        for n_pool in range(2, 100):
            for n_instances in range(1, n_pool):
                utility = np.ones(n_pool)
                query_idx = modAL.query.uncertainty_weighted_random(utility, n_instances)
                # testing for correct number of returned indices
                np.testing.assert_equal(len(query_idx), n_instances)
                # testing for uniqueness of each query index
                np.testing.assert_equal(len(query_idx), len(np.unique(query_idx)))

class TestActiveLearner(unittest.TestCase):

    def test_calculate_utility(self):
        test_cases = (Test(array, array) for k in range(1, 10) for l in range(1, 10) for array in random_array((k, l), 100))
        for case in test_cases:
            mock_classifier = MockClassifier()
            learner = modAL.models.ActiveLearner(mock_classifier, MockUtility(case.input))
            np.testing.assert_almost_equal(
                learner.calculate_uncertainty(case.input),
                case.output
            )

    def test_query(self):
        pass

    def test_predict(self):
        pass

    def test_predict_proba(self):
        pass

    def test_score(self):
        test_cases = (np.random.rand() for _ in range(10))
        for score_return in test_cases:
            mock_classifier = MockClassifier(score_return=score_return)
            learner = modAL.models.ActiveLearner(mock_classifier, MockUtility(None))
            np.testing.assert_almost_equal(
                learner.score(np.random.rand(5, 2), np.random.rand(5, )),
                score_return
            )

    def test_keras(self):
        pass

    def test_sklearn(self):
        pass

class TestCommittee(unittest.TestCase):

    def test_calculate_utility(self):
        for n_learners in range(1, 200):
            utility = np.random.rand(100, n_learners)
            committee = modAL.models.Committee(
                learner_list=[MockActiveLearner(
                                    MockClassifier(classes_=np.asarray([0])),
                                    calculate_utility_return=utility[:, learner_idx].reshape(-1)
                              )
                              for learner_idx in range(n_learners)]
            )
            np.testing.assert_almost_equal(
                committee.calculate_uncertainty(np.random.rand(100, 1)),
                utility
            )

    def test_predict(self):
        for n_learners in range(1, 10):
            for n_instances in range(1, 10):
                prediction = np.random.randint(10, size=(n_instances, n_learners))
                committee = modAL.models.Committee(
                    learner_list=[MockActiveLearner(
                                          MockClassifier(classes_=np.asarray([0])),
                                          predict_return=prediction[:, learner_idx]
                                  )
                                  for learner_idx in range(n_learners)]
                )
                np.testing.assert_equal(
                    committee.vote(np.random.rand(n_instances, 5)),
                    prediction
                )

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before>from django.test import LiveServerTestCase
from selenium import webdriver

class MainPageTestCase(LiveServerTestCase):
	def setUp(self):
		self.selenium = webdriver.Firefox()

	def tearDown(self):
		self.selenium.quit()

	def test_new_meeting_addition(self):

		# User opens main page
		self.selenium.get('%s%s' % (self.live_server_url, '/meeting/'))

		# User tries adding new appointment(name, description and date)
		self.selenium.find_element_by_xpath('//input[@value="New appointment"]').click()

		name = 'New meeting'
		date = '01.01.2001 08:13'
		desc = 'Looking for company on mornign train'
		place = 'Klavdieve station'
		name_input = self.selenium.find_element_by_id('id_name')
		name_input.send_keys(name)
		date_input = self.selenium.find_element_by_id('id_date')
		date_input.send_keys(date)
		desc_input = self.selenium.find_element_by_id('id_desc')
		desc_input.send_keys(desc)
		place_input = self.selenium.find_element_by_id('id_place')
		place_input.send_keys(place)

		self.selenium.find_element_by_xpath('//input[@value="Add appointment"]').click()

		# User sees appointment page and see all info and plays to add comment
		self.assertRegex(self.selenium.current_url, '/meeting/\d+')
		page_text = self.selenium.find_element_by_tag_name('body').text
		self.assertIn(name, page_text)
		self.assertIn(date, page_text)
		self.assertIn(place, page_text)

		# New user connected
		self.selenium.quit()
		self.selenium = webdriver.Firefox()

		# new user sees main page
		self.selenium.get('%s%s' % (self.live_server_url, '/meeting/'))
		page_text = self.selenium.find_element_by_tag_name('body').text
		self.assertIn(name, page_text)
		self.assertIn(date, page_text)

		# he open the same appoitment and see all information and comment

		self.selenium.get('%s%s%d' % (self.live_server_url, '/meeting/', 1))
		page_text = self.selenium.find_element_by_tag_name('body').text
		self.assertIn(name, page_text)
		self.assertIn(date, page_text)
		self.assertIn(desc, page_text)
		self.assertIn(place, page_text)

		# Both users leave the page.
		self.selenium.quit()

<commit_msg>add FT for schedule view<commit_after>from django.test import LiveServerTestCase

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.keys import Keys

class MainPageTestCase(LiveServerTestCase):
	def setUp(self):
		self.selenium = webdriver.Firefox()

	def tearDown(self):
		self.selenium.quit()

	def test_schedule_view(self):

		# User opens main page
		self.selenium.get('%s%s' % (self.live_server_url, '/meeting/'))

		# User tries adding new appointment(name, description and date)
		self.selenium.find_element_by_partial_link_text('Suburban').click()
		self.selenium.implicitly_wait(1)

		from_station = 'Клавдиево'
		to_station = 'Святошин'

		from_input = self.selenium.find_element_by_id('id_from')
		from_input.send_keys(from_station)
		to_input = self.selenium.find_element_by_id('id_to')
		to_input.send_keys(to_station)
		from_input = self.selenium.find_element_by_id('id_from')

		self.fail("Finish test")
		row = WebDriverWait(self.selenium, 10).until(
			expected_conditions.presence_of_element_located((By.XPATH, '//table/tbody/tr/td[3]'))
		)

		new_meeting_button = self.selenium.find_element_by_xpath('//table/tbody/tr/td[3]/a')
		self.assertEqual(from_station, new_meeting_button.text)

		# Both users leave the page.
		self.selenium.quit()

<|endoftext|>


<commit_before>import numpy.testing as test
import numpy as np

from unittest import TestCase

from PyFVCOM.read_results import nodes2elems
from PyFVCOM.grid_tools import get_area, node_control_area, element_control_area, control_volumes

class GridToolsTest(TestCase):

    def setUp(self):
        """ Make a really simple unstructured grid of 8 elements as two rows of 4 elements. """
        self.x = np.array([0, 1, 0, 1, 0, 1, 2, 2, 2])
        self.y = np.array([0, 0, 1, 1, 2, 2, 0, 1, 2])
        self.tri = np.array([[0, 2, 1], [1, 2, 3], [2, 5, 3], [2, 4, 5], [1, 3, 7], [1, 7, 6], [3, 5, 7], [7, 5, 8]])
        self.xc = nodes2elems(self.x, self.tri)
        self.yc = nodes2elems(self.y, self.tri)
        self.z = np.array([0, 1, 1, 0, 2, 1, 2, 3, 3])

    def test_get_node_control_area(self):
        test_node_area = 2 / 3
        node = 1
        node_area = node_control_area(node, self.x, self.y, self.xc, self.yc, self.tri)
        test.assert_almost_equal(node_area, test_node_area)

    def test_get_element_control_area(self):
        test_element_area = 2
        node = 2
        art = get_area(np.asarray((self.x[self.tri[:, 0]], self.y[self.tri[:, 0]])).T,
                       np.asarray((self.x[self.tri[:, 1]], self.y[self.tri[:, 1]])).T,
                       np.asarray((self.x[self.tri[:, 2]], self.y[self.tri[:, 2]])).T)
        element_area = element_control_area(node, self.tri, art)
        test.assert_almost_equal(element_area, test_element_area)

    def test_get_control_volumes(self):
        test_node_areas = [1 / 6, 2 / 3, 2 / 3,
                           2 / 3, 1 / 6, 2 / 3,
                           1 / 6, 2 / 3, 1 / 6]
        test_element_areas = [0.5, 2, 2, 2, 0.5, 2, 0.5, 2, 0.5]
        node_areas, element_areas = control_volumes(self.x, self.y, self.tri)<commit_msg>Add missing test comparisons.<commit_after>import numpy.testing as test
import numpy as np

from unittest import TestCase

from PyFVCOM.read_results import nodes2elems
from PyFVCOM.grid_tools import get_area, node_control_area, element_control_area, control_volumes

class GridToolsTest(TestCase):

    def setUp(self):
        """ Make a really simple unstructured grid of 8 elements as two rows of 4 elements. """
        self.x = np.array([0, 1, 0, 1, 0, 1, 2, 2, 2])
        self.y = np.array([0, 0, 1, 1, 2, 2, 0, 1, 2])
        self.tri = np.array([[0, 2, 1], [1, 2, 3], [2, 5, 3], [2, 4, 5], [1, 3, 7], [1, 7, 6], [3, 5, 7], [7, 5, 8]])
        self.xc = nodes2elems(self.x, self.tri)
        self.yc = nodes2elems(self.y, self.tri)
        self.z = np.array([0, 1, 1, 0, 2, 1, 2, 3, 3])

    def test_get_node_control_area(self):
        test_node_area = 2 / 3
        node = 1
        node_area = node_control_area(node, self.x, self.y, self.xc, self.yc, self.tri)
        test.assert_almost_equal(node_area, test_node_area)

    def test_get_element_control_area(self):
        test_element_area = 2
        node = 2
        art = get_area(np.asarray((self.x[self.tri[:, 0]], self.y[self.tri[:, 0]])).T,
                       np.asarray((self.x[self.tri[:, 1]], self.y[self.tri[:, 1]])).T,
                       np.asarray((self.x[self.tri[:, 2]], self.y[self.tri[:, 2]])).T)
        element_area = element_control_area(node, self.tri, art)
        test.assert_almost_equal(element_area, test_element_area)

    def test_get_control_volumes(self):
        test_node_areas = [1 / 6, 2 / 3, 2 / 3,
                           2 / 3, 1 / 6, 2 / 3,
                           1 / 6, 2 / 3, 1 / 6]
        test_element_areas = [0.5, 2, 2, 2, 0.5, 2, 0.5, 2, 0.5]
        node_areas, element_areas = control_volumes(self.x, self.y, self.tri)        test.assert_almost_equal(node_areas, test_node_areas)
        test.assert_almost_equal(element_areas, test_element_areas)
<|endoftext|>


<commit_before>import unittest

from heal import Heal

class HealTests(unittest.TestCase):
    def test_str(self):
        heal_amount = 5.111
        heal = Heal(heal_amount=heal_amount)
        expected_str = f'{heal_amount:.2f}'

        self.assertEqual(str(heal), expected_str)

    def test_add(self):
        """ __add__ takes a numeric type (int/float) """
        heal_amount = 5
        heal = Heal(heal_amount=heal_amount)

        add_amount = 10
        expected_amount = heal_amount + add_amount

        self.assertEqual(heal + add_amount, expected_amount)

    def test_radd(self):
        heal_amount = 5
        heal = Heal(heal_amount=heal_amount)

        add_amount = 10
        expected_amount = heal_amount + add_amount

        add_amount += heal
        self.assertEqual(add_amount, expected_amount)

    def test_iadd(self):
        heal_amount = 5
        heal = Heal(heal_amount=heal_amount)
        add_amount = 10

        expected_heal = Heal(heal_amount=heal_amount + add_amount)

        heal += add_amount
        self.assertEqual(heal, expected_heal)

if __name__ == '__main__':
    unittest.main()
<commit_msg>Test for Heal __sub__<commit_after>import unittest

from heal import Heal

class HealTests(unittest.TestCase):
    def test_str(self):
        heal_amount = 5.111
        heal = Heal(heal_amount=heal_amount)
        expected_str = f'{heal_amount:.2f}'

        self.assertEqual(str(heal), expected_str)

    def test_add(self):
        """ __add__ takes a numeric type (int/float) """
        heal_amount = 5
        heal = Heal(heal_amount=heal_amount)

        add_amount = 10
        expected_amount = heal_amount + add_amount

        self.assertEqual(heal + add_amount, expected_amount)

    def test_radd(self):
        heal_amount = 5
        heal = Heal(heal_amount=heal_amount)

        add_amount = 10
        expected_amount = heal_amount + add_amount

        add_amount += heal
        self.assertEqual(add_amount, expected_amount)

    def test_iadd(self):
        heal_amount = 5
        heal = Heal(heal_amount=heal_amount)
        add_amount = 10

        expected_heal = Heal(heal_amount=heal_amount + add_amount)

        heal += add_amount
        self.assertEqual(heal, expected_heal)

    def test_sub(self):
        heal_amount = 5
        heal = Heal(heal_amount=heal_amount)
        sub_amount = 3

        expected_amount = 2

        self.assertEqual(heal - sub_amount, expected_amount)

if __name__ == '__main__':
    unittest.main()
<|endoftext|>