<commit_before>from __future__ import division, print_function
import unittest
import pickle
from collections import OrderedDict

import numpy as np
from scipy.optimize import OptimizeResult
from symfit import (
    Variable, Parameter, Fit, FitResults, Eq, Ge, CallableNumericalModel, Model
)
from symfit.distributions import BivariateGaussian
from symfit.core.minimizers import (
    BaseMinimizer, MINPACK, BFGS, NelderMead, ChainedMinimizer, BasinHopping
)
from symfit.core.objectives import (
    LogLikelihood, LeastSquares, VectorLeastSquares, MinimizeModel
)

def ge_constraint(a):  # Has to be in the global namespace for pickle.
    return a - 1

class TestFitResults(unittest.TestCase):
    """
    Tests for the FitResults object.
    """
    def setUp(self):
        xdata = np.linspace(1, 10, 10)
        ydata = 3 * xdata ** 2

        a = Parameter('a')
        b = Parameter('b')
        x = Variable('x')
        y = Variable('y')
        model = Model({y: a * x ** b})
        self.params = [a, b]

        fit = Fit(model, x=xdata, y=ydata)
        self.fit_result = fit.execute()
        fit = Fit(model, x=xdata, y=ydata, minimizer=MINPACK)
        self.minpack_result = fit.execute()
        fit = Fit(model, x=xdata, objective=LogLikelihood)
        self.likelihood_result = fit.execute()
        fit = Fit(model, x=xdata, y=ydata, minimizer=[BFGS, NelderMead])
        self.chained_result = fit.execute()

        z = Variable('z')
        constraints = [
            Eq(a, b),
            CallableNumericalModel.as_constraint(
                {z: ge_constraint}, connectivity_mapping={z: {a}},
                constraint_type=Ge, model=model
            )
        ]
        fit = Fit(model, x=xdata, y=ydata, constraints=constraints)
        self.constrained_result = fit.execute()
        fit = Fit(model, x=xdata, y=ydata, constraints=constraints,
                  minimizer=BasinHopping)
        self.constrained_basinhopping_result = fit.execute()

    def test_params_type(self):
        self.assertIsInstance(self.fit_result.params, OrderedDict)

    def test_minimizer_output_type(self):
        self.assertIsInstance(self.fit_result.minimizer_output, dict)
        self.assertIsInstance(self.minpack_result.minimizer_output, dict)
        self.assertIsInstance(self.likelihood_result.minimizer_output, dict)

    def test_fitting(self):
        """
        Test if the fitting worked in the first place.
        """
        a, b = self.params
        fit_result = self.fit_result
        self.assertIsInstance(fit_result, FitResults)
        self.assertAlmostEqual(fit_result.value(a), 3.0)
        self.assertAlmostEqual(fit_result.value(b), 2.0)

        self.assertIsInstance(fit_result.stdev(a), float)
        self.assertIsInstance(fit_result.stdev(b), float)

        self.assertIsInstance(fit_result.r_squared, float)
        # by definition since there's no fuzzyness
        self.assertEqual(fit_result.r_squared, 1.0)

    def test_fitting_2(self):
        np.random.seed(43)
        mean = (0.62, 0.71)  # x, y mean 0.7, 0.7
        cov = [
            [0.102**2, 0],
            [0, 0.07**2]
        ]
        data_1 = np.random.multivariate_normal(mean, cov, 10**5)
        mean = (0.33, 0.28)  # x, y mean 0.3, 0.3
        cov = [  # rho = 0.25
            [0.05 ** 2, 0.25 * 0.05 * 0.101],
            [0.25 * 0.05 * 0.101, 0.101 ** 2]
        ]
        data_2 = np.random.multivariate_normal(mean, cov, 10**5)
        data = np.vstack((data_1, data_2))

        # Insert them as y,x here as np fucks up cartesian conventions.
        ydata, xedges, yedges = np.histogram2d(data[:, 1], data[:, 0], bins=200,
                                               range=[[0.0, 1.0], [0.0, 1.0]],
                                               density=True)
        xcentres = (xedges[:-1] + xedges[1:]) / 2
        ycentres = (yedges[:-1] + yedges[1:]) / 2

        # Make a valid grid to match ydata
        xx, yy = np.meshgrid(xcentres, ycentres, sparse=False)

        x = Variable('x')
        y = Variable('y')

        x0_1 = Parameter('x0_1', value=0.6, min=0.5, max=0.7)
        sig_x_1 = Parameter('sig_x_1', value=0.1, min=0.0, max=0.2)
        y0_1 = Parameter('y0_1', value=0.7, min=0.6, max=0.8)
        sig_y_1 = Parameter('sig_y_1', value=0.05, min=0.0, max=0.2)
        rho_1 = Parameter('rho_1', value=0.0, min=-0.5, max=0.5)
        A_1 = Parameter('A_1', value=0.5, min=0.3, max=0.7)
        g_1 = A_1 * BivariateGaussian(x=x, y=y, mu_x=x0_1, mu_y=y0_1,
                                      sig_x=sig_x_1, sig_y=sig_y_1, rho=rho_1)

        x0_2 = Parameter('x0_2', value=0.3, min=0.2, max=0.4)
        sig_x_2 = Parameter('sig_x_2', value=0.05, min=0.0, max=0.2)
        y0_2 = Parameter('y0_2', value=0.3, min=0.2, max=0.4)
        sig_y_2 = Parameter('sig_y_2', value=0.1, min=0.0, max=0.2)
        rho_2 = Parameter('rho_2', value=0.26, min=0.0, max=0.8)
        A_2 = Parameter('A_2', value=0.5, min=0.3, max=0.7)
        g_2 = A_2 * BivariateGaussian(x=x, y=y, mu_x=x0_2, mu_y=y0_2,
                                      sig_x=sig_x_2, sig_y=sig_y_2, rho=rho_2)

        model = g_1 + g_2
        fit = Fit(model, xx, yy, ydata)
        fit_result = fit.execute()

        self.assertGreater(fit_result.r_squared, 0.95)
        for param in fit.model.params:
            try:
                self.assertAlmostEqual(fit_result.stdev(param)**2 / fit_result.variance(param), 1.0)
            except AssertionError:
                self.assertLessEqual(fit_result.variance(param), 0.0)
                self.assertTrue(np.isnan(fit_result.stdev(param)))

        # Covariance matrix should be symmetric
        for param_1 in fit.model.params:
            for param_2 in fit.model.params:
                self.assertAlmostEqual(fit_result.covariance(param_1, param_2) / fit_result.covariance(param_2, param_1), 1.0, 3)

    def test_minimizer_included(self):
        """"The minimizer used should be included in the results."""
        self.assertIsInstance(self.constrained_result.minimizer, BaseMinimizer)
        self.assertIsInstance(self.constrained_basinhopping_result.minimizer,
                              BaseMinimizer)
        self.assertIsInstance(self.likelihood_result.minimizer, BaseMinimizer)
        self.assertIsInstance(self.fit_result.minimizer, BaseMinimizer)
        self.assertIsInstance(self.chained_result.minimizer, ChainedMinimizer)
        for minimizer, cls in zip(self.chained_result.minimizer.minimizers,
                                  [BFGS, NelderMead]):
            self.assertIsInstance(minimizer, cls)

    def test_objective_included(self):
        """"The objective used should be included in the results."""
        self.assertIsInstance(self.fit_result.objective, LeastSquares)
        self.assertIsInstance(self.minpack_result.objective, VectorLeastSquares)
        self.assertIsInstance(self.likelihood_result.objective, LogLikelihood)
        self.assertIsInstance(self.constrained_result.objective, LeastSquares)
        self.assertIsInstance(self.constrained_basinhopping_result.objective, LeastSquares)

    def test_constraints_included(self):
        """
        Test if the constraints have been properly fed to the results object so
        we can easily print their compliance.
        """
        # For a constrained fit we expect a list of MinimizeModel objectives.
        for constrained_result in [self.constrained_result,
                                   self.constrained_basinhopping_result]:
            self.assertIsInstance(constrained_result.constraints, list)
            for constraint in constrained_result.constraints:
                self.assertIsInstance(constraint, MinimizeModel)

    def test_message_included(self):
        """Status message should be included."""
        self.assertIsInstance(self.fit_result.status_message, str)
        self.assertIsInstance(self.minpack_result.status_message, str)
        self.assertIsInstance(self.likelihood_result.status_message, str)
        self.assertIsInstance(self.constrained_result.status_message, str)
        self.assertIsInstance(
            self.constrained_basinhopping_result.status_message, str
        )

    def test_pickle(self):
        for fit_result in [self.fit_result, self.chained_result,
                           self.constrained_basinhopping_result,
                           self.constrained_result, self.likelihood_result]:
            dumped = pickle.dumps(fit_result)
            new_result = pickle.loads(dumped)
            self.assertEqual(sorted(fit_result.__dict__.keys()),
                             sorted(new_result.__dict__.keys()))
            for k, v1 in fit_result.__dict__.items():
                v2 = new_result.__dict__[k]
                if k == 'minimizer':
                    self.assertEqual(type(v1), type(v2))
                elif k != 'minimizer_output':  # Ignore minimizer_output
                    if isinstance(v1, np.ndarray):
                        np.testing.assert_almost_equal(v1, v2)
                    else:
                        self.assertEqual(v1, v2)

    def test_gof_presence(self):
        """
        Test if the expected goodness of fit estimators are present.
        """
        self.assertTrue(hasattr(self.fit_result, 'objective_value'))
        self.assertTrue(hasattr(self.fit_result, 'r_squared'))
        self.assertTrue(hasattr(self.fit_result, 'chi_squared'))
        self.assertFalse(hasattr(self.fit_result, 'log_likelihood'))
        self.assertFalse(hasattr(self.fit_result, 'likelihood'))

        self.assertTrue(hasattr(self.minpack_result, 'objective_value'))
        self.assertTrue(hasattr(self.minpack_result, 'r_squared'))
        self.assertTrue(hasattr(self.minpack_result, 'chi_squared'))
        self.assertFalse(hasattr(self.minpack_result, 'log_likelihood'))
        self.assertFalse(hasattr(self.minpack_result, 'likelihood'))

        self.assertTrue(hasattr(self.likelihood_result, 'objective_value'))
        self.assertFalse(hasattr(self.likelihood_result, 'r_squared'))
        self.assertFalse(hasattr(self.likelihood_result, 'chi_squared'))
        self.assertTrue(hasattr(self.likelihood_result, 'log_likelihood'))
        self.assertTrue(hasattr(self.likelihood_result, 'likelihood'))

if __name__ == '__main__':
    unittest.main()
<commit_msg>Convert test_fit_result.py<commit_after>from __future__ import division, print_function
import pytest
import pickle
from collections import OrderedDict

import numpy as np
from scipy.optimize import OptimizeResult
from symfit import (
    Variable, Parameter, Fit, FitResults, Eq, Ge, CallableNumericalModel, Model
)
from symfit.distributions import BivariateGaussian
from symfit.core.minimizers import (
    BaseMinimizer, MINPACK, BFGS, NelderMead, ChainedMinimizer, BasinHopping
)
from symfit.core.objectives import (
    LogLikelihood, LeastSquares, VectorLeastSquares, MinimizeModel
)

def ge_constraint(a):  # Has to be in the global namespace for pickle.
    return a - 1

