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

import logging
logger = logging.getLogger(__name__)

import unittest
from scipy.signal import butter, lfilter, freqz
import matplotlib.pyplot as plt
import numpy as np
from ndnoise import filtration
import ndnoise.generator

class MyTestCase(unittest.TestCase):

    def test_butter(self):

        # Sample rate and desired cutoff frequencies (in Hz).
        fs = 5000.0
        lowcut = 500.0
        highcut = 1250.0

        # Plot the frequency response for a few different orders.
        plt.figure(1)
        plt.clf()
        for order in [3, 6, 9]:
            b, a = filtration.butter_bandpass(lowcut, highcut, fs, order=order)
            w, h = freqz(b, a, worN=2000)
            # plt.plot((fs * 0.5 / np.pi) * w, abs(h), label="order = %d" % order)

        # Filter a noisy signal.
        T = 0.05
        nsamples = T * fs
        t = np.linspace(0, T, nsamples, endpoint=False)
        a = 0.02
        f0 = 600.0

        ys = a * np.cos(2 * np.pi * f0 * t + .11)
        x = 0.1 * np.sin(2 * np.pi * 1.2 * np.sqrt(t))
        x += 0.01 * np.cos(2 * np.pi * 312 * t + 0.1)
        x += a * np.cos(2 * np.pi * f0 * t + .11)
        x += 0.03 * np.cos(2 * np.pi * 2000 * t)

        y1 = filtration.butter_bandpass_filter(x, lowcut, highcut, fs, order=6)
        y2 = filtration.butter_bandpass_freq_filter(x, lowcut, highcut, fs, order=6)

        energy_ys = np.sum(ys**2)
        energy_error_y1 = np.sum((ys - y1)**2)
        energy_error_y2 = np.sum((ys - y2)**2)
        energy_error_y2_y1 = np.sum((y1 - y2)**2)

        self.assertLess(energy_error_y2_y1, energy_ys * 0.2, "Both filtered signals are similar")
        # self.assertLess(energy_error_y1, energy_ys * 0.1)

        # plt.plot([0, 0.5 * fs], [np.sqrt(0.5), np.sqrt(0.5)],
        #          '--', label='sqrt(0.5)')
        # plt.xlabel('Frequency (Hz)')
        # plt.ylabel('Gain')
        # plt.grid(True)
        # plt.legend(loc='best')
        #
        # plt.figure(2)
        # plt.clf()
        # plt.plot(t, x, label='Noisy signal')
        #
        # plt.plot(t, ys, label='Original signal (%g Hz)' % f0)
        # plt.plot(t, y1, label='Filtered signal (%g Hz)' % f0)
        # plt.plot(t, y2 + 0.0, label='Filtered signal FFT (%g Hz)' % f0)
        # plt.xlabel('time (seconds)')
        # plt.hlines([-a, a], 0, T, linestyles='--')
        # plt.grid(True)
        # plt.axis('tight')
        # plt.legend(loc='upper left')
        #
        # plt.show()

    def test_dist(self):
        import ndnoise
        dst = filtration.dist_from_center([5, 5])
        self.assertAlmostEqual(dst[0][0], 2 * np.sqrt(2))

    def test_dist_with_defined_voxelsize(self):
        import ndnoise
        dst = filtration.dist_from_center([5, 5], [2.0, 2.0])
        self.assertAlmostEqual(dst[2][2], 0)
        self.assertAlmostEqual(dst[0][0], 4 * np.sqrt(2))

    def test_dist_with_defined_voxelsize_3d(self):
        import ndnoise
        dst = ndnoise.filtration.dist_from_center([5, 5, 5], [1.0, 2.0, 3.0])
        self.assertAlmostEqual(dst[2][2][2], 0)
        self.assertAlmostEqual(dst[0][0][0], np.sqrt(2**2 + 4**2 + 6**2))

    def test_max_x_freq_spectrum_processing(self):
        """
        Test of maximal frequence
        :return:
        """
        shape = [24, 24]
        center = (np.asarray(shape) / 2)

        shspectrum = np.zeros(shape=shape, dtype=np.complex)
        shspectrum[center[0], 0] = 1
        # shspectrum[center[0], 24] = 0
        shspectrum[center[0], center[1]] = 2
        spectrum = np.fft.ifftshift(shspectrum)

        signal, filt, spectrum_ret, freqs = ndnoise.generator.noisef(shape, spectrum=spectrum, return_spectrum=True)
        # import matplotlib.pyplot as plt
        # plt.figure()
        # plt.subplot(131)
        # plt.imshow(signal, cmap="gray")
        # plt.subplot(132)
        # plt.imshow(np.abs(shspectrum), cmap="gray")
        # plt.subplot(133)
        # plt.imshow(np.abs(spectrum_ret), cmap="gray")
        # plt.show()

        # a = signal[[:]]
        suda = signal[:, ::2]
        licha = signal[:, 1::2]

        var0 = np.var(signal)
        vars = np.var(suda)
        varl = np.var(licha)

        self.assertGreater(var0, 2.5 * vars)
        self.assertGreater(var0, 2.5 * varl)

    def test_max_y_freq_spectrum_processing(self):
        """
        Test of maximal frequence
        :return:
        """
        shape = [20, 20]
        # center = np.round((np.asarray(shape) / 2.0))
        center = (np.asarray(shape) / 2)

        shspectrum = np.zeros(shape=shape, dtype=np.complex)
        shspectrum[0, center[1]] = 1
        # shspectrum[center[0], 24] = 0
        shspectrum[center[0], center[1]] = 2
        spectrum = np.fft.ifftshift(shspectrum)

        signal, filt, spectrum_ret, freqs = ndnoise.generator.noisef(shape, spectrum=spectrum, return_spectrum=True)
        # import matplotlib.pyplot as plt
        # plt.figure()
        # plt.subplot(131)
        # plt.imshow(signal, cmap="gray")
        # plt.subplot(132)
        # plt.imshow(np.abs(shspectrum), cmap="gray")
        # plt.subplot(133)
        # plt.imshow(np.abs(spectrum_ret), cmap="gray")
        # plt.show()

        # a = signal[[:]]
        suda = signal[::2, :]
        licha = signal[1::2, :]

        var0 = np.var(signal)
        vars = np.var(suda)
        varl = np.var(licha)

        self.assertGreater(var0, 2.5 * vars)
        self.assertGreater(var0, 2.5 * varl)

    def test_compare_fftfreq(self):
        """
        Test few setups
        :return:
        """
        import itertools
        sampling_rates = [10, 15, 2.1, 0.01]
        ns = [4, 9, 5, 6]
        for params in itertools.product(sampling_rates, ns):
            sampling_rate, n = params
            freq1 = np.fft.fftfreq(n, d=1./sampling_rate)
            freq2 = ndnoise.filtration.fftfreq([n], [1. / sampling_rate])
            logger.debug("freq1 " + str(freq1))
            logger.debug("freq2 " + str(freq2))

            self.assertAlmostEqual(0.0, np.sum((np.abs(freq1) - freq2)**2), msg="parameters " + str(params))

    def test_2d_fftfreq(self):
        import itertools
        spacings = [[10, 1.0], [0.12, 5.0], None, 3]
        shapes = [[10, 11], [9, 11], [8, 10], [15, 2]]
        for params in itertools.product(spacings, shapes):
            spacing, shape = params
            freq = ndnoise.filtration.fftfreq(shape, spacing=spacing)
            logger.debug("freq " + str(freq))

    def test_nd_fftfreq(self):
        import itertools
        spacings = [None, 5, 1.2]
        shapes = [[2], 3, [10, 11], [9, 11, 15], [8, 10, 5, 2], [15, 2, 3, 2, 5]]
        for params in itertools.product(spacings, shapes):
            spacing, shape = params
            freq = ndnoise.filtration.fftfreq(shape, spacing=spacing)
            logger.debug("freq " + str(freq))

    def test_R2P(self):
        x = [10 + 10 * 1j , 1 + 0 * 1j, 0 + 1j]
        expected_absolute = [10 * 2**0.5, 1.0, 1.0]
        expected_angle = [0.25 * np.pi, 0.0, 0.5 * np.pi]
        absolute, angle = ndnoise.filtration.R2P(x)

        for eab, ean, ab, an in zip(expected_absolute, expected_angle, absolute, angle):
            self.assertAlmostEquals(eab, ab)
            self.assertAlmostEquals(ean, an)

    def test_P2R(self):
        expected_x = [10 + 10 * 1j , 1 + 0 * 1j, 0 + 1j]
        absolute = [10 * 2**0.5, 1.0, 1.0]
        angle = [0.25 * np.pi, 0.0, 0.5 * np.pi]
        x = ndnoise.filtration.P2R(absolute, angle)

        for xi, exi in zip(x, expected_x):
            self.assertAlmostEquals(xi, exi)

    def test_lena_filter_ideal(self):
        import scipy.misc
        lena = scipy.misc.ascent()
        spectrum = np.fft.fftn(lena)

        # spectrum = ndnoise.generator.generate_spectrum_seed([100, 100])

        out = filtration.spectrum_filtration(
            spectrum,
            freq_start=0,
            freq_range=0.1,
            exponent=0.1
        )
        signal, filter, spectrum, freqs = out

        ndnoise.show(signal, filter, spectrum, log_view=True)
        plt.show()
        # self.assertEqual(True, False)

    def test_lena_filter_butter(self):
        import scipy.misc
        lena = scipy.misc.ascent()
        spectrum = np.fft.fftn(lena)

        # spectrum = ndnoise.generator.generate_spectrum_seed([100, 100])

        out = filtration.spectrum_filtration(
            spectrum,
            freq_start=0,
            freq_range=0.1,
            exponent=-0.1,
            filter_type="butter"
        )
        signal, filter, spectrum, freqs = out

        ndnoise.show(signal, filter, spectrum, log_view=True)

    def test_2d_butter(self):
        import scipy
        import scipy.signal

        # fs = 100.0
        # lowcut = 0
        # highcut = 10
        # nyq = 0.5 * fs
        # low = lowcut / nyq
        # high = highcut / nyq
        order = 2
        low = 10
        high = 15
        # TODO why is there problem with frequeny# TODO why is there problem with frequeny??

        # worN = np.asarray([[0, 5 , 10], [1, 5, 9], [1, 4, 10]])
        worN = filtration.fftfreq([100, 150], [0.01, 0.01])
        b, a = scipy.signal.butter(order, [low, high], btype='band')
        w, h = response = scipy.signal.freqs(a, b, worN=worN)
        # plt.plot(w, abs(h))
        plt.imshow(abs(h))
        plt.show()
        # y = scipy.signal.lfilter(b, a, data)

if __name__ == '__main__':
    unittest.main()
<commit_msg>skip incomplete unittest<commit_after># -*- coding: utf-8 -*-

import logging
logger = logging.getLogger(__name__)

import unittest
from scipy.signal import butter, lfilter, freqz
import matplotlib.pyplot as plt
import numpy as np
from ndnoise import filtration
import ndnoise.generator

