<commit_before>import __builtin__
import textwrap
import unittest
import warnings
from StringIO import StringIO

from jinja2.loaders import DictLoader
from webassets import Environment

from blueberrypy.config import BlueberryPyConfiguration
from blueberrypy.exc import BlueberryPyConfigurationError, BlueberryPyNotConfiguredError

# dummy controllers
import cherrypy

class Root(object):

    def index(self):
        return "hello world!"
    index.exposed = True

class DummyRestController(object):

    def dummy(self, **kwargs):
        return "hello world!"

rest_controller = cherrypy.dispatch.RoutesDispatcher()
rest_controller.connect("dummy", "/dummy", DummyRestController, action="dummy")

class BlueberryPyConfigurationTest(unittest.TestCase):

    def setUp(self):
        self.basic_valid_app_config = {"controllers": {"controller": Root,
                                                       "rest_controller": rest_controller}}

    def test_validate(self):
        self.assertRaisesRegexp(BlueberryPyNotConfiguredError,
                                "BlueberryPy application configuration not found.",
                                BlueberryPyConfiguration)

    def test_config_file_paths(self):

        import os.path
        old_exists = os.path.exists

        def proxied_exists(path):
            if path == "/tmp/dev/app.yml":
                return True
            elif path == "/tmp/dev/bundles.yml":
                return True
            elif path == "/tmp/dev/logging.yml":
                return True
            return old_exists(path)

        os.path.exists = proxied_exists

        old_open = __builtin__.open

        class FakeFile(StringIO):

            def __enter__(self):
                return self
            def __exit__(self, exc_type=None, exc_value=None, traceback=None):
                return False

        def proxied_open(filename, mode='r', buffering=1):
            if filename == "/tmp/dev/app.yml":
                return FakeFile()
            elif filename == "/tmp/dev/bundles.yml":
                return FakeFile(textwrap.dedent("""
                directory: /tmp
                url: /
                """))
            elif filename == "/tmp/dev/logging.yml":
                return FakeFile()
            else:
                return old_open(filename, mode, buffering)

        __builtin__.open = proxied_open

        old_validate = BlueberryPyConfiguration.validate
        try:
            BlueberryPyConfiguration.validate = lambda self: None

            config = BlueberryPyConfiguration(config_dir="/tmp")
            config_file_paths = config.config_file_paths
            self.assertEqual(len(config_file_paths), 3)
            self.assertEqual(config_file_paths[0], "/tmp/dev/app.yml")
            self.assertEqual(config_file_paths[1], "/tmp/dev/bundles.yml")
            self.assertEqual(config_file_paths[2], "/tmp/dev/logging.yml")
        finally:
            BlueberryPyConfiguration.validate = old_validate
            os.path.exists = old_exists
            __builtin__.open = old_open

    def test_use_email(self):
        app_config = self.basic_valid_app_config.copy()
        app_config.update({"email": {}})

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("error")
            self.assertRaisesRegexp(UserWarning,
                                    "BlueberryPy email configuration is empty.",
                                    callable_obj=BlueberryPyConfiguration,
                                    app_config=app_config)

        app_config.update({"email": {"debug": 1}})
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("error")
            self.assertRaisesRegexp(UserWarning,
                                    "Unknown key 'debug' found for \[email\]. Did you mean 'debuglevel'?",
                                    callable_obj=BlueberryPyConfiguration,
                                    app_config=app_config)

        app_config.update({"email": {"host": "localhost",
                                     "port": 1025}})
        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertTrue(config.use_email)

    def test_use_redis(self):
        app_config = self.basic_valid_app_config.copy()
        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertFalse(config.use_redis)

        app_config.update({"/": {"tools.sessions.storage_type": "redis"}})

        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertTrue(config.use_redis)

    def test_use_sqlalchemy(self):
        app_config = self.basic_valid_app_config.copy()
        app_config.update({"global": {"engine.sqlalchemy.on": True}})

        self.assertRaisesRegexp(BlueberryPyNotConfiguredError,
                                "SQLAlchemy configuration not found.",
                                callable_obj=BlueberryPyConfiguration,
                                app_config=app_config)

        app_config.update({"global": {"engine.sqlalchemy.on": True},
                           "sqlalchemy_engine": {"url": "sqlite://"}})

        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertTrue(config.use_sqlalchemy)

        app_config.update({"global": {"engine.sqlalchemy.on": True},
                           "sqlalchemy_engine_Model": {"url": "sqlite://"}})

        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertTrue(config.use_sqlalchemy)

    def test_use_jinja2(self):
        app_config = self.basic_valid_app_config.copy()
        app_config.update({"jinja2": {}})
        self.assertRaisesRegexp(BlueberryPyNotConfiguredError,
                                "Jinja2 configuration not found.",
                                callable_obj=BlueberryPyConfiguration,
                                app_config=app_config)

        app_config.update({"jinja2": {"loader": DictLoader({})}})
        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertTrue(config.use_jinja2)

    def test_use_webassets(self):
        app_config = self.basic_valid_app_config.copy()
        app_config.update({"jinja2": {"use_webassets": True,
                                      "loader": DictLoader({})}})
        self.assertRaisesRegexp(BlueberryPyNotConfiguredError,
                                "Webassets configuration not found.",
                                callable_obj=BlueberryPyConfiguration,
                                app_config=app_config)

        webassets_env = Environment("/tmp", "/")
        self.assertRaisesRegexp(BlueberryPyNotConfiguredError,
                                "No bundles found in webassets env.",
                                callable_obj=BlueberryPyConfiguration,
                                app_config=app_config,
                                webassets_env=webassets_env)

        webassets_env = Environment("/tmp", "/")
        webassets_env.register("js", "dummy.js", "dummy2.js", output="dummy.js")
        config = BlueberryPyConfiguration(app_config=app_config,
                                          webassets_env=webassets_env)
        self.assertTrue(config.use_webassets)

    def test_use_controller(self):
        class Root(object):
            def index(self):
                return "hello world!"

        app_config = {"controllers": {"controller": Root}}
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("error")
            self.assertRaisesRegexp(UserWarning,
                                    "controller has no exposed method.",
                                    callable_obj=BlueberryPyConfiguration,
                                    app_config=app_config)

        class Root(object):
            def index(self):
                return "hello world!"
            index.exposed = True

        app_config = {"controllers": {"controller": Root}}
        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertTrue(config.use_controller)

    def test_use_rest_controller(self):
        rest_controller = cherrypy.dispatch.RoutesDispatcher()

        app_config = {"controllers": {"rest_controller": rest_controller}}
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("error")
            self.assertRaisesRegexp(UserWarning,
                                    "rest_controller has no connected routes.",
                                    callable_obj=BlueberryPyConfiguration,
                                    app_config=app_config)

        app_config = {"controllers": {"rest_controller": object()}}
        self.assertRaisesRegexp(BlueberryPyConfigurationError,
                                "rest_controller must be an instance of cherrypy.dispatch.RoutesDispatcher.",
                                callable_obj=BlueberryPyConfiguration,
                                app_config=app_config)

        class DummyRestController(object):
            def dummy(self, **kwargs):
                return "hello world!"

        rest_controller.connect("dummy", "/dummy", DummyRestController, action="dummy")
        app_config = {"controllers": {"rest_controller": rest_controller}}

        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertTrue(config.use_rest_controller)

    def test_jinja2_config(self):
        app_config = self.basic_valid_app_config.copy()
        dict_loader = DictLoader({})
        app_config.update({"jinja2": {"loader": dict_loader,
                                      "use_webassets": True}})

        webassets_env = Environment("/tmp", "/")
        webassets_env.register("js", "dummy.js", "dummy2.js", output="dummy.js")
        config = BlueberryPyConfiguration(app_config=app_config,
                                          webassets_env=webassets_env)
        self.assertEqual(config.jinja2_config, {"loader": dict_loader})

    def test_sqlalchemy_config(self):
        app_config = self.basic_valid_app_config.copy()
        app_config.update({"global": {"engine.sqlalchemy.on": True},
                           "sqlalchemy_engine": {"url": "sqlite://"}})

        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertEqual(config.sqlalchemy_config, {"sqlalchemy_engine": {"url": "sqlite://"}})

        app_config = self.basic_valid_app_config.copy()
        app_config.update({"global": {"engine.sqlalchemy.on": True},
                           "sqlalchemy_engine_Model1": {"url": "sqlite://"},
                           "sqlalchemy_engine_Model2": {"url": "sqlite://"}})

        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertEqual(config.sqlalchemy_config, {"sqlalchemy_engine_Model1": {"url": "sqlite://"},
                                                    "sqlalchemy_engine_Model2": {"url": "sqlite://"}})
<commit_msg>formatting and comments<commit_after>import __builtin__
import textwrap
import unittest
import warnings
from StringIO import StringIO

from jinja2.loaders import DictLoader
from webassets import Environment

from blueberrypy.config import BlueberryPyConfiguration
from blueberrypy.exc import BlueberryPyConfigurationError, BlueberryPyNotConfiguredError

# dummy controllers
import cherrypy

class Root(object):

    def index(self):
        return "hello world!"
    index.exposed = True

class DummyRestController(object):

    def dummy(self, **kwargs):
        return "hello world!"

rest_controller = cherrypy.dispatch.RoutesDispatcher()
rest_controller.connect("dummy", "/dummy", DummyRestController, action="dummy")