class TestTestResult():

    @classmethod
    def setup_class(cls):

        xdata = np.linspace(1, 10, 10)
        ydata = 3 * xdata ** 2

        cls.a = Parameter('a')
        cls.b = Parameter('b')

        x = Variable('x')
        y = Variable('y')
        model = Model({y: cls.a * x ** cls.b})

        fit = Fit(model, x=xdata, y=ydata)
        cls.fit_result = fit.execute()
        fit = Fit(model, x=xdata, y=ydata, minimizer=MINPACK)
        cls.minpack_result = fit.execute()
        fit = Fit(model, x=xdata, objective=LogLikelihood)
        cls.likelihood_result = fit.execute()
        fit = Fit(model, x=xdata, y=ydata, minimizer=[BFGS, NelderMead])
        cls.chained_result = fit.execute()

        z = Variable('z')
        constraints = [
            Eq(cls.a, cls.b),
            CallableNumericalModel.as_constraint(
                {z: ge_constraint}, connectivity_mapping={z: {cls.a}},
                constraint_type=Ge, model=model
            )
        ]
        fit = Fit(model, x=xdata, y=ydata, constraints=constraints)
        cls.constrained_result = fit.execute()
        fit = Fit(model, x=xdata, y=ydata, constraints=constraints,
                minimizer=BasinHopping)
        cls.constrained_basinhopping_result = fit.execute()

    def test_params_type(self):
        assert isinstance(self.fit_result.params, OrderedDict)

    def test_minimizer_output_type(self):
        assert isinstance(self.fit_result.minimizer_output, dict)
        assert isinstance(self.minpack_result.minimizer_output, dict)
        assert isinstance(self.likelihood_result.minimizer_output, dict)

    def test_fitting(self):
        """
        Test if the fitting worked in the first place.
        """

        assert isinstance(self.fit_result, FitResults)
        assert self.fit_result.value(self.a) == pytest.approx(3.0)
        assert self.fit_result.value(self.b) == pytest.approx(2.0)

        assert isinstance(self.fit_result.stdev(self.a), float)
        assert isinstance(self.fit_result.stdev(self.b), float)

        assert isinstance(self.fit_result.r_squared, float)
        # by definition since there's no fuzzyness
        assert self.fit_result.r_squared == 1.0

    def test_fitting_2(self):
        np.random.seed(43)
        mean = (0.62, 0.71)  # x, y mean 0.7, 0.7
        cov = [
            [0.102**2, 0],
            [0, 0.07**2]
        ]
        data_1 = np.random.multivariate_normal(mean, cov, 10**5)
        mean = (0.33, 0.28)  # x, y mean 0.3, 0.3
        cov = [  # rho = 0.25
            [0.05 ** 2, 0.25 * 0.05 * 0.101],
            [0.25 * 0.05 * 0.101, 0.101 ** 2]
        ]
        data_2 = np.random.multivariate_normal(mean, cov, 10**5)
        data = np.vstack((data_1, data_2))

        # Insert them as y,x here as np fucks up cartesian conventions.
        ydata, xedges, yedges = np.histogram2d(data[:, 1], data[:, 0], bins=200,
                                            range=[[0.0, 1.0], [0.0, 1.0]],
                                            density=True)
        xcentres = (xedges[:-1] + xedges[1:]) / 2
        ycentres = (yedges[:-1] + yedges[1:]) / 2

        # Make a valid grid to match ydata
        xx, yy = np.meshgrid(xcentres, ycentres, sparse=False)

        x = Variable('x')
        y = Variable('y')

        x0_1 = Parameter('x0_1', value=0.6, min=0.5, max=0.7)
        sig_x_1 = Parameter('sig_x_1', value=0.1, min=0.0, max=0.2)
        y0_1 = Parameter('y0_1', value=0.7, min=0.6, max=0.8)
        sig_y_1 = Parameter('sig_y_1', value=0.05, min=0.0, max=0.2)
        rho_1 = Parameter('rho_1', value=0.0, min=-0.5, max=0.5)
        A_1 = Parameter('A_1', value=0.5, min=0.3, max=0.7)
        g_1 = A_1 * BivariateGaussian(x=x, y=y, mu_x=x0_1, mu_y=y0_1,
                                    sig_x=sig_x_1, sig_y=sig_y_1, rho=rho_1)

        x0_2 = Parameter('x0_2', value=0.3, min=0.2, max=0.4)
        sig_x_2 = Parameter('sig_x_2', value=0.05, min=0.0, max=0.2)
        y0_2 = Parameter('y0_2', value=0.3, min=0.2, max=0.4)
        sig_y_2 = Parameter('sig_y_2', value=0.1, min=0.0, max=0.2)
        rho_2 = Parameter('rho_2', value=0.26, min=0.0, max=0.8)
        A_2 = Parameter('A_2', value=0.5, min=0.3, max=0.7)
        g_2 = A_2 * BivariateGaussian(x=x, y=y, mu_x=x0_2, mu_y=y0_2,
                                    sig_x=sig_x_2, sig_y=sig_y_2, rho=rho_2)

        model = g_1 + g_2
        fit = Fit(model, xx, yy, ydata)
        fit_result = fit.execute()

        assert fit_result.r_squared > 0.95
        for param in fit.model.params:
            try:
                assert fit_result.stdev(
                    param)**2 / fit_result.variance(param) == pytest.approx(1.0)
            except AssertionError:
                assert fit_result.variance(param) <= 0.0
                assert np.isnan(fit_result.stdev(param))

        # Covariance matrix should be symmetric
        for param_1 in fit.model.params:
            for param_2 in fit.model.params:
                assert fit_result.covariance(
                    param_1, param_2) / fit_result.covariance(param_2, param_1) == pytest.approx(1.0, 1e-3)

    def test_minimizer_included(self):
        """"The minimizer used should be included in the results."""

        assert isinstance(self.constrained_result.minimizer, BaseMinimizer)
        assert isinstance(self.constrained_basinhopping_result.minimizer,
                        BaseMinimizer)
        assert isinstance(self.likelihood_result.minimizer, BaseMinimizer)
        assert isinstance(self.fit_result.minimizer, BaseMinimizer)
        assert isinstance(self.chained_result.minimizer, ChainedMinimizer)
        for minimizer, cls in zip(self.chained_result.minimizer.minimizers,
                                [BFGS, NelderMead]):
            assert isinstance(minimizer, cls)

    def test_objective_included(self):
        """"The objective used should be included in the results."""
        assert isinstance(self.fit_result.objective, LeastSquares)
        assert isinstance(self.minpack_result.objective, VectorLeastSquares)
        assert isinstance(self.likelihood_result.objective, LogLikelihood)
        assert isinstance(self.constrained_result.objective, LeastSquares)
        assert isinstance(self.constrained_basinhopping_result.objective, LeastSquares)

    def test_constraints_included(self):
        """
        Test if the constraints have been properly fed to the results object so
        we can easily print their compliance.
        """
        # For a constrained fit we expect a list of MinimizeModel objectives.
        for constrained_result in [self.constrained_result,
                                self.constrained_basinhopping_result]:
            assert isinstance(constrained_result.constraints, list)
            for constraint in self.constrained_result.constraints:
                assert isinstance(constraint, MinimizeModel)

    def test_message_included(self):
        """Status message should be included."""
        assert isinstance(self.fit_result.status_message, str)
        assert isinstance(self.minpack_result.status_message, str)
        assert isinstance(self.likelihood_result.status_message, str)
        assert isinstance(self.constrained_result.status_message, str)
        assert isinstance(
            self.constrained_basinhopping_result.status_message, str
        )

    def test_pickle(self):
        for fit_result in [self.fit_result, self.chained_result,
                        self.constrained_basinhopping_result,
                        self.constrained_result, self.likelihood_result]:
            dumped = pickle.dumps(fit_result)
            new_result = pickle.loads(dumped)
            assert sorted(fit_result.__dict__.keys()) == sorted(
                new_result.__dict__.keys())
            for k, v1 in fit_result.__dict__.items():
                v2 = new_result.__dict__[k]
                if k == 'minimizer':
                    assert type(v1) == type(v2)
                elif k != 'minimizer_output':  # Ignore minimizer_output
                    if isinstance(v1, np.ndarray):
                        assert v1 == pytest.approx(v2)
                    else:
                        assert v1 == v2

    def test_gof_presence(self):
        """
        Test if the expected goodness of fit estimators are present.
        """
        assert hasattr(self.fit_result, 'objective_value')
        assert hasattr(self.fit_result, 'r_squared')
        assert hasattr(self.fit_result, 'chi_squared')
        assert not hasattr(self.fit_result, 'log_likelihood')
        assert not hasattr(self.fit_result, 'likelihood')

        assert hasattr(self.minpack_result, 'objective_value')
        assert hasattr(self.minpack_result, 'r_squared')
        assert hasattr(self.minpack_result, 'chi_squared')
        assert not hasattr(self.minpack_result, 'log_likelihood')
        assert not hasattr(self.minpack_result, 'likelihood')

        assert hasattr(self.likelihood_result, 'objective_value')
        assert not hasattr(self.likelihood_result, 'r_squared')
        assert not hasattr(self.likelihood_result, 'chi_squared')
        assert hasattr(self.likelihood_result, 'log_likelihood')
        assert hasattr(self.likelihood_result, 'likelihood')<|endoftext|>


<commit_before>r"""*Test(s) to ensure full loading of flake8 extensions*.

``sphobjinv`` is a toolkit for manipulation and inspection of
Sphinx |objects.inv| files.

**Author**
    Brian Skinn (bskinn@alum.mit.edu)

**File Created**
    27 Apr 2019

**Copyright**
    \(c) Brian Skinn 2016-2019

**Source Repository**
    http://www.github.com/bskinn/sphobjinv

**Documentation**
    http://sphobjinv.readthedocs.io

**License**
    The MIT License; see |license_txt|_ for full license terms

**Members**

"""

import re
import subprocess as sp  # noqa: S404
import sys
import urllib.request as urlrq

import certifi
import pytest

@pytest.fixture(scope="module", autouse=True)
def skip_if_no_nonloc(pytestconfig):
    """Skip test if --nonloc not provided.

    Auto-applied to all functions in module, since module is nonlocal.

    """
    if not pytestconfig.getoption("--nonloc"):
        pytest.skip("'--nonloc' not specified")

@pytest.fixture()
def testmodule(tmp_path):
    """Retrieve the flake8 extension test module as a BytesIO."""
    main_page = urlrq.urlopen(  # noqa: S310
        "https://gist.github.com/bskinn/ab328d0d9233586641455ac73056f946",
        cafile=certifi.where(),
    ).read()
    raw_url_tail = re.search(rb'"([^"]+/raw/[^"]+)"', main_page, re.I).group(1)
    module_contents = urlrq.urlopen(  # noqa: S310
        "https://gist.github.com" + raw_url_tail.decode(), cafile=certifi.where()
    ).read()
    testmod_path = tmp_path / "flake8_ext_test.py"

    with testmod_path.open("w") as f:
        f.write(module_contents.decode())

    yield testmod_path

@pytest.mark.skipif(
    sys.version_info < (3, 6),
    reason="Some flake8 extensions require Python 3.6 or later",
)
@pytest.mark.nonloc
@pytest.mark.timeout(10)
def test_flake8_extensions(testmodule, subtests, tmp_path):
    """Check the flake8 extension tester module and confirm all expected reports.

    This test is set up to retrieve the file from gist because it would be
    annoying to keep it in-repo: the flake8 config would take all sorts of rejiggering.

    """
    expected_errors = """

    (core flake8)
    flake8/pyflakes         F401, W293
    pycodestyle             E501
    mccabe                  C901

    (extensions)
    flake8-2020             YTT303
    flake8-bandit           S110
    flake8-black            BLK100
    flake8-bugbear          B006
    flake8-builtins         A002
    flake8-comprehensions   C400
    flake8-docstrings       D103
    flake8-eradicate        E800
    flake8-import-order     I100
    flake8-pie              PIE781
    flake8-rst-docstrings   RST212
    pep8-naming             N802

    """
    try:
        sp.check_output(
            "flake8 --max-complexity 1 {}".format(str(testmodule.resolve())),
            shell=True,  # noqa: S602
        )
    except sp.CalledProcessError as e:
        retcode = e.returncode
        output = e.output.decode()
    else:
        pytest.fail("Test file unexpectedly passed flake8 check.")

    assert 1 == retcode

    for code in (
        _.group(0) for _ in re.finditer(r"[A-Z]{1,3}[0-9]{1,3}", expected_errors)
    ):
        with subtests.test(id=code):
            assert code in output, output
<commit_msg>Switch to direct test of flake8 plugins<commit_after>r"""*Test(s) to ensure full loading of flake8 extensions*.

``sphobjinv`` is a toolkit for manipulation and inspection of
Sphinx |objects.inv| files.

**Author**
    Brian Skinn (bskinn@alum.mit.edu)

**File Created**
    27 Apr 2019

**Copyright**
    \(c) Brian Skinn 2016-2019

**Source Repository**
    http://www.github.com/bskinn/sphobjinv

**Documentation**
    http://sphobjinv.readthedocs.io

**License**
    The MIT License; see |license_txt|_ for full license terms

**Members**

"""

import re
import subprocess as sp  # noqa: S404
import sys
from pathlib import Path

import pytest