class MyTestCase(unittest.TestCase):

    def test_butter(self):

        # Sample rate and desired cutoff frequencies (in Hz).
        fs = 5000.0
        lowcut = 500.0
        highcut = 1250.0

        # Plot the frequency response for a few different orders.
        plt.figure(1)
        plt.clf()
        for order in [3, 6, 9]:
            b, a = filtration.butter_bandpass(lowcut, highcut, fs, order=order)
            w, h = freqz(b, a, worN=2000)
            # plt.plot((fs * 0.5 / np.pi) * w, abs(h), label="order = %d" % order)

        # Filter a noisy signal.
        T = 0.05
        nsamples = T * fs
        t = np.linspace(0, T, nsamples, endpoint=False)
        a = 0.02
        f0 = 600.0

        ys = a * np.cos(2 * np.pi * f0 * t + .11)
        x = 0.1 * np.sin(2 * np.pi * 1.2 * np.sqrt(t))
        x += 0.01 * np.cos(2 * np.pi * 312 * t + 0.1)
        x += a * np.cos(2 * np.pi * f0 * t + .11)
        x += 0.03 * np.cos(2 * np.pi * 2000 * t)

        y1 = filtration.butter_bandpass_filter(x, lowcut, highcut, fs, order=6)
        y2 = filtration.butter_bandpass_freq_filter(x, lowcut, highcut, fs, order=6)

        energy_ys = np.sum(ys**2)
        energy_error_y1 = np.sum((ys - y1)**2)
        energy_error_y2 = np.sum((ys - y2)**2)
        energy_error_y2_y1 = np.sum((y1 - y2)**2)

        self.assertLess(energy_error_y2_y1, energy_ys * 0.2, "Both filtered signals are similar")
        # self.assertLess(energy_error_y1, energy_ys * 0.1)

        # plt.plot([0, 0.5 * fs], [np.sqrt(0.5), np.sqrt(0.5)],
        #          '--', label='sqrt(0.5)')
        # plt.xlabel('Frequency (Hz)')
        # plt.ylabel('Gain')
        # plt.grid(True)
        # plt.legend(loc='best')
        #
        # plt.figure(2)
        # plt.clf()
        # plt.plot(t, x, label='Noisy signal')
        #
        # plt.plot(t, ys, label='Original signal (%g Hz)' % f0)
        # plt.plot(t, y1, label='Filtered signal (%g Hz)' % f0)
        # plt.plot(t, y2 + 0.0, label='Filtered signal FFT (%g Hz)' % f0)
        # plt.xlabel('time (seconds)')
        # plt.hlines([-a, a], 0, T, linestyles='--')
        # plt.grid(True)
        # plt.axis('tight')
        # plt.legend(loc='upper left')
        #
        # plt.show()

    def test_dist(self):
        import ndnoise
        dst = filtration.dist_from_center([5, 5])
        self.assertAlmostEqual(dst[0][0], 2 * np.sqrt(2))

    def test_dist_with_defined_voxelsize(self):
        import ndnoise
        dst = filtration.dist_from_center([5, 5], [2.0, 2.0])
        self.assertAlmostEqual(dst[2][2], 0)
        self.assertAlmostEqual(dst[0][0], 4 * np.sqrt(2))

    def test_dist_with_defined_voxelsize_3d(self):
        import ndnoise
        dst = ndnoise.filtration.dist_from_center([5, 5, 5], [1.0, 2.0, 3.0])
        self.assertAlmostEqual(dst[2][2][2], 0)
        self.assertAlmostEqual(dst[0][0][0], np.sqrt(2**2 + 4**2 + 6**2))

    def test_max_x_freq_spectrum_processing(self):
        """
        Test of maximal frequence
        :return:
        """
        shape = [24, 24]
        center = (np.asarray(shape) / 2)

        shspectrum = np.zeros(shape=shape, dtype=np.complex)
        shspectrum[center[0], 0] = 1
        # shspectrum[center[0], 24] = 0
        shspectrum[center[0], center[1]] = 2
        spectrum = np.fft.ifftshift(shspectrum)

        signal, filt, spectrum_ret, freqs = ndnoise.generator.noisef(shape, spectrum=spectrum, return_spectrum=True)
        # import matplotlib.pyplot as plt
        # plt.figure()
        # plt.subplot(131)
        # plt.imshow(signal, cmap="gray")
        # plt.subplot(132)
        # plt.imshow(np.abs(shspectrum), cmap="gray")
        # plt.subplot(133)
        # plt.imshow(np.abs(spectrum_ret), cmap="gray")
        # plt.show()

        # a = signal[[:]]
        suda = signal[:, ::2]
        licha = signal[:, 1::2]

        var0 = np.var(signal)
        vars = np.var(suda)
        varl = np.var(licha)

        self.assertGreater(var0, 2.5 * vars)
        self.assertGreater(var0, 2.5 * varl)

    def test_max_y_freq_spectrum_processing(self):
        """
        Test of maximal frequence
        :return:
        """
        shape = [20, 20]
        # center = np.round((np.asarray(shape) / 2.0))
        center = (np.asarray(shape) / 2)

        shspectrum = np.zeros(shape=shape, dtype=np.complex)
        shspectrum[0, center[1]] = 1
        # shspectrum[center[0], 24] = 0
        shspectrum[center[0], center[1]] = 2
        spectrum = np.fft.ifftshift(shspectrum)

        signal, filt, spectrum_ret, freqs = ndnoise.generator.noisef(shape, spectrum=spectrum, return_spectrum=True)
        # import matplotlib.pyplot as plt
        # plt.figure()
        # plt.subplot(131)
        # plt.imshow(signal, cmap="gray")
        # plt.subplot(132)
        # plt.imshow(np.abs(shspectrum), cmap="gray")
        # plt.subplot(133)
        # plt.imshow(np.abs(spectrum_ret), cmap="gray")
        # plt.show()

        # a = signal[[:]]
        suda = signal[::2, :]
        licha = signal[1::2, :]

        var0 = np.var(signal)
        vars = np.var(suda)
        varl = np.var(licha)

        self.assertGreater(var0, 2.5 * vars)
        self.assertGreater(var0, 2.5 * varl)

    def test_compare_fftfreq(self):
        """
        Test few setups
        :return:
        """
        import itertools
        sampling_rates = [10, 15, 2.1, 0.01]
        ns = [4, 9, 5, 6]
        for params in itertools.product(sampling_rates, ns):
            sampling_rate, n = params
            freq1 = np.fft.fftfreq(n, d=1./sampling_rate)
            freq2 = ndnoise.filtration.fftfreq([n], [1. / sampling_rate])
            logger.debug("freq1 " + str(freq1))
            logger.debug("freq2 " + str(freq2))

            self.assertAlmostEqual(0.0, np.sum((np.abs(freq1) - freq2)**2), msg="parameters " + str(params))

    def test_2d_fftfreq(self):
        import itertools
        spacings = [[10, 1.0], [0.12, 5.0], None, 3]
        shapes = [[10, 11], [9, 11], [8, 10], [15, 2]]
        for params in itertools.product(spacings, shapes):
            spacing, shape = params
            freq = ndnoise.filtration.fftfreq(shape, spacing=spacing)
            logger.debug("freq " + str(freq))

    def test_nd_fftfreq(self):
        import itertools
        spacings = [None, 5, 1.2]
        shapes = [[2], 3, [10, 11], [9, 11, 15], [8, 10, 5, 2], [15, 2, 3, 2, 5]]
        for params in itertools.product(spacings, shapes):
            spacing, shape = params
            freq = ndnoise.filtration.fftfreq(shape, spacing=spacing)
            logger.debug("freq " + str(freq))

    def test_R2P(self):
        x = [10 + 10 * 1j , 1 + 0 * 1j, 0 + 1j]
        expected_absolute = [10 * 2**0.5, 1.0, 1.0]
        expected_angle = [0.25 * np.pi, 0.0, 0.5 * np.pi]
        absolute, angle = ndnoise.filtration.R2P(x)

        for eab, ean, ab, an in zip(expected_absolute, expected_angle, absolute, angle):
            self.assertAlmostEquals(eab, ab)
            self.assertAlmostEquals(ean, an)

    def test_P2R(self):
        expected_x = [10 + 10 * 1j , 1 + 0 * 1j, 0 + 1j]
        absolute = [10 * 2**0.5, 1.0, 1.0]
        angle = [0.25 * np.pi, 0.0, 0.5 * np.pi]
        x = ndnoise.filtration.P2R(absolute, angle)

        for xi, exi in zip(x, expected_x):
            self.assertAlmostEquals(xi, exi)

    def test_lena_filter_ideal(self):
        import scipy.misc
        lena = scipy.misc.ascent()
        spectrum = np.fft.fftn(lena)

        # spectrum = ndnoise.generator.generate_spectrum_seed([100, 100])

        out = filtration.spectrum_filtration(
            spectrum,
            freq_start=0,
            freq_range=0.1,
            exponent=0.1
        )
        signal, filter, spectrum, freqs = out

        ndnoise.show(signal, filter, spectrum, log_view=True)
        plt.show()
        # self.assertEqual(True, False)

    def test_lena_filter_butter(self):
        import scipy.misc
        lena = scipy.misc.ascent()
        spectrum = np.fft.fftn(lena)

        # spectrum = ndnoise.generator.generate_spectrum_seed([100, 100])

        out = filtration.spectrum_filtration(
            spectrum,
            freq_start=0,
            freq_range=0.1,
            exponent=-0.1,
            filter_type="butter"
        )
        signal, filter, spectrum, freqs = out

        ndnoise.show(signal, filter, spectrum, log_view=True)

    @unittest.skip("incomplete")
    def test_2d_butter(self):
        import scipy
        import scipy.signal

        # fs = 100.0
        # lowcut = 0
        # highcut = 10
        # nyq = 0.5 * fs
        # low = lowcut / nyq
        # high = highcut / nyq
        order = 2
        low = 10
        high = 15
        # TODO why is there problem with frequeny# TODO why is there problem with frequeny??

        # worN = np.asarray([[0, 5 , 10], [1, 5, 9], [1, 4, 10]])
        worN = filtration.fftfreq([100, 150], [0.01, 0.01])
        b, a = scipy.signal.butter(order, [low, high], btype='band')
        w, h = response = scipy.signal.freqs(a, b, worN=worN)
        # plt.plot(w, abs(h))
        plt.imshow(abs(h))
        plt.show()
        # y = scipy.signal.lfilter(b, a, data)

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


<commit_before>import unittest
import os

from ufo2ft.outlineOTF import OutlineTTFCompiler
from defcon import Font

def getTestUFO():
    dirname = os.path.dirname(__file__)
    return Font(os.path.join(dirname, 'data', 'TestFont.ufo'))

class TestGlyphOrder(unittest.TestCase):

    NEW_GLYPH_ORDER = ['.notdef', 'space', 'b', 'a', 'c']

    def setUp(self):
        self.ufo = getTestUFO()
        self.ufo.lib['public.glyphOrder'] = self.NEW_GLYPH_ORDER

    def test_compile_glyph_order(self):
        compiler = OutlineTTFCompiler(self.ufo)
        compiler.compile()
        self.assertEqual(compiler.otf.getGlyphOrder(), self.NEW_GLYPH_ORDER)

if __name__ == "__main__":
    unittest.main()
<commit_msg>More robust edge case testing.<commit_after>import unittest
import os

from ufo2ft.outlineOTF import OutlineTTFCompiler
from defcon import Font

def getTestUFO():
    dirname = os.path.dirname(__file__)
    return Font(os.path.join(dirname, 'data', 'TestFont.ufo'))

class TestGlyphOrder(unittest.TestCase):

    def setUp(self):
        self.ufo = getTestUFO()

    def test_compile_original_glyph_order(self):
        DEFAULT_ORDER = ['.notdef', 'space', 'a', 'b', 'c']
        compiler = OutlineTTFCompiler(self.ufo)
        compiler.compile()
        self.assertEqual(compiler.otf.getGlyphOrder(), DEFAULT_ORDER)

    def test_compile_tweaked_glyph_order(self):
        NEW_ORDER = ['.notdef', 'space', 'b', 'a', 'c']
        self.ufo.lib['public.glyphOrder'] = NEW_ORDER
        compiler = OutlineTTFCompiler(self.ufo)
        compiler.compile()
        self.assertEqual(compiler.otf.getGlyphOrder(), NEW_ORDER)

    def test_compile_strange_glyph_order(self):
        """Move space and .notdef to end of glyph ids
        ufo2ft always puts .notdef first.
        """
        NEW_ORDER = ['b', 'a', 'c', 'space', '.notdef']
        EXPECTED_ORDER = ['.notdef', 'b', 'a', 'c', 'space']
        self.ufo.lib['public.glyphOrder'] = NEW_ORDER
        compiler = OutlineTTFCompiler(self.ufo)
        compiler.compile()
        self.assertEqual(compiler.otf.getGlyphOrder(), EXPECTED_ORDER)

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


<commit_before>"""Tests by parsing the file modules.py exported by inspectdb."""
from unittest import TestCase
import os
import re

def relative_path(path):
	"""
	Return the given path relative to this file.
	"""
	return os.path.join(os.path.dirname(__file__), path)

def get_classes_texts():
	"""
	Get classes texts as a dict.
	"""
	result = {}
	excluded_pattern = re.compile(r'^('
			r'# This is an auto-generated Django model|'
			r'from salesforce import models$'
		')')
	with open(relative_path('models.py'), 'rU') as f:
		for text in f.read().split('\n\n'):
			text = text.strip()
			if text and not excluded_pattern.match(text):
				class_name = re.match(r'class (\w+)\(', text).groups()[0]
				result[class_name] = text
	return result