class BlueberryPyConfigurationTest(unittest.TestCase):

    def setUp(self):
        self.basic_valid_app_config = {"controllers": {"controller": Root,
                                                       "rest_controller": rest_controller}}

    def test_validate(self):
        self.assertRaisesRegexp(BlueberryPyNotConfiguredError,
                                "BlueberryPy application configuration not found.",
                                BlueberryPyConfiguration)

    def test_config_file_paths(self):
        # stub out os.path.exists
        import os.path
        old_exists = os.path.exists
        def proxied_exists(path):
            if path == "/tmp/dev/app.yml":
                return True
            elif path == "/tmp/dev/bundles.yml":
                return True
            elif path == "/tmp/dev/logging.yml":
                return True
            return old_exists(path)
        os.path.exists = proxied_exists
        old_open = __builtin__.open

        # stub out open
        class FakeFile(StringIO):
            def __enter__(self):
                return self
            def __exit__(self, exc_type=None, exc_value=None, traceback=None):
                return False
        def proxied_open(filename, mode='r', buffering=1):
            if filename == "/tmp/dev/app.yml":
                return FakeFile()
            elif filename == "/tmp/dev/bundles.yml":
                return FakeFile(textwrap.dedent("""
                directory: /tmp
                url: /
                """))
            elif filename == "/tmp/dev/logging.yml":
                return FakeFile()
            else:
                return old_open(filename, mode, buffering)
        __builtin__.open = proxied_open

        # stub out validate()
        old_validate = BlueberryPyConfiguration.validate
        BlueberryPyConfiguration.validate = lambda self: None

        try:
            config = BlueberryPyConfiguration(config_dir="/tmp")
            config_file_paths = config.config_file_paths
            self.assertEqual(len(config_file_paths), 3)
            self.assertEqual(config_file_paths[0], "/tmp/dev/app.yml")
            self.assertEqual(config_file_paths[1], "/tmp/dev/bundles.yml")
            self.assertEqual(config_file_paths[2], "/tmp/dev/logging.yml")
        finally:
            BlueberryPyConfiguration.validate = old_validate
            os.path.exists = old_exists
            __builtin__.open = old_open

    def test_use_email(self):
        app_config = self.basic_valid_app_config.copy()
        app_config.update({"email": {}})

        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("error")
            self.assertRaisesRegexp(UserWarning,
                                    "BlueberryPy email configuration is empty.",
                                    callable_obj=BlueberryPyConfiguration,
                                    app_config=app_config)

        app_config.update({"email": {"debug": 1}})
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("error")
            self.assertRaisesRegexp(UserWarning,
                                    "Unknown key 'debug' found for \[email\]. Did you mean 'debuglevel'?",
                                    callable_obj=BlueberryPyConfiguration,
                                    app_config=app_config)

        app_config.update({"email": {"host": "localhost",
                                     "port": 1025}})
        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertTrue(config.use_email)

    def test_use_redis(self):
        app_config = self.basic_valid_app_config.copy()
        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertFalse(config.use_redis)

        app_config.update({"/": {"tools.sessions.storage_type": "redis"}})

        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertTrue(config.use_redis)

    def test_use_sqlalchemy(self):
        app_config = self.basic_valid_app_config.copy()
        app_config.update({"global": {"engine.sqlalchemy.on": True}})

        self.assertRaisesRegexp(BlueberryPyNotConfiguredError,
                                "SQLAlchemy configuration not found.",
                                callable_obj=BlueberryPyConfiguration,
                                app_config=app_config)

        app_config.update({"global": {"engine.sqlalchemy.on": True},
                           "sqlalchemy_engine": {"url": "sqlite://"}})

        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertTrue(config.use_sqlalchemy)

        app_config.update({"global": {"engine.sqlalchemy.on": True},
                           "sqlalchemy_engine_Model": {"url": "sqlite://"}})

        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertTrue(config.use_sqlalchemy)

    def test_use_jinja2(self):
        app_config = self.basic_valid_app_config.copy()
        app_config.update({"jinja2": {}})
        self.assertRaisesRegexp(BlueberryPyNotConfiguredError,
                                "Jinja2 configuration not found.",
                                callable_obj=BlueberryPyConfiguration,
                                app_config=app_config)

        app_config.update({"jinja2": {"loader": DictLoader({})}})
        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertTrue(config.use_jinja2)

    def test_use_webassets(self):
        app_config = self.basic_valid_app_config.copy()
        app_config.update({"jinja2": {"use_webassets": True,
                                      "loader": DictLoader({})}})
        self.assertRaisesRegexp(BlueberryPyNotConfiguredError,
                                "Webassets configuration not found.",
                                callable_obj=BlueberryPyConfiguration,
                                app_config=app_config)

        webassets_env = Environment("/tmp", "/")
        self.assertRaisesRegexp(BlueberryPyNotConfiguredError,
                                "No bundles found in webassets env.",
                                callable_obj=BlueberryPyConfiguration,
                                app_config=app_config,
                                webassets_env=webassets_env)

        webassets_env = Environment("/tmp", "/")
        webassets_env.register("js", "dummy.js", "dummy2.js", output="dummy.js")
        config = BlueberryPyConfiguration(app_config=app_config,
                                          webassets_env=webassets_env)
        self.assertTrue(config.use_webassets)

    def test_use_controller(self):
        class Root(object):
            def index(self):
                return "hello world!"

        app_config = {"controllers": {"controller": Root}}
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("error")
            self.assertRaisesRegexp(UserWarning,
                                    "controller has no exposed method.",
                                    callable_obj=BlueberryPyConfiguration,
                                    app_config=app_config)

        class Root(object):
            def index(self):
                return "hello world!"
            index.exposed = True

        app_config = {"controllers": {"controller": Root}}
        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertTrue(config.use_controller)

    def test_use_rest_controller(self):
        rest_controller = cherrypy.dispatch.RoutesDispatcher()

        app_config = {"controllers": {"rest_controller": rest_controller}}
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("error")
            self.assertRaisesRegexp(UserWarning,
                                    "rest_controller has no connected routes.",
                                    callable_obj=BlueberryPyConfiguration,
                                    app_config=app_config)

        app_config = {"controllers": {"rest_controller": object()}}
        self.assertRaisesRegexp(BlueberryPyConfigurationError,
                                "rest_controller must be an instance of cherrypy.dispatch.RoutesDispatcher.",
                                callable_obj=BlueberryPyConfiguration,
                                app_config=app_config)

        class DummyRestController(object):
            def dummy(self, **kwargs):
                return "hello world!"

        rest_controller.connect("dummy", "/dummy", DummyRestController, action="dummy")
        app_config = {"controllers": {"rest_controller": rest_controller}}

        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertTrue(config.use_rest_controller)

    def test_jinja2_config(self):
        app_config = self.basic_valid_app_config.copy()
        dict_loader = DictLoader({})
        app_config.update({"jinja2": {"loader": dict_loader,
                                      "use_webassets": True}})

        webassets_env = Environment("/tmp", "/")
        webassets_env.register("js", "dummy.js", "dummy2.js", output="dummy.js")
        config = BlueberryPyConfiguration(app_config=app_config,
                                          webassets_env=webassets_env)
        self.assertEqual(config.jinja2_config, {"loader": dict_loader})

    def test_sqlalchemy_config(self):
        app_config = self.basic_valid_app_config.copy()
        app_config.update({"global": {"engine.sqlalchemy.on": True},
                           "sqlalchemy_engine": {"url": "sqlite://"}})

        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertEqual(config.sqlalchemy_config, {"sqlalchemy_engine": {"url": "sqlite://"}})

        app_config = self.basic_valid_app_config.copy()
        app_config.update({"global": {"engine.sqlalchemy.on": True},
                           "sqlalchemy_engine_Model1": {"url": "sqlite://"},
                           "sqlalchemy_engine_Model2": {"url": "sqlite://"}})

        config = BlueberryPyConfiguration(app_config=app_config)
        self.assertEqual(config.sqlalchemy_config, {"sqlalchemy_engine_Model1": {"url": "sqlite://"},
                                                    "sqlalchemy_engine_Model2": {"url": "sqlite://"}})
<|endoftext|>


<commit_before>import os
import tempfile
import textwrap
from tests.test_pip import reset_env, run_pip, clear_environ, write_file

def test_options_from_env_vars():
    """
    Test if ConfigOptionParser reads env vars (e.g. not using PyPI here)

    """
    environ = clear_environ(os.environ.copy())
    environ['PIP_NO_INDEX'] = '1'
    reset_env(environ)
    result = run_pip('install', '-vvv', 'INITools', expect_error=True)
    assert "Ignoring indexes:" in result.stdout, str(result)
    assert "DistributionNotFound: No distributions at all found for INITools" in result.stdout

def test_command_line_options_override_env_vars():
    """
    Test that command line options override environmental variables.

    """
    environ = clear_environ(os.environ.copy())
    environ['PIP_INDEX_URL'] = 'http://b.pypi.python.org/simple/'
    reset_env(environ)
    result = run_pip('install', '-vvv', 'INITools', expect_error=True)
    assert "Getting page http://b.pypi.python.org/simple/INITools" in result.stdout
    reset_env(environ)
    result = run_pip('install', '-vvv', '--index-url', 'http://download.zope.org/ppix', 'INITools', expect_error=True)
    assert "b.pypi.python.org" not in result.stdout
    assert "Getting page http://download.zope.org/ppix" in result.stdout

def test_env_vars_override_config_file():
    """
    Test that environmental variables override settings in config files.

    """
    fd, config_file = tempfile.mkstemp('-pip.cfg', 'test-')
    try:
        _test_env_vars_override_config_file(config_file)
    finally:
        # `os.close` is a workaround for a bug in subprocess
        # http://bugs.python.org/issue3210
        os.close(fd)
        os.remove(config_file)

def _test_env_vars_override_config_file(config_file):
    environ = clear_environ(os.environ.copy())
    environ['PIP_CONFIG_FILE'] = config_file # set this to make pip load it
    reset_env(environ)
    # It's important that we test this particular config value ('no-index')
    # because their is/was a bug which only shows up in cases in which
    # 'config-item' and 'config_item' hash to the same value modulo the size
    # of the config dictionary.
    write_file(config_file, textwrap.dedent("""\
        [global]
        no-index = 1
        """))
    result = run_pip('install', '-vvv', 'INITools', expect_error=True)
    assert "DistributionNotFound: No distributions at all found for INITools" in result.stdout
    environ['PIP_NO_INDEX'] = '0'
    reset_env(environ)
    result = run_pip('install', '-vvv', 'INITools', expect_error=True)
    assert "Successfully installed INITools" in result.stdout

def test_command_line_append_flags():
    """
    Test command line flags that append to defaults set by environmental variables.

    """
    environ = clear_environ(os.environ.copy())
    environ['PIP_FIND_LINKS'] = 'http://pypi.pinaxproject.com'
    reset_env(environ)
    result = run_pip('install', '-vvv', 'INITools', expect_error=True)
    assert "Analyzing links from page http://pypi.pinaxproject.com" in result.stdout
    reset_env(environ)
    result = run_pip('install', '-vvv', '--find-links', 'http://example.com', 'INITools', expect_error=True)
    assert "Analyzing links from page http://pypi.pinaxproject.com" in result.stdout
    assert "Analyzing links from page http://example.com" in result.stdout

def test_command_line_appends_correctly():
    """
    Test multiple appending options set by environmental variables.

    """
    environ = clear_environ(os.environ.copy())
    environ['PIP_FIND_LINKS'] = 'http://pypi.pinaxproject.com http://example.com'
    reset_env(environ)
    result = run_pip('install', '-vvv', 'INITools', expect_error=True)

    assert "Analyzing links from page http://pypi.pinaxproject.com" in result.stdout, result.stdout
    assert "Analyzing links from page http://example.com" in result.stdout, result.stdout

def test_config_file_override_stack():
    """
    Test config files (global, overriding a global config with a
    local, overriding all with a command line flag).

    """
    fd, config_file = tempfile.mkstemp('-pip.cfg', 'test-')
    try:
        _test_config_file_override_stack(config_file)
    finally:
        # `os.close` is a workaround for a bug in subprocess
        # http://bugs.python.org/issue3210
        os.close(fd)
        os.remove(config_file)

def _test_config_file_override_stack(config_file):
    environ = clear_environ(os.environ.copy())
    environ['PIP_CONFIG_FILE'] = config_file # set this to make pip load it
    reset_env(environ)
    write_file(config_file, textwrap.dedent("""\
        [global]
        index-url = http://download.zope.org/ppix
        """))
    result = run_pip('install', '-vvv', 'INITools', expect_error=True)
    assert "Getting page http://download.zope.org/ppix/INITools" in result.stdout
    reset_env(environ)
    write_file(config_file, textwrap.dedent("""\
        [global]
        index-url = http://download.zope.org/ppix
        [install]
        index-url = http://pypi.appspot.com/
        """))
    result = run_pip('install', '-vvv', 'INITools', expect_error=True)
    assert "Getting page http://pypi.appspot.com/INITools" in result.stdout
    result = run_pip('install', '-vvv', '--index-url', 'http://pypi.python.org/simple', 'INITools', expect_error=True)
    assert "Getting page http://download.zope.org/ppix/INITools" not in result.stdout
    assert "Getting page http://pypi.appspot.com/INITools" not in result.stdout
    assert "Getting page http://pypi.python.org/simple/INITools" in result.stdout