@pytest.mark.skipif(
    sys.version_info < (3, 6),
    reason="Some flake8 extensions require Python 3.6 or later",
)
def test_flake8_version_output(subtests):
    """Confirm that all desired plugins actually report as loaded."""
    p_pkgname = re.compile("^[0-9a-z_-]+", re.I)
    plugins = Path("requirements-flake8.txt").read_text().splitlines()[1:]
    plugins = [p_pkgname.search(p).group(0) for p in plugins]

    # This is fragile if anything ends up not having a prefix that needs
    # stripping
    plugins = [p.partition("-")[-1] for p in plugins]

    flake8_ver_output = sp.check_output(  # noqa: S607,S603
        ["flake8", "--version"], text=True
    )  # noqa: S607,S603

    for i, p in enumerate(plugins):
        with subtests.test(msg=p, i=i):
            assert p in flake8_ver_output
<|endoftext|>


<commit_before>#!/usr/bin/env python

"""Functional tests for tvnamer tests
"""

import os
from functional_runner import run_tvnamer, verify_out_data
from helpers import attr
import pytest

@attr("functional")
def test_simple_single_file():
    """Test most simple usage
    """

    out_data = run_tvnamer(
        with_files = ['scrubs.s01e01.avi'],
        with_input = "1\ny\n")

    expected_files = ['Scrubs - [01x01] - My First Day.avi']

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_simple_multiple_files():
    """Tests simple interactive usage with multiple files
    """

    input_files = [
        'scrubs.s01e01.hdtv.fake.avi',
        'my.name.is.earl.s01e01.fake.avi',
        'a.nonsensical.fake.show.s12e24.fake.avi',
        'total.access.s01e01.avi']

    expected_files = [
        'Scrubs - [01x01] - My First Day.avi',
        'My Name Is Earl - [01x01] - Pilot.avi',
        'a nonsensical fake show - [12x24].avi',
         'Total Access 24_7 - [01x01] - Episode #1.avi']

    out_data = run_tvnamer(
        with_files = input_files,
        with_input = "y\n1\ny\n1\ny\n1\ny\ny\n")

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_simple_batch_functionality():
    """Tests renaming single files at a time, in batch mode
    """

    tests = [
        {'in':'scrubs.s01e01.hdtv.fake.avi',
        'expected':'Scrubs - [01x01] - My First Day.avi'},
        {'in':'my.name.is.earl.s01e01.fake.avi',
        'expected':'My Name Is Earl - [01x01] - Pilot.avi'},
        {'in':'a.fake.show.s12e24.fake.avi',
        'expected':'a.fake.show.s12e24.fake.avi'},
        {'in': 'total.access.s01e01.avi',
        'expected': 'Total Access 24_7 - [01x01] - Episode #1.avi'},
    ]

    for curtest in tests:

        print("Expecting %r to turn into %r" % (
            curtest['in'], curtest['expected']))
        out_data = run_tvnamer(
            with_files = [curtest['in'], ],
            with_flags = ['--batch'],
        )
        verify_out_data(out_data, [curtest['expected'], ])

@attr("functional")
def test_interactive_always_option():
    """Tests the "a" always rename option in interactive UI
    """

    input_files = [
        'scrubs.s01e01.hdtv.fake.avi',
        'my.name.is.earl.s01e01.fake.avi',
        'a.nonsensical.fake.show.s12e24.fake.avi',
        'total.access.s01e01.avi']

    expected_files = [
        'Scrubs - [01x01] - My First Day.avi',
        'My Name Is Earl - [01x01] - Pilot.avi',
        'a nonsensical fake show - [12x24].avi',
         'Total Access 24_7 - [01x01] - Episode #1.avi']

    out_data = run_tvnamer(
        with_files = input_files,
        with_flags = ["--selectfirst"],
        with_input = "a\n")

    verify_out_data(out_data, expected_files)

@attr("functional")
@pytest.mark.skipif(os.getenv("TRAVIS", "false")=="true", reason="Test fails for some reason on Travis-CI")
def test_unicode_in_inputname():
    """Tests parsing a file with unicode in the input filename
    """

    import os, sys
    if os.getenv("TRAVIS", "false") == "true" and sys.version_info[0:2] == (2.6):
        from nose.plugins.skip import SkipTest
        raise SkipTest("Ignoring test which triggers bizarre bug in nosetests, in python 2.6, only on travis.")

    input_files = [
        u'The Big Bang Theory - S02E07 - The Panty Pin\u0303ata Polarization.avi']

    expected_files = [
        u'The Big Bang Theory - [02x07] - The Panty Pin\u0303ata Polarization.avi']

    out_data = run_tvnamer(
        with_files = input_files,
        with_flags = ["--batch"])

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_unicode_in_search_results():
    """Show with unicode in search results
    """
    input_files = [
        'psych.s04e11.avi']

    expected_files = [
        'Psych - [04x11] - Thrill Seekers and Hell-Raisers.avi']

    out_data = run_tvnamer(
        with_files = input_files,
        with_input = '1\ny\n')

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_renaming_always_doesnt_overwrite():
    """If trying to rename a file that exists, should not create new file
    """
    input_files = [
        'Scrubs.s01e01.avi',
        'Scrubs - [01x01] - My First Day.avi']

    expected_files = [
        'Scrubs.s01e01.avi',
        'Scrubs - [01x01] - My First Day.avi']

    out_data = run_tvnamer(
        with_files = input_files,
        with_flags = ['--batch'])

    verify_out_data(out_data, expected_files)

@attr("functional")
@pytest.mark.skipif(os.getenv("TRAVIS", "false")=="true", reason="Test fails for some reason on Travis-CI")
def test_not_overwritting_unicode_filename():
    """Test no error occurs when warning about a unicode filename being overwritten
    """
    input_files = [
        u'The Big Bang Theory - S02E07.avi',
        u'The Big Bang Theory - [02x07] - The Panty Pin\u0303ata Polarization.avi']

    expected_files = [
        u'The Big Bang Theory - S02E07.avi',
        u'The Big Bang Theory - [02x07] - The Panty Pin\u0303ata Polarization.avi']

    out_data = run_tvnamer(
        with_files = input_files,
        with_flags = ['--batch'])

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_not_recursive():
    """Tests the nested files aren't found when not recursive
    """
    input_files = [
        'Scrubs.s01e01.avi',
        'nested/subdir/Scrubs.s01e02.avi']

    expected_files = [
        'Scrubs - [01x01] - My First Day.avi',
        'nested/subdir/Scrubs.s01e02.avi']

    out_data = run_tvnamer(
        with_files = input_files,
        with_flags = ['--not-recursive', '--batch'],
        run_on_directory = True)

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_correct_filename():
    """If the filename is already correct, don't prompt
    """

    out_data = run_tvnamer(
        with_files = ['Scrubs - [01x01] - My First Day.avi'],
        with_input = "1\ny\n")

    expected_files = ['Scrubs - [01x01] - My First Day.avi']

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_filename_already_exists():
    """Don't overwrite
    """

    out_data = run_tvnamer(
        with_files = ['Scrubs - [01x01] - My First Day.avi', 'scrubs.s01e01.avi'],
        with_input = "1\ny\n")

    expected_files = ['Scrubs - [01x01] - My First Day.avi', 'scrubs.s01e01.avi']

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_no_seasonnumber():
    """Test episode with no series number
    """

    out_data = run_tvnamer(
        with_files = ['scrubs.e01.avi'],
        with_flags = ['--batch'])

    expected_files = ['Scrubs - [01] - My First Day.avi']

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_skipping_after_replacements():
    """When custom-replacement is specified, should still skip file if name is correct
    """

    conf = """
    {"select_first": true,
    "input_filename_replacements": [
        {"is_regex": false,
        "match": "v",
        "replacement": "u"}
    ],
    "output_filename_replacements": [
        {"is_regex": false,
        "match": "u",
        "replacement": "v"}
    ]
    }
    """

    out_data = run_tvnamer(
        with_files = ['Scrvbs - [01x01] - My First Day.avi'],
        with_config = conf,
        with_input = "")

    expected_files = ['Scrvbs - [01x01] - My First Day.avi']

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_dvd_order():
    """Tests TvDB dvd order naming
    """

    input_files = [
        'batman the animated series s01e01.xvid']

    expected_files = [
        'Batman - The Animated Series - [01x01] - On Leather Wings.xvid']

    conf = r"""
    {
      "output_filename_replacements": [
        {"is_regex": true,
        "match": ": ",
        "replacement": " - "}
      ]
    }
    """

    out_data = run_tvnamer(
        with_files = input_files,
        with_flags = ["--order", 'dvd'],
        with_input = "1\ny\n",
        with_config = conf)

    verify_out_data(out_data, expected_files)
<commit_msg>Fix test - cache is case-sensitive<commit_after>#!/usr/bin/env python

"""Functional tests for tvnamer tests
"""

import os
from functional_runner import run_tvnamer, verify_out_data
from helpers import attr
import pytest

@attr("functional")
def test_simple_single_file():
    """Test most simple usage
    """

    out_data = run_tvnamer(
        with_files = ['scrubs.s01e01.avi'],
        with_input = "1\ny\n")

    expected_files = ['Scrubs - [01x01] - My First Day.avi']

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_simple_multiple_files():
    """Tests simple interactive usage with multiple files
    """

    input_files = [
        'scrubs.s01e01.hdtv.fake.avi',
        'my.name.is.earl.s01e01.fake.avi',
        'a.nonsensical.fake.show.s12e24.fake.avi',
        'total.access.s01e01.avi']

    expected_files = [
        'Scrubs - [01x01] - My First Day.avi',
        'My Name Is Earl - [01x01] - Pilot.avi',
        'a nonsensical fake show - [12x24].avi',
         'Total Access 24_7 - [01x01] - Episode #1.avi']

    out_data = run_tvnamer(
        with_files = input_files,
        with_input = "y\n1\ny\n1\ny\n1\ny\ny\n")

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_simple_batch_functionality():
    """Tests renaming single files at a time, in batch mode
    """

    tests = [
        {'in':'scrubs.s01e01.hdtv.fake.avi',
        'expected':'Scrubs - [01x01] - My First Day.avi'},
        {'in':'my.name.is.earl.s01e01.fake.avi',
        'expected':'My Name Is Earl - [01x01] - Pilot.avi'},
        {'in':'a.fake.show.s12e24.fake.avi',
        'expected':'a.fake.show.s12e24.fake.avi'},
        {'in': 'total.access.s01e01.avi',
        'expected': 'Total Access 24_7 - [01x01] - Episode #1.avi'},
    ]

    for curtest in tests:

        print("Expecting %r to turn into %r" % (
            curtest['in'], curtest['expected']))
        out_data = run_tvnamer(
            with_files = [curtest['in'], ],
            with_flags = ['--batch'],
        )
        verify_out_data(out_data, [curtest['expected'], ])

@attr("functional")
def test_interactive_always_option():
    """Tests the "a" always rename option in interactive UI
    """

    input_files = [
        'scrubs.s01e01.hdtv.fake.avi',
        'my.name.is.earl.s01e01.fake.avi',
        'a.nonsensical.fake.show.s12e24.fake.avi',
        'total.access.s01e01.avi']

    expected_files = [
        'Scrubs - [01x01] - My First Day.avi',
        'My Name Is Earl - [01x01] - Pilot.avi',
        'a nonsensical fake show - [12x24].avi',
         'Total Access 24_7 - [01x01] - Episode #1.avi']

    out_data = run_tvnamer(
        with_files = input_files,
        with_flags = ["--selectfirst"],
        with_input = "a\n")

    verify_out_data(out_data, expected_files)

@attr("functional")
@pytest.mark.skipif(os.getenv("TRAVIS", "false")=="true", reason="Test fails for some reason on Travis-CI")
def test_unicode_in_inputname():
    """Tests parsing a file with unicode in the input filename
    """

    import os, sys
    if os.getenv("TRAVIS", "false") == "true" and sys.version_info[0:2] == (2.6):
        from nose.plugins.skip import SkipTest
        raise SkipTest("Ignoring test which triggers bizarre bug in nosetests, in python 2.6, only on travis.")

    input_files = [
        u'The Big Bang Theory - S02E07 - The Panty Pin\u0303ata Polarization.avi']

    expected_files = [
        u'The Big Bang Theory - [02x07] - The Panty Pin\u0303ata Polarization.avi']

    out_data = run_tvnamer(
        with_files = input_files,
        with_flags = ["--batch"])

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_unicode_in_search_results():
    """Show with unicode in search results
    """
    input_files = [
        'psych.s04e11.avi']

    expected_files = [
        'Psych - [04x11] - Thrill Seekers and Hell-Raisers.avi']

    out_data = run_tvnamer(
        with_files = input_files,
        with_input = '1\ny\n')

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_renaming_always_doesnt_overwrite():
    """If trying to rename a file that exists, should not create new file
    """
    input_files = [
        'Scrubs.s01e01.avi',
        'Scrubs - [01x01] - My First Day.avi']

    expected_files = [
        'Scrubs.s01e01.avi',
        'Scrubs - [01x01] - My First Day.avi']

    out_data = run_tvnamer(
        with_files = input_files,
        with_flags = ['--batch'])

    verify_out_data(out_data, expected_files)