class ExportedModelTest(TestCase):
	def test_nice_fields_names(self):
		"""Test the typical nice name 'last_modified_date'"""
		for text in classes_texts.values():
			if re.search(r' last_modified_date = ', text):
				(matched_line,) = [line for line in text.split('\n')
						if re.match(r'    last_modified_date = ', line)]
				self.assertNotIn('db_column', matched_line)
			else:
				self.assertNotIn('lastmodifieddate', text)
				self.assertNotIn('LastModifiedDate', text)

	def test_custom_test_class(self):
		"""Test the typical nice name 'last_modified_date'"""
		self.assertTrue('AccountContactRole' in classes_texts.keys())
		for name, text in classes_texts.items():
			if re.search(r"        db_table = 'Test__c'", text):
				# test the class name
				self.assertEqual(name, 'Test')
				# test the field name without db_column
				(matched_line,) = [line for line in text.split('\n')
						if re.match(r'    test_field = ', line)]
				self.assertNotIn('db_column', matched_line)
				break
		else:
			self.skipTest("The model for the table Test__c not exported.")

classes_texts = get_classes_texts()
<commit_msg>Preserved order in get_classes_texts to improve reusability.<commit_after>"""Tests by parsing the file modules.py exported by inspectdb."""
from collections import OrderedDict
from unittest import TestCase
import os
import re

def relative_path(path):
	"""
	Return the given path relative to this file.
	"""
	return os.path.join(os.path.dirname(__file__), path)

def get_classes_texts():
	"""
	Get classes texts as a dict.
	"""
	result = OrderedDict()
	excluded_pattern = re.compile(r'^('
			r'# This is an auto-generated Django model|'
			r'from salesforce import models$'
		')')
	with open(relative_path('models.py'), 'rU') as f:
		for text in f.read().split('\n\n'):
			text = text.strip()
			if text and not excluded_pattern.match(text):
				class_name = re.match(r'class (\w+)\(', text).groups()[0]
				result[class_name] = text
	return result

class ExportedModelTest(TestCase):
	def test_nice_fields_names(self):
		"""Test the typical nice field name 'last_modified_date'."""
		for text in classes_texts.values():
			if re.search(r' last_modified_date = ', text):
				(matched_line,) = [line for line in text.split('\n')
						if re.match(r'    last_modified_date = ', line)]
				self.assertNotIn('db_column', matched_line)
			else:
				self.assertNotIn('lastmodifieddate', text)
				self.assertNotIn('LastModifiedDate', text)

	def test_custom_test_class(self):
		"""Test the typical nice class name 'Test'."""
		self.assertTrue('AccountContactRole' in classes_texts.keys())
		for name, text in classes_texts.items():
			if re.search(r"        db_table = 'Test__c'", text):
				# test the class name
				self.assertEqual(name, 'Test')
				# test the field name without db_column
				(matched_line,) = [line for line in text.split('\n')
						if re.match(r'    test_field = ', line)]
				self.assertNotIn('db_column', matched_line)
				break
		else:
			self.skipTest("The model for the table Test__c not exported.")

classes_texts = get_classes_texts()
<|endoftext|>


<commit_before>import asyncio
import unittest
from unittest import mock

from aioredis.commands import MultiExec
from aioredis.commands import Redis

class MultiExecTest(unittest.TestCase):

    def test_global_loop(self):
        conn = mock.Mock()
        # Needs to return yourself in get_atomic_connection()
        conn.get_atomic_connection.side_effect = \
            asyncio.coroutine(lambda: conn)
        try:
            old_loop = asyncio.get_event_loop()
        except AssertionError:
            old_loop = None
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)

        tr = MultiExec(conn, commands_factory=Redis)
        self.assertIs(tr._loop, loop)

        def make_fut(cmd, *args, **kw):
            fut = asyncio.Future()
            if cmd == 'PING':
                fut.set_result(b'QUEUED')
            elif cmd == 'EXEC':
                fut.set_result([b'PONG'])
            else:
                fut.set_result(b'OK')
            return fut

        conn.execute.side_effect = make_fut
        conn.closed = False
        conn._transaction_error = None

        @asyncio.coroutine
        def go():
            tr.ping()
            res = yield from tr.execute()
            self.assertEqual(res, [b'PONG'])
        loop.run_until_complete(go())
        asyncio.set_event_loop(old_loop)
<commit_msg>test fixed<commit_after>import asyncio
import unittest
from unittest import mock

from aioredis.commands import MultiExec
from aioredis.commands import Redis

class MultiExecTest(unittest.TestCase):

    def test_global_loop(self):
        conn = mock.Mock()
        # Needs to return yourself in get_atomic_connection()
        conn.get_atomic_connection.side_effect = \
            asyncio.coroutine(lambda: conn)
        try:
            old_loop = asyncio.get_event_loop()
        except (AssertionError, RuntimeError):
            old_loop = None
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)

        tr = MultiExec(conn, commands_factory=Redis)
        self.assertIs(tr._loop, loop)

        def make_fut(cmd, *args, **kw):
            fut = asyncio.Future()
            if cmd == 'PING':
                fut.set_result(b'QUEUED')
            elif cmd == 'EXEC':
                fut.set_result([b'PONG'])
            else:
                fut.set_result(b'OK')
            return fut

        conn.execute.side_effect = make_fut
        conn.closed = False
        conn._transaction_error = None

        @asyncio.coroutine
        def go():
            tr.ping()
            res = yield from tr.execute()
            self.assertEqual(res, [b'PONG'])
        loop.run_until_complete(go())
        asyncio.set_event_loop(old_loop)
<|endoftext|>


<commit_before>"""
Tests for net.models module
"""
import mock

import pytest
import numpy as np

import net.models
import net.layers

class TestModel:

    def test_constructor_check_layers_builds_called(self):

        layers = [mock.Mock() for _ in range(3)]

        net.models.Model(layers)

        assert layers[0].build.called
        assert layers[1].build.called
        assert layers[2].build.called

    def test_constructor_check_simple_model(self):

        input = net.layers.Input(sample_shape=(3, 1))
        flatten = net.layers.Flatten()

        net.models.Model([input, flatten])

        assert (None, 3, 1) == input.input_shape
        assert (None, 3, 1) == input.output_shape

        assert (None, 3, 1) == flatten.input_shape
        assert (None, 3) == flatten.output_shape

    def test_predict_simple(self):

        layers = [mock.Mock() for _ in range(3)]

        model = net.models.Model(layers)
        model.predict(x=1)

        assert layers[0].forward.called
        assert layers[1].forward.called
        assert layers[2].forward.called

    def test_predict_input_flatten_network(self):

        input = net.layers.Input(sample_shape=(3, 1))
        flatten = net.layers.Flatten()

        model = net.models.Model([input, flatten])

        x = np.arange(6).reshape((2, 3, 1))

        expected = x.reshape(2, 3)
        actual = model.predict(x)

        assert np.all(expected == actual)

    def test_predict_input_flatten_softmax_network(self):

        input = net.layers.Input(sample_shape=[2])
        flatten = net.layers.Flatten()
        softmax = net.layers.Softmax()

        model = net.models.Model([input, flatten, softmax])

        x = np.array(
            [
                [1, 2],
                [1, 4]
            ])

        expected = np.array(
            [
                [np.exp(1) / (np.exp(1) + np.exp(2)), np.exp(2) / (np.exp(1) + np.exp(2))],
                [np.exp(1) / (np.exp(1) + np.exp(4)), np.exp(4) / (np.exp(1) + np.exp(4))]
            ]
        )

        actual = model.predict(x)

        assert np.all(expected == actual)

    def test_predict_input_convolution_network(self):

        input = net.layers.Input(sample_shape=[3, 3, 1])
        convolution = net.layers.Convolution2D(filters=1, rows=2, columns=2)

        model = net.models.Model([input, convolution])

        x = np.array([
            [1, 0, 2],
            [2, 0, 1],
            [-1, 0, -1]
        ]).reshape((1, 3, 3, 1))

        kernel = np.array([
            [2, 3],
            [1, 2]
        ]).reshape(1, 2, 2, 1)

        # Overwrite kernels with known values
        convolution.kernels = kernel

        # Overwrite biases with known values
        convolution.biases = np.array([2])

        expected = np.array([
            [6, 10],
            [5, 3]
        ]).reshape((1, 2, 2, 1))

        actual = model.predict(x)

        assert expected.shape == actual.shape
        assert np.all(expected == actual)

    def test_predict_input_convolution_flatten_softmax_network(self):

        input = net.layers.Input(sample_shape=[3, 3, 2])
        convolution = net.layers.Convolution2D(filters=2, rows=3, columns=3)
        flatten = net.layers.Flatten()
        softmax = net.layers.Softmax()

        model = net.models.Model([input, convolution, flatten, softmax])

        first_channel = np.array([
            [1, 0, 2],
            [2, 0, 1],
            [-1, 0, -1]
        ])

        second_channel = np.array([
            [2, 1, 0],
            [1, 0, 2],
            [-4, 0, 1]
        ])

        x = np.dstack([first_channel, second_channel]).reshape((1, 3, 3, 2))

        first_kernel_first_channel = np.array([
            [2, 3, 0],
            [1, 2, 1],
            [0, -1, -1]
        ])

        first_kernel_second_channel = np.array([
            [1, 0, 2],
            [-1, 2, 0],
            [0, 1, 2]
        ])

        first_kernel = np.dstack([first_kernel_first_channel, first_kernel_second_channel])

        second_kernel_first_channel = np.array([
            [-1, -1, 0],
            [2, 0, 0],
            [1, 0, 4]
        ])

        second_kernel_second_channel = np.array([
            [2, 0, 0],
            [-1, -2, 0],
            [2, 3, 0]
        ])

        second_kernel = np.dstack([second_kernel_first_channel, second_kernel_second_channel])

        # Overwrite kernels with known values
        convolution.kernels = np.array([first_kernel, second_kernel])

        # Overwrite biases with known values
        convolution.biases = np.array([-2, 10])

        expected = np.array([0.982, 0.018]).reshape((1, 2))

        actual = model.predict(x)

        assert expected.shape == actual.shape
        assert np.allclose(expected, actual, atol=0.01)

    def test_get_loss_perfect_match(self):

        labels = np.array([
            [0, 1],
            [0, 1],
        ])

        predictions = np.array([
            [0, 1],
            [0, 1]
        ])

        epsilon = 1e-7
        expected = np.mean([epsilon, epsilon])

        actual = net.models.Model([]).get_loss(labels, predictions)

        assert np.allclose(expected, actual, atol=1e-4)

    def test_get_loss_perfect_mismatch(self):

        labels = np.array([
            [0, 1],
            [0, 1],
            [1, 0]
        ])

        predictions = np.array([
            [1, 0],
            [1, 0],
            [0, 1]
        ])

        epsilon = 1e-7
        expected = np.mean([-np.log(epsilon), -np.log(epsilon), -np.log(epsilon)])

        actual = net.models.Model([]).get_loss(labels, predictions)

        assert np.allclose(expected, actual, atol=1e-4)

    def test_get_loss_simple_values(self):

        labels = np.array([
            [0, 1],
            [0, 1],
            [1, 0]
        ])

        predictions = np.array([
            [0.2, 0.9],
            [0.8, 0.2],
            [0.3, 0.7]
        ])

        expected = np.mean([-np.log(0.9), -np.log(0.2), -np.log(0.3)])
        actual = net.models.Model([]).get_loss(labels, predictions)

        assert np.allclose(expected, actual, atol=1e-4)

<commit_msg>Added more model tests<commit_after>"""
Tests for net.models module
"""
import mock

import pytest
import numpy as np

import net.models
import net.layers