def test_log_file_no_directory():
    """
    Test opening a log file with no directory name.

    """
    from pip.basecommand import open_logfile
    fp = open_logfile('testpip.log')
    fp.write('can write')
    fp.close()
    assert os.path.exists(fp.name)
    os.remove(fp.name)
<commit_msg>misc test fixes<commit_after>import os
import tempfile
import textwrap
from tests.test_pip import reset_env, run_pip, clear_environ, write_file, path_to_url, here

def test_options_from_env_vars():
    """
    Test if ConfigOptionParser reads env vars (e.g. not using PyPI here)

    """
    environ = clear_environ(os.environ.copy())
    environ['PIP_NO_INDEX'] = '1'
    reset_env(environ)
    result = run_pip('install', '-vvv', 'INITools', expect_error=True)
    assert "Ignoring indexes:" in result.stdout, str(result)
    assert "DistributionNotFound: No distributions at all found for INITools" in result.stdout

def test_command_line_options_override_env_vars():
    """
    Test that command line options override environmental variables.

    """
    environ = clear_environ(os.environ.copy())
    environ['PIP_INDEX_URL'] = 'http://b.pypi.python.org/simple/'
    reset_env(environ)
    result = run_pip('install', '-vvv', 'INITools', expect_error=True)
    assert "Getting page http://b.pypi.python.org/simple/INITools" in result.stdout
    reset_env(environ)
    result = run_pip('install', '-vvv', '--index-url', 'http://download.zope.org/ppix', 'INITools', expect_error=True)
    assert "b.pypi.python.org" not in result.stdout
    assert "Getting page http://download.zope.org/ppix" in result.stdout

def test_env_vars_override_config_file():
    """
    Test that environmental variables override settings in config files.

    """
    fd, config_file = tempfile.mkstemp('-pip.cfg', 'test-')
    try:
        _test_env_vars_override_config_file(config_file)
    finally:
        # `os.close` is a workaround for a bug in subprocess
        # http://bugs.python.org/issue3210
        os.close(fd)
        os.remove(config_file)

def _test_env_vars_override_config_file(config_file):
    environ = clear_environ(os.environ.copy())
    environ['PIP_CONFIG_FILE'] = config_file # set this to make pip load it
    reset_env(environ)
    # It's important that we test this particular config value ('no-index')
    # because their is/was a bug which only shows up in cases in which
    # 'config-item' and 'config_item' hash to the same value modulo the size
    # of the config dictionary.
    write_file(config_file, textwrap.dedent("""\
        [global]
        no-index = 1
        """))
    result = run_pip('install', '-vvv', 'INITools', expect_error=True)
    assert "DistributionNotFound: No distributions at all found for INITools" in result.stdout
    environ['PIP_NO_INDEX'] = '0'
    reset_env(environ)
    result = run_pip('install', '-vvv', 'INITools', expect_error=True)
    assert "Successfully installed INITools" in result.stdout

def test_command_line_append_flags():
    """
    Test command line flags that append to defaults set by environmental variables.

    """
    environ = clear_environ(os.environ.copy())
    environ['PIP_FIND_LINKS'] = 'http://pypi.pinaxproject.com'
    reset_env(environ)
    result = run_pip('install', '-vvv', 'INITools', expect_error=True)
    assert "Analyzing links from page http://pypi.pinaxproject.com" in result.stdout
    reset_env(environ)
    find_links = path_to_url(os.path.join(here, 'packages'))
    result = run_pip('install', '-vvv', '--find-links', find_links, 'INITools', expect_error=True)
    assert "Analyzing links from page http://pypi.pinaxproject.com" in result.stdout
    assert "Skipping link %s" % find_links in result.stdout

def test_command_line_appends_correctly():
    """
    Test multiple appending options set by environmental variables.

    """
    environ = clear_environ(os.environ.copy())
    find_links = path_to_url(os.path.join(here, 'packages'))
    environ['PIP_FIND_LINKS'] = 'http://pypi.pinaxproject.com %s' % find_links
    reset_env(environ)
    result = run_pip('install', '-vvv', 'INITools', expect_error=True)

    assert "Analyzing links from page http://pypi.pinaxproject.com" in result.stdout, result.stdout
    assert "Skipping link %s" % find_links in result.stdout

def test_config_file_override_stack():
    """
    Test config files (global, overriding a global config with a
    local, overriding all with a command line flag).

    """
    fd, config_file = tempfile.mkstemp('-pip.cfg', 'test-')
    try:
        _test_config_file_override_stack(config_file)
    finally:
        # `os.close` is a workaround for a bug in subprocess
        # http://bugs.python.org/issue3210
        os.close(fd)
        os.remove(config_file)

def _test_config_file_override_stack(config_file):
    environ = clear_environ(os.environ.copy())
    environ['PIP_CONFIG_FILE'] = config_file # set this to make pip load it
    reset_env(environ)
    write_file(config_file, textwrap.dedent("""\
        [global]
        index-url = http://download.zope.org/ppix
        """))
    result = run_pip('install', '-vvv', 'INITools', expect_error=True)
    assert "Getting page http://download.zope.org/ppix/INITools" in result.stdout
    reset_env(environ)
    write_file(config_file, textwrap.dedent("""\
        [global]
        index-url = http://download.zope.org/ppix
        [install]
        index-url = http://pypi.appspot.com/
        """))
    result = run_pip('install', '-vvv', 'INITools', expect_error=True)
    assert "Getting page http://pypi.appspot.com/INITools" in result.stdout
    result = run_pip('install', '-vvv', '--index-url', 'http://pypi.python.org/simple', 'INITools', expect_error=True)
    assert "Getting page http://download.zope.org/ppix/INITools" not in result.stdout
    assert "Getting page http://pypi.appspot.com/INITools" not in result.stdout
    assert "Getting page http://pypi.python.org/simple/INITools" in result.stdout

def test_log_file_no_directory():
    """
    Test opening a log file with no directory name.

    """
    from pip.basecommand import open_logfile
    fp = open_logfile('testpip.log')
    fp.write('can write')
    fp.close()
    assert os.path.exists(fp.name)
    os.remove(fp.name)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import unittest
from unittest import mock
from toolset.corpus import Corpus
import joblib

class CorpusTest(unittest.TestCase):

    def setUp(self):
        self.filepath_dict = dict()
        for i in range(3):
            with open('file{}'.format(i), 'w+') as file_:
                file_.write('This is a test string.')
            self.filepath_dict[i] = 'file{}'.format(i)

        with open('file3', 'w+') as file_:
            file_.write('This is a longer test string.')
        self.filepath_dict[3] = 'file3'.format(i)

        joblib.dump(self.filepath_dict, 'filepath_dict')

        self.corpus = Corpus('filepath_dict')

    def test_corpus_document_generator(self):
        for doc in self.corpus.document_generator():
            self.assertTrue(doc == 'This is a test string.'
                    or doc == 'This is a longer test string.')

    def test_corpus_stats(self):
        stat_log = self.corpus.stats()
        self.assertEqual(stat_log['n_docs'], 4)
        self.assertEqual(stat_log['max_doc_size'], 7) # Dot is counterd a word.
        self.assertEqual(stat_log['min_doc_size'], 6)
        self.assertEqual(stat_log['avg_doc_size'], 6.2)

if __name__ == '__main__':
    unittest.main()
<commit_msg>Add tearDown method.<commit_after># -*- coding: utf-8 -*-
import os
import unittest
from unittest import mock
from toolset.corpus import Corpus
import joblib

class CorpusTest(unittest.TestCase):

    def setUp(self):
        self.filepath_dict = dict()
        for i in range(3):
            with open('file{}'.format(i), 'w+') as file_:
                file_.write('This is a test string.')
            self.filepath_dict[i] = 'file{}'.format(i)

        with open('file3', 'w+') as file_:
            file_.write('This is a longer test string.')
        self.filepath_dict[3] = 'file3'.format(i)

        joblib.dump(self.filepath_dict, 'filepath_dict')

        self.corpus = Corpus('filepath_dict')

    def test_corpus_document_generator(self):
        for doc in self.corpus.document_generator():
            self.assertTrue(doc == 'This is a test string.'
                    or doc == 'This is a longer test string.')

    def test_corpus_stats(self):
        stat_log = self.corpus.stats()
        self.assertEqual(stat_log['n_docs'], 4)
        self.assertEqual(stat_log['max_doc_size'], 7) # Dot is counterd a word.
        self.assertEqual(stat_log['min_doc_size'], 6)
        self.assertEqual(stat_log['avg_doc_size'], 6.2)

    def tearDown(self):
        for i in range(4):
            os.remove('file{}'.format(i))
        os.remove('filepath_dict')

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


<commit_before>import os
import json
import shutil
import functools
from unittest.mock import patch

from blgr.blgr import Create

def input_mock(ret_val, prompt):
    return ret_val

def test_prepare():
    create = Create()

    input_y = functools.partial(input_mock, 'y')
    input_n = functools.partial(input_mock, 'n')
    input_enter = functools.partial(input_mock, '')

    with patch.object(create, '_ask_post_meta', return_value=None) as mock_user_interaction:
        create.prepare(input_n)
        create.prepare(input_y)
    mock_user_interaction.assert_called_once_with()

    with patch.object(create, '_ask_post_meta', return_value=None) as mock_user_interaction:
        create.prepare(input_enter)  # default behaviour
    assert not mock_user_interaction.called

def test_metadata():
    create = Create()

    input_y = functools.partial(input_mock, 'y')
    input_n = functools.partial(input_mock, 'n')
    input_enter = functools.partial(input_mock, '')
    txt_data = 'post txt data'
    input_txt = functools.partial(input_mock, txt_data)

    create._ask_post_meta(input_txt, set_link_input=input_y, comments_input=input_y)
    assert create.post_data['title'] == txt_data
    assert create.post_data['slug'] == txt_data
    assert create.post_data['category'] == txt_data
    assert create.post_data['set_link']
    assert create.post_data['comments']

    create._ask_post_meta(input_txt, set_link_input=input_n, comments_input=input_y)
    assert create.post_data['title'] == txt_data
    assert create.post_data['slug'] == txt_data
    assert create.post_data['category'] == txt_data
    assert not create.post_data['set_link']
    assert create.post_data['comments']

    create._ask_post_meta(input_txt, set_link_input=input_y, comments_input=input_n)
    assert create.post_data['title'] == txt_data
    assert create.post_data['slug'] == txt_data
    assert create.post_data['category'] == txt_data
    assert create.post_data['set_link']
    assert not create.post_data['comments']

    create._ask_post_meta(input_txt, set_link_input=input_n, comments_input=input_n)
    assert create.post_data['title'] == txt_data
    assert create.post_data['slug'] == txt_data
    assert create.post_data['category'] == txt_data
    assert not create.post_data['set_link']
    assert not create.post_data['comments']

    create._ask_post_meta(input_txt, set_link_input=input_enter, comments_input=input_enter)
    assert not create.post_data['set_link']
    assert create.post_data['comments']