@attr("functional")
@pytest.mark.skipif(os.getenv("TRAVIS", "false")=="true", reason="Test fails for some reason on Travis-CI")
def test_not_overwritting_unicode_filename():
    """Test no error occurs when warning about a unicode filename being overwritten
    """
    input_files = [
        u'The Big Bang Theory - S02E07.avi',
        u'The Big Bang Theory - [02x07] - The Panty Pin\u0303ata Polarization.avi']

    expected_files = [
        u'The Big Bang Theory - S02E07.avi',
        u'The Big Bang Theory - [02x07] - The Panty Pin\u0303ata Polarization.avi']

    out_data = run_tvnamer(
        with_files = input_files,
        with_flags = ['--batch'])

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_not_recursive():
    """Tests the nested files aren't found when not recursive
    """
    input_files = [
        'Scrubs.s01e01.avi',
        'nested/subdir/Scrubs.s01e02.avi']

    expected_files = [
        'Scrubs - [01x01] - My First Day.avi',
        'nested/subdir/Scrubs.s01e02.avi']

    out_data = run_tvnamer(
        with_files = input_files,
        with_flags = ['--not-recursive', '--batch'],
        run_on_directory = True)

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_correct_filename():
    """If the filename is already correct, don't prompt
    """

    out_data = run_tvnamer(
        with_files = ['Scrubs - [01x01] - My First Day.avi'],
        with_input = "1\ny\n")

    expected_files = ['Scrubs - [01x01] - My First Day.avi']

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_filename_already_exists():
    """Don't overwrite
    """

    out_data = run_tvnamer(
        with_files = ['Scrubs - [01x01] - My First Day.avi', 'Scrubs.s01e01.avi'],
        with_input = "1\ny\n")

    expected_files = ['Scrubs - [01x01] - My First Day.avi', 'Scrubs.s01e01.avi']

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_no_seasonnumber():
    """Test episode with no series number
    """

    out_data = run_tvnamer(
        with_files = ['scrubs.e01.avi'],
        with_flags = ['--batch'])

    expected_files = ['Scrubs - [01] - My First Day.avi']

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_skipping_after_replacements():
    """When custom-replacement is specified, should still skip file if name is correct
    """

    conf = """
    {"select_first": true,
    "input_filename_replacements": [
        {"is_regex": false,
        "match": "v",
        "replacement": "u"}
    ],
    "output_filename_replacements": [
        {"is_regex": false,
        "match": "u",
        "replacement": "v"}
    ]
    }
    """

    out_data = run_tvnamer(
        with_files = ['Scrvbs - [01x01] - My First Day.avi'],
        with_config = conf,
        with_input = "")

    expected_files = ['Scrvbs - [01x01] - My First Day.avi']

    verify_out_data(out_data, expected_files)

@attr("functional")
def test_dvd_order():
    """Tests TvDB dvd order naming
    """

    input_files = [
        'batman the animated series s01e01.xvid']

    expected_files = [
        'Batman - The Animated Series - [01x01] - On Leather Wings.xvid']

    conf = r"""
    {
      "output_filename_replacements": [
        {"is_regex": true,
        "match": ": ",
        "replacement": " - "}
      ]
    }
    """

    out_data = run_tvnamer(
        with_files = input_files,
        with_flags = ["--order", 'dvd'],
        with_input = "1\ny\n",
        with_config = conf)

    verify_out_data(out_data, expected_files)
<|endoftext|>


<commit_before># flake8: noqa
import platform
import unittest

# noinspection PyUnresolvedReferences,PyMethodMayBeStatic
class CoreImportTest(unittest.TestCase):

    def test_import_event_engine(self):
        from vnpy.event import EventEngine

    def test_import_main_engine(self):
        from vnpy.trader.engine import MainEngine

    def test_import_ui(self):
        from vnpy.trader.ui import MainWindow, create_qapp

# noinspection PyUnresolvedReferences,PyMethodMayBeStatic
class GatewayImportTest(unittest.TestCase):

    def test_import_binance(self):
        from vnpy.gateway.binance import BinanceGateway

    def test_import_bitfinex(self):
        from vnpy.gateway.bitfinex import BitfinexGateway

    def test_import_bitmex(self):
        from vnpy.gateway.bitmex import BitmexGateway

    @unittest.skipIf(platform.system() == "Darwin", "Not supported yet under osx")
    def test_import_ctp(self):
        from vnpy.gateway.ctp import CtpGateway

    @unittest.skipIf(platform.system() == "Darwin", "Not supported yet under osx")
    def test_import_ctptest(self):
        from vnpy.gateway.ctptest import CtptestGateway

    @unittest.skipIf(platform.system() == "Darwin", "Not supported yet under osx")
    @unittest.skipIf(platform.system() == "Linux", "Not supported yet under Linux")
    def test_import_femas(self):
        from vnpy.gateway.femas import FemasGateway

    def test_import_futu(self):
        from vnpy.gateway.futu import FutuGateway

    def test_import_hbdm(self):
        from vnpy.gateway.hbdm import HbdmGateway

    def test_import_huobi(self):
        from vnpy.gateway.huobi import HuobiGateway

    def test_import_ib(self):
        from vnpy.gateway.ib import IbGateway

    @unittest.skipIf(platform.system() == "Darwin", "Not supported yet under osx")
    def test_import_oes(self):
        from vnpy.gateway.oes import OesGateway

    def test_import_okex(self):
        from vnpy.gateway.okex import OkexGateway

    def test_import_okexf(self):
        from vnpy.gateway.okexf import OkexfGateway

    def test_import_onetoken(self):
        from vnpy.gateway.onetoken import OnetokenGateway

    @unittest.skipIf(platform.system() == "Darwin", "Not supported yet under osx")
    @unittest.skipIf(platform.system() == "Linux", "Not supported yet under Linux")
    def test_import_tap(self):
        from vnpy.gateway.tap import TapGateway

    def test_import_tiger(self):
        from vnpy.gateway.tiger import TigerGateway

    @unittest.skipIf(platform.system() == "Darwin", "Not supported yet under osx")
    def test_import_tora(self):
        from vnpy.gateway.tora import ToraGateway

    def test_import_xtp(self):
        from vnpy.gateway.xtp import XtpGateway

# noinspection PyUnresolvedReferences,PyMethodMayBeStatic
class AppImportTest(unittest.TestCase):

    def test_import_cta_strategy_app(self):
        from vnpy.app.cta_strategy import CtaStrategyApp

    def test_import_csv_loader_app(self):
        from vnpy.app.csv_loader import CsvLoaderApp

if __name__ == '__main__':
    unittest.main()
<commit_msg>[Mod] tests_import_all: skip importing XTP under OSX<commit_after># flake8: noqa
import platform
import unittest

# noinspection PyUnresolvedReferences,PyMethodMayBeStatic
class CoreImportTest(unittest.TestCase):

    def test_import_event_engine(self):
        from vnpy.event import EventEngine

    def test_import_main_engine(self):
        from vnpy.trader.engine import MainEngine

    def test_import_ui(self):
        from vnpy.trader.ui import MainWindow, create_qapp

# noinspection PyUnresolvedReferences,PyMethodMayBeStatic
class GatewayImportTest(unittest.TestCase):

    def test_import_binance(self):
        from vnpy.gateway.binance import BinanceGateway

    def test_import_bitfinex(self):
        from vnpy.gateway.bitfinex import BitfinexGateway

    def test_import_bitmex(self):
        from vnpy.gateway.bitmex import BitmexGateway

    @unittest.skipIf(platform.system() == "Darwin", "Not supported yet under osx")
    def test_import_ctp(self):
        from vnpy.gateway.ctp import CtpGateway

    @unittest.skipIf(platform.system() == "Darwin", "Not supported yet under osx")
    def test_import_ctptest(self):
        from vnpy.gateway.ctptest import CtptestGateway

    @unittest.skipIf(platform.system() == "Darwin", "Not supported yet under osx")
    @unittest.skipIf(platform.system() == "Linux", "Not supported yet under Linux")
    def test_import_femas(self):
        from vnpy.gateway.femas import FemasGateway

    def test_import_futu(self):
        from vnpy.gateway.futu import FutuGateway

    def test_import_hbdm(self):
        from vnpy.gateway.hbdm import HbdmGateway

    def test_import_huobi(self):
        from vnpy.gateway.huobi import HuobiGateway

    def test_import_ib(self):
        from vnpy.gateway.ib import IbGateway

    @unittest.skipIf(platform.system() == "Darwin", "Not supported yet under osx")
    def test_import_oes(self):
        from vnpy.gateway.oes import OesGateway

    def test_import_okex(self):
        from vnpy.gateway.okex import OkexGateway

    def test_import_okexf(self):
        from vnpy.gateway.okexf import OkexfGateway

    def test_import_onetoken(self):
        from vnpy.gateway.onetoken import OnetokenGateway

    @unittest.skipIf(platform.system() == "Darwin", "Not supported yet under osx")
    @unittest.skipIf(platform.system() == "Linux", "Not supported yet under Linux")
    def test_import_tap(self):
        from vnpy.gateway.tap import TapGateway

    def test_import_tiger(self):
        from vnpy.gateway.tiger import TigerGateway

    @unittest.skipIf(platform.system() == "Darwin", "Not supported yet under osx")
    def test_import_tora(self):
        from vnpy.gateway.tora import ToraGateway

    @unittest.skipIf(platform.system() == "Linux", "Not supported yet under Linux")
    @unittest.skipIf(platform.system() == "Darwin", "Not supported yet under osx")
    def test_import_xtp(self):
        from vnpy.gateway.xtp import XtpGateway

# noinspection PyUnresolvedReferences,PyMethodMayBeStatic
class AppImportTest(unittest.TestCase):

    def test_import_cta_strategy_app(self):
        from vnpy.app.cta_strategy import CtaStrategyApp

    def test_import_csv_loader_app(self):
        from vnpy.app.csv_loader import CsvLoaderApp

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


<commit_before>from unittest import TestCase

from jujuconfig import (
    describe_substrate,
    get_jenv_path,
)

class TestDescribeSubstrate(TestCase):

    def test_describe_substrate_kvm(self):
        self.assertEqual('KVM (local)', describe_substrate(
            {'type': 'local', 'container': 'kvm'}))
        self.assertEqual('loca', describe_substrate(
            {'type': 'loca', 'container': 'kvm'}))

    def test_describe_substrate_lxc(self):
        self.assertEqual('LXC (local)', describe_substrate(
            {'type': 'local', 'container': 'lxc'}))
        self.assertEqual('LXC (local)', describe_substrate(
            {'type': 'local'}))

    def test_describe_substrate_openstack(self):
        self.assertEqual('Openstack', describe_substrate(
            {'type': 'openstack', 'auth-url': 'pcloudsvc.com:35357/v2.0/'}))

    def test_describe_substrate_canonistack(self):
        self.assertEqual('Canonistack', describe_substrate(
            {
                'type': 'openstack',
                'auth-url':
                'https://keystone.canonistack.canonical.com:443/v2.0/'}))

    def test_describe_substrate_aws(self):
        self.assertEqual('AWS', describe_substrate({'type': 'ec2'}))

    def test_describe_substrate_joyent(self):
        self.assertEqual('Joyent', describe_substrate({'type': 'joyent'}))

    def test_describe_substrate_azure(self):
        self.assertEqual('Azure', describe_substrate({'type': 'azure'}))

    def test_describe_substrate_maas(self):
        self.assertEqual('MAAS', describe_substrate({'type': 'maas'}))

    def test_describe_substrate_rackspace(self):
        self.assertEqual('Rackspace',
                         describe_substrate({'type': 'rackspace'}))

class TestGetJenvPath(TestCase):

    def test_get_jenv_path(self):
        self.assertEqual('home/environments/envname.jenv',
                         get_jenv_path('home', 'envname'))
<commit_msg>Remove corresponding unit tests.<commit_after>from unittest import TestCase

from jujuconfig import (
    get_jenv_path,
)