class TestModel:

    def test_constructor_check_layers_builds_called(self):

        layers = [mock.Mock() for _ in range(3)]

        net.models.Model(layers)

        assert layers[0].build.called
        assert layers[1].build.called
        assert layers[2].build.called

    def test_constructor_check_simple_model(self):

        input = net.layers.Input(sample_shape=(3, 1))
        flatten = net.layers.Flatten()

        net.models.Model([input, flatten])

        assert (None, 3, 1) == input.input_shape
        assert (None, 3, 1) == input.output_shape

        assert (None, 3, 1) == flatten.input_shape
        assert (None, 3) == flatten.output_shape

    def test_predict_simple(self):

        layers = [mock.Mock() for _ in range(3)]

        model = net.models.Model(layers)
        model.predict(x=1)

        assert layers[0].forward.called
        assert layers[1].forward.called
        assert layers[2].forward.called

    def test_predict_input_flatten_network(self):

        input = net.layers.Input(sample_shape=(3, 1))
        flatten = net.layers.Flatten()

        model = net.models.Model([input, flatten])

        x = np.arange(6).reshape((2, 3, 1))

        expected = x.reshape(2, 3)
        actual = model.predict(x)

        assert np.all(expected == actual)

    def test_predict_input_flatten_softmax_network(self):

        input = net.layers.Input(sample_shape=[2])
        flatten = net.layers.Flatten()
        softmax = net.layers.Softmax()

        model = net.models.Model([input, flatten, softmax])

        x = np.array(
            [
                [1, 2],
                [1, 4]
            ])

        expected = np.array(
            [
                [np.exp(1) / (np.exp(1) + np.exp(2)), np.exp(2) / (np.exp(1) + np.exp(2))],
                [np.exp(1) / (np.exp(1) + np.exp(4)), np.exp(4) / (np.exp(1) + np.exp(4))]
            ]
        )

        actual = model.predict(x)

        assert np.all(expected == actual)

    def test_predict_input_convolution_network(self):

        input = net.layers.Input(sample_shape=[3, 3, 1])
        convolution = net.layers.Convolution2D(filters=1, rows=2, columns=2)

        model = net.models.Model([input, convolution])

        x = np.array([
            [1, 0, 2],
            [2, 0, 1],
            [-1, 0, -1]
        ]).reshape((1, 3, 3, 1))

        kernel = np.array([
            [2, 3],
            [1, 2]
        ]).reshape(1, 2, 2, 1)

        # Overwrite kernels with known values
        convolution.kernels = kernel

        # Overwrite biases with known values
        convolution.biases = np.array([2])

        expected = np.array([
            [6, 10],
            [5, 3]
        ]).reshape((1, 2, 2, 1))

        actual = model.predict(x)

        assert expected.shape == actual.shape
        assert np.all(expected == actual)

    def test_predict_input_convolution_flatten_softmax_network(self):

        input = net.layers.Input(sample_shape=[3, 3, 2])
        convolution = net.layers.Convolution2D(filters=2, rows=3, columns=3)
        flatten = net.layers.Flatten()
        softmax = net.layers.Softmax()

        model = net.models.Model([input, convolution, flatten, softmax])

        first_channel = np.array([
            [1, 0, 2],
            [2, 0, 1],
            [-1, 0, -1]
        ])

        second_channel = np.array([
            [2, 1, 0],
            [1, 0, 2],
            [-4, 0, 1]
        ])

        x = np.dstack([first_channel, second_channel]).reshape((1, 3, 3, 2))

        first_kernel_first_channel = np.array([
            [2, 3, 0],
            [1, 2, 1],
            [0, -1, -1]
        ])

        first_kernel_second_channel = np.array([
            [1, 0, 2],
            [-1, 2, 0],
            [0, 1, 2]
        ])

        first_kernel = np.dstack([first_kernel_first_channel, first_kernel_second_channel])

        second_kernel_first_channel = np.array([
            [-1, -1, 0],
            [2, 0, 0],
            [1, 0, 4]
        ])

        second_kernel_second_channel = np.array([
            [2, 0, 0],
            [-1, -2, 0],
            [2, 3, 0]
        ])

        second_kernel = np.dstack([second_kernel_first_channel, second_kernel_second_channel])

        # Overwrite kernels with known values
        convolution.kernels = np.array([first_kernel, second_kernel])

        # Overwrite biases with known values
        convolution.biases = np.array([-2, 10])

        expected = np.array([0.982, 0.018]).reshape((1, 2))

        actual = model.predict(x)

        assert expected.shape == actual.shape
        assert np.allclose(expected, actual, atol=0.01)

    def test_get_loss_perfect_match(self):

        labels = np.array([
            [0, 1],
            [0, 1],
        ])

        predictions = np.array([
            [0, 1],
            [0, 1]
        ])

        epsilon = 1e-7
        expected = np.mean([epsilon, epsilon])

        actual = net.models.Model([]).get_loss(labels, predictions)

        assert np.allclose(expected, actual, atol=1e-4)

    def test_get_loss_perfect_mismatch(self):

        labels = np.array([
            [0, 1],
            [0, 1],
            [1, 0]
        ])

        predictions = np.array([
            [1, 0],
            [1, 0],
            [0, 1]
        ])

        epsilon = 1e-7
        expected = np.mean([-np.log(epsilon), -np.log(epsilon), -np.log(epsilon)])

        actual = net.models.Model([]).get_loss(labels, predictions)

        assert np.allclose(expected, actual, atol=1e-4)

    def test_get_loss_simple_values(self):

        labels = np.array([
            [0, 1],
            [0, 1],
            [1, 0]
        ])

        predictions = np.array([
            [0.2, 0.9],
            [0.8, 0.2],
            [0.3, 0.7]
        ])

        expected = np.mean([-np.log(0.9), -np.log(0.2), -np.log(0.3)])
        actual = net.models.Model([]).get_loss(labels, predictions)

        assert np.allclose(expected, actual, atol=1e-4)

    def test_get_accuracy(self):

        mock_layer = mock.Mock()
        mock_layer.forward.return_value = np.array([
            [1, 0, 0],
            [0, 1, 0],
            [0, 0, 1],
            [0, 0, 1],
        ])

        model = net.models.Model([mock_layer])

        x = np.array([1])

        y = np.array([
            [1, 0, 0],
            [1, 0, 0],
            [1, 0, 0],
            [1, 0, 0],
        ])

        assert 0.25 == model.get_accuracy(x, y)

    def test_train_model(self):

        first_layer = net.layers.Convolution2D(filters=1, rows=2, columns=2)
        second_layer = net.layers.Convolution2D(filters=2, rows=1, columns=1)
        softmax = net.layers.Softmax()

        layers = [
            net.layers.Input(sample_shape=(2, 2, 2)),
            first_layer,
            second_layer,
            net.layers.Flatten(),
            softmax
        ]

        model = net.models.Model(layers)

        # Substitute kernels and biases with known values
        first_kernel_first_channel = np.array([
            [1, 2],
            [0, 2]
        ])

        first_kernel_second_channel = np.array([
            [1, 2],
            [-3, 0]
        ])

        first_layer.kernels = np.dstack(
            [first_kernel_first_channel, first_kernel_second_channel]).reshape(1, 2, 2, 2).astype(np.float32)

        first_layer.biases = np.array([-4], dtype=np.float32)

        second_layer.kernels = np.array([2, 1]).reshape(2, 1, 1, 1).astype(np.float32)

        second_layer.biases = np.array([-1, -2], dtype=np.float32)

        image_first_layer = np.array([
            [2, 2],
            [-1, 2]
        ])

        image_second_layer = np.array([
            [-1, 3],
            [2, 1]
        ])

        x = np.dstack([image_first_layer, image_second_layer]).reshape(1, 2, 2, 2)
        y = np.array([0, 1], dtype=np.float32).reshape(1, 2)

        model.train(x, y, learning_rate=0.5)

        # Assert convolution layers outputs
        assert np.all(np.array(5).reshape(1, 1, 1, 1) == first_layer.last_output)
        assert np.all(np.array([9, 3]).reshape(1, 1, 1, 2) == second_layer.last_output)

        # Assert softmax computations
        assert np.allclose([0.9975, 0.0025], softmax.last_output, atol=0.01)
        assert np.allclose([0.9975, -0.9975], softmax.get_output_layer_error_gradients(y), atol=0.01)

        # Assert second layer updates
        assert np.allclose(np.array([-0.49375, 3.49375]).reshape(2, 1, 1, 1), second_layer.kernels, atol=0.01)
        assert np.allclose(np.array([-1.49875, -1.50125]), second_layer.biases, atol=0.01)

        # Assert first layer updates
        expected_first_kernel_first_channel = np.array([
            [0.0025, 1.0025],
            [0.49875, 1.0025]
        ])

        expected_first_kernel_second_channel = np.array([
            [1.49875, 0.50375],
            [-3.9975, -0.49875]
        ])

        expected_first_kernel = np.dstack(
            [expected_first_kernel_first_channel, expected_first_kernel_second_channel]).reshape(1, 2, 2, 2)

        assert np.allclose(expected_first_kernel, first_layer.kernels, atol=0.01)
        assert np.allclose(np.array([-4.49925]), first_layer.biases, atol=0.01)
<|endoftext|>


<commit_before>import pytest
import json
import uuid
import responses
import tests.common as common

# integration tests requires nomad Vagrant VM or Binary running
def test_register_job(nomad_setup):

    with open("example.json") as fh:
        job = json.loads(fh.read())
        nomad_setup.job.register_job("example", job)
        assert "example" in nomad_setup.job

def test_get_allocation(nomad_setup):
    id = nomad_setup.job.get_allocations("example")[0]["ID"]
    assert isinstance(nomad_setup.allocation.get_allocation(id), dict) == True

def test_stop_allocation(nomad_setup):
    id = nomad_setup.job.get_allocations("example")[0]["ID"]
    assert isinstance(nomad_setup.allocation.stop_allocation(id), dict) == True

def test_dunder_getitem_exist(nomad_setup):
    id = nomad_setup.job.get_allocations("example")[0]["ID"]
    a = nomad_setup.allocation[id]
    assert isinstance(a, dict)

def test_dunder_getitem_not_exist(nomad_setup):

    with pytest.raises(KeyError):  # restucture try/except/raises
        j = nomad_setup.allocation[str(uuid.uuid4())]

def test_dunder_contain_exists(nomad_setup):
    id = nomad_setup.job.get_allocations("example")[0]["ID"]
    assert id in nomad_setup.allocation

def test_dunder_contain_not_exist(nomad_setup):

    assert str(uuid.uuid4()) not in nomad_setup.allocation

def test_dunder_str(nomad_setup):
    assert isinstance(str(nomad_setup.allocation), str)

def test_dunder_repr(nomad_setup):
    assert isinstance(repr(nomad_setup.allocation), str)

def test_dunder_getattr(nomad_setup):

    with pytest.raises(AttributeError):
        d = nomad_setup.allocation.does_not_exist

@responses.activate
#
# fix No data when you are using namespaces #82
#
def test_get_allocation_with_namespace(nomad_setup_with_namespace):
    responses.add(
        responses.GET,
        "http://{ip}:{port}/v1/allocation/a8198d79-cfdb-6593-a999-1e9adabcba2e?namespace={namespace}".format(ip=common.IP, port=common.NOMAD_PORT, namespace=common.NOMAD_NAMESPACE),
        status=200,
        json={"ID": "a8198d79-cfdb-6593-a999-1e9adabcba2e","EvalID": "5456bd7a-9fc0-c0dd-6131-cbee77f57577","Namespace": common.NOMAD_NAMESPACE, "Name": "example.cache[0]","NodeID": "fb2170a8-257d-3c64-b14d-bc06cc94e34c","PreviousAllocation": "516d2753-0513-cfc7-57ac-2d6fac18b9dc","NextAllocation": "cd13d9b9-4f97-7184-c88b-7b451981616b"}
    )
    assert common.NOMAD_NAMESPACE in nomad_setup_with_namespace.allocation.get_allocation("a8198d79-cfdb-6593-a999-1e9adabcba2e")["Namespace"]
<commit_msg>Stop allocation support is present on Nomad >= 0.9.2<commit_after>import pytest
import json
import uuid
import responses
import tests.common as common
import os

# integration tests requires nomad Vagrant VM or Binary running
def test_register_job(nomad_setup):

    with open("example.json") as fh:
        job = json.loads(fh.read())
        nomad_setup.job.register_job("example", job)
        assert "example" in nomad_setup.job

def test_get_allocation(nomad_setup):
    id = nomad_setup.job.get_allocations("example")[0]["ID"]
    assert isinstance(nomad_setup.allocation.get_allocation(id), dict) == True

@pytest.mark.skipif(tuple(int(i) for i in os.environ.get("NOMAD_VERSION").split(".")) < (0, 9, 2), reason="Nomad alloc stop not supported")
def test_stop_allocation(nomad_setup):
    id = nomad_setup.job.get_allocations("example")[0]["ID"]
    assert isinstance(nomad_setup.allocation.stop_allocation(id), dict) == True

def test_dunder_getitem_exist(nomad_setup):
    id = nomad_setup.job.get_allocations("example")[0]["ID"]
    a = nomad_setup.allocation[id]
    assert isinstance(a, dict)