def test_execute():
    create = Create()

    mock_post_data = {'title': 'famous title', 'slug': 'great slug',
                      'category': 'awesome category', 'set_link': False,
                      'comments': True}
    create.post_data = mock_post_data

    config_path = 'blgr/config.json'
    with open(config_path, 'r') as cfg:
        config = json.load(cfg)

    create.config = config
    create.prj_path = 'blgr/'
    create.execute()

    posts_path = os.path.join(create.prj_path, config['posts']['path'])
    posts = os.listdir(posts_path)
    assert len(posts)
    post_path = os.path.join(posts_path, posts[0])
    post_files = os.listdir(post_path)
    assert len(post_files) == 2  # exactly 2 files ipynb and meta
    fexts = [os.path.splitext(post_file)[1] for post_file in post_files]
    print(fexts)
    assert '.json' in fexts
    assert '.ipynb' in fexts
    assert 'meta.json' in post_files

    with open(os.path.join(post_path, 'meta.json'), 'r') as meta_file:
        meta = json.load(meta_file)

    assert meta.get('title', None) == mock_post_data['title']
    assert meta.get('slug', None) == mock_post_data['slug']
    assert meta.get('category', None) == mock_post_data['category']
    assert meta.get('set_link', None) == mock_post_data['set_link']
    assert meta.get('comments', None) == mock_post_data['comments']

    # clean up posts folder
    shutil.rmtree(os.path.join('blgr', config['posts']['path']))
<commit_msg>fix create command read configs from filesystem<commit_after>import os
import json
import shutil
import functools
from unittest.mock import patch

from blgr.blgr import Create

def input_mock(ret_val, prompt):
    return ret_val

def test_prepare():
    create = Create()

    input_y = functools.partial(input_mock, 'y')
    input_n = functools.partial(input_mock, 'n')
    input_enter = functools.partial(input_mock, '')

    with patch.object(create, '_ask_post_meta', return_value=None) as mock_user_interaction:
        create.prepare(input_n)
        create.prepare(input_y)
    mock_user_interaction.assert_called_once_with()

    with patch.object(create, '_ask_post_meta', return_value=None) as mock_user_interaction:
        create.prepare(input_enter)  # default behaviour
    assert not mock_user_interaction.called

def test_metadata():
    create = Create()

    input_y = functools.partial(input_mock, 'y')
    input_n = functools.partial(input_mock, 'n')
    input_enter = functools.partial(input_mock, '')
    txt_data = 'post txt data'
    input_txt = functools.partial(input_mock, txt_data)

    create._ask_post_meta(input_txt, set_link_input=input_y, comments_input=input_y)
    assert create.post_data['title'] == txt_data
    assert create.post_data['slug'] == txt_data
    assert create.post_data['category'] == txt_data
    assert create.post_data['set_link']
    assert create.post_data['comments']

    create._ask_post_meta(input_txt, set_link_input=input_n, comments_input=input_y)
    assert create.post_data['title'] == txt_data
    assert create.post_data['slug'] == txt_data
    assert create.post_data['category'] == txt_data
    assert not create.post_data['set_link']
    assert create.post_data['comments']

    create._ask_post_meta(input_txt, set_link_input=input_y, comments_input=input_n)
    assert create.post_data['title'] == txt_data
    assert create.post_data['slug'] == txt_data
    assert create.post_data['category'] == txt_data
    assert create.post_data['set_link']
    assert not create.post_data['comments']

    create._ask_post_meta(input_txt, set_link_input=input_n, comments_input=input_n)
    assert create.post_data['title'] == txt_data
    assert create.post_data['slug'] == txt_data
    assert create.post_data['category'] == txt_data
    assert not create.post_data['set_link']
    assert not create.post_data['comments']

    create._ask_post_meta(input_txt, set_link_input=input_enter, comments_input=input_enter)
    assert not create.post_data['set_link']
    assert create.post_data['comments']

def test_execute():
    create = Create()

    mock_post_data = {'title': 'famous title', 'slug': 'great slug',
                      'category': 'awesome category', 'set_link': False,
                      'comments': True}
    create.post_data = mock_post_data

    conf = {'posts': {'path': './posts'}}

    create.config = conf
    create.prj_path = 'blgr/'
    create.execute()

    posts_path = os.path.join(create.prj_path, conf['posts']['path'])
    posts = os.listdir(posts_path)
    assert len(posts)
    post_path = os.path.join(posts_path, posts[0])
    post_files = os.listdir(post_path)
    assert len(post_files) == 2  # exactly 2 files ipynb and meta
    fexts = [os.path.splitext(post_file)[1] for post_file in post_files]
    print(fexts)
    assert '.json' in fexts
    assert '.ipynb' in fexts
    assert 'meta.json' in post_files

    with open(os.path.join(post_path, 'meta.json'), 'r') as meta_file:
        meta = json.load(meta_file)

    assert meta.get('title', None) == mock_post_data['title']
    assert meta.get('slug', None) == mock_post_data['slug']
    assert meta.get('category', None) == mock_post_data['category']
    assert meta.get('set_link', None) == mock_post_data['set_link']
    assert meta.get('comments', None) == mock_post_data['comments']

    # clean up posts folder
    shutil.rmtree(os.path.join('blgr', conf['posts']['path']))
<|endoftext|>


<commit_before>import pytest

from xbee_helper import device, exceptions, ZigBee

def test_raise_if_error_no_status():
    """
    Should return None without raising if there's no "status" key in frame.
    """
    assert device.raise_if_error({}) is None

def test_raise_if_error_zero():
    """
    Should return None without raising if "status" is set to b"\x00".
    """
    assert device.raise_if_error(dict(status=b"\x00")) is None

def test_raise_if_error_unknown():
    """
    Should raise ZigBeeUnknownError if "status" is set to b"\x01".
    """
    with pytest.raises(exceptions.ZigBeeUnknownError):
        device.raise_if_error(dict(status=b"\x01"))

def test_raise_if_error_invalid_cmd():
    """
    Should raise ZigBeeInvalidCommand if "status" is set to b"\x02".
    """
    with pytest.raises(exceptions.ZigBeeInvalidCommand):
        device.raise_if_error(dict(status=b"\x02"))

def test_raise_if_error_invalid_param():
    """
    Should raise ZigBeeInvalidParameter if "status" is set to b"\x03".
    """
    with pytest.raises(exceptions.ZigBeeInvalidParameter):
        device.raise_if_error(dict(status=b"\x03"))

def test_raise_if_error_tx_failure():
    """
    Should raise ZigBeeTxFailure if "status" is set to b"\x04".
    """
    with pytest.raises(exceptions.ZigBeeTxFailure):
        device.raise_if_error(dict(status=b"\x04"))

def test_raise_if_error_unknown_status():
    """
    Should raise ZigBeeUnknownStatus if "status" is unrecognised.
    """
    with pytest.raises(exceptions.ZigBeeUnknownStatus):
        device.raise_if_error(dict(status=b"\xFF"))
<commit_msg>Remove unused import<commit_after>import pytest

from xbee_helper import device, exceptions

def test_raise_if_error_no_status():
    """
    Should return None without raising if there's no "status" key in frame.
    """
    assert device.raise_if_error({}) is None

def test_raise_if_error_zero():
    """
    Should return None without raising if "status" is set to b"\x00".
    """
    assert device.raise_if_error(dict(status=b"\x00")) is None

def test_raise_if_error_unknown():
    """
    Should raise ZigBeeUnknownError if "status" is set to b"\x01".
    """
    with pytest.raises(exceptions.ZigBeeUnknownError):
        device.raise_if_error(dict(status=b"\x01"))

def test_raise_if_error_invalid_cmd():
    """
    Should raise ZigBeeInvalidCommand if "status" is set to b"\x02".
    """
    with pytest.raises(exceptions.ZigBeeInvalidCommand):
        device.raise_if_error(dict(status=b"\x02"))

def test_raise_if_error_invalid_param():
    """
    Should raise ZigBeeInvalidParameter if "status" is set to b"\x03".
    """
    with pytest.raises(exceptions.ZigBeeInvalidParameter):
        device.raise_if_error(dict(status=b"\x03"))

def test_raise_if_error_tx_failure():
    """
    Should raise ZigBeeTxFailure if "status" is set to b"\x04".
    """
    with pytest.raises(exceptions.ZigBeeTxFailure):
        device.raise_if_error(dict(status=b"\x04"))

def test_raise_if_error_unknown_status():
    """
    Should raise ZigBeeUnknownStatus if "status" is unrecognised.
    """
    with pytest.raises(exceptions.ZigBeeUnknownStatus):
        device.raise_if_error(dict(status=b"\xFF"))
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# ------------------------------------------------------------
# Copyright 2017, Matthew Pounsett <matt@conundrum.com>
# ------------------------------------------------------------
import os
import sys
import unittest
sys.path.insert(0,
                os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
                )

import arke.domain

class TestDomainClass(unittest.TestCase):
    def test_name_class(self):
        domain = 'domain.with.escaped\.periods.in.it.tld.'
        domain_l = ('domain', 'with', 'escaped.periods', 'in', 'it', 'tld', '')
        domain_r = "<Domain(name='domain.with.escaped\\.periods.in.it.tld.')>"
        name = arke.domain.Domain(domain)
        self.assertEqual(name.name, domain_l)
        self.assertEqual(str(name), domain)
        self.assertEqual(repr(name), domain_r)

    def test_name_with_origin(self):
        origin = arke.domain.Domain('example.com')
        domain = arke.domain.Domain('www', origin=origin)
        self.assertEqual(domain.fqdn(), 'www.example.com.')
        self.assertEqual(str(domain), 'www')

    @unittest.expectedFailure
    # Need to figure out how to get object equality from the domain generator.
    def test_domain_singleton(self):
        x = arke.domain.Domain('foo.example.com.')
        y = arke.domain.Domain('foo.example.com.')
        print(arke.domain.DOMAINS)
        self.assertEqual(x, y)
        self.assertIs(x, y)
<commit_msg>extend test coverage for domain.py<commit_after># -*- coding: utf-8 -*-
# ------------------------------------------------------------
# Copyright 2017, Matthew Pounsett <matt@conundrum.com>
# ------------------------------------------------------------
import os
import sys
import unittest
sys.path.insert(0,
                os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
                )

import arke.domain

class TestDomainClass(unittest.TestCase):
    def test_name_class(self):
        domain = 'domain\_with.escaped\.characters.in.it.tld.'
        domain_l = ('domain\_with', 'escaped.characters',
                    'in', 'it', 'tld', '')
        domain_r = (
            "<Domain(name='domain\_with.escaped\\.characters.in.it.tld.')>"
        )
        name = arke.domain.Domain(domain)
        self.assertEqual(name.name, domain_l)
        self.assertEqual(str(name), domain)
        self.assertEqual(repr(name), domain_r)

    def test_name_with_origin(self):
        origin = arke.domain.Domain('example.com')
        domain = arke.domain.Domain('www', origin=origin)
        self.assertEqual(domain.fqdn(), 'www.example.com.')
        self.assertEqual(str(domain), 'www')

    @unittest.expectedFailure
    # Need to figure out how to get object equality from the domain generator.
    def test_domain_singleton(self):
        x = arke.domain.Domain('foo.example.com.')
        y = arke.domain.Domain('foo.example.com.')
        print(arke.domain.DOMAINS)
        self.assertEqual(x, y)
        self.assertIs(x, y)
<|endoftext|>


<commit_before>import bloop
import bloop.engine
import pytest
import uuid

def ordered(obj):
    '''
    Return sorted version of nested dicts/lists for comparing.

    http://stackoverflow.com/a/25851972
    '''
    if isinstance(obj, dict):
        return sorted((k, ordered(v)) for k, v in obj.items())
    if isinstance(obj, list):
        return sorted(ordered(x) for x in obj)
    else:
        return obj