class TestGetJenvPath(TestCase):

    def test_get_jenv_path(self):
        self.assertEqual('home/environments/envname.jenv',
                         get_jenv_path('home', 'envname'))
<|endoftext|>


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

from taiga.requestmaker import RequestMaker
from taiga.models.base import InstanceResource, ListResource, SearchableList
import unittest
from mock import patch
import datetime

class Fake(InstanceResource):

    endpoint = 'fakes'

    allowed_params = ['param1', 'param2']

    repr_attribute = 'param1'

    def my_method(self):
        response = self.requester.get('/users/{id}/starred', id=self.id)
        return projects.Projects.parse(response.json(), self.requester)

class Fakes(ListResource):

    instance = Fake

class TestModelBase(unittest.TestCase):

    def test_encoding(self):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        param2 = {
            'list' : [u'Caf\xe9 project', 'Andrea'],
            'dict' : {
                'el1' : 'Andrea',
                'el2' : u'Caf\xe9 project'
            }
        }
        fake = Fake(rm, id=1, param1=u'Caf\xe9 project', param2=param2)
        self.assertEqual(fake.param1, 'Café project')
        self.assertEqual(fake.param2['list'][0], 'Café project')
        self.assertEqual(fake.param2['dict']['el2'], 'Café project')

    @patch('taiga.requestmaker.RequestMaker.put')
    def test_call_model_base_update(self, mock_requestmaker_put):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake = Fake(rm, id=1, param1='one', param2='two')
        fake.update()
        mock_requestmaker_put.assert_called_once_with('/{endpoint}/{id}', endpoint='fakes',
            id=1, payload=fake.to_dict())

    @patch('taiga.requestmaker.RequestMaker.delete')
    def test_call_model_base_delete(self, mock_requestmaker_delete):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake = Fake(rm, id=1, param1='one', param2='two')
        fake.delete()
        mock_requestmaker_delete.assert_called_once_with('/{endpoint}/{id}', endpoint='fakes', id=1)

    @patch('taiga.requestmaker.RequestMaker.get')
    def test_call_model_base_get_element(self, mock_requestmaker_get):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fakes = Fakes(rm)
        fakes.get(1)
        mock_requestmaker_get.assert_called_once_with('/{endpoint}/{id}', endpoint='fakes', id=1)

    @patch('taiga.requestmaker.RequestMaker.delete')
    def test_call_model_base_delete_element(self, mock_requestmaker_delete):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake = Fake(rm, id=1, param1='one', param2='two')
        fake.delete()
        mock_requestmaker_delete.assert_called_once_with('/{endpoint}/{id}', endpoint='fakes', id=1)

    @patch('taiga.requestmaker.RequestMaker.delete')
    def test_call_model_base_delete_element_from_list(self, mock_requestmaker_delete):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fakes = Fakes(rm)
        fakes.delete(1)
        mock_requestmaker_delete.assert_called_once_with('/{endpoint}/{id}', endpoint='fakes', id=1)

    @patch('taiga.requestmaker.RequestMaker.get')
    def test_call_model_base_list_elements(self, mock_requestmaker_get):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fakes = Fakes(rm)
        fakes.list()
        mock_requestmaker_get.assert_called_with('fakes',  query={})
        fakes.list(project_id=1)
        mock_requestmaker_get.assert_called_with('fakes', query={'project_id':1})

    def test_to_dict_method(self):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake = Fake(rm, id=1, param1='one', param2='two', param3='three')
        expected_dict = {'param1':'one', 'param2':'two'}
        self.assertEqual(len(fake.to_dict()), 2)
        self.assertEqual(fake.to_dict(), expected_dict)

    def test_searchable_list_filter(self):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake1 = Fake(rm, id=1, param1='one', param2='a')
        fake2 = Fake(rm, id=1, param1='one', param2='b')
        fake3 = Fake(rm, id=1, param1='two', param2='c')
        searchable_list = SearchableList()
        searchable_list.append(fake1)
        searchable_list.append(fake2)
        searchable_list.append(fake3)
        self.assertEqual(len(searchable_list.filter(param1='one')), 2)
        self.assertEqual(len(searchable_list.filter(param1='notexists')), 0)
        self.assertEqual(len(searchable_list.filter(param1='one', param2='a')), 1)
        self.assertEqual(len(searchable_list.filter()), 3)

    def test_searchable_list_get(self):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake1 = Fake(rm, id=1, param1='one', param2='a')
        fake2 = Fake(rm, id=1, param1='one', param2='b')
        fake3 = Fake(rm, id=1, param1='two', param2='c')
        searchable_list = SearchableList()
        searchable_list.append(fake1)
        searchable_list.append(fake2)
        searchable_list.append(fake3)
        self.assertTrue(searchable_list.get(param1='one'))
        self.assertFalse(searchable_list.get(param1='notexists'), 0)
        self.assertTrue(searchable_list.get(param1='one', param2='a'), 1)
        self.assertTrue(searchable_list.get())

    @patch('taiga.requestmaker.RequestMaker.put')
    def test_call_model_base_update(self, mock_requestmaker_put):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake = Fake(rm, id=1, param1='one', param2='two')
        fake.update()
        mock_requestmaker_put.assert_called_once_with('/{endpoint}/{id}', endpoint='fakes',
            id=1, payload=fake.to_dict())

    @patch('taiga.requestmaker.RequestMaker.put')
    def test_datetime_parsing(self, mock_requestmaker_put):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake = Fake(
            rm, id=1,
            created_date='2015-02-10T17:55:05+0000',
            modified_date='2015-02-10T17:55:05+0000'
        )
        self.assertTrue(isinstance(fake.created_date, datetime.datetime))
        self.assertTrue(isinstance(fake.modified_date, datetime.datetime))

        fake = Fake(
            rm, id=1,
            created_date='2015-02-10T17:55:0',
            modified_date='2015-02-10T17:55:05+0000'
        )
        self.assertFalse(isinstance(fake.created_date, datetime.datetime))
        self.assertTrue(isinstance(fake.modified_date, datetime.datetime))

    def test_repr(self):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake = Fake(rm, id=1, param1='one', param2='two', param3='three')
        rep = fake._rp()
        self.assertEqual(rep, 'one')
        self.assertEqual(fake._rp(), str(fake))
        fake.repr_attribute = 'notexisting'
        rep = fake._rp()
        self.assertEqual(rep, '{0}({1})'.format(fake.__class__.__name__, fake.id))
<commit_msg>Add tests for update() called with parameters<commit_after># -*- coding: utf-8 -*-

from taiga.requestmaker import RequestMaker
from taiga.models.base import InstanceResource, ListResource, SearchableList
import unittest
from mock import patch
import datetime

class Fake(InstanceResource):

    endpoint = 'fakes'

    allowed_params = ['param1', 'param2']

    repr_attribute = 'param1'

    def my_method(self):
        response = self.requester.get('/users/{id}/starred', id=self.id)
        return projects.Projects.parse(response.json(), self.requester)

class Fakes(ListResource):

    instance = Fake

class TestModelBase(unittest.TestCase):

    def test_encoding(self):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        param2 = {
            'list' : [u'Caf\xe9 project', 'Andrea'],
            'dict' : {
                'el1' : 'Andrea',
                'el2' : u'Caf\xe9 project'
            }
        }
        fake = Fake(rm, id=1, param1=u'Caf\xe9 project', param2=param2)
        self.assertEqual(fake.param1, 'Café project')
        self.assertEqual(fake.param2['list'][0], 'Café project')
        self.assertEqual(fake.param2['dict']['el2'], 'Café project')

    @patch('taiga.requestmaker.RequestMaker.put')
    def test_call_model_base_update(self, mock_requestmaker_put):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake = Fake(rm, id=1, param1='one', param2='two')
        fake.update()
        mock_requestmaker_put.assert_called_once_with('/{endpoint}/{id}', endpoint='fakes',
            id=1, payload=fake.to_dict())

    @patch('taiga.requestmaker.RequestMaker.put')
    def test_call_model_base_update_with_params(self, mock_requestmaker_put):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake = Fake(rm, id=1, param1='one', param2='two')
        fake.update(comment='comment')
        dict_res = fake.to_dict()
        dict_res['comment'] = 'comment'
        mock_requestmaker_put.assert_called_once_with(
            '/{endpoint}/{id}', endpoint='fakes',
            id=1, payload=dict_res
        )

    @patch('taiga.requestmaker.RequestMaker.delete')
    def test_call_model_base_delete(self, mock_requestmaker_delete):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake = Fake(rm, id=1, param1='one', param2='two')
        fake.delete()
        mock_requestmaker_delete.assert_called_once_with('/{endpoint}/{id}', endpoint='fakes', id=1)

    @patch('taiga.requestmaker.RequestMaker.get')
    def test_call_model_base_get_element(self, mock_requestmaker_get):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fakes = Fakes(rm)
        fakes.get(1)
        mock_requestmaker_get.assert_called_once_with('/{endpoint}/{id}', endpoint='fakes', id=1)

    @patch('taiga.requestmaker.RequestMaker.delete')
    def test_call_model_base_delete_element(self, mock_requestmaker_delete):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake = Fake(rm, id=1, param1='one', param2='two')
        fake.delete()
        mock_requestmaker_delete.assert_called_once_with('/{endpoint}/{id}', endpoint='fakes', id=1)

    @patch('taiga.requestmaker.RequestMaker.delete')
    def test_call_model_base_delete_element_from_list(self, mock_requestmaker_delete):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fakes = Fakes(rm)
        fakes.delete(1)
        mock_requestmaker_delete.assert_called_once_with('/{endpoint}/{id}', endpoint='fakes', id=1)

    @patch('taiga.requestmaker.RequestMaker.get')
    def test_call_model_base_list_elements(self, mock_requestmaker_get):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fakes = Fakes(rm)
        fakes.list()
        mock_requestmaker_get.assert_called_with('fakes',  query={})
        fakes.list(project_id=1)
        mock_requestmaker_get.assert_called_with('fakes', query={'project_id':1})

    def test_to_dict_method(self):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake = Fake(rm, id=1, param1='one', param2='two', param3='three')
        expected_dict = {'param1':'one', 'param2':'two'}
        self.assertEqual(len(fake.to_dict()), 2)
        self.assertEqual(fake.to_dict(), expected_dict)

    def test_searchable_list_filter(self):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake1 = Fake(rm, id=1, param1='one', param2='a')
        fake2 = Fake(rm, id=1, param1='one', param2='b')
        fake3 = Fake(rm, id=1, param1='two', param2='c')
        searchable_list = SearchableList()
        searchable_list.append(fake1)
        searchable_list.append(fake2)
        searchable_list.append(fake3)
        self.assertEqual(len(searchable_list.filter(param1='one')), 2)
        self.assertEqual(len(searchable_list.filter(param1='notexists')), 0)
        self.assertEqual(len(searchable_list.filter(param1='one', param2='a')), 1)
        self.assertEqual(len(searchable_list.filter()), 3)

    def test_searchable_list_get(self):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake1 = Fake(rm, id=1, param1='one', param2='a')
        fake2 = Fake(rm, id=1, param1='one', param2='b')
        fake3 = Fake(rm, id=1, param1='two', param2='c')
        searchable_list = SearchableList()
        searchable_list.append(fake1)
        searchable_list.append(fake2)
        searchable_list.append(fake3)
        self.assertTrue(searchable_list.get(param1='one'))
        self.assertFalse(searchable_list.get(param1='notexists'), 0)
        self.assertTrue(searchable_list.get(param1='one', param2='a'), 1)
        self.assertTrue(searchable_list.get())

    @patch('taiga.requestmaker.RequestMaker.put')
    def test_call_model_base_update(self, mock_requestmaker_put):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake = Fake(rm, id=1, param1='one', param2='two')
        fake.update()
        mock_requestmaker_put.assert_called_once_with('/{endpoint}/{id}', endpoint='fakes',
            id=1, payload=fake.to_dict())

    @patch('taiga.requestmaker.RequestMaker.put')
    def test_datetime_parsing(self, mock_requestmaker_put):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake = Fake(
            rm, id=1,
            created_date='2015-02-10T17:55:05+0000',
            modified_date='2015-02-10T17:55:05+0000'
        )
        self.assertTrue(isinstance(fake.created_date, datetime.datetime))
        self.assertTrue(isinstance(fake.modified_date, datetime.datetime))

        fake = Fake(
            rm, id=1,
            created_date='2015-02-10T17:55:0',
            modified_date='2015-02-10T17:55:05+0000'
        )
        self.assertFalse(isinstance(fake.created_date, datetime.datetime))
        self.assertTrue(isinstance(fake.modified_date, datetime.datetime))

    def test_repr(self):
        rm = RequestMaker('/api/v1', 'fakehost', 'faketoken')
        fake = Fake(rm, id=1, param1='one', param2='two', param3='three')
        rep = fake._rp()
        self.assertEqual(rep, 'one')
        self.assertEqual(fake._rp(), str(fake))
        fake.repr_attribute = 'notexisting'
        rep = fake._rp()
        self.assertEqual(rep, '{0}({1})'.format(fake.__class__.__name__, fake.id))