def test_dunder_getitem_not_exist(nomad_setup):

    with pytest.raises(KeyError):  # restucture try/except/raises
        j = nomad_setup.allocation[str(uuid.uuid4())]

def test_dunder_contain_exists(nomad_setup):
    id = nomad_setup.job.get_allocations("example")[0]["ID"]
    assert id in nomad_setup.allocation

def test_dunder_contain_not_exist(nomad_setup):

    assert str(uuid.uuid4()) not in nomad_setup.allocation

def test_dunder_str(nomad_setup):
    assert isinstance(str(nomad_setup.allocation), str)

def test_dunder_repr(nomad_setup):
    assert isinstance(repr(nomad_setup.allocation), str)

def test_dunder_getattr(nomad_setup):

    with pytest.raises(AttributeError):
        d = nomad_setup.allocation.does_not_exist

@responses.activate
#
# fix No data when you are using namespaces #82
#
def test_get_allocation_with_namespace(nomad_setup_with_namespace):
    responses.add(
        responses.GET,
        "http://{ip}:{port}/v1/allocation/a8198d79-cfdb-6593-a999-1e9adabcba2e?namespace={namespace}".format(ip=common.IP, port=common.NOMAD_PORT, namespace=common.NOMAD_NAMESPACE),
        status=200,
        json={"ID": "a8198d79-cfdb-6593-a999-1e9adabcba2e","EvalID": "5456bd7a-9fc0-c0dd-6131-cbee77f57577","Namespace": common.NOMAD_NAMESPACE, "Name": "example.cache[0]","NodeID": "fb2170a8-257d-3c64-b14d-bc06cc94e34c","PreviousAllocation": "516d2753-0513-cfc7-57ac-2d6fac18b9dc","NextAllocation": "cd13d9b9-4f97-7184-c88b-7b451981616b"}
    )
    assert common.NOMAD_NAMESPACE in nomad_setup_with_namespace.allocation.get_allocation("a8198d79-cfdb-6593-a999-1e9adabcba2e")["Namespace"]
<|endoftext|>


<commit_before># Be able to call directly such as `python test_annotators.py`
try:
    from .context import loctext
except SystemError: # Parent module '' not loaded, cannot perform relative import
    pass

from loctext.util import PRO_ID, LOC_ID, REL_PRO_LOC_ID
from nalaf.learning.evaluators import DocumentLevelRelationEvaluator, Evaluations
from nalaf.learning.taggers import StubSameSentenceRelationExtractor, StubRelationExtractor
from loctext.learning.train import read_corpus, evaluate_with_argv
from nalaf import print_verbose, print_debug
from nalaf.preprocessing.edges import SentenceDistanceEdgeGenerator
import math
import sys
from nalaf.preprocessing.spliters import NLTKSplitter
from nalaf.preprocessing.tokenizers import NLTK_TOKENIZER

# See conftest.py too
TEST_MIN_CORPUS_PERCENTAGE = 0.4

def test_SS_baseline():
    corpus = read_corpus("LocText")

    # Computation(precision=0.6083150984682714, precision_SE=0.002974704942625582, recall=0.6233183856502242, recall_SE=0.004130201948613626, f_measure=0.6157253599114065, f_measure_SE=0.0030062001054202924)
    EXPECTED_F = 0.6157  # 62
    EXPECTED_F_SE = 0.0030

    annotator_gen_fun = (lambda _: StubSameSentenceRelationExtractor(PRO_ID, LOC_ID, REL_PRO_LOC_ID).annotate)
    evaluator = DocumentLevelRelationEvaluator(rel_type=REL_PRO_LOC_ID)

    evaluations = Evaluations.cross_validate(annotator_gen_fun, corpus, evaluator, k_num_folds=5, use_validation_set=True)

    rel_evaluation = evaluations(REL_PRO_LOC_ID).compute(strictness="exact")

    assert math.isclose(rel_evaluation.f_measure, EXPECTED_F, abs_tol=EXPECTED_F_SE * 1.1), rel_evaluation.f_measure
    print("SS Baseline", rel_evaluation)

    return rel_evaluation

def _test_LocText(corpus_percentage, model, EXPECTED_F=None, EXPECTED_F_SE=0.001):
    # Note: EXPECTED_F=None will make the test fail for non-yet verified evaluations
    # Note: the real StdErr's are around ~0.0027-0.0095. Decrease them by default to be more strict with tests

    assert corpus_percentage in [TEST_MIN_CORPUS_PERCENTAGE, 1.0], "corpus_percentage must == {} or 1.0. You gave: {}".format(str(TEST_MIN_CORPUS_PERCENTAGE), str(corpus_percentage))

    corpus = read_corpus("LocText", corpus_percentage)

    rel_evaluation = evaluate_with_argv(['--corpus_percentage', str(corpus_percentage), '--model', model])

    print("LocText " + model, rel_evaluation)
    assert math.isclose(rel_evaluation.f_measure, EXPECTED_F, abs_tol=EXPECTED_F_SE * 1.1)

    return rel_evaluation

def test_LocText_SS(corpus_percentage):

    if (corpus_percentage == 1.0):
        # Computation(precision=0.6624365482233503, precision_SE=0.0029261497595035445, recall=0.5787139689578714, recall_SE=0.004036629092741261, f_measure=0.6177514792899409, f_measure_SE=0.0027412422752843557)
        EXPECTED_F = 0.6178
        EXPECTED_F_SE = 0.0027
    else:
        # Computation(precision=0.753731343283582, precision_SE=0.004064280002070562, recall=0.6158536585365854, recall_SE=0.006147889829984894, f_measure=0.6778523489932885, f_measure_SE=0.004461978434634661)
        EXPECTED_F = 0.6779
        EXPECTED_F_SE = 0.0045

    _test_LocText(corpus_percentage, model='SS', EXPECTED_F=EXPECTED_F)

def test_DS_baseline():
    corpus = read_corpus("LocText")

    # Computation(precision=0.4196969696969697, precision_SE=0.0024518002206926812, recall=0.6210762331838565, recall_SE=0.0038995731275083797, f_measure=0.5009041591320074, f_measure_SE=0.002367560805132471)
    EXPECTED_F = 0.5009
    EXPECTED_F_SE = 0.0024

    edge_generator = SentenceDistanceEdgeGenerator(PRO_ID, LOC_ID, REL_PRO_LOC_ID, distance=1)

    annotator_gen_fun = (lambda _: StubRelationExtractor(edge_generator).annotate)
    evaluator = DocumentLevelRelationEvaluator(rel_type=REL_PRO_LOC_ID)

    evaluations = Evaluations.cross_validate(annotator_gen_fun, corpus, evaluator, k_num_folds=5, use_validation_set=True)
    rel_evaluation = evaluations(REL_PRO_LOC_ID).compute(strictness="exact")

    assert math.isclose(rel_evaluation.f_measure, EXPECTED_F, abs_tol=EXPECTED_F_SE * 1.1), rel_evaluation.f_measure
    print("DS Baseline", rel_evaluation)

    return rel_evaluation

def test_LocText_DS(corpus_percentage):

    if (corpus_percentage == 1.0):
        # Computation(precision=0.41743119266055045, precision_SE=0.0030117270329129047, recall=0.40176600441501104, recall_SE=0.0032167797183620065, f_measure=0.4094488188976378, f_measure_SE=0.0025398561212811644)
        EXPECTED_F = 0.4094
        EXPECTED_F_SE = 0.0024
    else:
        # Computation(precision=0.5217391304347826, precision_SE=0.004661534209213446, recall=0.36585365853658536, recall_SE=0.00518652082659298, f_measure=0.43010752688172044, f_measure_SE=0.004278857080197886)
        EXPECTED_F = 0.4301
        EXPECTED_F_SE = 0.0043

    _test_LocText(corpus_percentage, model='DS', EXPECTED_F=EXPECTED_F)

# Note: would be way better to be able to reuse the already trained models in the other tests methods
def test_LocText_Combined(corpus_percentage):

    if (corpus_percentage == 1.0):
        # Computation(precision=0.4774381368267831, precision_SE=0.002812150029056132, recall=0.7177242888402626, recall_SE=0.0038763518483362733, f_measure=0.5734265734265734, f_measure_SE=0.002422393130974523)
        EXPECTED_F = 0.5734
        EXPECTED_F_SE = 0.0024
    else:
        # Computation(precision=0.5887850467289719, precision_SE=0.0033947585471817603, recall=0.7682926829268293, recall_SE=0.004128234454709121, f_measure=0.6666666666666666, f_measure_SE=0.0027263757211551205)
        EXPECTED_F = 0.6667
        EXPECTED_F_SE = 0.0027

    _test_LocText(corpus_percentage, model='Combined', EXPECTED_F=EXPECTED_F)

if __name__ == "__main__":

    test_baseline()

    corpus_percentage = float(sys.argv[2]) if len(sys.argv) == 3 else TEST_MIN_CORPUS_PERCENTAGE
    test_LocText_SS(corpus_percentage)
<commit_msg>minor<commit_after># Be able to call directly such as `python test_annotators.py`
try:
    from .context import loctext
except SystemError:  # Parent module '' not loaded, cannot perform relative import
    pass

from loctext.util import PRO_ID, LOC_ID, REL_PRO_LOC_ID
from nalaf.learning.evaluators import DocumentLevelRelationEvaluator, Evaluations
from nalaf.learning.taggers import StubSameSentenceRelationExtractor, StubRelationExtractor
from loctext.learning.train import read_corpus, evaluate_with_argv
from nalaf import print_verbose, print_debug
from nalaf.preprocessing.edges import SentenceDistanceEdgeGenerator
import math
import sys
from nalaf.preprocessing.spliters import NLTKSplitter
from nalaf.preprocessing.tokenizers import NLTK_TOKENIZER

# See conftest.py too
TEST_MIN_CORPUS_PERCENTAGE = 0.4

def test_SS_baseline():
    corpus = read_corpus("LocText")

    # Computation(precision=0.6083150984682714, precision_SE=0.002974704942625582, recall=0.6233183856502242, recall_SE=0.004130201948613626, f_measure=0.6157253599114065, f_measure_SE=0.0030062001054202924)
    EXPECTED_F = 0.6157  # 62
    EXPECTED_F_SE = 0.0030

    annotator_gen_fun = (lambda _: StubSameSentenceRelationExtractor(PRO_ID, LOC_ID, REL_PRO_LOC_ID).annotate)
    evaluator = DocumentLevelRelationEvaluator(rel_type=REL_PRO_LOC_ID)

    evaluations = Evaluations.cross_validate(annotator_gen_fun, corpus, evaluator, k_num_folds=5, use_validation_set=True)

    rel_evaluation = evaluations(REL_PRO_LOC_ID).compute(strictness="exact")

    assert math.isclose(rel_evaluation.f_measure, EXPECTED_F, abs_tol=EXPECTED_F_SE * 1.1), rel_evaluation.f_measure
    print("SS Baseline", rel_evaluation)

    return rel_evaluation

def _test_LocText(corpus_percentage, model, EXPECTED_F=None, EXPECTED_F_SE=0.001):
    # Note: EXPECTED_F=None will make the test fail for non-yet verified evaluations
    # Note: the real StdErr's are around ~0.0027-0.0095. Decrease them by default to be more strict with tests

    assert corpus_percentage in [TEST_MIN_CORPUS_PERCENTAGE, 1.0], "corpus_percentage must == {} or 1.0. You gave: {}".format(str(TEST_MIN_CORPUS_PERCENTAGE), str(corpus_percentage))

    corpus = read_corpus("LocText", corpus_percentage)

    rel_evaluation = evaluate_with_argv(['--corpus_percentage', str(corpus_percentage), '--model', model])

    print("LocText " + model, rel_evaluation)
    assert math.isclose(rel_evaluation.f_measure, EXPECTED_F, abs_tol=EXPECTED_F_SE * 1.1)

    return rel_evaluation

def test_LocText_SS(corpus_percentage):

    if (corpus_percentage == 1.0):
        # TODO Computation(precision=0.6743515850144092, precision_SE=0.0029906794627101553, recall=0.5246636771300448, recall_SE=0.003956801825290298, f_measure=0.5901639344262295, f_measure_SE=0.002994553617880869)

        # Computation(precision=0.6624365482233503, precision_SE=0.0029261497595035445, recall=0.5787139689578714, recall_SE=0.004036629092741261, f_measure=0.6177514792899409, f_measure_SE=0.0027412422752843557)
        EXPECTED_F = 0.6178
        EXPECTED_F_SE = 0.0027
    else:
        # Computation(precision=0.753731343283582, precision_SE=0.004064280002070562, recall=0.6158536585365854, recall_SE=0.006147889829984894, f_measure=0.6778523489932885, f_measure_SE=0.004461978434634661)
        EXPECTED_F = 0.6779
        EXPECTED_F_SE = 0.0045

    _test_LocText(corpus_percentage, model='SS', EXPECTED_F=EXPECTED_F)