def test_missing_objects(User, engine):
    '''
    When objects aren't loaded, ObjectsNotFound is raised with a list of
    missing objects
    '''
    # Patch batch_get_items to return no results
    engine.client.batch_get_items = lambda *a, **kw: {}

    users = [User(id=uuid.uuid4()) for _ in range(3)]

    with pytest.raises(bloop.engine.ObjectsNotFound) as excinfo:
        engine.load(users)

    assert set(excinfo.value.missing) == set(users)

def test_register_bound_model(User, engine):
    assert User in engine.models
    engine.register(User)
    assert User not in engine.unbound_models

def test_dump_key(User, engine, local_bind):
    class HashAndRange(engine.model):
        foo = bloop.Column(bloop.Integer, hash_key=True)
        bar = bloop.Column(bloop.Integer, range_key=True)
    engine.bind()

    user = User(id=uuid.uuid4())
    user_key = {'id': {'S': str(user.id)}}
    assert bloop.engine.dump_key(engine, user) == user_key

    obj = HashAndRange(foo=4, bar=5)
    obj_key = {'bar': {'N': '5'}, 'foo': {'N': '4'}}
    assert bloop.engine.dump_key(engine, obj) == obj_key

def test_load_object(User, engine):
    user_id = uuid.uuid4()
    expected = {'User': {'Keys': [{'id': {'S': str(user_id)}}],
                         'ConsistentRead': False}}
    response = {'User': [{'age': {'N': 5},
                          'name': {'S': 'foo'},
                          'id': {'S': str(user_id)}}]}

    def respond(input):
        assert input == expected
        return response
    engine.client.batch_get_items = respond

    user = User(id=user_id)
    engine.load(user)

    assert user.age == 5
    assert user.name == 'foo'
    assert user.id == user_id

def test_load_objects(User, engine):
    user1 = User(id=uuid.uuid4())
    user2 = User(id=uuid.uuid4())
    expected = {'User': {'Keys': [{'id': {'S': str(user1.id)}},
                                  {'id': {'S': str(user2.id)}}],
                         'ConsistentRead': False}}
    response = {'User': [{'age': {'N': 5},
                          'name': {'S': 'foo'},
                          'id': {'S': str(user1.id)}},
                         {'age': {'N': 10},
                          'name': {'S': 'bar'},
                          'id': {'S': str(user2.id)}}]}

    def respond(input):
        assert ordered(input) == ordered(expected)
        return response
    engine.client.batch_get_items = respond

    engine.load((user1, user2))

    assert user1.age == 5
    assert user1.name == 'foo'
    assert user2.age == 10
    assert user2.name == 'bar'

def test_load_dump_unbound(UnboundUser, engine):
    user_id = uuid.uuid4()
    user = UnboundUser(id=user_id, age=5, name='foo')
    value = {'User': [{'age': {'N': 5},
                       'name': {'S': 'foo'},
                       'id': {'S': str(user_id)}}]}

    with pytest.raises(RuntimeError):
        engine.__load__(UnboundUser, value)
    with pytest.raises(RuntimeError):
        engine.__dump__(UnboundUser, user)

def test_load_dump_unknown(engine):
    class NotModeled:
        pass
    obj = NotModeled()
    user_id = uuid.uuid4()
    value = {'User': [{'age': {'N': 5},
                       'name': {'S': 'foo'},
                       'id': {'S': str(user_id)}}]}

    with pytest.raises(ValueError):
        engine.__load__(NotModeled, value)
    with pytest.raises(ValueError):
        engine.__dump__(NotModeled, obj)

def test_illegal_save(User, engine):
    users = [User(id=uuid.uuid4()) for _ in range(3)]
    condition = User.id.is_(None)

    with pytest.raises(ValueError):
        engine.save(users, condition=condition)

def test_save_condition(User, engine):
    user_id = uuid.uuid4()
    user = User(id=user_id)
    condition = User.id.is_(None)
    expected = {'TableName': 'User',
                'ExpressionAttributeNames': {'#n0': 'id'},
                'ConditionExpression': '(attribute_not_exists(#n0))',
                'Item': {'id': {'S': str(user_id)}}}

    def validate(item):
        assert item == expected
    engine.client.put_item = validate
    engine.save(user, condition=condition)

def test_save_multiple(User, engine):
    user1 = User(id=uuid.uuid4())
    user2 = User(id=uuid.uuid4())

    expected = {'User': [
        {'PutRequest': {'Item': {'id': {'S': str(user1.id)}}}},
        {'PutRequest': {'Item': {'id': {'S': str(user2.id)}}}}]}

    def validate(items):
        assert ordered(items) == ordered(expected)
    engine.client.batch_write_items = validate
    engine.save((user1, user2))

def test_illegal_delete(User, engine):
    users = [User(id=uuid.uuid4()) for _ in range(3)]
    condition = User.id.is_(None)

    with pytest.raises(ValueError):
        engine.delete(users, condition=condition)

def test_delete_condition(User, engine):
    user_id = uuid.uuid4()
    user = User(id=user_id)
    condition = User.id.is_(None)
    expected = {'TableName': 'User',
                'ExpressionAttributeNames': {'#n0': 'id'},
                'ConditionExpression': '(attribute_not_exists(#n0))',
                'Key': {'id': {'S': str(user_id)}}}

    def validate(item):
        assert item == expected
    engine.client.delete_item = validate
    engine.delete(user, condition=condition)

def test_delete_multiple(User, engine):
    user1 = User(id=uuid.uuid4())
    user2 = User(id=uuid.uuid4())

    expected = {'User': [
        {'DeleteRequest': {'Key': {'id': {'S': str(user1.id)}}}},
        {'DeleteRequest': {'Key': {'id': {'S': str(user2.id)}}}}]}

    def validate(items):
        assert ordered(items) == ordered(expected)
    engine.client.batch_write_items = validate
    engine.delete((user1, user2))
<commit_msg>Test engine.query, engine.scan<commit_after>import bloop
import bloop.engine
import pytest
import uuid

def ordered(obj):
    '''
    Return sorted version of nested dicts/lists for comparing.

    http://stackoverflow.com/a/25851972
    '''
    if isinstance(obj, dict):
        return sorted((k, ordered(v)) for k, v in obj.items())
    if isinstance(obj, list):
        return sorted(ordered(x) for x in obj)
    else:
        return obj

def test_missing_objects(User, engine):
    '''
    When objects aren't loaded, ObjectsNotFound is raised with a list of
    missing objects
    '''
    # Patch batch_get_items to return no results
    engine.client.batch_get_items = lambda *a, **kw: {}

    users = [User(id=uuid.uuid4()) for _ in range(3)]

    with pytest.raises(bloop.engine.ObjectsNotFound) as excinfo:
        engine.load(users)

    assert set(excinfo.value.missing) == set(users)

def test_register_bound_model(User, engine):
    assert User in engine.models
    engine.register(User)
    assert User not in engine.unbound_models

def test_dump_key(User, engine, local_bind):
    class HashAndRange(engine.model):
        foo = bloop.Column(bloop.Integer, hash_key=True)
        bar = bloop.Column(bloop.Integer, range_key=True)
    engine.bind()

    user = User(id=uuid.uuid4())
    user_key = {'id': {'S': str(user.id)}}
    assert bloop.engine.dump_key(engine, user) == user_key

    obj = HashAndRange(foo=4, bar=5)
    obj_key = {'bar': {'N': '5'}, 'foo': {'N': '4'}}
    assert bloop.engine.dump_key(engine, obj) == obj_key

def test_load_object(User, engine):
    user_id = uuid.uuid4()
    expected = {'User': {'Keys': [{'id': {'S': str(user_id)}}],
                         'ConsistentRead': False}}
    response = {'User': [{'age': {'N': 5},
                          'name': {'S': 'foo'},
                          'id': {'S': str(user_id)}}]}

    def respond(input):
        assert input == expected
        return response
    engine.client.batch_get_items = respond

    user = User(id=user_id)
    engine.load(user)

    assert user.age == 5
    assert user.name == 'foo'
    assert user.id == user_id

def test_load_objects(User, engine):
    user1 = User(id=uuid.uuid4())
    user2 = User(id=uuid.uuid4())
    expected = {'User': {'Keys': [{'id': {'S': str(user1.id)}},
                                  {'id': {'S': str(user2.id)}}],
                         'ConsistentRead': False}}
    response = {'User': [{'age': {'N': 5},
                          'name': {'S': 'foo'},
                          'id': {'S': str(user1.id)}},
                         {'age': {'N': 10},
                          'name': {'S': 'bar'},
                          'id': {'S': str(user2.id)}}]}

    def respond(input):
        assert ordered(input) == ordered(expected)
        return response
    engine.client.batch_get_items = respond

    engine.load((user1, user2))

    assert user1.age == 5
    assert user1.name == 'foo'
    assert user2.age == 10
    assert user2.name == 'bar'

def test_load_dump_unbound(UnboundUser, engine):
    user_id = uuid.uuid4()
    user = UnboundUser(id=user_id, age=5, name='foo')
    value = {'User': [{'age': {'N': 5},
                       'name': {'S': 'foo'},
                       'id': {'S': str(user_id)}}]}

    with pytest.raises(RuntimeError):
        engine.__load__(UnboundUser, value)
    with pytest.raises(RuntimeError):
        engine.__dump__(UnboundUser, user)

def test_load_dump_unknown(engine):
    class NotModeled:
        pass
    obj = NotModeled()
    user_id = uuid.uuid4()
    value = {'User': [{'age': {'N': 5},
                       'name': {'S': 'foo'},
                       'id': {'S': str(user_id)}}]}

    with pytest.raises(ValueError):
        engine.__load__(NotModeled, value)
    with pytest.raises(ValueError):
        engine.__dump__(NotModeled, obj)

def test_illegal_save(User, engine):
    users = [User(id=uuid.uuid4()) for _ in range(3)]
    condition = User.id.is_(None)

    with pytest.raises(ValueError):
        engine.save(users, condition=condition)

def test_save_condition(User, engine):
    user_id = uuid.uuid4()
    user = User(id=user_id)
    condition = User.id.is_(None)
    expected = {'TableName': 'User',
                'ExpressionAttributeNames': {'#n0': 'id'},
                'ConditionExpression': '(attribute_not_exists(#n0))',
                'Item': {'id': {'S': str(user_id)}}}

    def validate(item):
        assert item == expected
    engine.client.put_item = validate
    engine.save(user, condition=condition)

def test_save_multiple(User, engine):
    user1 = User(id=uuid.uuid4())
    user2 = User(id=uuid.uuid4())

    expected = {'User': [
        {'PutRequest': {'Item': {'id': {'S': str(user1.id)}}}},
        {'PutRequest': {'Item': {'id': {'S': str(user2.id)}}}}]}

    def validate(items):
        assert ordered(items) == ordered(expected)
    engine.client.batch_write_items = validate
    engine.save((user1, user2))

def test_illegal_delete(User, engine):
    users = [User(id=uuid.uuid4()) for _ in range(3)]
    condition = User.id.is_(None)

    with pytest.raises(ValueError):
        engine.delete(users, condition=condition)

