<commit_before># Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import importlib
import inspect
import os
import sys

from st2common.exceptions.plugins import IncompatiblePluginException
from st2common import log as logging

LOG = logging.getLogger(__name__)
PYTHON_EXTENSIONS = ('.py')

def _register_plugin_path(plugin_dir_abs_path):
    if not os.path.isdir(plugin_dir_abs_path):
        raise Exception('Directory containing plugins must be provided.')
    for x in sys.path:
        if plugin_dir_abs_path in (x, x + os.sep):
            return
    sys.path.append(plugin_dir_abs_path)

def _get_plugin_module(plugin_file_path):
    plugin_module = os.path.basename(plugin_file_path)
    if plugin_module.endswith(PYTHON_EXTENSIONS):
        plugin_module = plugin_module[:plugin_module.rfind('.py')]
    else:
        plugin_module = None
    return plugin_module

def _get_classes_in_module(module):
    return [kls for name, kls in inspect.getmembers(module,
        lambda member: inspect.isclass(member) and member.__module__ == module.__name__)]

def _get_plugin_classes(module_name):
    return _get_classes_in_module(module_name)

def _get_plugin_methods(plugin_klass):
    return [name for name, method in inspect.getmembers(plugin_klass, inspect.ismethod)]

def _validate_methods(plugin_base_class, plugin_klass):
    '''
    XXX: This is hacky but we'd like to validate the methods
    in plugin_impl at least has all the *abstract* methods in
    plugin_base_class.
    '''
    expected_methods = plugin_base_class.__abstractmethods__
    plugin_methods = _get_plugin_methods(plugin_klass)
    for method in expected_methods:
        if method not in plugin_methods:
            raise IncompatiblePluginException('Class %s does not implement method %s in base class.'
                            % (plugin_klass, method))

def _register_plugin(plugin_base_class, plugin_impl):
    _validate_methods(plugin_base_class, plugin_impl)
    plugin_base_class.register(plugin_impl)

def register_plugin(plugin_base_class, plugin_abs_file_path):
    registered_plugins = []
    plugin_dir = os.path.dirname(os.path.realpath(plugin_abs_file_path))
    _register_plugin_path(plugin_dir)
    module_name = _get_plugin_module(plugin_abs_file_path)
    if module_name is None:
        return None
    module = importlib.import_module(module_name)
    klasses = _get_plugin_classes(module)

    # Try registering classes in plugin file. Some may fail.
    for klass in klasses:
        try:
            _register_plugin(plugin_base_class, klass)
            registered_plugins.append(klass)
        except Exception as e:
            LOG.exception(e)
            LOG.debug('Skipping class %s as it doesn\'t match specs.', klass)
            continue

    if len(registered_plugins) == 0:
        raise Exception('Found no classes in plugin file ' + plugin_abs_file_path
                        + ' matching requirements.')

    return registered_plugins
<commit_msg>Fix "get_plugin_methods" so it excludes inherited abstract methods.<commit_after># Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import importlib
import inspect
import os
import sys

from st2common.exceptions.plugins import IncompatiblePluginException
from st2common import log as logging

LOG = logging.getLogger(__name__)
PYTHON_EXTENSIONS = ('.py')

def _register_plugin_path(plugin_dir_abs_path):
    if not os.path.isdir(plugin_dir_abs_path):
        raise Exception('Directory containing plugins must be provided.')
    for x in sys.path:
        if plugin_dir_abs_path in (x, x + os.sep):
            return
    sys.path.append(plugin_dir_abs_path)

def _get_plugin_module(plugin_file_path):
    plugin_module = os.path.basename(plugin_file_path)
    if plugin_module.endswith(PYTHON_EXTENSIONS):
        plugin_module = plugin_module[:plugin_module.rfind('.py')]
    else:
        plugin_module = None
    return plugin_module

def _get_classes_in_module(module):
    return [kls for name, kls in inspect.getmembers(module,
        lambda member: inspect.isclass(member) and member.__module__ == module.__name__)]

def _get_plugin_classes(module_name):
    return _get_classes_in_module(module_name)

def _get_plugin_methods(plugin_klass):
    """
    Return a list of names of all the methods in the provided class.

    Note: Abstract methods which are not implemented are excluded from the
    list.

    :rtype: ``list`` of ``str``
    """
    methods = inspect.getmembers(plugin_klass, inspect.ismethod)

    # Exclude inherited abstract methods from the parent class
    method_names = []
    for name, method in methods:
        method_properties = method.__dict__
        is_abstract = method_properties.get('__isabstractmethod__', False)

        if is_abstract:
            continue

        method_names.append(name)
    return method_names

def _validate_methods(plugin_base_class, plugin_klass):
    '''
    XXX: This is hacky but we'd like to validate the methods
    in plugin_impl at least has all the *abstract* methods in
    plugin_base_class.
    '''
    expected_methods = plugin_base_class.__abstractmethods__
    plugin_methods = _get_plugin_methods(plugin_klass)
    for method in expected_methods:
        if method not in plugin_methods:
            raise IncompatiblePluginException('Class %s does not implement method %s in base class.'
                            % (plugin_klass, method))

def _register_plugin(plugin_base_class, plugin_impl):
    _validate_methods(plugin_base_class, plugin_impl)
    plugin_base_class.register(plugin_impl)

def register_plugin(plugin_base_class, plugin_abs_file_path):
    registered_plugins = []
    plugin_dir = os.path.dirname(os.path.realpath(plugin_abs_file_path))
    _register_plugin_path(plugin_dir)
    module_name = _get_plugin_module(plugin_abs_file_path)
    if module_name is None:
        return None
    module = importlib.import_module(module_name)
    klasses = _get_plugin_classes(module)

    # Try registering classes in plugin file. Some may fail.
    for klass in klasses:
        try:
            _register_plugin(plugin_base_class, klass)
            registered_plugins.append(klass)
        except Exception as e:
            LOG.exception(e)
            LOG.debug('Skipping class %s as it doesn\'t match specs.', klass)
            continue

    if len(registered_plugins) == 0:
        raise Exception('Found no classes in plugin file ' + plugin_abs_file_path
                        + ' matching requirements.')

    return registered_plugins
<|endoftext|>


<commit_before>from Tools.ChemicalInfo import BohrR
from ReportGenerator.Top_ReportGenerator import Top_ReportGenerator
import Tools.misc as misc

import logging
log = logging.getLogger(__name__)