def test_DS_baseline():
    corpus = read_corpus("LocText")

    # Computation(precision=0.4196969696969697, precision_SE=0.0024518002206926812, recall=0.6210762331838565, recall_SE=0.0038995731275083797, f_measure=0.5009041591320074, f_measure_SE=0.002367560805132471)
    EXPECTED_F = 0.5009
    EXPECTED_F_SE = 0.0024

    edge_generator = SentenceDistanceEdgeGenerator(PRO_ID, LOC_ID, REL_PRO_LOC_ID, distance=1)

    annotator_gen_fun = (lambda _: StubRelationExtractor(edge_generator).annotate)
    evaluator = DocumentLevelRelationEvaluator(rel_type=REL_PRO_LOC_ID)

    evaluations = Evaluations.cross_validate(annotator_gen_fun, corpus, evaluator, k_num_folds=5, use_validation_set=True)
    rel_evaluation = evaluations(REL_PRO_LOC_ID).compute(strictness="exact")

    assert math.isclose(rel_evaluation.f_measure, EXPECTED_F, abs_tol=EXPECTED_F_SE * 1.1), rel_evaluation.f_measure
    print("DS Baseline", rel_evaluation)

    return rel_evaluation

def test_LocText_DS(corpus_percentage):

    if (corpus_percentage == 1.0):
        # Computation(precision=0.41743119266055045, precision_SE=0.0030117270329129047, recall=0.40176600441501104, recall_SE=0.0032167797183620065, f_measure=0.4094488188976378, f_measure_SE=0.0025398561212811644)
        EXPECTED_F = 0.4094
        EXPECTED_F_SE = 0.0024
    else:
        # Computation(precision=0.5217391304347826, precision_SE=0.004661534209213446, recall=0.36585365853658536, recall_SE=0.00518652082659298, f_measure=0.43010752688172044, f_measure_SE=0.004278857080197886)
        EXPECTED_F = 0.4301
        EXPECTED_F_SE = 0.0043

    _test_LocText(corpus_percentage, model='DS', EXPECTED_F=EXPECTED_F)

# Note: would be way better to be able to reuse the already trained models in the other tests methods
def test_LocText_Combined(corpus_percentage):

    if (corpus_percentage == 1.0):
        # Computation(precision=0.4774381368267831, precision_SE=0.002812150029056132, recall=0.7177242888402626, recall_SE=0.0038763518483362733, f_measure=0.5734265734265734, f_measure_SE=0.002422393130974523)
        EXPECTED_F = 0.5734
        EXPECTED_F_SE = 0.0024
    else:
        # Computation(precision=0.5887850467289719, precision_SE=0.0033947585471817603, recall=0.7682926829268293, recall_SE=0.004128234454709121, f_measure=0.6666666666666666, f_measure_SE=0.0027263757211551205)
        EXPECTED_F = 0.6667
        EXPECTED_F_SE = 0.0027

    _test_LocText(corpus_percentage, model='Combined', EXPECTED_F=EXPECTED_F)

if __name__ == "__main__":

    test_baseline()

    corpus_percentage = float(sys.argv[2]) if len(sys.argv) == 3 else TEST_MIN_CORPUS_PERCENTAGE
    test_LocText_SS(corpus_percentage)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals
from ftfy.fixes import fix_encoding, fix_encoding_and_explain, apply_plan, possible_encoding, fix_surrogates
from ftfy.badness import sequence_weirdness
import unicodedata
import sys
from nose.tools import eq_

if sys.hexversion >= 0x03000000:
    unichr = chr

# Most single-character strings which have been misencoded should be restored.
def test_bmp_characters():
    for index in range(0xa0, 0xfffd):
        char = unichr(index)
        # Exclude code points that are not assigned
        if unicodedata.category(char) not in ('Co', 'Cn', 'Cs', 'Mc', 'Mn'):
            garble = char.encode('utf-8').decode('latin-1')
            # Exclude characters whose re-encoding is protected by the
            # 'sequence_weirdness' metric
            if sequence_weirdness(garble) >= 0:
                garble2 = char.encode('utf-8').decode('latin-1').encode('utf-8').decode('latin-1')
                for garb in (garble, garble2):
                    fixed, plan = fix_encoding_and_explain(garb)
                    eq_(fixed, char)
                    eq_(apply_plan(garb, plan), char)
            else:
                print("%r <- %r" % (char, garble))

def test_possible_encoding():
    for codept in range(256):
        char = chr(codept)
        assert possible_encoding(char, 'latin-1')

def test_byte_order_mark():
    eq_(fix_encoding('ï»¿'), '\ufeff')

def test_emoji_variation_selector():
    # The hearts here are explicitly marked as emoji using the variation
    # selector U+FE0F. This is not weird.
    eq_(sequence_weirdness('❤❤❤❤❤❤❤❤❤'), 0)

def test_surrogates():
    eq_(fix_surrogates('\udbff\udfff'), '\U0010ffff')
    eq_(fix_surrogates('\ud800\udc00'), '\U00010000')

<commit_msg>remove debugging output from test<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals
from ftfy.fixes import fix_encoding, fix_encoding_and_explain, apply_plan, possible_encoding, fix_surrogates
from ftfy.badness import sequence_weirdness
import unicodedata
import sys
from nose.tools import eq_

if sys.hexversion >= 0x03000000:
    unichr = chr

# Most single-character strings which have been misencoded should be restored.
def test_bmp_characters():
    for index in range(0xa0, 0xfffd):
        char = unichr(index)
        # Exclude code points that are not assigned
        if unicodedata.category(char) not in ('Co', 'Cn', 'Cs', 'Mc', 'Mn'):
            garble = char.encode('utf-8').decode('latin-1')
            # Exclude characters whose re-encoding is protected by the
            # 'sequence_weirdness' metric
            if sequence_weirdness(garble) >= 0:
                garble2 = char.encode('utf-8').decode('latin-1').encode('utf-8').decode('latin-1')
                for garb in (garble, garble2):
                    fixed, plan = fix_encoding_and_explain(garb)
                    eq_(fixed, char)
                    eq_(apply_plan(garb, plan), char)

def test_possible_encoding():
    for codept in range(256):
        char = chr(codept)
        assert possible_encoding(char, 'latin-1')

def test_byte_order_mark():
    eq_(fix_encoding('ï»¿'), '\ufeff')

def test_emoji_variation_selector():
    # The hearts here are explicitly marked as emoji using the variation
    # selector U+FE0F. This is not weird.
    eq_(sequence_weirdness('❤❤❤❤❤❤❤❤❤'), 0)

def test_surrogates():
    eq_(fix_surrogates('\udbff\udfff'), '\U0010ffff')
    eq_(fix_surrogates('\ud800\udc00'), '\U00010000')

<|endoftext|>


<commit_before>import unittest

from . import fixture

class FindDocumentTestCase(fixture.MongoextTestCase):
    def setUp(self):
        self.documents = [{
            'title': u'Title',
            'description': u'Description',
            'content': u'Content',
            'created_ts': 1,
        }, {
            'title': u'Title2',
            'description': u'Description2',
            'content': u'Content2',
            'created_ts': 2,
        }, {
            'title': u'Title3',
            'description': u'Description3',
            'content': u'Content3',
            'created_ts': 3,
        }]

        fixture.Collection().insert(self.documents)

    def equal(self, documents1, documents2):
        documents1 = [d for d in documents1]
        for document in documents1:
            document.pop('_id')
        self.assertEqual(documents1, documents2)

    def test_find_documents(self):
        documents = fixture.Collection().find()
        self.equal(documents, self.documents)

    def test_find_documents_by_spec(self):
        spec = {
            'created_ts': 2
        }
        documents = fixture.Collection().find(spec)
        self.equal(documents, [self.documents[1]])

    def test_find_documents_by_spec_with_empty_result(self):
        spec = {
            'created_ts': -1
        }
        documents = fixture.Collection().find(spec)
        self.equal(documents, [])

    def test_find_documents_by_spec_with_more_the_one_result(self):
        spec = {
            'created_ts': {'$gt': 1},
        }
        documents = fixture.Collection().find(spec)
        self.equal(documents, self.documents[1:])

    def test_find_documents_with_specified_fields(self):
        documents = fixture.Collection().find(fields={'created_ts': 1})
        self.equal(documents, [{'created_ts': d['created_ts']} for d in self.documents])

    def test_find_documents_with_skip(self):
        documents = fixture.Collection().find(skip=1)
        self.equal(documents, self.documents[1:])

    def test_find_documents_by_spec_with_fields_and_skip(self):
        spec = {
            'created_ts': {'$gt': 1},
        }
        fields = {'created_ts': 1}
        skip = 1
        documents = fixture.Collection().find(spec, fields, skip)
        self.equal(documents, [{'created_ts': d['created_ts']} for d in self.documents[2:]])

    def test_find_one_document(self):
        document = fixture.Collection().find_one({'created_ts': 1})
        self.equal([document], [self.documents[0]])

    def test_find_one_document_for_none_results(self):
        document = fixture.Collection().find_one({'created_ts': -1})
        self.assertIsNone(document)

    def test_find_one_document_for_multiple_results(self):
        document = fixture.Collection().find_one()
        self.equal([document], [self.documents[0]])

class FindModelsTestCase(fixture.MongoextTestCase):
    def setUp(self):
        self.models = [fixture.Model(**{
            'title': u'Title',
            'description': u'Description',
            'content': u'Content',
            'created_ts': 1,
        }), fixture.Model(**{
            'title': u'Title2',
            'description': u'Description2',
            'content': u'Content2',
            'created_ts': 2,
        }), fixture.Model(**{
            'title': u'Title3',
            'description': u'Description3',
            'content': u'Content3',
            'created_ts': 3,
        })]

        document_ids = fixture.Model.objects.insert(self.models)
        for document_id, model in zip(document_ids, self.models):
            model._id = document_id

    def equal(self, documents1, documents2):
        self.assertEqual([d.to_dict() for d in documents1], [d.to_dict() for d in documents2])

    def test_find_models(self):
        models = fixture.Model.objects.find()
        self.equal(models, self.models)

    def test_find_models_by_spec(self):
        spec = {
            'created_ts': 2
        }
        models = fixture.Model.objects.find(spec)
        self.equal(models, [self.models[1]])

    def test_find_models_by_spec_with_empty_result(self):
        spec = {
            'created_ts': -1
        }
        models = fixture.Model.objects.find(spec)
        self.equal(models, [])

    def test_find_models_by_spec_with_more_the_one_result(self):
        spec = {
            'created_ts': {'$gt': 1},
        }
        models = fixture.Model.objects.find(spec)
        self.equal(models, self.models[1:])

    def test_find_documents_with_skip(self):
        models = fixture.Model.objects.find(skip=1)
        self.equal(models, self.models[1:])

    def test_find_documents_by_spec_with_fields_and_skip(self):
        spec = {
            'created_ts': {'$gt': 1},
        }
        skip = 1
        models = fixture.Model.objects.find(spec, skip=skip)
        self.equal(models, self.models[2:])

class CountTestCase(fixture.MongoextTestCase):
    def setUp(self):
        pass

    def test_single_document_in_collection(self):
        documents = [{
            'title': u'Title',
            'description': u'Description',
            'content': u'Content',
            'created_ts': 1,
        }]

        fixture.Collection().insert(documents)
        self.assertEqual(fixture.Collection().count(), 1)

    def test_multiple_document_in_collection(self):
        documents = [{
            'title': u'Title',
            'description': u'Description',
            'content': u'Content',
            'created_ts': 1,
        }, {
            'title': u'Title2',
            'description': u'Description2',
            'content': u'Content2',
            'created_ts': 2,
        }, {
            'title': u'Title3',
            'description': u'Description3',
            'content': u'Content3',
            'created_ts': 3,
        }]

        fixture.Collection().insert(documents)
        self.assertEqual(fixture.Collection().count(), 3)