def test_delete_condition(User, engine):
    user_id = uuid.uuid4()
    user = User(id=user_id)
    condition = User.id.is_(None)
    expected = {'TableName': 'User',
                'ExpressionAttributeNames': {'#n0': 'id'},
                'ConditionExpression': '(attribute_not_exists(#n0))',
                'Key': {'id': {'S': str(user_id)}}}

    def validate(item):
        assert item == expected
    engine.client.delete_item = validate
    engine.delete(user, condition=condition)

def test_delete_multiple(User, engine):
    user1 = User(id=uuid.uuid4())
    user2 = User(id=uuid.uuid4())

    expected = {'User': [
        {'DeleteRequest': {'Key': {'id': {'S': str(user1.id)}}}},
        {'DeleteRequest': {'Key': {'id': {'S': str(user2.id)}}}}]}

    def validate(items):
        assert ordered(items) == ordered(expected)
    engine.client.batch_write_items = validate
    engine.delete((user1, user2))

def test_query(User, engine):
    ''' Engine.query supports model and index-based queries '''
    index_query = engine.query(User.by_email)
    assert index_query.model is User
    assert index_query.index is User.by_email

    model_query = engine.query(User)
    assert model_query.model is User
    assert model_query.index is None

def test_scan(User, engine):
    ''' Engine.scan supports model and index-based queries '''
    index_scan = engine.scan(User.by_email)
    assert index_scan.model is User
    assert index_scan.index is User.by_email

    model_scan = engine.scan(User)
    assert model_scan.model is User
    assert model_scan.index is None
<|endoftext|>


<commit_before>import unittest

import fastai
import pandas as pd
import torch

from fastai.docs import *
from fastai.tabular import *
from fastai.core import partition
from fastai.torch_core import tensor

class TestFastAI(unittest.TestCase):
    def test_partition(self):
        result = partition([1,2,3,4,5], 2)

        self.assertEqual(3, len(result))

    def test_has_version(self):
        self.assertGreater(len(fastai.__version__), 1)

    # based on https://github.com/fastai/fastai/blob/master/tests/test_torch_core.py#L17
    def test_torch_tensor(self):
        a = tensor([1, 2, 3])
        b = torch.tensor([1, 2, 3])

        self.assertTrue(torch.all(a == b))

    def test_tabular(self):
        df = pd.read_csv("/input/tests/data/train.csv")

        train_df, valid_df = df[:-5].copy(),df[-5:].copy()
        dep_var = "label"
        cont_names = []
        for i in range(784):
            cont_names.append("pixel" + str(i))

        data = tabular_data_from_df("", train_df, valid_df, dep_var, cont_names=cont_names, cat_names=[])
        learn = get_tabular_learner(data, layers=[200, 100])
        learn.fit(epochs=1)
<commit_msg>simplify fastai tests imports<commit_after>import unittest

import fastai
import pandas as pd
import torch

from fastai.tabular import tabular_data_from_df, get_tabular_learner
from fastai.core import partition
from fastai.torch_core import tensor

class TestFastAI(unittest.TestCase):
    def test_partition(self):
        result = partition([1,2,3,4,5], 2)

        self.assertEqual(3, len(result))

    def test_has_version(self):
        self.assertGreater(len(fastai.__version__), 1)

    # based on https://github.com/fastai/fastai/blob/master/tests/test_torch_core.py#L17
    def test_torch_tensor(self):
        a = tensor([1, 2, 3])
        b = torch.tensor([1, 2, 3])

        self.assertTrue(torch.all(a == b))

    def test_tabular(self):
        df = pd.read_csv("/input/tests/data/train.csv")

        train_df, valid_df = df[:-5].copy(),df[-5:].copy()
        dep_var = "label"
        cont_names = []
        for i in range(784):
            cont_names.append("pixel" + str(i))

        data = tabular_data_from_df("", train_df, valid_df, dep_var, cont_names=cont_names, cat_names=[])
        learn = get_tabular_learner(data, layers=[200, 100])
        learn.fit(epochs=1)
<|endoftext|>


<commit_before>import pytest
from subprocess import check_output, Popen, PIPE, call
import hashlib
import os
from os.path import expanduser
from os import listdir
import re

def test_genome_location():
    out, err = Popen(["vk","genome","location"], stdout = PIPE).communicate()
    if err:
        raise Exception(err)
    directory = out[out.find("Genome Directory"):].split(" ")[2].strip()
    assert os.path.exists(directory)

def test_set_genome_location():
    out, err = Popen(["vk","genome","location", "."], stdout = PIPE).communicate()
    if err:
        raise Exception(err)
    print(out[out.find("Set genome location"):].split(" ")[3])
    directory = out[out.find("Set genome location"):].split(" ")[4].strip()
    assert directory == os.getcwd() + "/"
    # Set genome location back to default
    out, err = Popen(["vk","genome","location", "-"], stdout = PIPE).communicate()

def test_search_genome():
    out, err = Popen(["vk", "genome", "--search", "coli"], stdout = PIPE).communicate()
    search_result = [re.split("\W+", x.strip()) for x in out.splitlines() if x.find("GCF_900042795.1") > 0][0][5]
    assert search_result == "F1L3"

def test_download_genome():
    call(["vk", "genome", "--download", "F1L3"])
    genome_files = listdir(expanduser("~/.genome/F1L3"))
    assert len(genome_files) == 11

def test_download_genome_files():
    genome_files = [expanduser("~/.genome/F1L3/") + x for x in listdir(expanduser("~/.genome/F1L3"))]
    # Hash files
    hashed_files = set([(os.path.split(fname)[1], hashlib.sha256(open(fname, 'rb').read()).digest()) for fname in genome_files])
    m = hashlib.sha256(str(hashed_files)).hexdigest()
    assert m == "ffef1336ec82212f2b2f10aa9bc5386af92646f53c576f0480270c092d770648"<commit_msg>fix hashed files<commit_after>import pytest
from subprocess import check_output, Popen, PIPE, call
import hashlib
import os
from os.path import expanduser
from os import listdir
import re

def test_genome_location():
    out, err = Popen(["vk","genome","location"], stdout = PIPE).communicate()
    if err:
        raise Exception(err)
    directory = out[out.find("Genome Directory"):].split(" ")[2].strip()
    assert os.path.exists(directory)

def test_set_genome_location():
    out, err = Popen(["vk","genome","location", "."], stdout = PIPE).communicate()
    if err:
        raise Exception(err)
    print(out[out.find("Set genome location"):].split(" ")[3])
    directory = out[out.find("Set genome location"):].split(" ")[4].strip()
    assert directory == os.getcwd() + "/"
    # Set genome location back to default
    out, err = Popen(["vk","genome","location", "-"], stdout = PIPE).communicate()

def test_search_genome():
    out, err = Popen(["vk", "genome", "--search", "coli"], stdout = PIPE).communicate()
    search_result = [re.split("\W+", x.strip()) for x in out.splitlines() if x.find("GCF_900042795.1") > 0][0][5]
    assert search_result == "F1L3"

def test_download_genome():
    call(["vk", "genome", "--download", "F1L3"])
    genome_files = listdir(expanduser("~/.genome/F1L3"))
    assert len(genome_files) == 11

F1L3_hashed = set(['1d24107a072a588de6b843036b4584938140a1c6', # F1L3.fa.gz.pac
                   '9568184f20821902ac0bbc1f4937691928515488', # F1L3.fa.gz.bwt
                   '85cef94245fdd399b692df36ba0ad7256f49904b', # F1L3.fa.gz.fai
                   'd1467e4a070af05eda87aa57890af5a81609f6b7', # F1L3.fa.gz.nsq
                   'bf0c4dac3b200d44cd567cdbab59a5a5cbdecb6a', # F1L3.fa.gz
                   '5a67b3343c618392bc6e10ae9241961b43509b91', # F1L3.fa.gz.amb
                   '5c77732ce11c5cc16622de85d322e2bf157c3489', # F1L3.fa.gz.nhr
                   '6ccd1454ba12fef067d637bb14300841bde3ed81', # F1L3.fa.gz.ann
                   'f68b57c5633a7fcb0e16f1a8f41c5927ef3f17aa', # F1L3.fa.gz.sa
                   # '8b5aeb9a99e902b2f34a8b2b7447af28b9d1fccc',
                   '29c1333a16e2f643048442e18da55eaf7df0931b']) # F1L3.fa.gz.gzi

def test_download_genome_files():
    genome_files = [expanduser("~/.genome/F1L3/") + x for x in listdir(expanduser("~/.genome/F1L3"))]
    # Hash files
    hashed_files = set([hashlib.sha1(open(fname, 'rb').read()).hexdigest() for fname in genome_files if not fname.endswith(".nin")])
    assert hashed_files == F1L3_hashed<|endoftext|>


<commit_before>from pyneat.genotype import Genome

def test_genome():
    genome = Genome.minimal_fully_connected((3, 2))

    assert genome.neurons == (3, 2)
    assert len(genome.genes) == 6
<commit_msg>Fixes bad genome test<commit_after>from pyneat.genotype import Genome

def test_genome():
    genome = Genome.minimal_fully_connected((3, 2))

    assert genome.neurons == (3, 0, 2)
    assert len(genome.genes) == 6
<|endoftext|>


<commit_before>import sys

import crow_modules.distribution1Dpy2
import crow_modules.interpolationNDpy2

sys.exit(0)
<commit_msg>Python 3 compatability.<commit_after>import sys

if sys.version_info.major > 2:
  import crow_modules.distribution1Dpy3
  import crow_modules.interpolationNDpy3
else:
  import crow_modules.distribution1Dpy2
  import crow_modules.interpolationNDpy2

sys.exit(0)
<|endoftext|>


<commit_before>import unittest

from bonsai import LDAPDN
from bonsai import errors

class LDAPDNTest(unittest.TestCase):
    """ Testing LDAP DN object. """
    def setUp(self):
        """ Set up distinguished name for testing. """
        self.strdn = "cn=user,dc=test,dc=local"
        self.dnobj = LDAPDN(self.strdn)

    def test_rdn(self):
        """ Test methods for retrieving and changing RDNs. """
        self.assertEqual(self.dnobj.rdns[0], (('cn', 'user'),))
        self.assertEqual(self.dnobj[0], "cn=user")
        self.assertEqual(self.dnobj[1:], "dc=test,dc=local")
        self.dnobj[1:] = "dc=test2"
        self.assertEqual(self.dnobj, "cn=user,dc=test2")
        self.assertRaises(IndexError, lambda: self.dnobj[7])
        self.assertRaises(TypeError, lambda: self.dnobj['test'])
        def set_rdns():
            self.dnobj.rdns = (("dc", "test"),)
        self.assertRaises(ValueError, set_rdns)

    def test_str(self):
        """ Test __str__ method of LDAPDN object. """
        self.assertEqual(str(self.dnobj), self.strdn)

    def test_emptydn(self):
        """ Test empty distinguished name. """
        empty = LDAPDN("")
        self.assertEqual(empty[1:], "")

    def test_equal(self):
        """ Test __eq__ method of LDAPDN object. """
        self.assertEqual(self.dnobj, LDAPDN(self.strdn))
        self.assertEqual(self.dnobj, LDAPDN(self.strdn.title()))
        self.assertEqual(self.dnobj, self.strdn.upper())

    def test_invaliddn(self):
        """ Test InvalidDN exception. """
        self.assertRaises(errors.InvalidDN,
                          lambda: LDAPDN("cn=test,dc=one+two"))

    def test_special_char(self):
        """ Test parsing special characters in DN string. """
        spec = LDAPDN(r"cn=special\, name,dc=test,dc=local")
        self.assertEqual(str(spec), r"cn=special\, name,dc=test,dc=local")

    def test_setitem(self):
        """ Test setting RDNs for DN object. """
        dnobj = LDAPDN("sn=some+gn=thing,dc=test,dc=local")
        self.assertEqual("sn=some+gn=thing", dnobj[0])
        dnobj[0] = "cn=user"
        self.assertEqual("cn=user,dc=test,dc=local", dnobj)
        dnobj[1] = "ou=group1,ou=group2"
        self.assertEqual("cn=user,ou=group1,ou=group2,dc=local", dnobj)
        dnobj[2:] = "dc=local"
        self.assertEqual("cn=user,ou=group1,dc=local", dnobj)
        def str_idx():
            dnobj['invalid'] = "ou=group1,ou=group2"
        self.assertRaises(TypeError, str_idx)
        def not_str():
            dnobj[0] = 3
        self.assertRaises(ValueError, not_str)
        def invalid():
            dnobj[1] = "test,group"
        self.assertRaises(errors.InvalidDN, invalid)

    def test_repr(self):
        """ Test representation. """
        self.assertIn("<LDAPDN", repr(self.dnobj))