<|endoftext|>


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

"""Tests for node utilities."""

import unittest

from pybel import BELGraph
from pybel.constants import INCREASES
from pybel.dsl import ComplexAbundance as g, CompositeAbundance as c, Protein, Reaction
from pybel.examples.various_example import single_reaction_graph, glucose_6_phosphate, glucose, hk1, atp, phosphate, adp
from pybel.testing.utils import n
from pybel_tools.mutation.collapse import collapse_nodes_with_same_names
from pybel_tools.node_utils import flatten_list_abundance, reaction_cartesian_expansion

class TestNodeUtils(unittest.TestCase):
    """Test node utilities."""

    def test_flatten_complex(self):
        """Test flattening a nested complex."""
        p1, p2, p3 = (Protein('N', str(i + 1)) for i in range(3))

        pairs = [
            # Mainly complexes
            (g([p1, p2, p3]), g([p1, p2, p3])),  # no nesting
            (g([p1, p2, p3]), g([g([p1, p2]), p3])),  # one nesting
            (g([p1, p2, p3]), g([g([p1]), p2, p3])),  # one nesting
            (g([p1, p2, p3]), g([g([p1]), g([p2]), p3])),  # one nesting
            # Mainly composites
            (c([p1, p2, p3]), c([p1, p2, p3])),  # no nesting
            (c([p1, p2, p3]), c([c([p1, p2]), p3])),  # one nesting
            (c([p1, p2, p3]), c([c([p1]), p2, p3])),  # one nesting
            (c([p1, p2, p3]), c([c([p1]), c([p2]), p3])),  # one nesting
            # TODO: mixtures of composites and complexes?
        ]

        for expected, source in pairs:
            self.assertEqual(expected, flatten_list_abundance(source))

    def test_flatten_reaction(self):
        """Test flattening a reaction."""
        single_reaction_graph_copy = single_reaction_graph.copy()

        self.assertEqual(single_reaction_graph_copy.number_of_nodes(), 7)
        self.assertEqual(single_reaction_graph_copy.number_of_edges(), 7)

        reaction_cartesian_expansion(single_reaction_graph_copy)

        self.assertEqual(single_reaction_graph_copy.number_of_nodes(), 6)
        self.assertEqual(single_reaction_graph_copy.number_of_edges(), 8)

        pairs = [
            (glucose, INCREASES, glucose_6_phosphate),
            (glucose, INCREASES, adp),
            (hk1, INCREASES, glucose_6_phosphate),
            (hk1, INCREASES, adp),
            (atp, INCREASES, glucose_6_phosphate),
            (atp, INCREASES, adp),
            (phosphate, INCREASES, glucose_6_phosphate),
            (phosphate, INCREASES, adp),
        ]

        for source, target, data in single_reaction_graph_copy.edges(data=True):
            self.assertIn((source, INCREASES, target), pairs)

    def test_flatten_reaction_2(self):
        """Test flattening a qualified reaction."""
        node_increases_reaction_graph = BELGraph(
            name='Node increases reaction',
            version='1.0.0',
            description="Example graph",
            authors='Charles Tapley Hoyt',
            contact='charles.hoyt@scai.fraunhofer.de',
        )

        node_increases_reaction_graph.namespace_url.update({
            'HGNC': 'https://arty.scai.fraunhofer.de/artifactory/bel/namespace/hgnc-human-genes/'
                    'hgnc-human-genes-20170725.belns',
            'CHEBI': 'https://arty.scai.fraunhofer.de/artifactory/bel/namespace/chebi/chebi-20170725.belns',
            'GOBP': 'https://arty.scai.fraunhofer.de/artifactory/bel/namespace/go-biological-process/'
                    'go-biological-process-20170725.belns'
        })

        glycolisis_step_1 = Reaction(reactants=[glucose, hk1, atp], products=[glucose_6_phosphate, adp, hk1])

        node_increases_reaction_graph.add_increases(glucose_6_phosphate, glycolisis_step_1, citation='X', evidence='X')

        self.assertEqual(node_increases_reaction_graph.number_of_nodes(), 6)
        self.assertEqual(node_increases_reaction_graph.number_of_edges(), 7)

        reaction_cartesian_expansion(node_increases_reaction_graph)

        self.assertEqual(node_increases_reaction_graph.number_of_nodes(), 5)
        # TODO Fix so unqualified duplicate edges are not created (it should be the 8 edges below)
        self.assertEqual(node_increases_reaction_graph.number_of_edges(), 12)

        # pairs = [
        #     (glucose, INCREASES, glucose_6_phosphate),
        #     (glucose, INCREASES, adp),
        #     (hk1, INCREASES, glucose_6_phosphate),
        #     (hk1, INCREASES, adp),
        #     (atp, INCREASES, glucose_6_phosphate),
        #     (atp, INCREASES, adp),
        #     (phosphate, INCREASES, glucose_6_phosphate),
        #     (phosphate, INCREASES, adp),
        # ]
        #
        # for source, target, data in node_increases_reaction_graph.edges(data=True):
        #     self.assertIn((source, INCREASES, target), pairs)

    def test_flatten_reaction_3(self):
        """Test flattening a graph containing 2 reactions connected to each other."""
        two_reactions_graph = BELGraph(
            name='Two reactions graph',
            version='1.0.0',
            description="Example graph",
            authors='Charles Tapley Hoyt',
            contact='charles.hoyt@scai.fraunhofer.de',
        )

        two_reactions_graph.namespace_url.update({
            'HGNC': 'https://arty.scai.fraunhofer.de/artifactory/bel/namespace/hgnc-human-genes/'
                    'hgnc-human-genes-20170725.belns',
            'CHEBI': 'https://arty.scai.fraunhofer.de/artifactory/bel/namespace/chebi/chebi-20170725.belns',
            'GOBP': 'https://arty.scai.fraunhofer.de/artifactory/bel/namespace/go-biological-process/'
                    'go-biological-process-20170725.belns'
        })

        reaction_1 = Reaction(reactants=[glucose, atp], products=[hk1])
        reaction_2 = Reaction(reactants=[glucose_6_phosphate], products=[adp])

        two_reactions_graph.add_increases(reaction_1, reaction_2, citation='X', evidence='X')

        self.assertEqual(two_reactions_graph.number_of_nodes(), 7)
        self.assertEqual(two_reactions_graph.number_of_edges(), 6)

        reaction_cartesian_expansion(two_reactions_graph)

        # TODO Fix so unqualified duplicate edges are not created (it should be the 6 edges below)
        self.assertEqual(two_reactions_graph.number_of_nodes(), 5)
        self.assertEqual(two_reactions_graph.number_of_edges(), 8)

    def test_merge_nodes_by_name(self):
        graph = BELGraph()
        a_hgnc, a_entrez = [Protein(namespace, 'a') for namespace in ('HGNC', 'ncbigene')]
        b = Protein('ncbigene', 'b')
        graph.add_increases(a_hgnc, b, n(), n())
        graph.add_increases(a_entrez, b, n(), n())
        self.assertEqual(3, graph.number_of_nodes())
        self.assertEqual(2, graph.number_of_edges())
        collapse_nodes_with_same_names(graph)
        self.assertEqual(2, graph.number_of_nodes(), msg='Wrong number remaining nodes')
        self.assertEqual(2, graph.number_of_edges(), msg=f'Wrong number remaining edges: {graph.edges()}')
        self.assertIn(a_entrez, graph)
        self.assertIn(b, graph)
        self.assertNotIn(a_hgnc, graph)
<commit_msg>Update the metadata<commit_after># -*- coding: utf-8 -*-

"""Tests for node utilities."""

import unittest

from pybel import BELGraph
from pybel.constants import INCREASES
from pybel.dsl import ComplexAbundance as g, CompositeAbundance as c, Protein, Reaction
from pybel.examples.various_example import single_reaction_graph, glucose_6_phosphate, glucose, hk1, atp, phosphate, adp
from pybel.testing.utils import n
from pybel_tools.mutation.collapse import collapse_nodes_with_same_names
from pybel_tools.node_utils import flatten_list_abundance, reaction_cartesian_expansion

class TestNodeUtils(unittest.TestCase):
    """Test node utilities."""

    def test_flatten_complex(self):
        """Test flattening a nested complex."""
        p1, p2, p3 = (Protein('N', str(i + 1)) for i in range(3))

        pairs = [
            # Mainly complexes
            (g([p1, p2, p3]), g([p1, p2, p3])),  # no nesting
            (g([p1, p2, p3]), g([g([p1, p2]), p3])),  # one nesting
            (g([p1, p2, p3]), g([g([p1]), p2, p3])),  # one nesting
            (g([p1, p2, p3]), g([g([p1]), g([p2]), p3])),  # one nesting
            # Mainly composites
            (c([p1, p2, p3]), c([p1, p2, p3])),  # no nesting
            (c([p1, p2, p3]), c([c([p1, p2]), p3])),  # one nesting
            (c([p1, p2, p3]), c([c([p1]), p2, p3])),  # one nesting
            (c([p1, p2, p3]), c([c([p1]), c([p2]), p3])),  # one nesting
            # TODO: mixtures of composites and complexes?
        ]

        for expected, source in pairs:
            self.assertEqual(expected, flatten_list_abundance(source))

    def test_flatten_reaction(self):
        """Test flattening a reaction."""
        single_reaction_graph_copy = single_reaction_graph.copy()

        self.assertEqual(single_reaction_graph_copy.number_of_nodes(), 7)
        self.assertEqual(single_reaction_graph_copy.number_of_edges(), 7)

        reaction_cartesian_expansion(single_reaction_graph_copy)

        self.assertEqual(single_reaction_graph_copy.number_of_nodes(), 6)
        self.assertEqual(single_reaction_graph_copy.number_of_edges(), 8)

        pairs = [
            (glucose, INCREASES, glucose_6_phosphate),
            (glucose, INCREASES, adp),
            (hk1, INCREASES, glucose_6_phosphate),
            (hk1, INCREASES, adp),
            (atp, INCREASES, glucose_6_phosphate),
            (atp, INCREASES, adp),
            (phosphate, INCREASES, glucose_6_phosphate),
            (phosphate, INCREASES, adp),
        ]

        for source, target, data in single_reaction_graph_copy.edges(data=True):
            self.assertIn((source, INCREASES, target), pairs)

    def test_flatten_reaction_2(self):
        """Test flattening a qualified reaction."""
        node_increases_reaction_graph = BELGraph()

        glycolisis_step_1 = Reaction(reactants=[glucose, hk1, atp], products=[glucose_6_phosphate, adp, hk1])

        node_increases_reaction_graph.add_increases(glucose_6_phosphate, glycolisis_step_1, citation='X', evidence='X')

        self.assertEqual(node_increases_reaction_graph.number_of_nodes(), 6)
        self.assertEqual(node_increases_reaction_graph.number_of_edges(), 7)

        reaction_cartesian_expansion(node_increases_reaction_graph)

        self.assertEqual(node_increases_reaction_graph.number_of_nodes(), 5)
        # TODO Fix so unqualified duplicate edges are not created (it should be the 8 edges below)
        self.assertEqual(node_increases_reaction_graph.number_of_edges(), 12)

        # pairs = [
        #     (glucose, INCREASES, glucose_6_phosphate),
        #     (glucose, INCREASES, adp),
        #     (hk1, INCREASES, glucose_6_phosphate),
        #     (hk1, INCREASES, adp),
        #     (atp, INCREASES, glucose_6_phosphate),
        #     (atp, INCREASES, adp),
        #     (phosphate, INCREASES, glucose_6_phosphate),
        #     (phosphate, INCREASES, adp),
        # ]
        #
        # for source, target, data in node_increases_reaction_graph.edges(data=True):
        #     self.assertIn((source, INCREASES, target), pairs)

    def test_flatten_reaction_3(self):
        """Test flattening a graph containing 2 reactions connected to each other."""
        two_reactions_graph = BELGraph()

        reaction_1 = Reaction(reactants=[glucose, atp], products=hk1)
        reaction_2 = Reaction(reactants=glucose_6_phosphate, products=adp)

        two_reactions_graph.add_increases(reaction_1, reaction_2, citation='X', evidence='X')

        self.assertEqual(two_reactions_graph.number_of_nodes(), 7)
        self.assertEqual(two_reactions_graph.number_of_edges(), 6)

        reaction_cartesian_expansion(two_reactions_graph)

        # TODO Fix so unqualified duplicate edges are not created (it should be the 6 edges below)
        self.assertEqual(two_reactions_graph.number_of_nodes(), 5)
        self.assertEqual(two_reactions_graph.number_of_edges(), 8)

    def test_merge_nodes_by_name(self):
        graph = BELGraph()
        a_hgnc, a_entrez = [Protein(namespace, 'a') for namespace in ('HGNC', 'ncbigene')]
        b = Protein('ncbigene', 'b')
        graph.add_increases(a_hgnc, b, citation=n(), evidence=n())
        graph.add_increases(a_entrez, b, citation=n(), evidence=n())
        self.assertEqual(3, graph.number_of_nodes())
        self.assertEqual(2, graph.number_of_edges())
        collapse_nodes_with_same_names(graph)
        self.assertEqual(2, graph.number_of_nodes(), msg='Wrong number remaining nodes')
        self.assertEqual(2, graph.number_of_edges(), msg=f'Wrong number remaining edges: {graph.edges()}')
        self.assertIn(a_entrez, graph)
        self.assertIn(b, graph)
        self.assertNotIn(a_hgnc, graph)