class DistinctTestCase(fixture.MongoextTestCase):
    def setUp(self):
        self.documents = [{
            'title': u'Title',
        }, {
            'title': u'Title',
        }, {
            'title': u'Title1',
        }]
        fixture.Collection().insert(self.documents)

    def test_distinct(self):
        self.assertEqual(fixture.Collection().distinct('title'), [u'Title', u'Title1'])

class DropTestCase(fixture.MongoextTestCase):
    def setUp(self):
        self.documents = [{
            'title': u'Title',
        }, {
            'title': u'Title',
        }, {
            'title': u'Title1',
        }]
        fixture.Collection().insert(self.documents)

    def test_drop(self):
        self.assertIsNone(fixture.Collection().drop())

    def test_multiple_drop(self):
        fixture.Collection().drop()
        fixture.Collection().drop()
        self.assertIsNone(fixture.Collection().drop())

class RemoveTestCase(fixture.MongoextTestCase):
    def setUp(self):
        self.documents = [{
            'title': u'Title',
        }, {
            'title': u'Title',
        }, {
            'title': u'Title1',
        }]
        fixture.Collection().insert(self.documents)

    def test_complete_remove(self):
        fixture.Collection().remove()
        self.assertEqual(fixture.Collection().count(), 0)

    def test_single_removal(self):
        fixture.Collection().remove({'title': u'Title1'})
        self.assertEqual(fixture.Collection().count(), 2)

    def test_multiple_removal(self):
        fixture.Collection().remove({'title': u'Title'})
        self.assertEqual(fixture.Collection().count(), 1)

    def test_multiple_removal_with_multi_false(self):
        fixture.Collection().remove({'title': u'Title'}, multi=False)
        self.assertEqual(fixture.Collection().count(), 2)

class UpdateTestCase(fixture.MongoextTestCase):
    def setUp(self):
        self.documents = [{
            'title': u'Title',
        }, {
            'title': u'Title',
        }, {
            'title': u'Title1',
        }]
        fixture.Collection().insert(self.documents)

    def test_update_one(self):
        fixture.Collection().update({'title': u'Title'}, {'$set': {'title': u'Title2'}})
        documents = [d for d in fixture.Collection().find({'title': u'Title2'})]
        self.assertEqual(len(documents), 1)

    def test_update_multi(self):
        fixture.Collection().update({'title': u'Title'}, {'$set': {'title': u'Title2'}}, multi=True)
        documents = [d for d in fixture.Collection().find({'title': u'Title2'})]
        self.assertEqual(len(documents), 2)

class SaveDocumentTestCase(fixture.MongoextTestCase):
    def setUp(self):
        pass

    def test_save_new_document(self):
        document = {
            'title': u'Title',
        }
        fixture.Collection().save(document)
        self.assertEqual(fixture.Collection().count(), 1)

    def test_save_existed_document(self):
        document = {'title': u'Title'}
        _id = fixture.Collection().save(document)
        document['_id'] = _id
        document['title'] = u'Title1'
        fixture.Collection().save(document)
        self.assertEqual(fixture.Collection().count(), 1)

class SaveModelTestCase(fixture.MongoextTestCase):
    def setUp(self):
        pass

    def test_save_new_model(self):
        model = fixture.Model(**{
            'created_ts': 1,
        })
        fixture.Collection().save(model)
        self.assertEqual(fixture.Collection().count(), 1)

    def test_save_existed_model(self):
        model = fixture.Model(**{
            'created_ts': 1,
        })
        _id = fixture.Collection().save(model)
        model._id = _id
        model.created_ts = 2
        fixture.Collection().save(model)
        self.assertEqual(fixture.Collection().count(), 1)

class NoCompressionCollection(fixture.Collection):
    KEYS_COMPRESSION = None