if __name__ == '__main__':
    unittest.main()
<commit_msg>Change LDAPDN tests to use pytest<commit_after>import pytest

from bonsai import LDAPDN
from bonsai import errors

VALID_STRDN = "cn=user,dc=test,dc=local"

@pytest.fixture
def dnobj():
    return LDAPDN(VALID_STRDN)

def test_rdn(dnobj):
    """ Test methods for retrieving and changing RDNs. """
    assert dnobj.rdns[0] == (("cn", "user"),)
    assert dnobj[0] == "cn=user"
    assert dnobj[1:] == "dc=test,dc=local"
    dnobj[1:] = "dc=test2"
    assert dnobj == "cn=user,dc=test2"
    with pytest.raises(IndexError):
        _ = dnobj[7]
    with pytest.raises(TypeError):
        _ = dnobj["test"]
    with pytest.raises(ValueError):
        dnobj.rdns = (("dc", "test"),)

def test_str(dnobj):
    """ Test __str__ method of LDAPDN object. """
    assert str(dnobj) == VALID_STRDN

def test_emptydn():
    """ Test empty distinguished name. """
    empty = LDAPDN("")
    assert empty[1:] == ""

def test_equal(dnobj):
    """ Test __eq__ method of LDAPDN object. """
    assert dnobj == LDAPDN(VALID_STRDN)
    assert dnobj == LDAPDN(VALID_STRDN.title())
    assert dnobj == VALID_STRDN.upper()

def test_invaliddn():
    """ Test InvalidDN exception. """
    with pytest.raises(errors.InvalidDN):
        _ = LDAPDN("cn=test,dc=one+two")

def test_special_char():
    """ Test parsing special characters in DN string. """
    spec = LDAPDN(r"cn=special\, name,dc=test,dc=local")
    assert str(spec) == r"cn=special\, name,dc=test,dc=local"

def test_setitem():
    """ Test setting RDNs for DN object. """
    dnobj = LDAPDN("sn=some+gn=thing,dc=test,dc=local")
    assert "sn=some+gn=thing" == dnobj[0]
    dnobj[0] = "cn=user"
    assert "cn=user,dc=test,dc=local" == dnobj
    dnobj[1] = "ou=group1,ou=group2"
    assert "cn=user,ou=group1,ou=group2,dc=local" == dnobj
    dnobj[2:] = "dc=local"
    assert "cn=user,ou=group1,dc=local" == dnobj

    with pytest.raises(TypeError):
        dnobj["invalid"] = "ou=group1,ou=group2"
    with pytest.raises(ValueError):
        dnobj[0] = 3
    with pytest.raises(errors.InvalidDN):
        dnobj[1] = "test,group"

def test_repr(dnobj):
    """ Test representation. """
    assert "<LDAPDN" in repr(dnobj)
<|endoftext|>


<commit_before>from __future__ import absolute_import
import itertools
import autograd.numpy as np
import autograd.numpy.random as npr
import autograd.scipy.linalg as spla
from autograd.util import *
from autograd import grad
from builtins import range

npr.seed(1)

def test_inv():
    def fun(x): return to_scalar(np.linalg.inv(x))
    d_fun = lambda x : to_scalar(grad(fun)(x))
    D = 8
    mat = npr.randn(D, D)
    mat = np.dot(mat, mat) + 1.0 * np.eye(D)
    check_grads(fun, mat)
    check_grads(d_fun, mat)

def test_solve_arg1():
    D = 8
    A = npr.randn(D, D) + 10.0 * np.eye(D)
    B = npr.randn(D, D - 1)
    def fun(a): return to_scalar(np.linalg.solve(a, B))
    d_fun = lambda x : to_scalar(grad(fun)(x))
    check_grads(fun, A)
    check_grads(d_fun, A)

def test_solve_arg1_1d():
    D = 8
    A = npr.randn(D, D) + 10.0 * np.eye(D)
    B = npr.randn(D)
    def fun(a): return to_scalar(np.linalg.solve(a, B))
    d_fun = lambda x : to_scalar(grad(fun)(x))
    check_grads(fun, A)
    check_grads(d_fun, A)

#return np.dot(b,np.dot(np.linalg.inv(A),b))

def test_solve_arg2():
    D = 6
    A = npr.randn(D, D) + 1.0 * np.eye(D)
    B = npr.randn(D, D - 1)
    def fun(b): return to_scalar(np.linalg.solve(A, b))
    d_fun = lambda x : to_scalar(grad(fun)(x))
    check_grads(fun, B)
    check_grads(d_fun, B)

def test_det():
    def fun(x): return np.linalg.det(x)
    d_fun = lambda x : to_scalar(grad(fun)(x))
    D = 6
    mat = npr.randn(D, D)
    check_grads(fun, mat)
    check_grads(d_fun, mat)

def test_slogdet():
    def fun(x):
        sign, logdet = np.linalg.slogdet(x)
        return logdet
    d_fun = lambda x : to_scalar(grad(fun)(x))
    D = 6
    mat = npr.randn(D, D)
    check_grads(fun, mat)
    check_grads(d_fun, mat)

def test_vector_2norm():
    def fun(x): return to_scalar(np.linalg.norm(x))
    d_fun = lambda x: to_scalar(grad(fun)(x))
    D = 6
    vec = npr.randn(D)
    check_grads(fun, vec)
    check_grads(d_fun, vec)

def test_frobenius_norm():
    def fun(x): return to_scalar(np.linalg.norm(x))
    d_fun = lambda x : to_scalar(grad(fun)(x))
    D = 6
    mat = npr.randn(D, D-1)
    check_grads(fun, mat)
    check_grads(d_fun, mat)

def test_vector_norm_ord():
    def helper(size, ord):
        def fun(x): return np.linalg.norm(x, ord=ord)
        vec = npr.randn(size)
        check_grads(fun, vec)
    for ord in range(2,5):
        yield helper, 6, ord

def test_norm_axis():
    def helper(shape, axis):
        def fun(x): return to_scalar(np.linalg.norm(x, axis=axis))
        arr = npr.randn(*shape)
        check_grads(fun, arr)
    for axis in range(3):
        yield helper, (6,5,4), axis

def test_eigvalh_lower():
    def fun(x):
        w, v = np.linalg.eigh(x)
        return to_scalar(w) + to_scalar(v)
    d_fun = lambda x : to_scalar(grad(fun)(x))
    D = 6
    mat = npr.randn(D, D-1)
    hmat = np.dot(mat.T, mat)
    check_grads(fun, hmat)
    check_grads(d_fun, hmat)

def test_eigvalh_upper():
    def fun(x):
        w, v = np.linalg.eigh(x, 'U')
        return to_scalar(w) + to_scalar(v)
    d_fun = lambda x : to_scalar(grad(fun)(x))
    D = 6
    mat = npr.randn(D, D-1)
    hmat = np.dot(mat.T, mat)
    check_grads(fun, hmat)
    check_grads(d_fun, hmat)

def test_cholesky():
    # we write a test function that explicitly symmetrizes because otherwise
    # cholesky only reads from the lower triangle and hence the numerical
    # gradient is lower triangular
    def symmetrize(A):
        L = np.tril(A)
        return (L + L.T)/2.
    def fun(A):
        return to_scalar(np.linalg.cholesky(symmetrize(A)))
    def rand_psd(D):
        mat = npr.randn(D,D)
        return np.dot(mat, mat.T)
    check_grads(fun, rand_psd(6))

def test_sqrtm():
    def fun(A):
        return to_scalar(spla.sqrtm(A))
    def rand_psd(D):
        mat = npr.randn(D,D)
        return np.dot(mat, mat.T)
    check_grads(fun, rand_psd(6))

def test_solve_triangular_arg1():
    D = 6
    b = npr.randn(D)
    trans_options = ['T', 'N', 'C', 0, 1, 2]
    lower_options = [True, False]
    for trans, lower in itertools.product(trans_options, lower_options):
        def fun(A):
            return to_scalar(spla.solve_triangular(A, b, trans=trans, lower=lower))
        yield check_grads, fun, npr.randn(D, D) + 10*np.eye(D)

def test_solve_triangular_arg2_1d():
    D = 6
    A = npr.randn(D, D) + 10*np.eye(D)
    trans_options = ['T', 'N', 'C', 0, 1, 2]
    lower_options = [True, False]
    for trans, lower in itertools.product(trans_options, lower_options):
        def fun(b):
            return to_scalar(spla.solve_triangular(A, b, trans=trans, lower=lower))
        yield check_grads, fun, npr.randn(D)

def test_solve_triangular_arg2_2d():
    D = 6
    A = npr.randn(D, D) + 10*np.eye(D)
    trans_options = ['T', 'N', 'C', 0, 1, 2]
    lower_options = [True, False]
    for trans, lower in itertools.product(trans_options, lower_options):
        def fun(B):
            return to_scalar(spla.solve_triangular(A, B, trans=trans, lower=lower))
        yield check_grads, fun, npr.randn(D, D-1)
<commit_msg>add check_symmetric_matrix_grads function<commit_after>from __future__ import absolute_import
import itertools
import autograd.numpy as np
import autograd.numpy.random as npr
import autograd.scipy.linalg as spla
from autograd.util import *
from autograd import grad
from builtins import range

npr.seed(1)

def check_symmetric_matrix_grads(fun, *args):
    def symmetrize(A):
        L = np.tril(A)
        return (L + L.T)/2.
    new_fun = lambda *args: fun(symmetrize(args[0]), *args[1:])
    return check_grads(new_fun, *args)

def test_inv():
    def fun(x): return to_scalar(np.linalg.inv(x))
    d_fun = lambda x : to_scalar(grad(fun)(x))
    D = 8
    mat = npr.randn(D, D)
    mat = np.dot(mat, mat) + 1.0 * np.eye(D)
    check_grads(fun, mat)
    check_grads(d_fun, mat)

def test_solve_arg1():
    D = 8
    A = npr.randn(D, D) + 10.0 * np.eye(D)
    B = npr.randn(D, D - 1)
    def fun(a): return to_scalar(np.linalg.solve(a, B))
    d_fun = lambda x : to_scalar(grad(fun)(x))
    check_grads(fun, A)
    check_grads(d_fun, A)