<|endoftext|>


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

"""
test_onemillion
----------------------------------

Tests for `onemillion` module.
"""

import sys
import unittest

from onemillion import onemillion

class TestOnemillion(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_000_something(self):
        pass
<commit_msg>Adding some tests (and using pytest)<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
test_onemillion
----------------------------------

Tests for `onemillion` module.
"""

import pytest

from onemillion import onemillion

@pytest.fixture
def onemillion_instance():
    onemillion_instance = onemillion.OneMillion()
    return onemillion_instance

def test_get_current_etag():
    """See if we have access to the datasets."""
    for dataset in onemillion.CONFIG['domain_lists']:
        etag = onemillion.get_current_etag(dataset['url'])
        assert(etag is not None)

def test_onemillion_object(onemillion_instance):
    """Test an instance of the onemillion class."""
    onemillion_instance.update_lists()

    # test the rank of 'google.com'
    rank = onemillion_instance.domain_in_million('google.com')
    assert(type(rank) == int)
    assert(rank <= 5)

    # test the rank of 'gaagle.com'
    rank = onemillion_instance.domain_in_million('gaagle.com')
    assert(rank is None)
<|endoftext|>


<commit_before>from incuna_auth.middleware import permission
from .utils import RequestTestCase

class TestBasePermissionMiddleware(RequestTestCase):
    middleware_class = permission.BasePermissionMiddleware

    def setUp(self):
        self.middleware = self.middleware_class()

    def test_deny_access(self):
        """Assert that a typical (GET) redirect defaults to /."""
        response = self.middleware.deny_access(self.create_request())
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response['location'], '/')

    def test_deny_access_post(self):
        """Assert that a POST request results in a HTTP 403 (Forbidden) error."""
        response = self.middleware.deny_access(self.create_request(method='post'))
        self.assertEqual(response.status_code, 403)

    def test_deny_access_no_message(self):
        """Assert that a blank `message` parameter results in no Django message."""
        request = self.create_request()
        self.middleware.deny_access(request)
        self.assertEqual(request._messages.store, [])

    def test_process_request_default(self):
        """Assert that the default process_request implementation does nothing."""
        response = self.middleware.process_request(self.create_request())
        self.assertIsNone(response)
<commit_msg>Test BasePermissionMiddleware comprehensively.<commit_after>import mock

from incuna_auth.middleware import permission
from .utils import RequestTestCase

class TestBasePermissionMiddleware(RequestTestCase):
    middleware_class = permission.BasePermissionMiddleware
    middleware_path = 'incuna_auth.middleware.permission.BasePermissionMiddleware.{}'

    def setUp(self):
        self.middleware = self.middleware_class()

    def test_deny_access(self):
        """Assert that a typical (GET) redirect defaults to /."""
        response = self.middleware.deny_access(self.create_request())
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response['location'], '/')

    def test_deny_access_post(self):
        """Assert that a POST request results in a HTTP 403 (Forbidden) error."""
        response = self.middleware.deny_access(self.create_request(method='post'))
        self.assertEqual(response.status_code, 403)

    def test_deny_access_no_message(self):
        """Assert that a blank `message` parameter results in no Django message."""
        request = self.create_request()
        self.middleware.deny_access(request)
        self.assertEqual(request._messages.store, [])

    def test_process_request_default(self):
        """Assert that the default process_request implementation does nothing."""
        response = self.middleware.process_request(self.create_request())
        self.assertIsNone(response)

    def test_process_request_default_protected(self):
        """
        Assert that the default process_request implementation does nothing even if the
        resource is protected.
        """
        method = self.middleware_path.format('is_resource_protected')
        with mock.patch(method, return_value=True):
            response = self.middleware.process_request(self.create_request())
            self.assertIsNone(response)

    def test_process_request_denies_access(self):
        """
        Assert that the default process_request implementation denies access when both
        is_resource_protected and deny_access_condition return True.
        """
        resource_method = self.middleware_path.format('is_resource_protected')
        condition_method = self.middleware_path.format('deny_access_condition')
        with mock.patch(resource_method, return_value=True):
            with mock.patch(condition_method, return_value=True):
                response = self.middleware.process_request(self.create_request())
                self.assertEqual(response.status_code, 302)

    def test_default_unauthorised_redirect_url(self):
        self.assertEqual('/', self.middleware.get_unauthorised_redirect_url())

    def test_default_access_denied_message(self):
        self.assertEqual('', self.middleware.get_access_denied_message())
<|endoftext|>


<commit_before># © 2013 Mark Harviston <mark.harviston@gmail.com>
# © 2014 Arve Knudsen <arve.knudsen@gmail.com>
# BSD License
import asyncio
import locale
import logging
import sys
import ctypes
import multiprocessing
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import socket

import quamash

import pytest

class _SubprocessProtocol(asyncio.SubprocessProtocol):
	def __init__(self, *args, **kwds):
		super(_SubprocessProtocol, self).__init__(*args, **kwds)
		self.received_stdout = None

	def pipe_data_received(self, fd, data):
		text = data.decode(locale.getpreferredencoding(False))
		if fd == 1:
			self.received_stdout = text.strip()

	def process_exited(self):
		asyncio.get_event_loop().stop()

@pytest.fixture(scope='session')
def application():
	return quamash.QApplication.instance() or quamash.QApplication([])

@pytest.fixture
def loop(request, application):
	lp = quamash.QEventLoop(application)
	asyncio.set_event_loop(lp)

	def fin():
		try:
			lp.close()
		finally:
			asyncio.set_event_loop(None)

	request.addfinalizer(fin)
	return lp

@pytest.fixture(
	params=[None, quamash.QThreadExecutor, ThreadPoolExecutor, ProcessPoolExecutor]
)
def executor(request):
	exc_cls = request.param
	if exc_cls is None:
		return None

	exc = exc_cls(1)  # FIXME? fixed number of workers?
	request.addfinalizer(exc.shutdown)
	return exc

class TestCanRunTasksInExecutor:
	"""
	This needs to be a class because pickle can't serialize closures,
	but can serialize bound methods.
	multiprocessing can only handle pickleable functions.
	"""
	def test_can_run_tasks_in_executor(self, loop, executor):
		"""Verify that tasks can be run in an executor."""
		logging.debug('Loop: {!r}'.format(loop))
		logging.debug('Executor: {!r}'.format(executor))

		manager = multiprocessing.Manager()
		was_invoked = manager.Value(ctypes.c_int, 0)
		logging.debug('running until complete')
		loop.run_until_complete(self.blocking_task(loop, executor, was_invoked))
		logging.debug('ran')
		assert was_invoked.value == 1

	def blocking_func(self, was_invoked):
		logging.debug('start blocking_func()')
		was_invoked.value = 1
		logging.debug('end blocking_func()')

	@asyncio.coroutine
	def blocking_task(self, loop, executor, was_invoked):
		logging.debug('start blocking task()')
		fut = loop.run_in_executor(executor, self.blocking_func, was_invoked)
		yield from asyncio.wait_for(fut, timeout=1.0)
		logging.debug('start blocking task()')

def test_can_handle_exception_in_default_executor(loop):
	"""Verify that exceptions from tasks run in default (threaded) executor are handled."""
	def blocking_func():
		raise Exception('Testing')

	with pytest.raises(Exception) as excinfo:
		loop.run_until_complete(loop.run_in_executor(None, blocking_func))

	assert str(excinfo.value) == 'Testing'

def test_can_execute_subprocess(loop):
	"""Verify that a subprocess can be executed."""
	transport, protocol = loop.run_until_complete(loop.subprocess_exec(
		_SubprocessProtocol, sys.executable or 'python', '-c', 'print(\'Hello async world!\')'))
	loop.run_forever()
	assert transport.get_returncode() == 0
	assert protocol.received_stdout == 'Hello async world!'

def test_can_terminate_subprocess(loop):
	"""Verify that a subprocess can be terminated."""
	# Start a never-ending process
	transport = loop.run_until_complete(
		loop.subprocess_exec(
			_SubprocessProtocol, sys.executable or 'python', '-c', 'import time\nwhile True: time.sleep(1)',
		),
	)[0]
	# Terminate!
	transport.kill()
	# Wait for process to die
	loop.run_forever()

	assert transport.get_returncode() != 0

def test_loop_running(loop):
	"""Verify that loop.is_running returns True when running"""
	@asyncio.coroutine
	def is_running():
		nonlocal loop
		assert loop.is_running()

	loop.run_until_complete(is_running())

def test_loop_not_running(loop):
	"""Verify that loop.is_running returns False when not running"""
	assert not loop.is_running()

def test_can_function_as_context_manager(application):
	"""Verify that a QEventLoop can function as its own context manager."""
	with quamash.QEventLoop(application) as loop:
		assert isinstance(loop, quamash.QEventLoop)
		loop.call_soon(loop.stop)
		loop.run_forever()

def test_future_not_done_on_loop_shutdown(loop):
	"""Verify RuntimError occurs when loop stopped before Future completed with run_until_complete."""
	loop.call_later(1, loop.stop)
	fut = asyncio.Future()
	with pytest.raises(RuntimeError):
		loop.run_until_complete(fut)

def test_call_later_must_not_coroutine(loop):
	"""Verify TypeError occurs call_later is given a coroutine."""
	mycoro = asyncio.coroutine(lambda: None)

	with pytest.raises(TypeError):
		loop.call_soon(mycoro)

def test_call_later_must_be_callable(loop):
	"""Verify TypeError occurs call_later is not given a callable."""
	not_callable = object()
	with pytest.raises(TypeError):
		loop.call_soon(not_callable)

def test_call_at(loop):
	"""Verify that loop.call_at works as expected."""
	def mycallback():
		nonlocal was_invoked
		was_invoked = True
	was_invoked = False

	loop.call_at(loop.time() + .1, mycallback)
	loop.run_until_complete(asyncio.sleep(.5))

	assert was_invoked

def test_get_set_debug(loop):
	"""Verify get_debug and set_debug work as expected."""
	loop.set_debug(True)
	assert loop.get_debug()
	loop.set_debug(False)
	assert not loop.get_debug()

@pytest.fixture
def sock_pair(request):
	"""Create socket pair.

	If socket.socketpair isn't available, we emulate it.
	"""
	def fin():
		if client_sock is not None:
			client_sock.close()
		if srv_sock is not None:
			srv_sock.close()

	client_sock = srv_sock = None
	request.addfinalizer(fin)

	# See if socketpair() is available.
	have_socketpair = hasattr(socket, 'socketpair')
	if have_socketpair:
		client_sock, srv_sock = socket.socketpair()
		return client_sock, srv_sock

	# Create a non-blocking temporary server socket
	temp_srv_sock = socket.socket()
	temp_srv_sock.setblocking(False)
	temp_srv_sock.bind(('', 0))
	port = temp_srv_sock.getsockname()[1]
	temp_srv_sock.listen(1)

	# Create non-blocking client socket
	client_sock = socket.socket()
	client_sock.setblocking(False)
	try:
		client_sock.connect(('localhost', port))
	except socket.error as err:
		# Error 10035 (operation would block) is not an error, as we're doing this with a
		# non-blocking socket.
		if err.errno != 10035:
			raise

	# Use select to wait for connect() to succeed.
	import select
	timeout = 1
	readable = select.select([temp_srv_sock], [], [], timeout)[0]
	if temp_srv_sock not in readable:
		raise Exception('Client socket not connected in {} second(s)'.format(timeout))
	srv_sock, _ = temp_srv_sock.accept()

	return client_sock, srv_sock

def test_can_add_reader(loop, sock_pair):
	"""Verify that we can add a reader callback to an event loop."""
	def can_read():
		data = srv_sock.recv(1)
		if len(data) != 1:
			return

		nonlocal got_msg
		got_msg = data
		# Indicate that we're done
		fut.set_result(None)

	def write():
		client_sock.send(ref_msg)
		client_sock.close()

	ref_msg = b'a'
	client_sock, srv_sock = sock_pair
	loop.call_soon(write)

	got_msg = None
	fut = asyncio.Future()
	loop.add_reader(srv_sock.fileno(), can_read)
	assert len(loop._read_notifiers) == 1, 'Notifier should be added'
	loop.run_until_complete(asyncio.wait_for(fut, timeout=1.0))

	assert got_msg == ref_msg

def test_can_remove_reader(loop, sock_pair):
	"""Verify that we can remove a reader callback from an event loop."""
	def can_read():
		data = srv_sock.recv(1)
		if len(data) != 1:
			return

		nonlocal got_msg
		got_msg = data

	client_sock, srv_sock = sock_pair

	got_msg = None
	loop.add_reader(srv_sock.fileno(), can_read)
	loop.remove_reader(srv_sock.fileno())
	assert not loop._read_notifiers, 'Notifier should be removed'
	client_sock.send(b'a')
	client_sock.close()
	# Run for a short while to see if we get a read notification
	loop.call_later(0.1, loop.stop)
	loop.run_forever()

	assert got_msg is None, 'Should not have received a read notification'

def test_can_add_writer(loop, sock_pair):
	"""Verify that we can add a writer callback to an event loop."""
	def can_write():
		# Indicate that we're done
		fut.set_result(None)

	client_sock, srv_sock = sock_pair
	fut = asyncio.Future()
	loop.add_writer(client_sock.fileno(), can_write)
	assert len(loop._write_notifiers) == 1, 'Notifier should be added'
	loop.run_until_complete(asyncio.wait_for(fut, timeout=1.0))

def test_can_remove_writer(loop, sock_pair):
	"""Verify that we can remove a writer callback from an event loop."""
	def can_write():
		pass

	client_sock, srv_sock = sock_pair
	loop.add_writer(client_sock.fileno(), can_write)
	loop.remove_writer(client_sock.fileno())
	assert not loop._write_notifiers, 'Notifier should be removed'
<commit_msg>Increase timeout in test, to avoid false negatives<commit_after># © 2013 Mark Harviston <mark.harviston@gmail.com>
# © 2014 Arve Knudsen <arve.knudsen@gmail.com>
# BSD License
import asyncio
import locale
import logging
import sys
import ctypes
import multiprocessing
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import socket

import quamash

import pytest

class _SubprocessProtocol(asyncio.SubprocessProtocol):
	def __init__(self, *args, **kwds):
		super(_SubprocessProtocol, self).__init__(*args, **kwds)
		self.received_stdout = None

	def pipe_data_received(self, fd, data):
		text = data.decode(locale.getpreferredencoding(False))
		if fd == 1:
			self.received_stdout = text.strip()

	def process_exited(self):
		asyncio.get_event_loop().stop()

@pytest.fixture(scope='session')
def application():
	return quamash.QApplication.instance() or quamash.QApplication([])

@pytest.fixture
def loop(request, application):
	lp = quamash.QEventLoop(application)
	asyncio.set_event_loop(lp)

	def fin():
		try:
			lp.close()
		finally:
			asyncio.set_event_loop(None)

	request.addfinalizer(fin)
	return lp

@pytest.fixture(
	params=[None, quamash.QThreadExecutor, ThreadPoolExecutor, ProcessPoolExecutor]
)
def executor(request):
	exc_cls = request.param
	if exc_cls is None:
		return None

	exc = exc_cls(1)  # FIXME? fixed number of workers?
	request.addfinalizer(exc.shutdown)
	return exc

class TestCanRunTasksInExecutor:
	"""
	This needs to be a class because pickle can't serialize closures,
	but can serialize bound methods.
	multiprocessing can only handle pickleable functions.
	"""
	def test_can_run_tasks_in_executor(self, loop, executor):
		"""Verify that tasks can be run in an executor."""
		logging.debug('Loop: {!r}'.format(loop))
		logging.debug('Executor: {!r}'.format(executor))

		manager = multiprocessing.Manager()
		was_invoked = manager.Value(ctypes.c_int, 0)
		logging.debug('running until complete')
		loop.run_until_complete(self.blocking_task(loop, executor, was_invoked))
		logging.debug('ran')

		assert was_invoked.value == 1

	def blocking_func(self, was_invoked):
		logging.debug('start blocking_func()')
		was_invoked.value = 1
		logging.debug('end blocking_func()')

	@asyncio.coroutine
	def blocking_task(self, loop, executor, was_invoked):
		logging.debug('start blocking task()')
		fut = loop.run_in_executor(executor, self.blocking_func, was_invoked)
		yield from asyncio.wait_for(fut, timeout=5.0)
		logging.debug('start blocking task()')

def test_can_handle_exception_in_default_executor(loop):
	"""Verify that exceptions from tasks run in default (threaded) executor are handled."""
	def blocking_func():
		raise Exception('Testing')

	with pytest.raises(Exception) as excinfo:
		loop.run_until_complete(loop.run_in_executor(None, blocking_func))

	assert str(excinfo.value) == 'Testing'

def test_can_execute_subprocess(loop):
	"""Verify that a subprocess can be executed."""
	transport, protocol = loop.run_until_complete(loop.subprocess_exec(
		_SubprocessProtocol, sys.executable or 'python', '-c', 'print(\'Hello async world!\')'))
	loop.run_forever()
	assert transport.get_returncode() == 0
	assert protocol.received_stdout == 'Hello async world!'

def test_can_terminate_subprocess(loop):
	"""Verify that a subprocess can be terminated."""
	# Start a never-ending process
	transport = loop.run_until_complete(
		loop.subprocess_exec(
			_SubprocessProtocol, sys.executable or 'python', '-c', 'import time\nwhile True: time.sleep(1)',
		),
	)[0]
	# Terminate!
	transport.kill()
	# Wait for process to die
	loop.run_forever()

	assert transport.get_returncode() != 0

def test_loop_running(loop):
	"""Verify that loop.is_running returns True when running"""
	@asyncio.coroutine
	def is_running():
		nonlocal loop
		assert loop.is_running()

	loop.run_until_complete(is_running())

def test_loop_not_running(loop):
	"""Verify that loop.is_running returns False when not running"""
	assert not loop.is_running()

def test_can_function_as_context_manager(application):
	"""Verify that a QEventLoop can function as its own context manager."""
	with quamash.QEventLoop(application) as loop:
		assert isinstance(loop, quamash.QEventLoop)
		loop.call_soon(loop.stop)
		loop.run_forever()

def test_future_not_done_on_loop_shutdown(loop):
	"""Verify RuntimError occurs when loop stopped before Future completed with run_until_complete."""
	loop.call_later(1, loop.stop)
	fut = asyncio.Future()
	with pytest.raises(RuntimeError):
		loop.run_until_complete(fut)

def test_call_later_must_not_coroutine(loop):
	"""Verify TypeError occurs call_later is given a coroutine."""
	mycoro = asyncio.coroutine(lambda: None)

	with pytest.raises(TypeError):
		loop.call_soon(mycoro)

def test_call_later_must_be_callable(loop):
	"""Verify TypeError occurs call_later is not given a callable."""
	not_callable = object()
	with pytest.raises(TypeError):
		loop.call_soon(not_callable)

def test_call_at(loop):
	"""Verify that loop.call_at works as expected."""
	def mycallback():
		nonlocal was_invoked
		was_invoked = True
	was_invoked = False

	loop.call_at(loop.time() + .1, mycallback)
	loop.run_until_complete(asyncio.sleep(.5))

	assert was_invoked

def test_get_set_debug(loop):
	"""Verify get_debug and set_debug work as expected."""
	loop.set_debug(True)
	assert loop.get_debug()
	loop.set_debug(False)
	assert not loop.get_debug()

@pytest.fixture
def sock_pair(request):
	"""Create socket pair.

	If socket.socketpair isn't available, we emulate it.
	"""
	def fin():
		if client_sock is not None:
			client_sock.close()
		if srv_sock is not None:
			srv_sock.close()

	client_sock = srv_sock = None
	request.addfinalizer(fin)

	# See if socketpair() is available.
	have_socketpair = hasattr(socket, 'socketpair')
	if have_socketpair:
		client_sock, srv_sock = socket.socketpair()
		return client_sock, srv_sock

	# Create a non-blocking temporary server socket
	temp_srv_sock = socket.socket()
	temp_srv_sock.setblocking(False)
	temp_srv_sock.bind(('', 0))
	port = temp_srv_sock.getsockname()[1]
	temp_srv_sock.listen(1)

	# Create non-blocking client socket
	client_sock = socket.socket()
	client_sock.setblocking(False)
	try:
		client_sock.connect(('localhost', port))
	except socket.error as err:
		# Error 10035 (operation would block) is not an error, as we're doing this with a
		# non-blocking socket.
		if err.errno != 10035:
			raise

	# Use select to wait for connect() to succeed.
	import select
	timeout = 1
	readable = select.select([temp_srv_sock], [], [], timeout)[0]
	if temp_srv_sock not in readable:
		raise Exception('Client socket not connected in {} second(s)'.format(timeout))
	srv_sock, _ = temp_srv_sock.accept()

	return client_sock, srv_sock

def test_can_add_reader(loop, sock_pair):
	"""Verify that we can add a reader callback to an event loop."""
	def can_read():
		data = srv_sock.recv(1)
		if len(data) != 1:
			return

		nonlocal got_msg
		got_msg = data
		# Indicate that we're done
		fut.set_result(None)

	def write():
		client_sock.send(ref_msg)
		client_sock.close()

	ref_msg = b'a'
	client_sock, srv_sock = sock_pair
	loop.call_soon(write)

	got_msg = None
	fut = asyncio.Future()
	loop.add_reader(srv_sock.fileno(), can_read)
	assert len(loop._read_notifiers) == 1, 'Notifier should be added'
	loop.run_until_complete(asyncio.wait_for(fut, timeout=1.0))

	assert got_msg == ref_msg

def test_can_remove_reader(loop, sock_pair):
	"""Verify that we can remove a reader callback from an event loop."""
	def can_read():
		data = srv_sock.recv(1)
		if len(data) != 1:
			return

		nonlocal got_msg
		got_msg = data

	client_sock, srv_sock = sock_pair

	got_msg = None
	loop.add_reader(srv_sock.fileno(), can_read)
	loop.remove_reader(srv_sock.fileno())
	assert not loop._read_notifiers, 'Notifier should be removed'
	client_sock.send(b'a')
	client_sock.close()
	# Run for a short while to see if we get a read notification
	loop.call_later(0.1, loop.stop)
	loop.run_forever()

	assert got_msg is None, 'Should not have received a read notification'

def test_can_add_writer(loop, sock_pair):
	"""Verify that we can add a writer callback to an event loop."""
	def can_write():
		# Indicate that we're done
		fut.set_result(None)

	client_sock, srv_sock = sock_pair
	fut = asyncio.Future()
	loop.add_writer(client_sock.fileno(), can_write)
	assert len(loop._write_notifiers) == 1, 'Notifier should be added'
	loop.run_until_complete(asyncio.wait_for(fut, timeout=1.0))

def test_can_remove_writer(loop, sock_pair):
	"""Verify that we can remove a writer callback from an event loop."""
	def can_write():
		pass

	client_sock, srv_sock = sock_pair
	loop.add_writer(client_sock.fileno(), can_write)
	loop.remove_writer(client_sock.fileno())
	assert not loop._write_notifiers, 'Notifier should be removed'
<|endoftext|>