class Compression(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        NoCompressionCollection().drop()

    def test_compression(self):
        NoCompressionCollection().find()
<commit_msg>silly test<commit_after>import unittest

from . import fixture

class FindDocumentTestCase(fixture.MongoextTestCase):
    def setUp(self):
        self.documents = [{
            'title': u'Title',
            'description': u'Description',
            'content': u'Content',
            'created_ts': 1,
        }, {
            'title': u'Title2',
            'description': u'Description2',
            'content': u'Content2',
            'created_ts': 2,
        }, {
            'title': u'Title3',
            'description': u'Description3',
            'content': u'Content3',
            'created_ts': 3,
        }]

        fixture.Collection().insert(self.documents)

    def equal(self, documents1, documents2):
        documents1 = [d for d in documents1]
        for document in documents1:
            document.pop('_id')
        self.assertEqual(documents1, documents2)

    def test_find_documents(self):
        documents = fixture.Collection().find()
        self.equal(documents, self.documents)

    def test_find_documents_by_spec(self):
        spec = {
            'created_ts': 2
        }
        documents = fixture.Collection().find(spec)
        self.equal(documents, [self.documents[1]])

    def test_find_documents_by_spec_with_empty_result(self):
        spec = {
            'created_ts': -1
        }
        documents = fixture.Collection().find(spec)
        self.equal(documents, [])

    def test_find_documents_by_spec_with_more_the_one_result(self):
        spec = {
            'created_ts': {'$gt': 1},
        }
        documents = fixture.Collection().find(spec)
        self.equal(documents, self.documents[1:])

    def test_find_documents_with_specified_fields(self):
        documents = fixture.Collection().find(fields={'created_ts': 1})
        self.equal(documents, [{'created_ts': d['created_ts']} for d in self.documents])

    def test_find_documents_with_skip(self):
        documents = fixture.Collection().find(skip=1)
        self.equal(documents, self.documents[1:])

    def test_find_documents_by_spec_with_fields_and_skip(self):
        spec = {
            'created_ts': {'$gt': 1},
        }
        fields = {'created_ts': 1}
        skip = 1
        documents = fixture.Collection().find(spec, fields, skip)
        self.equal(documents, [{'created_ts': d['created_ts']} for d in self.documents[2:]])

    def test_find_one_document(self):
        document = fixture.Collection().find_one({'created_ts': 1})
        self.equal([document], [self.documents[0]])

    def test_find_one_document_for_none_results(self):
        document = fixture.Collection().find_one({'created_ts': -1})
        self.assertIsNone(document)

    def test_find_one_document_for_multiple_results(self):
        document = fixture.Collection().find_one()
        self.equal([document], [self.documents[0]])

class FindModelsTestCase(fixture.MongoextTestCase):
    def setUp(self):
        self.models = [fixture.Model(**{
            'title': u'Title',
            'description': u'Description',
            'content': u'Content',
            'created_ts': 1,
        }), fixture.Model(**{
            'title': u'Title2',
            'description': u'Description2',
            'content': u'Content2',
            'created_ts': 2,
        }), fixture.Model(**{
            'title': u'Title3',
            'description': u'Description3',
            'content': u'Content3',
            'created_ts': 3,
        })]

        document_ids = fixture.Model.objects.insert(self.models)
        for document_id, model in zip(document_ids, self.models):
            model._id = document_id

    def equal(self, documents1, documents2):
        self.assertEqual([d.to_dict() for d in documents1], [d.to_dict() for d in documents2])

    def test_find_models(self):
        models = fixture.Model.objects.find()
        self.equal(models, self.models)

    def test_find_models_by_spec(self):
        spec = {
            'created_ts': 2
        }
        models = fixture.Model.objects.find(spec)
        self.equal(models, [self.models[1]])

    def test_find_models_by_spec_with_empty_result(self):
        spec = {
            'created_ts': -1
        }
        models = fixture.Model.objects.find(spec)
        self.equal(models, [])

    def test_find_models_by_spec_with_more_the_one_result(self):
        spec = {
            'created_ts': {'$gt': 1},
        }
        models = fixture.Model.objects.find(spec)
        self.equal(models, self.models[1:])

    def test_find_documents_with_skip(self):
        models = fixture.Model.objects.find(skip=1)
        self.equal(models, self.models[1:])

    def test_find_documents_by_spec_with_fields_and_skip(self):
        spec = {
            'created_ts': {'$gt': 1},
        }
        skip = 1
        models = fixture.Model.objects.find(spec, skip=skip)
        self.equal(models, self.models[2:])

class CountTestCase(fixture.MongoextTestCase):
    def setUp(self):
        pass

    def test_single_document_in_collection(self):
        documents = [{
            'title': u'Title',
            'description': u'Description',
            'content': u'Content',
            'created_ts': 1,
        }]

        fixture.Collection().insert(documents)
        self.assertEqual(fixture.Collection().count(), 1)

    def test_multiple_document_in_collection(self):
        documents = [{
            'title': u'Title',
            'description': u'Description',
            'content': u'Content',
            'created_ts': 1,
        }, {
            'title': u'Title2',
            'description': u'Description2',
            'content': u'Content2',
            'created_ts': 2,
        }, {
            'title': u'Title3',
            'description': u'Description3',
            'content': u'Content3',
            'created_ts': 3,
        }]

        fixture.Collection().insert(documents)
        self.assertEqual(fixture.Collection().count(), 3)

class DistinctTestCase(fixture.MongoextTestCase):
    def setUp(self):
        self.documents = [{
            'title': u'Title',
        }, {
            'title': u'Title',
        }, {
            'title': u'Title1',
        }]
        fixture.Collection().insert(self.documents)

    def test_distinct(self):
        self.assertEqual(fixture.Collection().distinct('title'), [u'Title', u'Title1'])

class DropTestCase(fixture.MongoextTestCase):
    def setUp(self):
        self.documents = [{
            'title': u'Title',
        }, {
            'title': u'Title',
        }, {
            'title': u'Title1',
        }]
        fixture.Collection().insert(self.documents)

    def test_drop(self):
        self.assertIsNone(fixture.Collection().drop())

    def test_multiple_drop(self):
        fixture.Collection().drop()
        fixture.Collection().drop()
        self.assertIsNone(fixture.Collection().drop())

class RemoveTestCase(fixture.MongoextTestCase):
    def setUp(self):
        self.documents = [{
            'title': u'Title',
        }, {
            'title': u'Title',
        }, {
            'title': u'Title1',
        }]
        fixture.Collection().insert(self.documents)

    def test_complete_remove(self):
        fixture.Collection().remove()
        self.assertEqual(fixture.Collection().count(), 0)

    def test_single_removal(self):
        fixture.Collection().remove({'title': u'Title1'})
        self.assertEqual(fixture.Collection().count(), 2)

    def test_multiple_removal(self):
        fixture.Collection().remove({'title': u'Title'})
        self.assertEqual(fixture.Collection().count(), 1)

    def test_multiple_removal_with_multi_false(self):
        fixture.Collection().remove({'title': u'Title'}, multi=False)
        self.assertEqual(fixture.Collection().count(), 2)

class UpdateTestCase(fixture.MongoextTestCase):
    def setUp(self):
        self.documents = [{
            'title': u'Title',
        }, {
            'title': u'Title',
        }, {
            'title': u'Title1',
        }]
        fixture.Collection().insert(self.documents)

    def test_update_one(self):
        fixture.Collection().update({'title': u'Title'}, {'$set': {'title': u'Title2'}})
        documents = [d for d in fixture.Collection().find({'title': u'Title2'})]
        self.assertEqual(len(documents), 1)

    def test_update_multi(self):
        fixture.Collection().update({'title': u'Title'}, {'$set': {'title': u'Title2'}}, multi=True)
        documents = [d for d in fixture.Collection().find({'title': u'Title2'})]
        self.assertEqual(len(documents), 2)

class SaveDocumentTestCase(fixture.MongoextTestCase):
    def setUp(self):
        pass

    def test_save_new_document(self):
        document = {
            'title': u'Title',
        }
        fixture.Collection().save(document)
        self.assertEqual(fixture.Collection().count(), 1)

    def test_save_existed_document(self):
        document = {'title': u'Title'}
        _id = fixture.Collection().save(document)
        document['_id'] = _id
        document['title'] = u'Title1'
        fixture.Collection().save(document)
        self.assertEqual(fixture.Collection().count(), 1)

class SaveModelTestCase(fixture.MongoextTestCase):
    def setUp(self):
        pass

    def test_save_new_model(self):
        model = fixture.Model(**{
            'created_ts': 1,
        })
        fixture.Collection().save(model)
        self.assertEqual(fixture.Collection().count(), 1)

    def test_save_existed_model(self):
        model = fixture.Model(**{
            'created_ts': 1,
        })
        _id = fixture.Collection().save(model)
        model._id = _id
        model.created_ts = 2
        fixture.Collection().save(model)
        self.assertEqual(fixture.Collection().count(), 1)

class NoCompressionCollection(fixture.Collection):
    KEYS_COMPRESSION = None

class Compression(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        NoCompressionCollection().drop()

    def test_compression(self):
        NoCompressionCollection()
<|endoftext|>


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

#   Copyright (c) 2010-2014, MIT Probabilistic Computing Project
#
#   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.

import numpy as np
import pytest
import sqlite3

import crosscat.LocalEngine

import bayeslite
import bayeslite.crosscat

# Synthetic dataset (x,y,z,v,w) for the tests. Fixed seed is not used since
# the tests should pass independently of the generated dataset.

def test_complex_dependencies():
    # Create real-valued data, such that DEP(x,y), DEP(y,z), and IND(x,z)
    mean = [4, -2, -11]
    cov = [[3.0, 0.7, 0.0],
           [0.7, 4.0, 0.6],
           [0.0, 0.6, 2.0]]
    numerical_data = np.random.multivariate_normal(mean, cov, size=250)
    x, y, z = numerical_data[:,0], numerical_data[:,1], numerical_data[:,2]

    # Create categorical data v, highly dependent on x.
    bins = [np.percentile(x,p) for p in xrange(0,101,10)]
    v = np.digitize(x, bins)

    # Create categorical data, independent of all other columns.
    w = np.random.choice(range(8), size=250)

    data = np.vstack((x,y,z,w,v)).T

    # Create the database.
    with bayeslite.bayesdb_open() as bdb:
        cc = crosscat.LocalEngine.LocalEngine(seed=0)
        ccme = bayeslite.crosscat.CrosscatMetamodel(cc)
        bayeslite.bayesdb_register_metamodel(bdb, ccme)

        # Read the dataset.
        bdb.sql_execute('CREATE TABLE foo(x,y,z,v,w)')
        for row in data:
            bdb.sql_execute('INSERT INTO foo VALUES(?,?,?,?,?)', row)

        # Create schema, we will force  IND(x y), IND(x v), and DEP(z v w).
        bql = '''
            CREATE GENERATOR bar FOR foo USING crosscat(
                GUESS(*),
                x NUMERICAL,
                y NUMERICAL,
                z NUMERICAL,
                v CATEGORICAL,
                w CATEGORICAL,
                INDEPENDENT(x, y),
                INDEPENDENT(x, v),
                DEPENDENT(z, v, w)
            );
        '''
        bdb.execute(bql)

        # Prepare the checker function.
        def check_dependencies():
            bql = '''
                ESTIMATE PAIRWISE DEPENDENCE PROBABILITY FROM bar
            '''
            for row in bdb.execute(bql):
                col1, col2, dep = row[1], row[2], row[3]
                # test IND(x y)
                if (col1, col2) in [('x','y'), ('y','x')]:
                    assert dep == 0
                    continue
                # test IND(x v)
                if (col1, col2) in [('x','v'), ('v','x')]:
                    assert dep == 0
                    continue
                # test DEP(z v)
                if (col1, col2) in [('z','v'), ('v','z')]:
                    assert dep == 1
                    continue
                # test DEP(z w)
                if (col1, col2) in [('z', 'w'), ('w', 'z')]:
                    assert dep == 1
                    continue

        # Test dependency pre-analysis.
        bdb.execute('INITIALIZE 10 MODELS FOR bar')
        check_dependencies()

        # Test dependency post-analysis.
        bdb.execute('ANALYZE bar for 10 ITERATION WAIT')
        check_dependencies()

def test_impossible_duplicate_dependency():
    # Throw exception when two columns X and Y are both dependent and
    # independent.

    data = [(1, 0, 0), (0, 0, 1)]

    # Create the database.
    with bayeslite.bayesdb_open() as bdb:
        cc = crosscat.LocalEngine.LocalEngine(seed=0)
        ccme = bayeslite.crosscat.CrosscatMetamodel(cc)
        bayeslite.bayesdb_register_metamodel(bdb, ccme)

        # Read the dataset.
        bdb.sql_execute('CREATE TABLE foo(a,b,c)')
        for row in data:
            bdb.sql_execute('INSERT INTO foo VALUES(?,?,?)', row)

        # Create schema, we will force DEP(a c) and IND(a c).
        bql = '''
            CREATE GENERATOR bar FOR foo USING crosscat(
                GUESS(*),
                a CATEGORICAL,
                b CATEGORICAL,
                c CATEGORICAL,
                INDEPENDENT(a,b,c),
                DEPENDENT(a,c),
            );
        '''

        # An error should be thrown about impossible schema.
        with pytest.raises(sqlite3.IntegrityError):
            bdb.execute(bql)

def test_impossible_nontransitive_dependency():
    # Test impossibility of non-transitive dependencies. While in the
    # general case, dependence is not transitive, crosscat assumes
    # transitive closure under dependency constraints.  The test is
    # valid since we are using a crosscat local engine.  Note that
    # transitivity under independence is not forced by crosscat.
    # Changing the behavior of CrossCat to deal with impossible
    # constraints (such as random dropout) will require updating this
    # test.
    data = [(1, 0, 0), (0, 0, 1)]

    # Create the database.
    with bayeslite.bayesdb_open() as bdb:
        cc = crosscat.LocalEngine.LocalEngine(seed=0)
        ccme = bayeslite.crosscat.CrosscatMetamodel(cc)
        bayeslite.bayesdb_register_metamodel(bdb, ccme)

        # Read the dataset.
        bdb.sql_execute('CREATE TABLE foo(a,b,c)')
        for row in data:
            bdb.sql_execute('INSERT INTO foo VALUES(?,?,?)', row)

        # Create schema, we will force DEP(a b), DEP(b c), and IND(a c) which
        # is non-transitive.
        bql = '''
            CREATE GENERATOR bar FOR foo USING crosscat(
                GUESS(*),
                a CATEGORICAL,
                b CATEGORICAL,
                c CATEGORICAL,
                DEPENDENT(a,b),
                DEPENDENT(b,c),
                INDEPENDENT(a,c)
            );
        '''

        # Creating the generator should succeed.
        bdb.execute(bql)

        # Error thrown when initializing since no initial state exists.
        # XXX Currently CrossCat throws a RuntimeError, we should fix
        # the CrossCat exception hierarchy.
        with pytest.raises(RuntimeError):
            bdb.execute('INITIALIZE 10 MODELS FOR bar')
<commit_msg>Omit needless intermediate.<commit_after># -*- coding: utf-8 -*-

#   Copyright (c) 2010-2014, MIT Probabilistic Computing Project
#
#   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.

import numpy as np
import pytest
import sqlite3

import crosscat.LocalEngine

import bayeslite
import bayeslite.crosscat

# Synthetic dataset (x,y,z,v,w) for the tests. Fixed seed is not used since
# the tests should pass independently of the generated dataset.

def test_complex_dependencies():
    # Create real-valued data, such that DEP(x,y), DEP(y,z), and IND(x,z)
    mean = [4, -2, -11]
    cov = [[3.0, 0.7, 0.0],
           [0.7, 4.0, 0.6],
           [0.0, 0.6, 2.0]]
    numerical_data = np.random.multivariate_normal(mean, cov, size=250)
    x, y, z = numerical_data[:,0], numerical_data[:,1], numerical_data[:,2]

    # Create categorical data v, highly dependent on x.
    bins = [np.percentile(x,p) for p in xrange(0,101,10)]
    v = np.digitize(x, bins)

    # Create categorical data, independent of all other columns.
    w = np.random.choice(range(8), size=250)

    data = np.vstack((x,y,z,w,v)).T

    # Create the database.
    with bayeslite.bayesdb_open() as bdb:
        cc = crosscat.LocalEngine.LocalEngine(seed=0)
        ccme = bayeslite.crosscat.CrosscatMetamodel(cc)
        bayeslite.bayesdb_register_metamodel(bdb, ccme)

        # Read the dataset.
        bdb.sql_execute('CREATE TABLE foo(x,y,z,v,w)')
        for row in data:
            bdb.sql_execute('INSERT INTO foo VALUES(?,?,?,?,?)', row)

        # Create schema, we will force  IND(x y), IND(x v), and DEP(z v w).
        bql = '''
            CREATE GENERATOR bar FOR foo USING crosscat(
                GUESS(*),
                x NUMERICAL,
                y NUMERICAL,
                z NUMERICAL,
                v CATEGORICAL,
                w CATEGORICAL,
                INDEPENDENT(x, y),
                INDEPENDENT(x, v),
                DEPENDENT(z, v, w)
            );
        '''
        bdb.execute(bql)

        # Prepare the checker function.
        def check_dependencies():
            bql = '''
                ESTIMATE PAIRWISE DEPENDENCE PROBABILITY FROM bar
            '''
            for _id, col1, col2, dep in bdb.execute(bql):
                # test IND(x y)
                if (col1, col2) in [('x','y'), ('y','x')]:
                    assert dep == 0
                    continue
                # test IND(x v)
                if (col1, col2) in [('x','v'), ('v','x')]:
                    assert dep == 0
                    continue
                # test DEP(z v)
                if (col1, col2) in [('z','v'), ('v','z')]:
                    assert dep == 1
                    continue
                # test DEP(z w)
                if (col1, col2) in [('z', 'w'), ('w', 'z')]:
                    assert dep == 1
                    continue

        # Test dependency pre-analysis.
        bdb.execute('INITIALIZE 10 MODELS FOR bar')
        check_dependencies()

        # Test dependency post-analysis.
        bdb.execute('ANALYZE bar for 10 ITERATION WAIT')
        check_dependencies()

def test_impossible_duplicate_dependency():
    # Throw exception when two columns X and Y are both dependent and
    # independent.

    data = [(1, 0, 0), (0, 0, 1)]

    # Create the database.
    with bayeslite.bayesdb_open() as bdb:
        cc = crosscat.LocalEngine.LocalEngine(seed=0)
        ccme = bayeslite.crosscat.CrosscatMetamodel(cc)
        bayeslite.bayesdb_register_metamodel(bdb, ccme)

        # Read the dataset.
        bdb.sql_execute('CREATE TABLE foo(a,b,c)')
        for row in data:
            bdb.sql_execute('INSERT INTO foo VALUES(?,?,?)', row)

        # Create schema, we will force DEP(a c) and IND(a c).
        bql = '''
            CREATE GENERATOR bar FOR foo USING crosscat(
                GUESS(*),
                a CATEGORICAL,
                b CATEGORICAL,
                c CATEGORICAL,
                INDEPENDENT(a,b,c),
                DEPENDENT(a,c),
            );
        '''

        # An error should be thrown about impossible schema.
        with pytest.raises(sqlite3.IntegrityError):
            bdb.execute(bql)

def test_impossible_nontransitive_dependency():
    # Test impossibility of non-transitive dependencies. While in the
    # general case, dependence is not transitive, crosscat assumes
    # transitive closure under dependency constraints.  The test is
    # valid since we are using a crosscat local engine.  Note that
    # transitivity under independence is not forced by crosscat.
    # Changing the behavior of CrossCat to deal with impossible
    # constraints (such as random dropout) will require updating this
    # test.
    data = [(1, 0, 0), (0, 0, 1)]

    # Create the database.
    with bayeslite.bayesdb_open() as bdb:
        cc = crosscat.LocalEngine.LocalEngine(seed=0)
        ccme = bayeslite.crosscat.CrosscatMetamodel(cc)
        bayeslite.bayesdb_register_metamodel(bdb, ccme)

        # Read the dataset.
        bdb.sql_execute('CREATE TABLE foo(a,b,c)')
        for row in data:
            bdb.sql_execute('INSERT INTO foo VALUES(?,?,?)', row)

        # Create schema, we will force DEP(a b), DEP(b c), and IND(a c) which
        # is non-transitive.
        bql = '''
            CREATE GENERATOR bar FOR foo USING crosscat(
                GUESS(*),
                a CATEGORICAL,
                b CATEGORICAL,
                c CATEGORICAL,
                DEPENDENT(a,b),
                DEPENDENT(b,c),
                INDEPENDENT(a,c)
            );
        '''

        # Creating the generator should succeed.
        bdb.execute(bql)

        # Error thrown when initializing since no initial state exists.
        # XXX Currently CrossCat throws a RuntimeError, we should fix
        # the CrossCat exception hierarchy.
        with pytest.raises(RuntimeError):
            bdb.execute('INITIALIZE 10 MODELS FOR bar')
<|endoftext|>