def test_solve_arg1_1d():
    D = 8
    A = npr.randn(D, D) + 10.0 * np.eye(D)
    B = npr.randn(D)
    def fun(a): return to_scalar(np.linalg.solve(a, B))
    d_fun = lambda x : to_scalar(grad(fun)(x))
    check_grads(fun, A)
    check_grads(d_fun, A)

#return np.dot(b,np.dot(np.linalg.inv(A),b))

def test_solve_arg2():
    D = 6
    A = npr.randn(D, D) + 1.0 * np.eye(D)
    B = npr.randn(D, D - 1)
    def fun(b): return to_scalar(np.linalg.solve(A, b))
    d_fun = lambda x : to_scalar(grad(fun)(x))
    check_grads(fun, B)
    check_grads(d_fun, B)

def test_det():
    def fun(x): return np.linalg.det(x)
    d_fun = lambda x : to_scalar(grad(fun)(x))
    D = 6
    mat = npr.randn(D, D)
    check_grads(fun, mat)
    check_grads(d_fun, mat)

def test_slogdet():
    def fun(x):
        sign, logdet = np.linalg.slogdet(x)
        return logdet
    d_fun = lambda x : to_scalar(grad(fun)(x))
    D = 6
    mat = npr.randn(D, D)
    check_grads(fun, mat)
    check_grads(d_fun, mat)

def test_vector_2norm():
    def fun(x): return to_scalar(np.linalg.norm(x))
    d_fun = lambda x: to_scalar(grad(fun)(x))
    D = 6
    vec = npr.randn(D)
    check_grads(fun, vec)
    check_grads(d_fun, vec)

def test_frobenius_norm():
    def fun(x): return to_scalar(np.linalg.norm(x))
    d_fun = lambda x : to_scalar(grad(fun)(x))
    D = 6
    mat = npr.randn(D, D-1)
    check_grads(fun, mat)
    check_grads(d_fun, mat)

def test_vector_norm_ord():
    def helper(size, ord):
        def fun(x): return np.linalg.norm(x, ord=ord)
        vec = npr.randn(size)
        check_grads(fun, vec)
    for ord in range(2,5):
        yield helper, 6, ord

def test_norm_axis():
    def helper(shape, axis):
        def fun(x): return to_scalar(np.linalg.norm(x, axis=axis))
        arr = npr.randn(*shape)
        check_grads(fun, arr)
    for axis in range(3):
        yield helper, (6,5,4), axis

def test_eigvalh_lower():
    def fun(x):
        w, v = np.linalg.eigh(x)
        return to_scalar(w) + to_scalar(v)
    d_fun = lambda x : to_scalar(grad(fun)(x))
    D = 6
    mat = npr.randn(D, D-1)
    hmat = np.dot(mat.T, mat)
    check_grads(fun, hmat)
    check_grads(d_fun, hmat)

def test_eigvalh_upper():
    def fun(x):
        w, v = np.linalg.eigh(x, 'U')
        return to_scalar(w) + to_scalar(v)
    d_fun = lambda x : to_scalar(grad(fun)(x))
    D = 6
    mat = npr.randn(D, D-1)
    hmat = np.dot(mat.T, mat)
    check_grads(fun, hmat)
    check_grads(d_fun, hmat)

def test_cholesky():
    def fun(A):
        return to_scalar(np.linalg.cholesky(A))
    def rand_psd(D):
        mat = npr.randn(D,D)
        return np.dot(mat, mat.T)
    check_symmetric_matrix_grads(fun, rand_psd(6))

def test_sqrtm():
    def fun(A):
        return to_scalar(spla.sqrtm(A))
    def rand_psd(D):
        mat = npr.randn(D,D)
        return np.dot(mat, mat.T)
    check_symmetric_matrix_grads(fun, rand_psd(6))

def test_solve_triangular_arg1():
    D = 6
    b = npr.randn(D)
    trans_options = ['T', 'N', 'C', 0, 1, 2]
    lower_options = [True, False]
    for trans, lower in itertools.product(trans_options, lower_options):
        def fun(A):
            return to_scalar(spla.solve_triangular(A, b, trans=trans, lower=lower))
        yield check_grads, fun, npr.randn(D, D) + 10*np.eye(D)

def test_solve_triangular_arg2_1d():
    D = 6
    A = npr.randn(D, D) + 10*np.eye(D)
    trans_options = ['T', 'N', 'C', 0, 1, 2]
    lower_options = [True, False]
    for trans, lower in itertools.product(trans_options, lower_options):
        def fun(b):
            return to_scalar(spla.solve_triangular(A, b, trans=trans, lower=lower))
        yield check_grads, fun, npr.randn(D)

def test_solve_triangular_arg2_2d():
    D = 6
    A = npr.randn(D, D) + 10*np.eye(D)
    trans_options = ['T', 'N', 'C', 0, 1, 2]
    lower_options = [True, False]
    for trans, lower in itertools.product(trans_options, lower_options):
        def fun(B):
            return to_scalar(spla.solve_triangular(A, B, trans=trans, lower=lower))
        yield check_grads, fun, npr.randn(D, D-1)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
test_djangocms_highlightjs
------------

Tests for `djangocms_highlightjs` modules module.
"""
from cms.views import details
from django.contrib.auth.models import AnonymousUser

from djangocms_highlightjs.models import HighlightText

from . import BaseTest

class TestHighlightjsModels(BaseTest):
    example_text = """
    def print_hello():
        print("hello world!")
    """

    def setUp(self):
        pass

    def test_add_plugin(self):
        from cms.api import add_plugin
        page_1, page_2 = self.get_pages()
        data = {
            'filename': 'test.py',
            'body': self.example_text,
            'theme': 'dark'
        }
        placeholder = page_1.placeholders.get(slot='content')
        add_plugin(placeholder, 'HighlightPlugin', 'en', **data)
        page_1.publish('en')

        # Get published page
        public = page_1.get_public_object()
        # plugin is the plugin instance we're going to render
        plugin = public.placeholders.get(slot='content').get_plugins_list()[0]

        request = self.get_page_request(public, AnonymousUser())
        response = details(request, '')
        self.assertContains(response, '<link rel="stylesheet" href="/static/djangocms_highlightjs/themes/dark.css">')
        self.assertContains(response, '''
\n\ttest.py\n\t
    def print_hello():
        print("hello world!")

''' % plugin.pk)
        self.assertContains(response, '')

    def test_model_save(self):

        plugin = HighlightText()
        plugin.body = self.example_text
        plugin.theme = "arta"
        plugin.save()

        self.assertEqual(self.example_text, plugin.body)

    def tearDown(self):
        pass<commit_msg>Fix tests<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
test_djangocms_highlightjs
------------

Tests for `djangocms_highlightjs` modules module.
"""
from cms.views import details
from django.contrib.auth.models import AnonymousUser

from djangocms_highlightjs.models import HighlightText

from . import BaseTest

class TestHighlightjsModels(BaseTest):
    example_text = """
    def print_hello():
        print("hello world!")
    """

    def setUp(self):
        pass

    def test_add_plugin(self):
        from cms.api import add_plugin
        page_1, page_2 = self.get_pages()
        data = {
            'filename': 'test.py',
            'body': self.example_text,
            'theme': 'dark'
        }
        placeholder = page_1.placeholders.get(slot='content')
        add_plugin(placeholder, 'HighlightPlugin', 'en', **data)
        page_1.publish('en')

        # Get published page
        public = page_1.get_public_object()
        # plugin is the plugin instance we're going to render
        plugin = public.placeholders.get(slot='content').get_plugins_list()[0]

        request = self.get_page_request(public, AnonymousUser())
        response = details(request, '')
        self.assertContains(response, '<link rel="stylesheet" href="/static/djangocms_highlightjs/themes/dark.css">')
        self.assertContains(response, '''
\n\ttest.py\n\t
    def print_hello():
        print("hello world!")

''' % plugin.pk)
        self.assertContains(response, '')

    def test_model_save(self):

        plugin = HighlightText()
        plugin.body = self.example_text
        plugin.theme = "arta"
        plugin.save()

        self.assertEqual(self.example_text, plugin.body)

    def tearDown(self):
        pass<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import unittest

from datetime import datetime

from mongoengine import connect
from mongoengine import Document
from mongoengine.fields import ListField, StringField, DateTimeField
from twitter_watcher.db import Listener

class ListernerModel(unittest.TestCase):

	@classmethod
	def setUpClass(cls):
		connect('twitter_watcher_test')

	def test_subclass_document(self):
		assert issubclass(Listener, Document)

	def test_has_field_usernames(self):
		assert hasattr(Listener, 'usernames')
		assert isinstance(Listener.usernames, ListField)

	def test_has_field_hashtags(self):
		assert hasattr(Listener, 'hashtags')
		assert isinstance(Listener.hashtags, ListField)

	def test_has_field_start_date(self):
		assert hasattr(Listener, 'start_date')
		assert isinstance(Listener.start_date, DateTimeField)

	def test_has_field_end_date(self):
		assert hasattr(Listener, 'end_date')
		assert isinstance(Listener.end_date, DateTimeField)

	def test_create_a_listener(self):
		listener = Listener(usernames=['@teste1', '@teste2'],
							hashtags=['#blabla', '#blabla2'],
							start_date=datetime.now(),
							end_date=datetime.now())

		assert listener.id is None
		listener.save()
		assert listener.id is not None

		#finding
		listerner = Listener.objects(id=listener.id)
		self.assertEquals(listener.usernames, ['@teste1', '@teste2'])
		self.assertEquals(listener.hashtags, ['#blabla', '#blabla2'])

<commit_msg>Putting a teardown method to cleanup every object created in database<commit_after># -*- coding: utf-8 -*-
import unittest

from datetime import datetime

from mongoengine import connect
from mongoengine import Document
from mongoengine.fields import ListField, StringField, DateTimeField
from twitter_watcher.db import Listener

class ListernerModel(unittest.TestCase):

	@classmethod
	def setUpClass(cls):
		connect('twitter_watcher_test')

	def tearDown(self):
		for listener in Listener.objects:
			listener.delete()

	def test_subclass_document(self):
		assert issubclass(Listener, Document)

	def test_has_field_usernames(self):
		assert hasattr(Listener, 'usernames')
		assert isinstance(Listener.usernames, ListField)

	def test_has_field_hashtags(self):
		assert hasattr(Listener, 'hashtags')
		assert isinstance(Listener.hashtags, ListField)

	def test_has_field_start_date(self):
		assert hasattr(Listener, 'start_date')
		assert isinstance(Listener.start_date, DateTimeField)

	def test_has_field_end_date(self):
		assert hasattr(Listener, 'end_date')
		assert isinstance(Listener.end_date, DateTimeField)

	def test_create_a_listener(self):
		listener = Listener(usernames=['@teste1', '@teste2'],
							hashtags=['#blabla', '#blabla2'],
							start_date=datetime.now(),
							end_date=datetime.now())

		assert listener.id is None
		listener.save()
		assert listener.id is not None

		#finding
		listerner = Listener.objects(id=listener.id)
		self.assertEquals(listener.usernames, ['@teste1', '@teste2'])
		self.assertEquals(listener.hashtags, ['#blabla', '#blabla2'])

class ListenerTestCase(unittest.TestCase):
	@classmethod
	def setUpClass(cls):
		connect('twitter_watcher_test')

<|endoftext|>