class Geom(Top_ReportGenerator):

    def __init__(self,we,parsed,show_vibration=None):
        # super().__init__(we,parsed) # cannot call it
        self.we = we
        self.parsed = parsed
        self.bohr_units = self.parsed.last_value('P_geom_bohr') != 'empty'
        self.geom = self.parsed.last_value('P_geom')
        self.show_vibration = show_vibration
        self.comment = ''
        self.coord = list()
        self.vibs = list()
        self.vectors = list()
        self.prepare_for_report()
        self.prepare_comments()

    def prepare_for_report(self):
        if self.geom is None:
            return None
        if self.bohr_units:
            for atom in self.geom:
                s = '%s %.12f %.12f %.12f' % (atom[0],float(atom[1])*BohrR, float(atom[2])*BohrR, float(atom[3])*BohrR)
                self.coord.append(s)
        else:
            self.coord = [' '.join(atom) for atom in self.geom]

        if self.show_vibration is not None:
            self.prepare_vibrations()

    def prepare_comments(self):
        # TODO extract s2, scan/IRC and convert into comment line
        scf_e = "SCF_Energy= %-11.6f " % float(self.parsed.last_value('P_scf_e'))
        self.comment = scf_e

    def prepare_vibrations(self):
        # Reformat frequencies
        self.vibs = []
        all_displacement = self.parsed.get_value('P_vibrations')
        if all_displacement is not None:
            for vs in all_displacement:
                for v in zip(*vs):
                    self.vibs.append(misc.split(v, 3))
        self.vectors = [' '.join(dq) for dq in self.vibs[self.show_vibration]]

    def __str__(self):
        if not self.coord:
            log.warning('No coordinates to write!')
            return ''

        n_atoms = len(self.coord)
        if self.vectors:
            if n_atoms!=len(self.vectors):
                log.warning('Number of atoms is different from !')
            coords = ["  ".join(v) for v in zip(self.coord,self.vectors)]
        else:
            coords = self.coord

        return "\n".join([str(n_atoms),self.comment]+coords)<commit_msg>Fixed a bug created by print 'SCF_Energy=' as a comment in terse-pics/*.xyz<commit_after>from Tools.ChemicalInfo import BohrR
from ReportGenerator.Top_ReportGenerator import Top_ReportGenerator
import Tools.misc as misc

import logging
log = logging.getLogger(__name__)

class Geom(Top_ReportGenerator):

    def __init__(self,we,parsed,show_vibration=None):
        # super().__init__(we,parsed) # cannot call it
        self.we = we
        self.parsed = parsed
        self.bohr_units = self.parsed.last_value('P_geom_bohr') != 'empty'
        self.geom = self.parsed.last_value('P_geom')
        self.show_vibration = show_vibration
        self.comment = ''
        self.coord = list()
        self.vibs = list()
        self.vectors = list()
        self.prepare_for_report()
        self.prepare_comments()

    def prepare_for_report(self):
        if self.geom is None:
            return None
        if self.bohr_units:
            for atom in self.geom:
                s = '%s %.12f %.12f %.12f' % (atom[0],float(atom[1])*BohrR, float(atom[2])*BohrR, float(atom[3])*BohrR)
                self.coord.append(s)
        else:
            self.coord = [' '.join(atom) for atom in self.geom]

        if self.show_vibration is not None:
            self.prepare_vibrations()

    def prepare_comments(self):
        # TODO extract s2, scan/IRC and convert into comment line
        P_scf_e = self.parsed.last_value('P_scf_e')
        if P_scf_e is not None:
            self.comment = "SCF_Energy= %-11.6f " % float(P_scf_e)

    def prepare_vibrations(self):
        # Reformat frequencies
        self.vibs = []
        all_displacement = self.parsed.get_value('P_vibrations')
        if all_displacement is not None:
            for vs in all_displacement:
                for v in zip(*vs):
                    self.vibs.append(misc.split(v, 3))
        self.vectors = [' '.join(dq) for dq in self.vibs[self.show_vibration]]

    def __str__(self):
        if not self.coord:
            log.warning('No coordinates to write!')
            return ''

        n_atoms = len(self.coord)
        if self.vectors:
            if n_atoms!=len(self.vectors):
                log.warning('Number of atoms is different from !')
            coords = ["  ".join(v) for v in zip(self.coord,self.vectors)]
        else:
            coords = self.coord

        return "\n".join([str(n_atoms),self.comment]+coords)<|endoftext|>


<commit_before>from Tools.ChemicalInfo import BohrR
from ReportGenerator.Top_ReportGenerator import Top_ReportGenerator
import Tools.misc as misc

import logging
log = logging.getLogger(__name__)

class Geom(Top_ReportGenerator):

    def __init__(self,we,parsed,show_vibration=None):
        # super().__init__(we,parsed) # cannot call it
        self.we = we
        self.parsed = parsed
        self.bohr_units = self.parsed.last_value('P_geom_bohr') != 'empty'
        self.geom = self.parsed.last_value('P_geom')
        self.show_vibration = show_vibration
        self.comment = ''
        self.coord = list()
        self.vibs = list()
        self.vectors = list()
        self.prepare_for_report()

    def prepare_for_report(self):
        if self.geom is None:
            return None
        if self.bohr_units:
            for atom in self.geom:
                s = '%s %.12f %.12f %.12f' % (atom[0],float(atom[1])*BohrR, float(atom[2])*BohrR, float(atom[3])*BohrR)
                self.coord.append(s)
        else:
            self.coord = [' '.join(atom) for atom in self.geom]

        if self.show_vibration is not None:
            self.prepare_vibrations()

    def prepare_comments(self):
        # TODO extract e_scf,s2,... and convert into comment line
        pass

    def prepare_vibrations(self):
        # Reformat frequencies
        self.vibs = []
        all_displacement = self.parsed.get_value('P_vibrations')
        if all_displacement is not None:
            for vs in all_displacement:
                for v in zip(*vs):
                    self.vibs.append(misc.split(v, 3))
        self.vectors = [' '.join(dq) for dq in self.vibs[self.show_vibration]]

    def __str__(self):
        if not self.coord:
            log.warning('No coordinates to write!')
            return ''

        n_atoms = len(self.coord)
        if self.vectors:
            if n_atoms!=len(self.vectors):
                log.warning('Number of atoms is different from !')
            coords = ["  ".join(v) for v in zip(self.coord,self.vectors)]
        else:
            coords = self.coord

        return "\n".join([str(n_atoms),self.comment]+coords)<commit_msg>print 'SCF_Energy=' as a comment in terse-pics/*.xyz<commit_after>from Tools.ChemicalInfo import BohrR
from ReportGenerator.Top_ReportGenerator import Top_ReportGenerator
import Tools.misc as misc

import logging
log = logging.getLogger(__name__)

class Geom(Top_ReportGenerator):

    def __init__(self,we,parsed,show_vibration=None):
        # super().__init__(we,parsed) # cannot call it
        self.we = we
        self.parsed = parsed
        self.bohr_units = self.parsed.last_value('P_geom_bohr') != 'empty'
        self.geom = self.parsed.last_value('P_geom')
        self.show_vibration = show_vibration
        self.comment = ''
        self.coord = list()
        self.vibs = list()
        self.vectors = list()
        self.prepare_for_report()
        self.prepare_comments()

    def prepare_for_report(self):
        if self.geom is None:
            return None
        if self.bohr_units:
            for atom in self.geom:
                s = '%s %.12f %.12f %.12f' % (atom[0],float(atom[1])*BohrR, float(atom[2])*BohrR, float(atom[3])*BohrR)
                self.coord.append(s)
        else:
            self.coord = [' '.join(atom) for atom in self.geom]

        if self.show_vibration is not None:
            self.prepare_vibrations()

    def prepare_comments(self):
        # TODO extract s2, scan/IRC and convert into comment line
        scf_e = "SCF_Energy= %-11.6f " % float(self.parsed.last_value('P_scf_e'))
        self.comment = scf_e

    def prepare_vibrations(self):
        # Reformat frequencies
        self.vibs = []
        all_displacement = self.parsed.get_value('P_vibrations')
        if all_displacement is not None:
            for vs in all_displacement:
                for v in zip(*vs):
                    self.vibs.append(misc.split(v, 3))
        self.vectors = [' '.join(dq) for dq in self.vibs[self.show_vibration]]

    def __str__(self):
        if not self.coord:
            log.warning('No coordinates to write!')
            return ''

        n_atoms = len(self.coord)
        if self.vectors:
            if n_atoms!=len(self.vectors):
                log.warning('Number of atoms is different from !')
            coords = ["  ".join(v) for v in zip(self.coord,self.vectors)]
        else:
            coords = self.coord

        return "\n".join([str(n_atoms),self.comment]+coords)<|endoftext|>


<commit_before>"""
Unit Tests for CorfuncCalculator class
"""
from __future__ import division, print_function

import os.path
import unittest
import time

import numpy as np

from sas.sascalc.corfunc.corfunc_calculator import CorfuncCalculator
from sas.sascalc.dataloader.data_info import Data1D

def find(filename):
    return os.path.join(os.path.dirname(__file__), 'data', filename)

class TestCalculator(unittest.TestCase):

    def setUp(self):
        self.data = load_data()
        # Note: to generate target values from the GUI:
        # * load the data from test/corfunc/test/98929.txt
        # * set qrange to (0, 0.013), (0.15, 0.24)
        # * select fourier transform type
        # * click Calculate Bg
        # * click Extrapolate
        # * click Compute Parameters
        # * copy the Guinier and Porod values to the extrapolate function
        # * for each graph, grab the data from DataInfo and store it in _out.txt
        self.calculator = CorfuncCalculator(data=self.data, lowerq=0.013,
            upperq=(0.15, 0.24))
        self.calculator.background = 0.3
        self.extrapolation = None
        self.transformation = None
        self.results = [np.loadtxt(find(filename+"_out.txt")).T[2]
                        for filename in ("gamma1", "gamma3", "idf")]

    def extrapolate(self):
        params, extrapolation, s2 = self.calculator.compute_extrapolation()
        # Check the extrapolation parameters
        self.assertAlmostEqual(params['A'], 4.18970, places=5)
        self.assertAlmostEqual(params['B'], -25469.9, places=1)
        self.assertAlmostEqual(params['K'], 4.44660e-5, places=10)
        #self.assertAlmostEqual(params['sigma'], 1.70181e-10, places=15)

        # Ensure the extraplation tends to the background value
        self.assertAlmostEqual(extrapolation.y[-1], self.calculator.background)

        # Test extrapolation for q values between 0.02 and 0.24
        mask = np.logical_and(self.data.x > 0.02, self.data.x < 0.24)
        qs = self.data.x[mask]
        iqs = self.data.y[mask]

        for q, iq in zip(qs, iqs):
            # Find the q value in the extraplation nearest to the value in
            # the data
            q_extrap = min(extrapolation.x, key=lambda x:abs(x-q))
            # Find the index of this value in the extrapolation
            index = list(extrapolation.x).index(q_extrap)
            # Find it's corresponding intensity value
            iq_extrap = extrapolation.y[index]
            # Check the extrapolation agrees to the data at this point to 1 d.p
            self.assertAlmostEqual(iq_extrap, iq, 1)

        self.extrapolation = extrapolation

    def transform(self):
        self.calculator.compute_transform(self.extrapolation, 'fourier',
            completefn=self.transform_callback)
        # Transform is performed asynchronously; give it time to run
        while True:
            time.sleep(0.001)
            if (not self.calculator.transform_isrunning() and
                self.transformation is not None):
                break

        transform1, transform3, idf = self.transformation
        self.assertIsNotNone(transform1)
        self.assertAlmostEqual(transform1.y[0], 1)
        self.assertAlmostEqual(transform1.y[-1], 0, 5)

    def transform_callback(self, transforms):
        self.transformation = transforms

    def extract_params(self):
        params = self.calculator.extract_parameters(self.transformation[0])

        self.assertLess(abs(params.long_period-75), 2.5) # L_p ~= 75

    def check_transforms(self):
        gamma1, gamma3, idf = self.transformation
        gamma1_out, gamma3_out, idf_out = self.results
        def compare(a, b):
            return max(abs((a-b)/b))
        #print("gamma1 diff", compare(gamma1.y[gamma1.x<=200.], gamma1_out))
        #print("gamma3 diff", compare(gamma3.y[gamma3.x<=200.], gamma3_out))
        #print("idf diff", compare(idf.y[idf.x<=200.], idf_out))
        #self.assertLess(compare(gamma1.y[gamma1.x<=200.], gamma1_out), 1e-10)
        #self.assertLess(compare(gamma3.y[gamma3.x<=200.], gamma3_out), 1e-10)
        #self.assertLess(compare(idf.y[idf.x<=200.], idf_out), 1e-10)

    # Ensure tests are ran in correct order;
    # Each test depends on the one before it
    def test_calculator(self):
        steps = [self.extrapolate, self.transform, self.extract_params, self.check_transforms]
        for test in steps:
            try:
                test()
            except Exception as e:
                raise
                self.fail("{} failed ({}: {})".format(test, type(e), e))

def load_data(filename="98929.txt"):
    data = np.loadtxt(find(filename), dtype=np.float64)
    q = data[:,0]
    iq = data[:,1]
    return Data1D(x=q, y=iq)

if __name__ == '__main__':
    unittest.main()
<commit_msg>Fixed test bug<commit_after>"""
Unit Tests for CorfuncCalculator class
"""
from __future__ import division, print_function

import os.path
import unittest
import time

import numpy as np

from sas.sascalc.corfunc.corfunc_calculator import CorfuncCalculator
from sas.sascalc.dataloader.data_info import Data1D

def find(filename):
    return os.path.join(os.path.dirname(__file__), 'data', filename)

class TestCalculator(unittest.TestCase):

    def setUp(self):
        self.data = load_data()
        # Note: to generate target values from the GUI:
        # * load the data from test/corfunc/test/98929.txt
        # * set qrange to (0, 0.013), (0.15, 0.24)
        # * select fourier transform type
        # * click Calculate Bg
        # * click Extrapolate
        # * click Compute Parameters
        # * copy the Guinier and Porod values to the extrapolate function
        # * for each graph, grab the data from DataInfo and store it in _out.txt
        self.calculator = CorfuncCalculator(data=self.data, lowerq=0.013,
            upperq=(0.15, 0.24))
        self.calculator.background = 0.3
        self.extrapolation = None
        self.transformation = None
        self.results = [np.loadtxt(find(filename+"_out.txt")).T[2]
                        for filename in ("gamma1", "gamma3", "idf")]

    def extrapolate(self):
        params, extrapolation, s2 = self.calculator.compute_extrapolation()
        # Check the extrapolation parameters
        self.assertAlmostEqual(params['A'], 4.18970, places=5)
        self.assertAlmostEqual(params['B'], -25469.9, places=1)
        self.assertAlmostEqual(params['K'], 4.44660e-5, places=10)
        #self.assertAlmostEqual(params['sigma'], 1.70181e-10, places=15)

        # Ensure the extraplation tends to the background value
        self.assertAlmostEqual(extrapolation.y[-1], self.calculator.background)

        # Test extrapolation for q values between 0.02 and 0.24
        mask = np.logical_and(self.data.x > 0.02, self.data.x < 0.24)
        qs = self.data.x[mask]
        iqs = self.data.y[mask]

        for q, iq in zip(qs, iqs):
            # Find the q value in the extraplation nearest to the value in
            # the data
            q_extrap = min(extrapolation.x, key=lambda x:abs(x-q))
            # Find the index of this value in the extrapolation
            index = list(extrapolation.x).index(q_extrap)
            # Find it's corresponding intensity value
            iq_extrap = extrapolation.y[index]
            # Check the extrapolation agrees to the data at this point to 1 d.p
            self.assertAlmostEqual(iq_extrap, iq, 1)

        self.extrapolation = extrapolation

    def transform(self):
        self.calculator.compute_transform(self.extrapolation, 'fourier',
            completefn=self.transform_callback)
        # Transform is performed asynchronously; give it time to run
        while True:
            time.sleep(0.001)
            if (not self.calculator.transform_isrunning() and
                self.transformation is not None):
                break

        transform1, transform3, idf = self.transformation
        self.assertIsNotNone(transform1)
        self.assertAlmostEqual(transform1.y[0], 1)
        self.assertAlmostEqual(transform1.y[-1], 0, 5)

    def transform_callback(self, transformed_data):
        self.transformation = transformed_data

    def extract_params(self):
        params = self.calculator.extract_parameters(self.transformation[0])

        self.assertLess(abs(params.long_period-75), 2.5) # L_p ~= 75

    def check_transforms(self):
        gamma1, gamma3, idf = self.transformation
        gamma1_out, gamma3_out, idf_out = self.results
        def compare(a, b):
            return max(abs((a-b)/b))
        #print("gamma1 diff", compare(gamma1.y[gamma1.x<=200.], gamma1_out))
        #print("gamma3 diff", compare(gamma3.y[gamma3.x<=200.], gamma3_out))
        #print("idf diff", compare(idf.y[idf.x<=200.], idf_out))
        #self.assertLess(compare(gamma1.y[gamma1.x<=200.], gamma1_out), 1e-10)
        #self.assertLess(compare(gamma3.y[gamma3.x<=200.], gamma3_out), 1e-10)
        #self.assertLess(compare(idf.y[idf.x<=200.], idf_out), 1e-10)

    # Ensure tests are ran in correct order;
    # Each test depends on the one before it
    def test_calculator(self):
        steps = [self.extrapolate, self.transform, self.extract_params, self.check_transforms]
        for test in steps:
            try:
                test()
            except Exception as e:
                raise
                self.fail("{} failed ({}: {})".format(test, type(e), e))

def load_data(filename="98929.txt"):
    data = np.loadtxt(find(filename), dtype=np.float64)
    q = data[:,0]
    iq = data[:,1]
    return Data1D(x=q, y=iq)

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


<commit_before># Load in core dependencies
import os
import glob

# Load in local dependencies
from sublime_plugin_tests import framework
from sublime_plugin_tests.utils.selection import split_selection

# Set up constants
__dir__ = os.path.dirname(os.path.abspath(__file__))
delete_dir = __dir__ + '/delete_files'

# Define our class
class TestVarDelete(framework.TestCase):
    @framework.template(delete_dir + '/plugin.template.py')
    def parse_io_files(self, base_path):
        # Load in input
        with open('%s.input.js' % base_path) as f:
            input = f.read()

        # Break up target selection from content
        input_obj = split_selection(input)

        # Load in single.output
        with open('%s.output.js' % base_path) as f:
            expected_output = f.read()

        # Break up expected selection from content
        expected_obj = split_selection(expected_output)

        # Return collected information
        return {
            'target_sel': input_obj['selection'],
            'content': input_obj['content'],
            'expected_sel': expected_obj['selection'],
            'expected_content': expected_obj['content'],
        }

    @classmethod
    def _add_io_test_case(cls, namespace):
        def test_var_delete_fn(self):
            return self.parse_io_files(delete_dir + '/' + namespace)
        setattr(cls, 'test_var_delete_' + namespace, test_var_delete_fn)

# Grab files to load in as tests
# TODO: Clump input/output into a single file
test_filenames = glob.glob(delete_dir + '/*.input.js')
test_namespaces = map(lambda filename: (filename.replace(delete_dir + '/', '')
                                                .replace('.input.js', '')), test_filenames)
skip_tests = [
    'comma-in-var',  # Edge case not yet supported (move to esprima)
    'multiline-all',  # Wide selection not yet supported (implement in plugin_tests)
    'multiline-multiple',  # Wide selection not yet supported (implement in plugin_tests)
    'same-line',  # TODO: Deal with nuance of cursor relocation during edit
    'multiline-middle',  # TODO: Deal with nuance of cursor relocation during edit
    'multiline-end-of-var',  # TODO: Deal with nuance of cursor relocation during edit
    # 'multi-var',  # TODO: Interesting edge case for sets...
    'multiline-start',  # TODO: This is an arguable case... I will wait until the logic is done
]

test_namespaces = ['single-var-start']

# For each of the namespaces
for namespace in test_namespaces:
    # If the test is marked for skipping, skip it
    if namespace in skip_tests:
        continue

    # Otherwise, define the test
    TestVarDelete._add_io_test_case(namespace)
<commit_msg>Adjusted parse_io_files method in test suite<commit_after># Load in core dependencies
import glob
import os
import re

# Load in local dependencies
from sublime_plugin_tests import framework
from sublime_plugin_tests.utils.selection import split_selection

# Set up constants
__dir__ = os.path.dirname(os.path.abspath(__file__))
delete_dir = __dir__ + '/delete_files'

# Define our class
class TestVarDelete(framework.TestCase):
    @framework.template(delete_dir + '/plugin.template.py')
    def parse_io_files(self, base_path):
        # Load in input/output
        with open('%s.js' % base_path) as f:
            input_output = f.read()

        # Break up input and output
        (input, expected_output) = re.split(r'---+', input_output)

        # Break up target and expected selection
        input_obj = split_selection(input.strip())
        expected_obj = split_selection(expected_output.strip())

        # Return collected information
        return {
            'target_sel': input_obj['selection'],
            'content': input_obj['content'],
            'expected_sel': expected_obj['selection'],
            'expected_content': expected_obj['content'],
        }

    @classmethod
    def _add_io_test_case(cls, namespace):
        def test_var_delete_fn(self):
            return self.parse_io_files(delete_dir + '/' + namespace)
        setattr(cls, 'test_var_delete_' + namespace, test_var_delete_fn)

# Grab files to load in as tests
# TODO: Clump input/output into a single file
test_filenames = glob.glob(delete_dir + '/*.input.js')
test_namespaces = map(lambda filename: (filename.replace(delete_dir + '/', '')
                                                .replace('.input.js', '')), test_filenames)
skip_tests = [
    'comma-in-var',  # Edge case not yet supported (move to esprima)
    'multiline-all',  # Wide selection not yet supported (implement in plugin_tests)
    'multiline-multiple',  # Wide selection not yet supported (implement in plugin_tests)
    'same-line',  # TODO: Deal with nuance of cursor relocation during edit
    'multiline-middle',  # TODO: Deal with nuance of cursor relocation during edit
    'multiline-end-of-var',  # TODO: Deal with nuance of cursor relocation during edit
    # 'multi-var',  # TODO: Interesting edge case for sets...
    'multiline-start',  # TODO: This is an arguable case... I will wait until the logic is done
]

test_namespaces = ['single-var-start']

# For each of the namespaces
for namespace in test_namespaces:
    # If the test is marked for skipping, skip it
    if namespace in skip_tests:
        continue

    # Otherwise, define the test
    TestVarDelete._add_io_test_case(namespace)
<|endoftext|>


<commit_before>from numpy.random.mtrand import uniform
from netCDF4 import Dataset
from numpy.testing import assert_almost_equal
import os, tempfile, unittest

ndim = 100000
filename1 = tempfile.NamedTemporaryFile(suffix='.nc', delete=False).name
filename2 = tempfile.NamedTemporaryFile(suffix='.nc', delete=False).name
array = uniform(size=(ndim,))

def write_netcdf(filename,dtype='f8',complevel=6):
    nc = Dataset(filename,'w')
    nc.createDimension('n', ndim)
    foo = nc.createVariable('data',\
            dtype,('n'),compression='blosc_lz4',blosc_shuffle=2,complevel=complevel)
    foo[:] = array
    nc.close()

class CompressionTestCase(unittest.TestCase):

    def setUp(self):
        self.filename1 = filename1
        self.filename2 = filename2
        write_netcdf(self.filename1,complevel=0) # no compression
        write_netcdf(self.filename2,complevel=4) # with compression

    def tearDown(self):
        # Remove the temporary files
        os.remove(self.filename1)
        os.remove(self.filename2)

    def runTest(self):
        uncompressed_size = os.stat(self.filename1).st_size
        # check uncompressed data
        f = Dataset(self.filename1)
        size = os.stat(self.filename1).st_size
        assert_almost_equal(array,f.variables['data'][:])
        assert f.variables['data'].filters() ==\
        {'zlib':False,'zstd':False,'bzip2':False,'blosc':False,'shuffle':False,'complevel':0,'fletcher32':False}
        assert_almost_equal(size,uncompressed_size)
        f.close()
        # check compressed data.
        f = Dataset(self.filename2)
        size = os.stat(self.filename2).st_size
        assert_almost_equal(array,f.variables['data'][:])
        dtest= {'zlib': False, 'zstd': False, 'bzip2': False, 'blosc':\
        {'compressor': 'blosc_lz4', 'shuffle': 2, 'blocksize': 800000},\
        'shuffle': False, 'complevel': 4, 'fletcher32': False}
        assert f.variables['data'].filters() == dtest
        assert(size < 0.96*uncompressed_size)
        f.close()

if __name__ == '__main__':
    unittest.main()
<commit_msg>test all blosc filters<commit_after>from numpy.random.mtrand import uniform
from netCDF4 import Dataset
from numpy.testing import assert_almost_equal
import os, tempfile, unittest

ndim = 100000
filename = tempfile.NamedTemporaryFile(suffix='.nc', delete=False).name
datarr = uniform(size=(ndim,))

def write_netcdf(filename,dtype='f8',complevel=6):
    nc = Dataset(filename,'w')
    nc.createDimension('n', ndim)
    foo = nc.createVariable('data',\
            dtype,('n'),compression=None)
    foo_lz = nc.createVariable('data_lz',\
            dtype,('n'),compression='blosc_lz',blosc_shuffle=2,complevel=complevel)
    foo_lz4 = nc.createVariable('data_lz4',\
            dtype,('n'),compression='blosc_lz4',blosc_shuffle=2,complevel=complevel)
    foo_lz4hc = nc.createVariable('data_lz4hc',\
            dtype,('n'),compression='blosc_lz4hc',blosc_shuffle=2,complevel=complevel)
    foo_zlib = nc.createVariable('data_zlib',\
            dtype,('n'),compression='blosc_zlib',blosc_shuffle=2,complevel=complevel)
    foo_zstd = nc.createVariable('data_zstd',\
            dtype,('n'),compression='blosc_zstd',blosc_shuffle=2,complevel=complevel)
    foo_lz[:] = datarr
    foo_lz4[:] = datarr
    foo_lz4hc[:] = datarr
    foo_zlib[:] = datarr
    foo_zstd[:] = datarr
    nc.close()

class CompressionTestCase(unittest.TestCase):

    def setUp(self):
        self.filename = filename
        write_netcdf(self.filename,complevel=4) # with compression

    def tearDown(self):
        # Remove the temporary files
        os.remove(self.filename)

    def runTest(self):
        f = Dataset(self.filename)
        assert_almost_equal(datarr,f.variables['data'][:])
        assert f.variables['data'].filters() ==\
        {'zlib':False,'zstd':False,'bzip2':False,'blosc':False,'shuffle':False,'complevel':0,'fletcher32':False}
        assert_almost_equal(datarr,f.variables['data_lz'][:])
        dtest = {'zlib': False, 'zstd': False, 'bzip2': False, 'blosc':
                {'compressor': 'blosc_lz', 'shuffle': 2, 'blocksize': 800000},
                'shuffle': False, 'complevel': 4, 'fletcher32': False}
        assert f.variables['data_lz'].filters() == dtest
        assert_almost_equal(datarr,f.variables['data_lz4'][:])
        dtest = {'zlib': False, 'zstd': False, 'bzip2': False, 'blosc':
                {'compressor': 'blosc_lz4', 'shuffle': 2, 'blocksize': 800000},
                'shuffle': False, 'complevel': 4, 'fletcher32': False}
        assert f.variables['data_lz4'].filters() == dtest
        assert_almost_equal(datarr,f.variables['data_lz4hc'][:])
        dtest = {'zlib': False, 'zstd': False, 'bzip2': False, 'blosc':
                {'compressor': 'blosc_lz4hc', 'shuffle': 2, 'blocksize': 800000},
                'shuffle': False, 'complevel': 4, 'fletcher32': False}
        assert f.variables['data_lz4hc'].filters() == dtest
        assert_almost_equal(datarr,f.variables['data_zlib'][:])
        dtest = {'zlib': False, 'zstd': False, 'bzip2': False, 'blosc':
                {'compressor': 'blosc_zlib', 'shuffle': 2, 'blocksize': 800000},
                'shuffle': False, 'complevel': 4, 'fletcher32': False}
        assert f.variables['data_zlib'].filters() == dtest
        assert_almost_equal(datarr,f.variables['data_zstd'][:])
        dtest = {'zlib': False, 'zstd': False, 'bzip2': False, 'blosc':
                {'compressor': 'blosc_zstd', 'shuffle': 2, 'blocksize': 800000},
                'shuffle': False, 'complevel': 4, 'fletcher32': False}
        assert f.variables['data_zstd'].filters() == dtest
        f.close()

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


<commit_before><commit_msg>Delete empty model<commit_after><|endoftext|>


<commit_before>from __future__ import print_function
import sys
import json
import time
from functools import wraps
from pprint import pprint
from relevanced_client import Client
from . import caching, urls, crawl

def get_client():
    return Client('localhost', 8097)

@caching.memoize0
def load_large_math():
    return crawl.crawl_urls(urls.MATH)

@caching.memoize0
def load_large_poli():
    return crawl.crawl_urls(urls.POLITICS)

def init_documents(client):
    existing_docs = set(client.list_all_documents().documents)
    math_docs = list(load_large_math())
    articles_by_url = {}
    urls_by_length = []
    for url, doc in load_large_math().iteritems():
        articles_by_url[doc['url']] = doc['text']
        urls_by_length.append((len(doc['text']), doc['url']))
    for url, doc in load_large_poli().iteritems():
        articles_by_url[doc['url']] = doc['text']
        urls_by_length.append((len(doc['text']), doc['url']))

    urls_by_length.sort(key=lambda x: x[0])
    urls_by_length.reverse()
    for _, url in urls_by_length:
        text = articles_by_url[url]
        if url not in existing_docs:
            res = client.create_document_with_id(
                url, text
            )
            print('created : %s' % res.id)

def init_centroids(client):
    existing_centroids = set(client.list_all_centroids().centroids)
    for coll in ('wiki_math', 'wiki_poli'):
        if coll not in existing_centroids:
            client.create_centroid(coll)
    existing_math_docs = set(client.list_all_documents_for_centroid('wiki_math').documents)
    existing_poli_docs = set(client.list_all_documents_for_centroid('wiki_poli').documents)

    for url in load_large_math().keys():
        if url not in existing_math_docs:
            client.add_document_to_centroid(
                'wiki_math', url
            )

    for url in load_large_poli().keys():
        if url not in existing_poli_docs:
            client.add_document_to_centroid(
                'wiki_poli', url
            )

def main():
    client = get_client()
    init_documents(client)
    if not len(client.list_all_centroids().centroids) >= 2:
        init_centroids(client)
    client.join_centroid('wiki_math')
    client.join_centroid('wiki_poli')
    print('\n\n')
    print('math vs math')
    for doc in load_large_math().values()[:10]:
        print(doc['title'])
        print(client.get_text_similarity('wiki_math', doc['text']))

    print('\n\n')
    print('math vs poli')
    for doc in load_large_math().values()[:10]:
        print(doc['title'])
        print(client.get_text_similarity('wiki_poli', doc['text']))

<commit_msg>make main func test use the new multi commands<commit_after>from __future__ import print_function
import sys
import json
import time
from functools import wraps
from pprint import pprint
from relevanced_client import Client
from . import caching, urls, crawl

def get_client():
    return Client('localhost', 8097)

@caching.memoize0
def load_large_math():
    return crawl.crawl_urls(urls.MATH)

@caching.memoize0
def load_large_poli():
    return crawl.crawl_urls(urls.POLITICS)

def init_documents(client):
    existing_docs = set(client.list_all_documents().documents)
    math_docs = list(load_large_math())
    articles_by_url = {}
    urls_by_length = []
    for url, doc in load_large_math().iteritems():
        articles_by_url[doc['url']] = doc['text']
        urls_by_length.append((len(doc['text']), doc['url']))
    for url, doc in load_large_poli().iteritems():
        articles_by_url[doc['url']] = doc['text']
        urls_by_length.append((len(doc['text']), doc['url']))

    urls_by_length.sort(key=lambda x: x[0])
    urls_by_length.reverse()
    for _, url in urls_by_length:
        text = articles_by_url[url]
        if url not in existing_docs:
            res = client.create_document_with_id(
                url, text
            )
            print('created : %s' % res.id)

def init_centroids(client):
    existing_centroids = set(client.list_all_centroids().centroids)
    for coll in ('wiki_math', 'wiki_poli'):
        if coll not in existing_centroids:
            client.create_centroid(coll)
    existing_math_docs = set(client.list_all_documents_for_centroid('wiki_math').documents)
    existing_poli_docs = set(client.list_all_documents_for_centroid('wiki_poli').documents)

    missing_math = [url for url in load_large_math().keys() if url not in existing_math_docs]
    client.add_documents_to_centroid(
        'wiki_math', missing_math
    )
    # for url in load_large_math().keys():
    #     if url not in existing_math_docs:
    #         client.add_document_to_centroid(
    #             'wiki_math', url
    #         )

    missing_poli = [url for url in load_large_poli().keys() if url not in existing_poli_docs]
    client.add_documents_to_centroid(
        'wiki_poli', missing_poli
    )
    # for url in load_large_poli().keys():
    #     if url not in existing_poli_docs:
    #         client.add_document_to_centroid(
    #             'wiki_poli', url
    #         )

def main():
    client = get_client()
    init_documents(client)
    if not len(client.list_all_centroids().centroids) >= 2:
        init_centroids(client)
    client.multi_join_centroids(['wiki_math', 'wiki_poli'])
    print('\n\n')
    print('math vs math')
    for doc in load_large_math().values()[:10]:
        print(doc['title'])
        print(client.get_text_similarity('wiki_math', doc['text']))

    print('\n\n')
    print('math vs poli')
    for doc in load_large_math().values()[:10]:
        print(doc['title'])
        print(client.get_text_similarity('wiki_poli', doc['text']))

<|endoftext|>


<commit_before># encoding: UTF-8
from __future__ import absolute_import, division, print_function
import pytest
import os
import re
import sys
import types

from _pytest.config import get_config, PytestPluginManager
from _pytest.main import EXIT_NOTESTSCOLLECTED, Session

@pytest.fixture
def pytestpm():
    return PytestPluginManager()

class TestPytestPluginInteractions(object):
    def test_addhooks_conftestplugin(self, testdir):
        testdir.makepyfile(
            newhooks="""
            def pytest_myhook(xyz):
                "new hook"
        """
        )
        conf = testdir.makeconftest(
            """
            import sys ; sys.path.insert(0, '.')
            import newhooks
            def pytest_addhooks(pluginmanager):
                pluginmanager.addhooks(newhooks)
            def pytest_myhook(xyz):
                return xyz + 1
        """
        )
        config = get_config()
        pm = config.pluginmanager
        pm.hook.pytest_addhooks.call_historic(
            kwargs=dict(pluginmanager=config.pluginmanager)
        )
        config.pluginmanager._importconftest(conf)
        # print(config.pluginmanager.get_plugins())
        res = config.hook.pytest_myhook(xyz=10)
        assert res == [11]

    def test_addhooks_nohooks(self, testdir):
        testdir.makeconftest(
            """
            import sys
            def pytest_addhooks(pluginmanager):
                pluginmanager.addhooks(sys)
        """
        )
        res = testdir.runpytest()
        assert res.ret != 0
        res.stderr.fnmatch_lines(["*did not find*sys*"])

    def test_namespace_early_from_import(self, testdir):
        p = testdir.makepyfile(
            """
            from pytest import Item
            from pytest import Item as Item2
            assert Item is Item2
        """
        )
        result = testdir.runpython(p)
        assert result.ret == 0

    @pytest.mark.filterwarnings("ignore:pytest_namespace is deprecated")
    def test_do_ext_namespace(self, testdir):
        testdir.makeconftest(
            """
            def pytest_namespace():
                return {'hello': 'world'}
        """
        )
        p = testdir.makepyfile(
            """
            from pytest import hello
            import pytest
            def test_hello():
                assert hello == "world"
                assert 'hello' in pytest.__all__
        """
        )
        reprec = testdir.inline_run(p)
        reprec.assertoutcome(passed=1)

    def test_do_option_postinitialize(self, testdir):
        config = testdir.parseconfigure()
        assert not hasattr(config.option, "test123")
        p = testdir.makepyfile(
            """
            def pytest_addoption(parser):
                parser.addoption('--test123', action="store_true",
                    default=True)
        """
        )
        config.pluginmanager._importconftest(p)
        assert config.option.test123

    def test_configure(self, testdir):
        config = testdir.parseconfig()
        values = []

        class A(object):
            def pytest_configure(self, config):
                values.append(self)

        config.pluginmanager.register(A())
        assert len(values) == 0
        config._do_configure()
        assert len(values) == 1
        config.pluginmanager.register(A())  # leads to a configured() plugin
        assert len(values) == 2
        assert values[0] != values[1]

        config._ensure_unconfigure()
        config.pluginmanager.register(A())
        assert len(values) == 2

    def test_hook_tracing(self):
        pytestpm = get_config().pluginmanager  # fully initialized with plugins
        saveindent = []

        class api1(object):
            def pytest_plugin_registered(self):
                saveindent.append(pytestpm.trace.root.indent)

        class api2(object):
            def pytest_plugin_registered(self):
                saveindent.append(pytestpm.trace.root.indent)
                raise ValueError()

        values = []
        pytestpm.trace.root.setwriter(values.append)
        undo = pytestpm.enable_tracing()
        try:
            indent = pytestpm.trace.root.indent
            p = api1()
            pytestpm.register(p)
            assert pytestpm.trace.root.indent == indent
            assert len(values) >= 2
            assert "pytest_plugin_registered" in values[0]
            assert "finish" in values[1]

            values[:] = []
            with pytest.raises(ValueError):
                pytestpm.register(api2())
            assert pytestpm.trace.root.indent == indent
            assert saveindent[0] > indent
        finally:
            undo()

    def test_hook_proxy(self, testdir):
        """Test the gethookproxy function(#2016)"""
        config = testdir.parseconfig()
        session = Session(config)
        testdir.makepyfile(**{"tests/conftest.py": "", "tests/subdir/conftest.py": ""})

        conftest1 = testdir.tmpdir.join("tests/conftest.py")
        conftest2 = testdir.tmpdir.join("tests/subdir/conftest.py")

        config.pluginmanager._importconftest(conftest1)
        ihook_a = session.gethookproxy(testdir.tmpdir.join("tests"))
        assert ihook_a is not None
        config.pluginmanager._importconftest(conftest2)
        ihook_b = session.gethookproxy(testdir.tmpdir.join("tests"))
        assert ihook_a is not ihook_b

    def test_warn_on_deprecated_addhooks(self, pytestpm):
        warnings = []

        class get_warnings(object):
            def pytest_logwarning(self, code, fslocation, message, nodeid):
                warnings.append(message)

        class Plugin(object):
            def pytest_testhook():
                pass

        pytestpm.register(get_warnings())
        before = list(warnings)
        pytestpm.addhooks(Plugin())
        assert len(warnings) == len(before) + 1
        assert "deprecated" in warnings[-1]

def test_namespace_has_default_and_env_plugins(testdir):
    p = testdir.makepyfile(
        """
        import pytest
        pytest.mark
    """
    )
    result = testdir.runpython(p)
    assert result.ret == 0

def test_default_markers(testdir):
    result = testdir.runpytest("--markers")
    result.stdout.fnmatch_lines(["*tryfirst*first*", "*trylast*last*"])

def test_importplugin_error_message(testdir, pytestpm):
    """Don't hide import errors when importing plugins and provide
    an easy to debug message.

    See #375 and #1998.
    """
    testdir.syspathinsert(testdir.tmpdir)
    testdir.makepyfile(
        qwe="""
        # encoding: UTF-8
        def test_traceback():
            raise ImportError(u'Not possible to import: ☺')
        test_traceback()
    """
    )
    with pytest.raises(ImportError) as excinfo:
        pytestpm.import_plugin("qwe")

    expected_message = '.*Error importing plugin "qwe": Not possible to import: .'
    expected_traceback = ".*in test_traceback"
    assert re.match(expected_message, str(excinfo.value))
    assert re.match(expected_traceback, str(excinfo.traceback[-1]))

class TestPytestPluginManager(object):
    def test_register_imported_modules(self):
        pm = PytestPluginManager()
        mod = types.ModuleType("x.y.pytest_hello")
        pm.register(mod)
        assert pm.is_registered(mod)
        values = pm.get_plugins()
        assert mod in values
        pytest.raises(ValueError, "pm.register(mod)")
        pytest.raises(ValueError, lambda: pm.register(mod))
        # assert not pm.is_registered(mod2)
        assert pm.get_plugins() == values

    def test_canonical_import(self, monkeypatch):
        mod = types.ModuleType("pytest_xyz")
        monkeypatch.setitem(sys.modules, "pytest_xyz", mod)
        pm = PytestPluginManager()
        pm.import_plugin("pytest_xyz")
        assert pm.get_plugin("pytest_xyz") == mod
        assert pm.is_registered(mod)

    def test_consider_module(self, testdir, pytestpm):
        testdir.syspathinsert()
        testdir.makepyfile(pytest_p1="#")
        testdir.makepyfile(pytest_p2="#")
        mod = types.ModuleType("temp")
        mod.pytest_plugins = ["pytest_p1", "pytest_p2"]
        pytestpm.consider_module(mod)
        assert pytestpm.get_plugin("pytest_p1").__name__ == "pytest_p1"
        assert pytestpm.get_plugin("pytest_p2").__name__ == "pytest_p2"

    def test_consider_module_import_module(self, testdir):
        pytestpm = get_config().pluginmanager
        mod = types.ModuleType("x")
        mod.pytest_plugins = "pytest_a"
        aplugin = testdir.makepyfile(pytest_a="#")
        reprec = testdir.make_hook_recorder(pytestpm)
        # syspath.prepend(aplugin.dirpath())
        sys.path.insert(0, str(aplugin.dirpath()))
        pytestpm.consider_module(mod)
        call = reprec.getcall(pytestpm.hook.pytest_plugin_registered.name)
        assert call.plugin.__name__ == "pytest_a"

        # check that it is not registered twice
        pytestpm.consider_module(mod)
        values = reprec.getcalls("pytest_plugin_registered")
        assert len(values) == 1

    def test_consider_env_fails_to_import(self, monkeypatch, pytestpm):
        monkeypatch.setenv("PYTEST_PLUGINS", "nonexisting", prepend=",")
        with pytest.raises(ImportError):
            pytestpm.consider_env()

    def test_plugin_skip(self, testdir, monkeypatch):
        p = testdir.makepyfile(
            skipping1="""
            import pytest
            pytest.skip("hello")
        """
        )
        p.copy(p.dirpath("skipping2.py"))
        monkeypatch.setenv("PYTEST_PLUGINS", "skipping2")
        result = testdir.runpytest("-rw", "-p", "skipping1", syspathinsert=True)
        assert result.ret == EXIT_NOTESTSCOLLECTED
        result.stdout.fnmatch_lines(
            ["*skipped plugin*skipping1*hello*", "*skipped plugin*skipping2*hello*"]
        )

    def test_consider_env_plugin_instantiation(self, testdir, monkeypatch, pytestpm):
        testdir.syspathinsert()
        testdir.makepyfile(xy123="#")
        monkeypatch.setitem(os.environ, "PYTEST_PLUGINS", "xy123")
        l1 = len(pytestpm.get_plugins())
        pytestpm.consider_env()
        l2 = len(pytestpm.get_plugins())
        assert l2 == l1 + 1
        assert pytestpm.get_plugin("xy123")
        pytestpm.consider_env()
        l3 = len(pytestpm.get_plugins())
        assert l2 == l3

    def test_pluginmanager_ENV_startup(self, testdir, monkeypatch):
        testdir.makepyfile(pytest_x500="#")
        p = testdir.makepyfile(
            """
            import pytest
            def test_hello(pytestconfig):
                plugin = pytestconfig.pluginmanager.get_plugin('pytest_x500')
                assert plugin is not None
        """
        )
        monkeypatch.setenv("PYTEST_PLUGINS", "pytest_x500", prepend=",")
        result = testdir.runpytest(p, syspathinsert=True)
        assert result.ret == 0
        result.stdout.fnmatch_lines(["*1 passed*"])

    def test_import_plugin_importname(self, testdir, pytestpm):
        pytest.raises(ImportError, 'pytestpm.import_plugin("qweqwex.y")')
        pytest.raises(ImportError, 'pytestpm.import_plugin("pytest_qweqwx.y")')

        testdir.syspathinsert()
        pluginname = "pytest_hello"
        testdir.makepyfile(**{pluginname: ""})
        pytestpm.import_plugin("pytest_hello")
        len1 = len(pytestpm.get_plugins())
        pytestpm.import_plugin("pytest_hello")
        len2 = len(pytestpm.get_plugins())
        assert len1 == len2
        plugin1 = pytestpm.get_plugin("pytest_hello")
        assert plugin1.__name__.endswith("pytest_hello")
        plugin2 = pytestpm.get_plugin("pytest_hello")
        assert plugin2 is plugin1

    def test_import_plugin_dotted_name(self, testdir, pytestpm):
        pytest.raises(ImportError, 'pytestpm.import_plugin("qweqwex.y")')
        pytest.raises(ImportError, 'pytestpm.import_plugin("pytest_qweqwex.y")')

        testdir.syspathinsert()
        testdir.mkpydir("pkg").join("plug.py").write("x=3")
        pluginname = "pkg.plug"
        pytestpm.import_plugin(pluginname)
        mod = pytestpm.get_plugin("pkg.plug")
        assert mod.x == 3

    def test_consider_conftest_deps(self, testdir, pytestpm):
        mod = testdir.makepyfile("pytest_plugins='xyz'").pyimport()
        with pytest.raises(ImportError):
            pytestpm.consider_conftest(mod)

class TestPytestPluginManagerBootstrapming(object):
    def test_preparse_args(self, pytestpm):
        pytest.raises(
            ImportError, lambda: pytestpm.consider_preparse(["xyz", "-p", "hello123"])
        )

    def test_plugin_prevent_register(self, pytestpm):
        pytestpm.consider_preparse(["xyz", "-p", "no:abc"])
        l1 = pytestpm.get_plugins()
        pytestpm.register(42, name="abc")
        l2 = pytestpm.get_plugins()
        assert len(l2) == len(l1)
        assert 42 not in l2

    def test_plugin_prevent_register_unregistered_alredy_registered(self, pytestpm):
        pytestpm.register(42, name="abc")
        l1 = pytestpm.get_plugins()
        assert 42 in l1
        pytestpm.consider_preparse(["xyz", "-p", "no:abc"])
        l2 = pytestpm.get_plugins()
        assert 42 not in l2
<commit_msg>Remove dangerous sys.path manipulations in test_pluginmanager<commit_after># encoding: UTF-8
from __future__ import absolute_import, division, print_function
import pytest
import os
import re
import sys
import types

from _pytest.config import get_config, PytestPluginManager
from _pytest.main import EXIT_NOTESTSCOLLECTED, Session

@pytest.fixture
def pytestpm():
    return PytestPluginManager()

class TestPytestPluginInteractions(object):
    def test_addhooks_conftestplugin(self, testdir):
        testdir.makepyfile(
            newhooks="""
            def pytest_myhook(xyz):
                "new hook"
        """
        )
        conf = testdir.makeconftest(
            """
            import newhooks
            def pytest_addhooks(pluginmanager):
                pluginmanager.addhooks(newhooks)
            def pytest_myhook(xyz):
                return xyz + 1
        """
        )
        config = get_config()
        pm = config.pluginmanager
        pm.hook.pytest_addhooks.call_historic(
            kwargs=dict(pluginmanager=config.pluginmanager)
        )
        config.pluginmanager._importconftest(conf)
        # print(config.pluginmanager.get_plugins())
        res = config.hook.pytest_myhook(xyz=10)
        assert res == [11]

    def test_addhooks_nohooks(self, testdir):
        testdir.makeconftest(
            """
            import sys
            def pytest_addhooks(pluginmanager):
                pluginmanager.addhooks(sys)
        """
        )
        res = testdir.runpytest()
        assert res.ret != 0
        res.stderr.fnmatch_lines(["*did not find*sys*"])

    def test_namespace_early_from_import(self, testdir):
        p = testdir.makepyfile(
            """
            from pytest import Item
            from pytest import Item as Item2
            assert Item is Item2
        """
        )
        result = testdir.runpython(p)
        assert result.ret == 0

    @pytest.mark.filterwarnings("ignore:pytest_namespace is deprecated")
    def test_do_ext_namespace(self, testdir):
        testdir.makeconftest(
            """
            def pytest_namespace():
                return {'hello': 'world'}
        """
        )
        p = testdir.makepyfile(
            """
            from pytest import hello
            import pytest
            def test_hello():
                assert hello == "world"
                assert 'hello' in pytest.__all__
        """
        )
        reprec = testdir.inline_run(p)
        reprec.assertoutcome(passed=1)

    def test_do_option_postinitialize(self, testdir):
        config = testdir.parseconfigure()
        assert not hasattr(config.option, "test123")
        p = testdir.makepyfile(
            """
            def pytest_addoption(parser):
                parser.addoption('--test123', action="store_true",
                    default=True)
        """
        )
        config.pluginmanager._importconftest(p)
        assert config.option.test123

    def test_configure(self, testdir):
        config = testdir.parseconfig()
        values = []

        class A(object):
            def pytest_configure(self, config):
                values.append(self)

        config.pluginmanager.register(A())
        assert len(values) == 0
        config._do_configure()
        assert len(values) == 1
        config.pluginmanager.register(A())  # leads to a configured() plugin
        assert len(values) == 2
        assert values[0] != values[1]

        config._ensure_unconfigure()
        config.pluginmanager.register(A())
        assert len(values) == 2

    def test_hook_tracing(self):
        pytestpm = get_config().pluginmanager  # fully initialized with plugins
        saveindent = []

        class api1(object):
            def pytest_plugin_registered(self):
                saveindent.append(pytestpm.trace.root.indent)

        class api2(object):
            def pytest_plugin_registered(self):
                saveindent.append(pytestpm.trace.root.indent)
                raise ValueError()

        values = []
        pytestpm.trace.root.setwriter(values.append)
        undo = pytestpm.enable_tracing()
        try:
            indent = pytestpm.trace.root.indent
            p = api1()
            pytestpm.register(p)
            assert pytestpm.trace.root.indent == indent
            assert len(values) >= 2
            assert "pytest_plugin_registered" in values[0]
            assert "finish" in values[1]

            values[:] = []
            with pytest.raises(ValueError):
                pytestpm.register(api2())
            assert pytestpm.trace.root.indent == indent
            assert saveindent[0] > indent
        finally:
            undo()

    def test_hook_proxy(self, testdir):
        """Test the gethookproxy function(#2016)"""
        config = testdir.parseconfig()
        session = Session(config)
        testdir.makepyfile(**{"tests/conftest.py": "", "tests/subdir/conftest.py": ""})

        conftest1 = testdir.tmpdir.join("tests/conftest.py")
        conftest2 = testdir.tmpdir.join("tests/subdir/conftest.py")

        config.pluginmanager._importconftest(conftest1)
        ihook_a = session.gethookproxy(testdir.tmpdir.join("tests"))
        assert ihook_a is not None
        config.pluginmanager._importconftest(conftest2)
        ihook_b = session.gethookproxy(testdir.tmpdir.join("tests"))
        assert ihook_a is not ihook_b

    def test_warn_on_deprecated_addhooks(self, pytestpm):
        warnings = []

        class get_warnings(object):
            def pytest_logwarning(self, code, fslocation, message, nodeid):
                warnings.append(message)

        class Plugin(object):
            def pytest_testhook():
                pass

        pytestpm.register(get_warnings())
        before = list(warnings)
        pytestpm.addhooks(Plugin())
        assert len(warnings) == len(before) + 1
        assert "deprecated" in warnings[-1]

def test_namespace_has_default_and_env_plugins(testdir):
    p = testdir.makepyfile(
        """
        import pytest
        pytest.mark
    """
    )
    result = testdir.runpython(p)
    assert result.ret == 0

def test_default_markers(testdir):
    result = testdir.runpytest("--markers")
    result.stdout.fnmatch_lines(["*tryfirst*first*", "*trylast*last*"])

def test_importplugin_error_message(testdir, pytestpm):
    """Don't hide import errors when importing plugins and provide
    an easy to debug message.

    See #375 and #1998.
    """
    testdir.syspathinsert(testdir.tmpdir)
    testdir.makepyfile(
        qwe="""
        # encoding: UTF-8
        def test_traceback():
            raise ImportError(u'Not possible to import: ☺')
        test_traceback()
    """
    )
    with pytest.raises(ImportError) as excinfo:
        pytestpm.import_plugin("qwe")

    expected_message = '.*Error importing plugin "qwe": Not possible to import: .'
    expected_traceback = ".*in test_traceback"
    assert re.match(expected_message, str(excinfo.value))
    assert re.match(expected_traceback, str(excinfo.traceback[-1]))

class TestPytestPluginManager(object):
    def test_register_imported_modules(self):
        pm = PytestPluginManager()
        mod = types.ModuleType("x.y.pytest_hello")
        pm.register(mod)
        assert pm.is_registered(mod)
        values = pm.get_plugins()
        assert mod in values
        pytest.raises(ValueError, "pm.register(mod)")
        pytest.raises(ValueError, lambda: pm.register(mod))
        # assert not pm.is_registered(mod2)
        assert pm.get_plugins() == values

    def test_canonical_import(self, monkeypatch):
        mod = types.ModuleType("pytest_xyz")
        monkeypatch.setitem(sys.modules, "pytest_xyz", mod)
        pm = PytestPluginManager()
        pm.import_plugin("pytest_xyz")
        assert pm.get_plugin("pytest_xyz") == mod
        assert pm.is_registered(mod)

    def test_consider_module(self, testdir, pytestpm):
        testdir.syspathinsert()
        testdir.makepyfile(pytest_p1="#")
        testdir.makepyfile(pytest_p2="#")
        mod = types.ModuleType("temp")
        mod.pytest_plugins = ["pytest_p1", "pytest_p2"]
        pytestpm.consider_module(mod)
        assert pytestpm.get_plugin("pytest_p1").__name__ == "pytest_p1"
        assert pytestpm.get_plugin("pytest_p2").__name__ == "pytest_p2"

    def test_consider_module_import_module(self, testdir):
        pytestpm = get_config().pluginmanager
        mod = types.ModuleType("x")
        mod.pytest_plugins = "pytest_a"
        aplugin = testdir.makepyfile(pytest_a="#")
        reprec = testdir.make_hook_recorder(pytestpm)
        testdir.syspathinsert(aplugin.dirpath())
        pytestpm.consider_module(mod)
        call = reprec.getcall(pytestpm.hook.pytest_plugin_registered.name)
        assert call.plugin.__name__ == "pytest_a"

        # check that it is not registered twice
        pytestpm.consider_module(mod)
        values = reprec.getcalls("pytest_plugin_registered")
        assert len(values) == 1

    def test_consider_env_fails_to_import(self, monkeypatch, pytestpm):
        monkeypatch.setenv("PYTEST_PLUGINS", "nonexisting", prepend=",")
        with pytest.raises(ImportError):
            pytestpm.consider_env()

    def test_plugin_skip(self, testdir, monkeypatch):
        p = testdir.makepyfile(
            skipping1="""
            import pytest
            pytest.skip("hello")
        """
        )
        p.copy(p.dirpath("skipping2.py"))
        monkeypatch.setenv("PYTEST_PLUGINS", "skipping2")
        result = testdir.runpytest("-rw", "-p", "skipping1", syspathinsert=True)
        assert result.ret == EXIT_NOTESTSCOLLECTED
        result.stdout.fnmatch_lines(
            ["*skipped plugin*skipping1*hello*", "*skipped plugin*skipping2*hello*"]
        )

    def test_consider_env_plugin_instantiation(self, testdir, monkeypatch, pytestpm):
        testdir.syspathinsert()
        testdir.makepyfile(xy123="#")
        monkeypatch.setitem(os.environ, "PYTEST_PLUGINS", "xy123")
        l1 = len(pytestpm.get_plugins())
        pytestpm.consider_env()
        l2 = len(pytestpm.get_plugins())
        assert l2 == l1 + 1
        assert pytestpm.get_plugin("xy123")
        pytestpm.consider_env()
        l3 = len(pytestpm.get_plugins())
        assert l2 == l3

    def test_pluginmanager_ENV_startup(self, testdir, monkeypatch):
        testdir.makepyfile(pytest_x500="#")
        p = testdir.makepyfile(
            """
            import pytest
            def test_hello(pytestconfig):
                plugin = pytestconfig.pluginmanager.get_plugin('pytest_x500')
                assert plugin is not None
        """
        )
        monkeypatch.setenv("PYTEST_PLUGINS", "pytest_x500", prepend=",")
        result = testdir.runpytest(p, syspathinsert=True)
        assert result.ret == 0
        result.stdout.fnmatch_lines(["*1 passed*"])

    def test_import_plugin_importname(self, testdir, pytestpm):
        pytest.raises(ImportError, 'pytestpm.import_plugin("qweqwex.y")')
        pytest.raises(ImportError, 'pytestpm.import_plugin("pytest_qweqwx.y")')

        testdir.syspathinsert()
        pluginname = "pytest_hello"
        testdir.makepyfile(**{pluginname: ""})
        pytestpm.import_plugin("pytest_hello")
        len1 = len(pytestpm.get_plugins())
        pytestpm.import_plugin("pytest_hello")
        len2 = len(pytestpm.get_plugins())
        assert len1 == len2
        plugin1 = pytestpm.get_plugin("pytest_hello")
        assert plugin1.__name__.endswith("pytest_hello")
        plugin2 = pytestpm.get_plugin("pytest_hello")
        assert plugin2 is plugin1

    def test_import_plugin_dotted_name(self, testdir, pytestpm):
        pytest.raises(ImportError, 'pytestpm.import_plugin("qweqwex.y")')
        pytest.raises(ImportError, 'pytestpm.import_plugin("pytest_qweqwex.y")')

        testdir.syspathinsert()
        testdir.mkpydir("pkg").join("plug.py").write("x=3")
        pluginname = "pkg.plug"
        pytestpm.import_plugin(pluginname)
        mod = pytestpm.get_plugin("pkg.plug")
        assert mod.x == 3

    def test_consider_conftest_deps(self, testdir, pytestpm):
        mod = testdir.makepyfile("pytest_plugins='xyz'").pyimport()
        with pytest.raises(ImportError):
            pytestpm.consider_conftest(mod)

class TestPytestPluginManagerBootstrapming(object):
    def test_preparse_args(self, pytestpm):
        pytest.raises(
            ImportError, lambda: pytestpm.consider_preparse(["xyz", "-p", "hello123"])
        )

    def test_plugin_prevent_register(self, pytestpm):
        pytestpm.consider_preparse(["xyz", "-p", "no:abc"])
        l1 = pytestpm.get_plugins()
        pytestpm.register(42, name="abc")
        l2 = pytestpm.get_plugins()
        assert len(l2) == len(l1)
        assert 42 not in l2

    def test_plugin_prevent_register_unregistered_alredy_registered(self, pytestpm):
        pytestpm.register(42, name="abc")
        l1 = pytestpm.get_plugins()
        assert 42 in l1
        pytestpm.consider_preparse(["xyz", "-p", "no:abc"])
        l2 = pytestpm.get_plugins()
        assert 42 not in l2
<|endoftext|>


<commit_before>from djangosanetesting.cases import UnitTestCase

from testapp.models import ExampleModel

class TestUnitSimpleMetods(UnitTestCase):
    def test_true(self):
        self.assert_true(True)

    def test_true_false(self):
        self.assert_raises(AssertionError, lambda:self.assert_true(False))

    def raise_value_error(self):
        # lambda cannot do it, fix Python
        raise ValueError()

    def test_raises(self):
        self.assert_true(True, self.assert_raises(ValueError, lambda:self.raise_value_error()))

    def test_raises_raise_assertion(self):
        self.assert_raises(AssertionError, lambda: self.assert_raises(ValueError, lambda: "a"))

    def test_equals(self):
        self.assert_equals(1, 1)

    def test_equals_false(self):
        self.assert_raises(AssertionError, lambda:self.assert_equals(1, 2))

class TestUnitAliases(UnitTestCase):

    def get_camel(self, name):
        """ Transform under_score_names to underScoreNames """
        if name.startswith("_") or name.endswith("_"):
            raise ValueError(u"Cannot ransform to CamelCase world when name begins or ends with _")

        camel = list(name)

        while "_" in camel:
            index = camel.index("_")
            del camel[index]
            if camel[index] is "_":
                raise ValueError(u"Double underscores are not allowed")
            camel[index] = camel[index].upper()
        return ''.join(camel)

    def test_camelcase_aliases(self):
        for i in ["assert_true", "assert_equals", "assert_false", "assert_almost_equals"]:
            #FIXME: yield tests after #12 is resolved
            #yield lambda x, y: self.assert_equals, getattr(self, i), getattr(self, self.get_camel(i))
            self.assert_equals(getattr(self, i), getattr(self, self.get_camel(i)))

    def test_get_camel(self):
        self.assert_equals("assertTrue", self.get_camel("assert_true"))

    def test_get_camel_invalid_trail(self):
        self.assert_raises(ValueError, lambda:self.get_camel("some_trailing_test_"))

    def test_get_camel_invalid_double_under(self):
        self.assert_raises(ValueError, lambda:self.get_camel("toomuchtrail__between"))

    def test_get_camel_invalid_prefix(self):
        self.assert_raises(ValueError, lambda:self.get_camel("_prefix"))

class TestProperClashing(UnitTestCase):
    """
    Test we're getting expected failures when working with db,
    i.e. that database is not purged between tests.
    We rely that test suite executed methods in this order:
      (1) test_aaa_inserting_two
      (2) test_bbb_inserting_another_two

    This is antipattern and should not be used, but it's hard to test
    framework from within ;) Better solution would be greatly appreciated.
    """

    def test_aaa_inserting_two(self):
        ExampleModel.objects.create(name="test1")
        self.assert_equals(1, len(ExampleModel.objects.all()))

    def test_bbb_inserting_another_two(self):
        ExampleModel.objects.create(name="test2")
        self.assert_equals(2, len(ExampleModel.objects.all()))

def function_test():
    # just to verify we work with them
    assert True is True<commit_msg>Fixed bad test names<commit_after>from djangosanetesting.cases import UnitTestCase

from testapp.models import ExampleModel

class TestUnitSimpleMetods(UnitTestCase):
    def test_true(self):
        self.assert_true(True)

    def test_true_false(self):
        self.assert_raises(AssertionError, lambda:self.assert_true(False))

    def raise_value_error(self):
        # lambda cannot do it, fix Python
        raise ValueError()

    def test_raises(self):
        self.assert_true(True, self.assert_raises(ValueError, lambda:self.raise_value_error()))

    def test_raises_raise_assertion(self):
        self.assert_raises(AssertionError, lambda: self.assert_raises(ValueError, lambda: "a"))

    def test_equals(self):
        self.assert_equals(1, 1)

    def test_equals_false(self):
        self.assert_raises(AssertionError, lambda:self.assert_equals(1, 2))

class TestUnitAliases(UnitTestCase):

    def get_camel(self, name):
        """ Transform under_score_names to underScoreNames """
        if name.startswith("_") or name.endswith("_"):
            raise ValueError(u"Cannot ransform to CamelCase world when name begins or ends with _")

        camel = list(name)

        while "_" in camel:
            index = camel.index("_")
            del camel[index]
            if camel[index] is "_":
                raise ValueError(u"Double underscores are not allowed")
            camel[index] = camel[index].upper()
        return ''.join(camel)

    def test_camelcase_aliases(self):
        for i in ["assert_true", "assert_equals", "assert_false", "assert_almost_equals"]:
            #FIXME: yield tests after #12 is resolved
            #yield lambda x, y: self.assert_equals, getattr(self, i), getattr(self, self.get_camel(i))
            self.assert_equals(getattr(self, i), getattr(self, self.get_camel(i)))

    def test_get_camel(self):
        self.assert_equals("assertTrue", self.get_camel("assert_true"))

    def test_get_camel_invalid_trail(self):
        self.assert_raises(ValueError, lambda:self.get_camel("some_trailing_test_"))

    def test_get_camel_invalid_double_under(self):
        self.assert_raises(ValueError, lambda:self.get_camel("toomuchtrail__between"))

    def test_get_camel_invalid_prefix(self):
        self.assert_raises(ValueError, lambda:self.get_camel("_prefix"))

class TestProperClashing(UnitTestCase):
    """
    Test we're getting expected failures when working with db,
    i.e. that database is not purged between tests.
    We rely that test suite executed methods in this order:
      (1) test_aaa_inserting_model
      (2) test_bbb_inserting_another

    This is antipattern and should not be used, but it's hard to test
    framework from within ;) Better solution would be greatly appreciated.
    """

    def test_aaa_inserting_model(self):
        ExampleModel.objects.create(name="test1")
        self.assert_equals(1, len(ExampleModel.objects.all()))

    def test_bbb_inserting_another(self):
        ExampleModel.objects.create(name="test2")
        self.assert_equals(2, len(ExampleModel.objects.all()))

def function_test():
    # just to verify we work with them
    assert True is True<|endoftext|>


<commit_before>from django.db import models
from django.utils import timezone
from parting import PartitionForeignKey, PartitionManager
from dateutil.relativedelta import relativedelta

def _key_from_dt(dt):
    return dt.strftime('%Y_%m')

class CustomManager(models.Manager):
    def my_custom_method(self):
        return u'hi!'

class TweetPartitionManager(PartitionManager):

    def current_partition_key(self):
        return _key_from_dt(timezone.now())

    def next_partition_key(self):
        return _key_from_dt(timezone.now() + relativedelta(months=+1))

    def get_managers(self, partition):
        return [
            ('objects', CustomManager()),
        ]

class Tweet(models.Model):

    json = models.TextField()
    created = models.DateTimeField(default=timezone.now)

    partitions = TweetPartitionManager()

    class Meta:
        abstract = True

class Star(models.Model):

    user = models.TextField()
    tweet = PartitionForeignKey(Tweet)

    partitions = PartitionManager()

    class Meta:
        abstract = True
<commit_msg>Travis debugging<commit_after>from django.db import models
from django.utils import timezone
try:
    from parting import PartitionForeignKey, PartitionManager
except ImportError:
    print('Hoo!')
    raise
from dateutil.relativedelta import relativedelta

def _key_from_dt(dt):
    return dt.strftime('%Y_%m')

class CustomManager(models.Manager):
    def my_custom_method(self):
        return u'hi!'

class TweetPartitionManager(PartitionManager):

    def current_partition_key(self):
        return _key_from_dt(timezone.now())

    def next_partition_key(self):
        return _key_from_dt(timezone.now() + relativedelta(months=+1))

    def get_managers(self, partition):
        return [
            ('objects', CustomManager()),
        ]

class Tweet(models.Model):

    json = models.TextField()
    created = models.DateTimeField(default=timezone.now)

    partitions = TweetPartitionManager()

    class Meta:
        abstract = True

class Star(models.Model):

    user = models.TextField()
    tweet = PartitionForeignKey(Tweet)

    partitions = PartitionManager()

    class Meta:
        abstract = True
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
import sys
sys.path.insert(0,".")
import unittest
import neuroml
import neuroml.writers as writers
import PyOpenWorm
from PyOpenWorm import *
import networkx
import rdflib
import rdflib as R
import pint as Q
import os
import subprocess as SP
import subprocess
import tempfile
import doctest

from glob import glob

from GraphDBInit import *

from DataTestTemplate import _DataTest

class EvidenceCoverageTest(_DataTest):

    def test_verify_neurons_have_evidence(self):
        """ For each neuron in PyOpenWorm, verify
        that there is supporting evidence"""
        net = Worm().neuron_network()
        neurons = list(net.neurons())
        evcheck = []
        for n in neurons:
            hasEvidence = len(get_supporting_evidence(n))
            evcheck.append(hasEvidence)
            hasEvidence = len(get_supporting_evidence(n.neurotransmitter))
            evcheck.append(hasEvidence)
            hasEvidence = len(get_supporting_evidence(n.type))
            evcheck.append(hasEvidence)
            hasEvidence = len(get_supporting_evidence(n.innexin))
            evcheck.append(hasEvidence)
            hasEvidence = len(get_supporting_evidence(n.neuropeptide))
            evcheck.append(hasEvidence)
            hasEvidence = len(get_supporting_evidence(n.receptor))
            evcheck.append(hasEvidence)

        self.assertTrue(0 not in evcheck)

    @unittest.expectedFailure
    def test_verify_muslces_have_evidence(self):
        """ For each muscle in PyOpenWorm, verify
        that there is supporting evidence"""
        muscles = list(Worm().muscles())
        muscle_evcheck = []
        for mobj in muscles:
            hasEvidence = len(get_supporting_evidence(mobj))
            muscle_evcheck.append(hasEvidence)

        self.assertTrue(0 not in muscle_evcheck)

    @unittest.expectedFailure
    def test_verify_connections_have_evidence(self):
        """ For each connection in PyOpenWorm, verify that there is
        supporting evidence. """
        net = Worm().neuron_network()
        connections = list(net.synapses())
        evcheck = []
        for c in connections:
            has_evidence = len(get_supporting_evidence(c))
            evcheck.append(has_evidence)

        self.assertTrue(0 not in evcheck)

    @unittest.skip('There is no information at present about channels')
    def test_verify_channels_have_evidence(self):
        """ For each channel in PyOpenWorm, verify that there is
        supporting evidence. """
        pass

    def get_supporting_evidence(fact):
        """ Helper function for checking amount of Evidence.
        Returns list of Evidence supporting fact. """
        ev = Evidence()
        ev.asserts(fact)
        return list(ev.load())
<commit_msg>still at it: fixing evidence coverage test<commit_after># -*- coding: utf-8 -*-
import sys
sys.path.insert(0,".")
import unittest
import neuroml
import neuroml.writers as writers
import PyOpenWorm as P
from PyOpenWorm import *
import networkx
import rdflib
import rdflib as R
import pint as Q
import os
import subprocess as SP
import subprocess
import tempfile
import doctest

from glob import glob

from GraphDBInit import *

from DataTestTemplate import _DataTest

class EvidenceCoverageTest(_DataTest):

    def test_verify_neurons_have_evidence(self):
        """ For each neuron in PyOpenWorm, verify
        that there is supporting evidence"""

        neurons = list(P.Neuron().load())
        evcheck = []
        for n in neurons:

            hasEvidence = len(get_supporting_evidence(n)) +
            len(get_supporting_evidence(n.neurotransmitter)) +
            len(get_supporting_evidence(n.type)) +
            len(get_supporting_evidence(n.innexin)) +
            len(get_supporting_evidence(n.neuropeptide)) +
            len(get_supporting_evidence(n.receptor))

            evcheck.append(hasEvidence)

        self.assertTrue(0 not in evcheck)

    @unittest.expectedFailure
    def test_verify_muslces_have_evidence(self):
        """ For each muscle in PyOpenWorm, verify
        that there is supporting evidence"""
        muscles = list(Worm().muscles())
        muscle_evcheck = []
        for mobj in muscles:
            hasEvidence = len(get_supporting_evidence(mobj))
            muscle_evcheck.append(hasEvidence)

        self.assertTrue(0 not in muscle_evcheck)

    @unittest.expectedFailure
    def test_verify_connections_have_evidence(self):
        """ For each connection in PyOpenWorm, verify that there is
        supporting evidence. """
        net = Worm().neuron_network()
        connections = list(net.synapses())
        evcheck = []
        for c in connections:
            has_evidence = len(get_supporting_evidence(c))
            evcheck.append(has_evidence)

        self.assertTrue(0 not in evcheck)

    @unittest.skip('There is no information at present about channels')
    def test_verify_channels_have_evidence(self):
        """ For each channel in PyOpenWorm, verify that there is
        supporting evidence. """
        pass

    def get_supporting_evidence(fact):
        """ Helper function for checking amount of Evidence.
        Returns list of Evidence supporting fact. """
        ev = Evidence()
        ev.asserts(fact)
        return list(ev.load())
<|endoftext|>


<commit_before>
# Copyright 2014 Open Connectome Project (http://openconnecto.me)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Created by Disa Mhembere
# Email: disa@jhu.edu
"""
WSGI config for ocpipeline project.

This module contains the WSGI application used by Django's development server
and any production WSGI deployments. It should expose a module-level variable
named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover
this application via the ``WSGI_APPLICATION`` setting.

Usually you will have the standard Django WSGI application here, but it also
might make sense to replace the whole Django WSGI application with a custom one
that later delegates to the Django one. For example, you could introduce WSGI
middleware here, or combine a Django application with an application of another
framework.

"""
import os
import sys

# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application

os.sys.path += [ os.path.abspath(os.path.join(os.path.dirname(__file__), ".." )),  os.path.abspath(os.path.join(os.path.dirname(__file__), "../../..")) ]
os.environ["DJANGO_SETTINGS_MODULE"] = "ocpipeline.settings"
os.environ["HOME"] = "/tmp"
#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ocpipeline.settings")

application = get_wsgi_application()

# Http add on
#os.environ['HTTPS'] = "on" # DM 03/24/2013
<commit_msg>wsgi add M2G_HOME<commit_after>
# Copyright 2014 Open Connectome Project (http://openconnecto.me)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Created by Disa Mhembere
# Email: disa@jhu.edu
"""
WSGI config for ocpipeline project.

This module contains the WSGI application used by Django's development server
and any production WSGI deployments. It should expose a module-level variable
named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover
this application via the ``WSGI_APPLICATION`` setting.

Usually you will have the standard Django WSGI application here, but it also
might make sense to replace the whole Django WSGI application with a custom one
that later delegates to the Django one. For example, you could introduce WSGI
middleware here, or combine a Django application with an application of another
framework.

"""
import os
import sys

# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.wsgi import get_wsgi_application

os.sys.path += [ os.path.abspath(os.path.join(os.path.dirname(__file__), ".." )),  os.path.abspath(os.path.join(os.path.dirname(__file__), "../../..")) ]
os.environ["DJANGO_SETTINGS_MODULE"] = "ocpipeline.settings"
os.environ["HOME"] = "/tmp"
#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ocpipeline.settings")

_application = get_wsgi_application()

env_variables_to_pass = ['M2G_HOME']
def application(environ, start_response):
  # pass the WSGI environment variables on through to os.environ
  for var in env_variables_to_pass:
      os.environ[var] = environ.get(var, '')
  return _application(environ, start_response)
<|endoftext|>


<commit_before>import os
import shutil
import subprocess

import pytest

from bcbio import utils

from tests.conftest import (make_workdir, get_post_process_yaml, install_cwl_test_files)

@pytest.mark.docker
@pytest.mark.docker_multicore
def test_docker(install_test_files, data_dir):
    """Run an analysis with code and tools inside a docker container.

    Requires https://github.com/chapmanb/bcbio-nextgen-vm
    """
    with make_workdir() as workdir:
        cl = [
            "bcbio_vm.py",
            "--datadir=%s" % data_dir,
            "run",
            "--image=quay.io/bcbio/bcbio-vc",
            "--systemconfig=%s" % get_post_process_yaml(data_dir, workdir),
            "--fcdir=%s" % os.path.join(
                data_dir, os.pardir, "100326_FC6107FAAXX"),
            os.path.join(data_dir, "run_info-bam.yaml")
        ]
        subprocess.check_call(cl)

@pytest.mark.docker
@pytest.mark.docker_ipython
def test_docker_ipython(install_test_files, data_dir):
    """Run an analysis with code and tools inside a docker container,
    driven via IPython.

    Requires https://github.com/chapmanb/bcbio-nextgen-vm
    """
    with make_workdir() as workdir:
        cl = [
            "bcbio_vm.py",
            "--datadir=%s" % data_dir,
            "ipython",
            "--systemconfig=%s" % get_post_process_yaml(data_dir, workdir),
            "--fcdir=%s" % os.path.join(
                data_dir, os.pardir, "100326_FC6107FAAXX"),
            os.path.join(data_dir, "run_info-bam.yaml"),
            "lsf", "localrun"
        ]
        subprocess.check_call(cl)

class TestCWL():
    """ Run simple CWL workflows.

    Requires https://github.com/chapmanb/bcbio-nextgen-vm
    """
    @pytest.mark.cwl
    @pytest.mark.cwl_docker
    @pytest.mark.cwl_docker_somatic
    def test_2_cwl_docker_somatic(self, data_dir):
        """CWL: run a somatic workflow using Docker.
        """
        with install_cwl_test_files(data_dir) as workdir:
            with utils.chdir(os.path.join(workdir, "somatic")):
                cl = ["bash", "./run_generate_cwl.sh"]
                subprocess.check_call(cl)
                if os.path.exists("toil_work"):
                    shutil.rmtree("toil_work")
                cl = ["bash", "./run_toil.sh"]
                subprocess.check_call(cl)

    @pytest.mark.cwl
    @pytest.mark.cwl_docker
    @pytest.mark.cwl_docker_joint
    def test_3_cwl_docker_joint(self, data_dir):
        """CWL: run a gVCF based joint-calling workflow using Docker.
        """
        with install_cwl_test_files(data_dir) as workdir:
            with utils.chdir(os.path.join(workdir, "gvcf_joint")):
                cl = ["bash", "./run_generate_cwl.sh"]
                subprocess.check_call(cl)
                if os.path.exists("bunny_work"):
                    shutil.rmtree("bunny_work")
                cl = ["bash", "./run_bunny.sh"]
                subprocess.check_call(cl)

    @pytest.mark.cwl
    @pytest.mark.cwl_docker_rnaseq
    def test_4_cwl_rnaseq(self, install_test_files, data_dir):
        """CWL: run a RNA-seq workflow using Docker.
        """
        with install_cwl_test_files(data_dir) as workdir:
            with utils.chdir(os.path.join(workdir, "rnaseq")):
                cl = ["bash", "./run_generate_cwl.sh"]
                if os.path.exists("bunny_work"):
                    shutil.rmtree("bunny_work")
                cl = ["bash", "./run_bunny.sh"]
                subprocess.check_call(cl)

    @pytest.mark.cwl
    @pytest.mark.cwl_local
    @pytest.mark.install_required
    def test_1_cwl_local(self, install_test_files, data_dir):
        """CWL: prepare somatic workflow and run on local installation.
        """
        with install_cwl_test_files(data_dir) as workdir:
            with utils.chdir(os.path.join(workdir, "somatic")):
                cl = ["bash", "./run_generate_cwl.sh"]
                subprocess.check_call(cl)
                if os.path.exists("cwltool_work"):
                    shutil.rmtree("cwltool_work")
                cl = ["bash", "./run_cwltool.sh"]
                subprocess.check_call(cl)

    @pytest.mark.cwl
    @pytest.mark.cwl_arvados
    def test_5_cwl_arvados(self, install_test_files, data_dir):
        """CWL: prepare workflow for running on Arvados.

        Requires ARVADOS_API_HOST and ARVADOS_API_TOKEN set
        """
        if os.environ.get("ARVADOS_API_HOST") and os.environ.get("ARVADOS_API_TOKEN"):
            with install_cwl_test_files(data_dir) as workdir:
                with utils.chdir(os.path.join(workdir, "arvados")):
                    cl = ["bash", "./run_generate_cwl.sh"]
                    subprocess.check_call(cl)<commit_msg>CWL RNA-seq: test with Toil runner<commit_after>import os
import shutil
import subprocess

import pytest

from bcbio import utils

from tests.conftest import (make_workdir, get_post_process_yaml, install_cwl_test_files)

@pytest.mark.docker
@pytest.mark.docker_multicore
def test_docker(install_test_files, data_dir):
    """Run an analysis with code and tools inside a docker container.

    Requires https://github.com/chapmanb/bcbio-nextgen-vm
    """
    with make_workdir() as workdir:
        cl = [
            "bcbio_vm.py",
            "--datadir=%s" % data_dir,
            "run",
            "--image=quay.io/bcbio/bcbio-vc",
            "--systemconfig=%s" % get_post_process_yaml(data_dir, workdir),
            "--fcdir=%s" % os.path.join(
                data_dir, os.pardir, "100326_FC6107FAAXX"),
            os.path.join(data_dir, "run_info-bam.yaml")
        ]
        subprocess.check_call(cl)

@pytest.mark.docker
@pytest.mark.docker_ipython
def test_docker_ipython(install_test_files, data_dir):
    """Run an analysis with code and tools inside a docker container,
    driven via IPython.

    Requires https://github.com/chapmanb/bcbio-nextgen-vm
    """
    with make_workdir() as workdir:
        cl = [
            "bcbio_vm.py",
            "--datadir=%s" % data_dir,
            "ipython",
            "--systemconfig=%s" % get_post_process_yaml(data_dir, workdir),
            "--fcdir=%s" % os.path.join(
                data_dir, os.pardir, "100326_FC6107FAAXX"),
            os.path.join(data_dir, "run_info-bam.yaml"),
            "lsf", "localrun"
        ]
        subprocess.check_call(cl)

class TestCWL():
    """ Run simple CWL workflows.

    Requires https://github.com/chapmanb/bcbio-nextgen-vm
    """
    @pytest.mark.cwl
    @pytest.mark.cwl_docker
    @pytest.mark.cwl_docker_somatic
    def test_2_cwl_docker_somatic(self, data_dir):
        """CWL: run a somatic workflow using Docker.
        """
        with install_cwl_test_files(data_dir) as workdir:
            with utils.chdir(os.path.join(workdir, "somatic")):
                cl = ["bash", "./run_generate_cwl.sh"]
                subprocess.check_call(cl)
                if os.path.exists("toil_work"):
                    shutil.rmtree("toil_work")
                cl = ["bash", "./run_toil.sh"]
                subprocess.check_call(cl)

    @pytest.mark.cwl
    @pytest.mark.cwl_docker
    @pytest.mark.cwl_docker_joint
    def test_3_cwl_docker_joint(self, data_dir):
        """CWL: run a gVCF based joint-calling workflow using Docker.
        """
        with install_cwl_test_files(data_dir) as workdir:
            with utils.chdir(os.path.join(workdir, "gvcf_joint")):
                cl = ["bash", "./run_generate_cwl.sh"]
                subprocess.check_call(cl)
                if os.path.exists("bunny_work"):
                    shutil.rmtree("bunny_work")
                cl = ["bash", "./run_bunny.sh"]
                subprocess.check_call(cl)

    @pytest.mark.cwl
    @pytest.mark.cwl_docker_rnaseq
    def test_4_cwl_rnaseq(self, install_test_files, data_dir):
        """CWL: run a RNA-seq workflow using Docker.
        """
        with install_cwl_test_files(data_dir) as workdir:
            with utils.chdir(os.path.join(workdir, "rnaseq")):
                cl = ["bash", "./run_generate_cwl.sh"]
                if os.path.exists("toil_work"):
                    shutil.rmtree("toil_work")
                cl = ["bash", "./run_toil.sh"]
                subprocess.check_call(cl)

    @pytest.mark.cwl
    @pytest.mark.cwl_local
    @pytest.mark.install_required
    def test_1_cwl_local(self, install_test_files, data_dir):
        """CWL: prepare somatic workflow and run on local installation.
        """
        with install_cwl_test_files(data_dir) as workdir:
            with utils.chdir(os.path.join(workdir, "somatic")):
                cl = ["bash", "./run_generate_cwl.sh"]
                subprocess.check_call(cl)
                if os.path.exists("cwltool_work"):
                    shutil.rmtree("cwltool_work")
                cl = ["bash", "./run_cwltool.sh"]
                subprocess.check_call(cl)

    @pytest.mark.cwl
    @pytest.mark.cwl_arvados
    def test_5_cwl_arvados(self, install_test_files, data_dir):
        """CWL: prepare workflow for running on Arvados.

        Requires ARVADOS_API_HOST and ARVADOS_API_TOKEN set
        """
        if os.environ.get("ARVADOS_API_HOST") and os.environ.get("ARVADOS_API_TOKEN"):
            with install_cwl_test_files(data_dir) as workdir:
                with utils.chdir(os.path.join(workdir, "arvados")):
                    cl = ["bash", "./run_generate_cwl.sh"]
                    subprocess.check_call(cl)<|endoftext|>