<commit_before>import unittest
import ois
import numpy as np

class TestSubtract(unittest.TestCase):
    def setUp(self):
        import urllib
        import cStringIO
        from PIL import Image
        # http://homepages.cae.wisc.edu/~ece533/images/cameraman.tif
        # http://links.uwaterloo.ca/Repository/TIF/camera.tif
        f = cStringIO.StringIO(urllib.urlopen(
            'http://homepages.cae.wisc.edu/~ece533/images/cameraman.tif')
            .read())
        self.ref_img = np.array(Image.open(f), dtype='float32')
        self.degradereference()

    def tearDown(self):
        pass

    def degradereference(self):
        from scipy import signal

        # Set some arbitrary kernel to convolve with
        def gauss(shape=(11, 11), center=None, sx=2, sy=2):
            h, w = shape
            if center is None:
                center = ((h - 1) / 2., (w - 1) / 2.)
            x0, y0 = center
            x, y = np.meshgrid(range(w), range(h))
            kernel = np.exp(-0.5 * ((x - x0) ** 2 / sx ** 2 +
                            (y - y0) ** 2 / sy ** 2))
            norm = kernel.sum()
            return kernel / norm

        def createkernel(coeffs, gausslist, kernelshape=(10, 10)):
            kh, kw = kernelshape
            v, u = np.mgrid[:kh, :kw]
            mykernel = np.zeros((kh, kw))
            for aGauss in gausslist:
                if 'modPolyDeg' in aGauss:
                    degmod = aGauss['modPolyDeg']
                else:
                    degmod = 2
                allus = [pow(u, i) for i in range(degmod + 1)]
                allvs = [pow(v, i) for i in range(degmod + 1)]
                if 'center' in aGauss:
                    center = aGauss['center']
                else:
                    center = None
                gaussk = gauss(shape=kernelshape, center=center,
                               sx=aGauss['sx'], sy=aGauss['sy'])
                ind = 0
                for i, aU in enumerate(allus):
                    for aV in allvs[:degmod + 1 - i]:
                        mykernel += coeffs[ind] * aU * aV
                        ind += 1
                mykernel *= gaussk
            # mykernel /= mykernel.sum()
            return mykernel

        # mygausslist = [{'sx': 2., 'sy': 2., 'modPolyDeg': 3},
        # {'sx': 1., 'sy': 3.}, {'sx': 3., 'sy': 1.}]
        self.mygausslist = [{'sx': 2., 'sy': 2., 'modPolyDeg': 3}]
        # mykcoeffs = np.random.rand(10) * 90 + 10
        mykcoeffs = np.array([0., -7.3, 0., 0., 0., 2., 0., 1.5, 0., 0.])

        mykernel = createkernel(mykcoeffs, self.mygausslist,
                                kernelshape=(11, 11))
        # mykernel = gauss()
        kh, kw = mykernel.shape

        self.image = signal.convolve2d(self.ref_img, mykernel, mode='same')

        # Add a varying background:
        self.bkgdeg = 2

        h, w = self.ref_img.shape
        y, x = np.mgrid[:h, :w]
        allxs = [pow(x, i) for i in range(self.bkgdeg + 1)]
        allys = [pow(y, i) for i in range(self.bkgdeg + 1)]

        mybkg = np.zeros(self.ref_img.shape)
        mybkgcoeffs = np.random.rand(6) * 1E-3

        ind = 0
        for i, anX in enumerate(allxs):
            for aY in allys[:self.bkgdeg + 1 - i]:
                mybkg += mybkgcoeffs[ind] * anX * aY
                ind += 1

        self.image += mybkg

    def test_optimalkernelandbkg(self):
        # Test Bramich
        ruined_image, optKernel, bkg = ois.optimalkernelandbkg(
            self.image, self.ref_img, bkgdegree=self.bkgdeg,
            kernelshape=(11, 11))
        norm_diff = np.linalg.norm(ruined_image - self.image) \
            / np.linalg.norm(self.image)
        self.assertLess(norm_diff, 1E-10)

        # Test Alard & Lupton
        ruined_image, optKernel, bkg = ois.optimalkernelandbkg(
            self.image, self.ref_img, gausslist=self.mygausslist,
            bkgdegree=self.bkgdeg,
            kernelshape=(11, 11))
        norm_diff = np.linalg.norm(ruined_image - self.image) \
            / np.linalg.norm(self.image)
        self.assertLess(norm_diff, 1E-10)

    def test_subtractongrid(self):

        # Test without mask:
        # Test Bramich
        subt_img = ois.subtractongrid(self.image, self.ref_img,
                                      gausslist=None,
                                      bkgdegree=self.bkgdeg,
                                      kernelshape=(11, 11),
                                      gridshape=(1, 1))
        norm_diff = np.linalg.norm(subt_img) / np.linalg.norm(self.image)
        self.assertLess(norm_diff, 1E-10)

        # Test Alard & Lupton
        subt_img = ois.subtractongrid(self.image, self.ref_img,
                                      gausslist=self.mygausslist,
                                      bkgdegree=self.bkgdeg,
                                      kernelshape=(11, 11),
                                      gridshape=(1, 1))
        norm_diff = np.linalg.norm(subt_img) / np.linalg.norm(self.image)
        self.assertLess(norm_diff, 1E-10)

        # Assert that subt_img is always a masked array?
        # Does it ever return a plain ndarray?

        # Test when a mask is added on the image
        mask = np.zeros(self.image.shape, dtype='bool')
        h, w = mask.shape
        mask[h / 10:h / 10 + 10, w / 10: w / 10 + 10] = True
        mask[:, 50:60] = True
        image_masked = np.ma.array(self.image, mask=mask)

        mask_ref = np.zeros(self.ref_img.shape, dtype='bool')
        mask_ref[100:110, 100:110] = True
        mask_ref[200:205, :] = True
        ref_img_masked = np.ma.array(self.ref_img, mask=mask_ref)

        # Test Bramich:
        # Image masked, ref masked
        subt_img = ois.subtractongrid(image_masked, ref_img_masked,
                                      gausslist=None,
                                      bkgdegree=self.bkgdeg,
                                      kernelshape=(11, 11),
                                      gridshape=(1, 1))
        norm_diff = np.linalg.norm(subt_img.compressed()) \
            / np.linalg.norm(image_masked.compressed())
        self.assertLess(norm_diff, 1E-10)
        # Image not masked, ref masked
        subt_img = ois.subtractongrid(self.image, ref_img_masked,
                                      gausslist=None,
                                      bkgdegree=self.bkgdeg,
                                      kernelshape=(11, 11),
                                      gridshape=(1, 1))
        norm_diff = np.linalg.norm(subt_img.compressed()) \
            / np.linalg.norm(self.image)
        self.assertLess(norm_diff, 1E-10)
        # Image masked, ref not masked
        subt_img = ois.subtractongrid(image_masked, self.ref_img,
                                      gausslist=None,
                                      bkgdegree=self.bkgdeg,
                                      kernelshape=(11, 11),
                                      gridshape=(1, 1))
        norm_diff = np.linalg.norm(subt_img.compressed()) \
            / np.linalg.norm(image_masked.compressed())
        self.assertLess(norm_diff, 1E-10)
        # Image not masked, ref masked
        subt_img = ois.subtractongrid(self.image, ref_img_masked,
                                      gausslist=None,
                                      bkgdegree=self.bkgdeg,
                                      kernelshape=(11, 11),
                                      gridshape=(1, 1))
        norm_diff = np.linalg.norm(subt_img.compressed()) \
            / np.linalg.norm(self.image)
        self.assertLess(norm_diff, 1E-10)

        # Test Alard & Lupton
        # Image masked, ref masked
        subt_img = ois.subtractongrid(image_masked, ref_img_masked,
                                      gausslist=self.mygausslist,
                                      bkgdegree=self.bkgdeg,
                                      kernelshape=(11, 11),
                                      gridshape=(1, 1))
        norm_diff = np.linalg.norm(subt_img.compressed()) \
            / np.linalg.norm(image_masked.compressed())
        self.assertLess(norm_diff, 1E-10)
        # Image masked, ref not masked
        subt_img = ois.subtractongrid(image_masked, self.ref_img,
                                      gausslist=self.mygausslist,
                                      bkgdegree=self.bkgdeg,
                                      kernelshape=(11, 11),
                                      gridshape=(1, 1))
        norm_diff = np.linalg.norm(subt_img.compressed()) \
            / np.linalg.norm(image_masked.compressed())
        self.assertLess(norm_diff, 1E-10)
        # Image not masked, ref masked
        subt_img = ois.subtractongrid(self.image, ref_img_masked,
                                      gausslist=self.mygausslist,
                                      bkgdegree=self.bkgdeg,
                                      kernelshape=(11, 11),
                                      gridshape=(1, 1))
        norm_diff = np.linalg.norm(subt_img.compressed()) \
            / np.linalg.norm(self.image)
        self.assertLess(norm_diff, 1E-10)

if __name__ == "__main__":
    unittest.main()
<commit_msg>Get test image locally instead of remotely<commit_after>import unittest
import ois
import numpy as np

class TestSubtract(unittest.TestCase):
    def setUp(self):
        from PIL import Image
        self.ref_img = np.array(
            Image.open("tests/cameraman.tif"), dtype='float32')
        self.degradereference()

    def tearDown(self):
        pass

    def degradereference(self):
        from scipy import signal

        # Set some arbitrary kernel to convolve with
        def gauss(shape=(11, 11), center=None, sx=2, sy=2):
            h, w = shape
            if center is None:
                center = ((h - 1) / 2., (w - 1) / 2.)
            x0, y0 = center
            x, y = np.meshgrid(range(w), range(h))
            kernel = np.exp(-0.5 * ((x - x0) ** 2 / sx ** 2 +
                            (y - y0) ** 2 / sy ** 2))
            norm = kernel.sum()
            return kernel / norm

        def createkernel(coeffs, gausslist, kernelshape=(10, 10)):
            kh, kw = kernelshape
            v, u = np.mgrid[:kh, :kw]
            mykernel = np.zeros((kh, kw))
            for aGauss in gausslist:
                if 'modPolyDeg' in aGauss:
                    degmod = aGauss['modPolyDeg']
                else:
                    degmod = 2
                allus = [pow(u, i) for i in range(degmod + 1)]
                allvs = [pow(v, i) for i in range(degmod + 1)]
                if 'center' in aGauss:
                    center = aGauss['center']
                else:
                    center = None
                gaussk = gauss(shape=kernelshape, center=center,
                               sx=aGauss['sx'], sy=aGauss['sy'])
                ind = 0
                for i, aU in enumerate(allus):
                    for aV in allvs[:degmod + 1 - i]:
                        mykernel += coeffs[ind] * aU * aV
                        ind += 1
                mykernel *= gaussk
            # mykernel /= mykernel.sum()
            return mykernel

        # mygausslist = [{'sx': 2., 'sy': 2., 'modPolyDeg': 3},
        # {'sx': 1., 'sy': 3.}, {'sx': 3., 'sy': 1.}]
        self.mygausslist = [{'sx': 2., 'sy': 2., 'modPolyDeg': 3}]
        # mykcoeffs = np.random.rand(10) * 90 + 10
        mykcoeffs = np.array([0., -7.3, 0., 0., 0., 2., 0., 1.5, 0., 0.])

        mykernel = createkernel(mykcoeffs, self.mygausslist,
                                kernelshape=(11, 11))
        # mykernel = gauss()
        kh, kw = mykernel.shape

        self.image = signal.convolve2d(self.ref_img, mykernel, mode='same')

        # Add a varying background:
        self.bkgdeg = 2

        h, w = self.ref_img.shape
        y, x = np.mgrid[:h, :w]
        allxs = [pow(x, i) for i in range(self.bkgdeg + 1)]
        allys = [pow(y, i) for i in range(self.bkgdeg + 1)]

        mybkg = np.zeros(self.ref_img.shape)
        mybkgcoeffs = np.random.rand(6) * 1E-3

        ind = 0
        for i, anX in enumerate(allxs):
            for aY in allys[:self.bkgdeg + 1 - i]:
                mybkg += mybkgcoeffs[ind] * anX * aY
                ind += 1

        self.image += mybkg

    def test_optimalkernelandbkg(self):
        # Test Bramich
        ruined_image, optKernel, bkg = ois.optimalkernelandbkg(
            self.image, self.ref_img, bkgdegree=self.bkgdeg,
            kernelshape=(11, 11))
        norm_diff = np.linalg.norm(ruined_image - self.image) \
            / np.linalg.norm(self.image)
        self.assertLess(norm_diff, 1E-10)

        # Test Alard & Lupton
        ruined_image, optKernel, bkg = ois.optimalkernelandbkg(
            self.image, self.ref_img, gausslist=self.mygausslist,
            bkgdegree=self.bkgdeg,
            kernelshape=(11, 11))
        norm_diff = np.linalg.norm(ruined_image - self.image) \
            / np.linalg.norm(self.image)
        self.assertLess(norm_diff, 1E-10)

    def test_subtractongrid(self):

        # Test without mask:
        # Test Bramich
        subt_img = ois.subtractongrid(self.image, self.ref_img,
                                      gausslist=None,
                                      bkgdegree=self.bkgdeg,
                                      kernelshape=(11, 11),
                                      gridshape=(1, 1))
        norm_diff = np.linalg.norm(subt_img) / np.linalg.norm(self.image)
        self.assertLess(norm_diff, 1E-10)

        # Test Alard & Lupton
        subt_img = ois.subtractongrid(self.image, self.ref_img,
                                      gausslist=self.mygausslist,
                                      bkgdegree=self.bkgdeg,
                                      kernelshape=(11, 11),
                                      gridshape=(1, 1))
        norm_diff = np.linalg.norm(subt_img) / np.linalg.norm(self.image)
        self.assertLess(norm_diff, 1E-10)

        # Assert that subt_img is always a masked array?
        # Does it ever return a plain ndarray?

        # Test when a mask is added on the image
        mask = np.zeros(self.image.shape, dtype='bool')
        h, w = mask.shape
        mask[h / 10:h / 10 + 10, w / 10: w / 10 + 10] = True
        mask[:, 50:60] = True
        image_masked = np.ma.array(self.image, mask=mask)

        mask_ref = np.zeros(self.ref_img.shape, dtype='bool')
        mask_ref[100:110, 100:110] = True
        mask_ref[200:205, :] = True
        ref_img_masked = np.ma.array(self.ref_img, mask=mask_ref)

        # Test Bramich:
        # Image masked, ref masked
        subt_img = ois.subtractongrid(image_masked, ref_img_masked,
                                      gausslist=None,
                                      bkgdegree=self.bkgdeg,
                                      kernelshape=(11, 11),
                                      gridshape=(1, 1))
        norm_diff = np.linalg.norm(subt_img.compressed()) \
            / np.linalg.norm(image_masked.compressed())
        self.assertLess(norm_diff, 1E-10)
        # Image not masked, ref masked
        subt_img = ois.subtractongrid(self.image, ref_img_masked,
                                      gausslist=None,
                                      bkgdegree=self.bkgdeg,
                                      kernelshape=(11, 11),
                                      gridshape=(1, 1))
        norm_diff = np.linalg.norm(subt_img.compressed()) \
            / np.linalg.norm(self.image)
        self.assertLess(norm_diff, 1E-10)
        # Image masked, ref not masked
        subt_img = ois.subtractongrid(image_masked, self.ref_img,
                                      gausslist=None,
                                      bkgdegree=self.bkgdeg,
                                      kernelshape=(11, 11),
                                      gridshape=(1, 1))
        norm_diff = np.linalg.norm(subt_img.compressed()) \
            / np.linalg.norm(image_masked.compressed())
        self.assertLess(norm_diff, 1E-10)
        # Image not masked, ref masked
        subt_img = ois.subtractongrid(self.image, ref_img_masked,
                                      gausslist=None,
                                      bkgdegree=self.bkgdeg,
                                      kernelshape=(11, 11),
                                      gridshape=(1, 1))
        norm_diff = np.linalg.norm(subt_img.compressed()) \
            / np.linalg.norm(self.image)
        self.assertLess(norm_diff, 1E-10)

        # Test Alard & Lupton
        # Image masked, ref masked
        subt_img = ois.subtractongrid(image_masked, ref_img_masked,
                                      gausslist=self.mygausslist,
                                      bkgdegree=self.bkgdeg,
                                      kernelshape=(11, 11),
                                      gridshape=(1, 1))
        norm_diff = np.linalg.norm(subt_img.compressed()) \
            / np.linalg.norm(image_masked.compressed())
        self.assertLess(norm_diff, 1E-10)
        # Image masked, ref not masked
        subt_img = ois.subtractongrid(image_masked, self.ref_img,
                                      gausslist=self.mygausslist,
                                      bkgdegree=self.bkgdeg,
                                      kernelshape=(11, 11),
                                      gridshape=(1, 1))
        norm_diff = np.linalg.norm(subt_img.compressed()) \
            / np.linalg.norm(image_masked.compressed())
        self.assertLess(norm_diff, 1E-10)
        # Image not masked, ref masked
        subt_img = ois.subtractongrid(self.image, ref_img_masked,
                                      gausslist=self.mygausslist,
                                      bkgdegree=self.bkgdeg,
                                      kernelshape=(11, 11),
                                      gridshape=(1, 1))
        norm_diff = np.linalg.norm(subt_img.compressed()) \
            / np.linalg.norm(self.image)
        self.assertLess(norm_diff, 1E-10)

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


<commit_before>import glob
import os
import sys
import shutil, tempfile
import unittest

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

from tests.test_common import create_css_file

import icon_font_to_png

class TestRun(unittest.TestCase):
    def setUp(self):
        # Create a temporary directory
        self.test_dir = tempfile.mkdtemp()

    def tearDown(self):
        # Remove the temporary directory when we're done
        shutil.rmtree(self.test_dir)

    def test_usage(self):
        orig_stderr = sys.stderr
        sys.stderr = StringIO()

        self.assertRaises(SystemExit, icon_font_to_png.run, [])

        err = sys.stderr.getvalue().strip()
        self.assertRegexpMatches(err, '^usage: .*')

        sys.stderr = orig_stderr

    def test_list(self):
        css_file = create_css_file(
            ".foo-xyzzy:before { content: '\\f003' }\n"
            ".foo-baz:before { content: '\\f002' }\n"
            ".foo-bar:before { content: '\\f001' }\n"
        )

        orig_stdout = sys.stdout
        sys.stdout = StringIO()

        self.assertRaisesRegexp(SystemExit, '^0',
            icon_font_to_png.run, ['foo.ttf', css_file.name, 'bar', '--list'])

        out = sys.stdout.getvalue()
        self.assertEqual(out,
            "bar\n"
            "baz\n"
            "xyzzy\n"
        )

        sys.stdout = StringIO()

        self.assertRaisesRegexp(SystemExit, '^0',
            icon_font_to_png.run, ['foo.ttf', css_file.name, 'bar', '--list',
                '--keep-prefix'])

        out = sys.stdout.getvalue()
        self.assertEqual(out,
            "foo-bar\n"
            "foo-baz\n"
            "foo-xyzzy\n"
        )

        sys.stdout = orig_stdout

    def test_icon(self):
        css_file = create_css_file(
            ".beer:before { content: '\\f069' }\n"
            ".squirrel:before { content: '\\f0b2' }\n"
            ".zap:before { content: '\\26A1' }\n"
        )

        this_dir = os.getcwd()

        os.chdir(self.test_dir)

        # Export one icon
        icon_font_to_png.run([
            os.path.join(this_dir, 'tests/run/icon/octicons.ttf'),
            css_file.name,
            'beer'
        ]);

        self.assertTrue(os.path.isfile('beer.png'))

        for f in glob.glob(os.path.join('*.png')):
            os.remove(f)

        # Export two icons
        icon_font_to_png.run([
            os.path.join(this_dir, 'tests/run/icon/octicons.ttf'),
            css_file.name,
            'beer', 'squirrel'
        ]);

        self.assertTrue(os.path.isfile('beer.png'))
        self.assertTrue(os.path.isfile('squirrel.png'))

        for f in glob.glob(os.path.join('*.png')):
            os.remove(f)

        # Export all icons
        icon_font_to_png.run([
            os.path.join(this_dir, 'tests/run/icon/octicons.ttf'),
            css_file.name,
            'ALL'
        ]);

        self.assertTrue(os.path.isfile('beer.png'))
        self.assertTrue(os.path.isfile('squirrel.png'))
        self.assertTrue(os.path.isfile('zap.png'))

        for f in glob.glob(os.path.join('*.png')):
            os.remove(f)

        os.chdir(this_dir)

    def test_filename(self):
        css_file = create_css_file(
            ".beer:before { content: '\\f069' }\n"
            ".squirrel:before { content: '\\f0b2' }\n"
            ".zap:before { content: '\\26A1' }\n"
        )

        this_dir = os.getcwd()

        os.chdir(self.test_dir)

        # Export one icon
        icon_font_to_png.run([
            os.path.join(this_dir, 'tests/run/icon/octicons.ttf'),
            css_file.name,
            'beer',
            '--filename', 'beverage.png'
        ]);

        self.assertTrue(os.path.isfile('beverage.png'))

        for f in glob.glob(os.path.join('*.png')):
            os.remove(f)

        # Export multiple icons
        icon_font_to_png.run([
            os.path.join(this_dir, 'tests/run/icon/octicons.ttf'),
            css_file.name,
            'beer', 'squirrel',
            '--filename', 'foo'
        ]);

        self.assertTrue(os.path.isfile('foobeer.png'))
        self.assertTrue(os.path.isfile('foosquirrel.png'))

        for f in glob.glob(os.path.join('*.png')):
            os.remove(f)

        os.chdir(this_dir)

if __name__ == '__main__':
    unittest.main
<commit_msg>Remove icon name from --list tests<commit_after>import glob
import os
import sys
import shutil, tempfile
import unittest

try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

from tests.test_common import create_css_file

import icon_font_to_png

class TestRun(unittest.TestCase):
    def setUp(self):
        # Create a temporary directory
        self.test_dir = tempfile.mkdtemp()

    def tearDown(self):
        # Remove the temporary directory when we're done
        shutil.rmtree(self.test_dir)

    def test_usage(self):
        orig_stderr = sys.stderr
        sys.stderr = StringIO()

        self.assertRaises(SystemExit, icon_font_to_png.run, [])

        err = sys.stderr.getvalue().strip()
        self.assertRegexpMatches(err, '^usage: .*')

        sys.stderr = orig_stderr

    def test_list(self):
        css_file = create_css_file(
            ".foo-xyzzy:before { content: '\\f003' }\n"
            ".foo-baz:before { content: '\\f002' }\n"
            ".foo-bar:before { content: '\\f001' }\n"
        )

        orig_stdout = sys.stdout
        sys.stdout = StringIO()

        self.assertRaisesRegexp(SystemExit, '^0',
            icon_font_to_png.run, ['foo.ttf', css_file.name, '--list'])

        out = sys.stdout.getvalue()
        self.assertEqual(out,
            "bar\n"
            "baz\n"
            "xyzzy\n"
        )

        sys.stdout = StringIO()

        self.assertRaisesRegexp(SystemExit, '^0',
            icon_font_to_png.run, ['foo.ttf', css_file.name, '--list',
                '--keep-prefix'])

        out = sys.stdout.getvalue()
        self.assertEqual(out,
            "foo-bar\n"
            "foo-baz\n"
            "foo-xyzzy\n"
        )

        sys.stdout = orig_stdout

    def test_icon(self):
        css_file = create_css_file(
            ".beer:before { content: '\\f069' }\n"
            ".squirrel:before { content: '\\f0b2' }\n"
            ".zap:before { content: '\\26A1' }\n"
        )

        this_dir = os.getcwd()

        os.chdir(self.test_dir)

        # Export one icon
        icon_font_to_png.run([
            os.path.join(this_dir, 'tests/run/icon/octicons.ttf'),
            css_file.name,
            'beer'
        ]);

        self.assertTrue(os.path.isfile('beer.png'))

        for f in glob.glob(os.path.join('*.png')):
            os.remove(f)

        # Export two icons
        icon_font_to_png.run([
            os.path.join(this_dir, 'tests/run/icon/octicons.ttf'),
            css_file.name,
            'beer', 'squirrel'
        ]);

        self.assertTrue(os.path.isfile('beer.png'))
        self.assertTrue(os.path.isfile('squirrel.png'))

        for f in glob.glob(os.path.join('*.png')):
            os.remove(f)

        # Export all icons
        icon_font_to_png.run([
            os.path.join(this_dir, 'tests/run/icon/octicons.ttf'),
            css_file.name,
            'ALL'
        ]);

        self.assertTrue(os.path.isfile('beer.png'))
        self.assertTrue(os.path.isfile('squirrel.png'))
        self.assertTrue(os.path.isfile('zap.png'))

        for f in glob.glob(os.path.join('*.png')):
            os.remove(f)

        os.chdir(this_dir)

    def test_filename(self):
        css_file = create_css_file(
            ".beer:before { content: '\\f069' }\n"
            ".squirrel:before { content: '\\f0b2' }\n"
            ".zap:before { content: '\\26A1' }\n"
        )

        this_dir = os.getcwd()

        os.chdir(self.test_dir)

        # Export one icon
        icon_font_to_png.run([
            os.path.join(this_dir, 'tests/run/icon/octicons.ttf'),
            css_file.name,
            'beer',
            '--filename', 'beverage.png'
        ]);

        self.assertTrue(os.path.isfile('beverage.png'))

        for f in glob.glob(os.path.join('*.png')):
            os.remove(f)

        # Export multiple icons
        icon_font_to_png.run([
            os.path.join(this_dir, 'tests/run/icon/octicons.ttf'),
            css_file.name,
            'beer', 'squirrel',
            '--filename', 'foo'
        ]);

        self.assertTrue(os.path.isfile('foobeer.png'))
        self.assertTrue(os.path.isfile('foosquirrel.png'))

        for f in glob.glob(os.path.join('*.png')):
            os.remove(f)

        os.chdir(this_dir)

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


<commit_before>import os
import numpy as np
import pandas as pd
import unittest
from urllib2 import urlopen

from bom_data_parser import read_soi_html

class SOITest(unittest.TestCase):
    def setUp(self):
        self.test_soi_file = os.path.join(os.path.dirname(__file__), 'data', 'SOI', 'soiplaintext.html')

    def test_soi(self):
        with open(self.test_soi_file, 'r') as soi_file:
            soi_data = read_soi_html(soi_file)

        self.assertTrue('soi' in soi_data.columns)
        self.assertEqual(soi_data.ix['1876-01'].values.item(), 11.3)
        self.assertEqual(soi_data.ix['1984-12'].values.item(), -1.4)
        self.assertEqual(soi_data.ix['2015-01'].values.item(), -7.8)
<commit_msg>Remove unused urllib import.<commit_after>import os
import numpy as np
import pandas as pd
import unittest

from bom_data_parser import read_soi_html

class SOITest(unittest.TestCase):
    def setUp(self):
        self.test_soi_file = os.path.join(os.path.dirname(__file__), 'data', 'SOI', 'soiplaintext.html')

    def test_soi(self):
        with open(self.test_soi_file, 'r') as soi_file:
            soi_data = read_soi_html(soi_file)

        self.assertTrue('soi' in soi_data.columns)
        self.assertEqual(soi_data.ix['1876-01'].values.item(), 11.3)
        self.assertEqual(soi_data.ix['1984-12'].values.item(), -1.4)
        self.assertEqual(soi_data.ix['2015-01'].values.item(), -7.8)
<|endoftext|>


<commit_before>import operator
import time

import pytest
import six

import consul
import consul.std

class TestHTTPClient(object):
    def test_uri(self):
        http = consul.std.HTTPClient()
        assert http.uri('/v1/kv') == 'http://127.0.0.1:8500/v1/kv'
        assert http.uri('/v1/kv', params={'index': 1}) == \
            'http://127.0.0.1:8500/v1/kv?index=1'

class TestConsul(object):
    def test_kv(self, consul_port):
        c = consul.Consul(port=consul_port)
        index, data = c.kv.get('foo')
        assert data is None
        assert c.kv.put('foo', 'bar') is True
        index, data = c.kv.get('foo')
        assert data['Value'] == six.b('bar')

    def test_kv_put_cas(self, consul_port):
        c = consul.Consul(port=consul_port)
        assert c.kv.put('foo', 'bar', cas=50) is False
        assert c.kv.put('foo', 'bar', cas=0) is True
        index, data = c.kv.get('foo')

        assert c.kv.put('foo', 'bar2', cas=data['ModifyIndex']-1) is False
        assert c.kv.put('foo', 'bar2', cas=data['ModifyIndex']) is True
        index, data = c.kv.get('foo')
        assert data['Value'] == six.b('bar2')

    def test_kv_put_flags(self, consul_port):
        c = consul.Consul(port=consul_port)
        c.kv.put('foo', 'bar')
        index, data = c.kv.get('foo')
        assert data['Flags'] == 0

        assert c.kv.put('foo', 'bar', flags=50) is True
        index, data = c.kv.get('foo')
        assert data['Flags'] == 50

    def test_kv_recurse(self, consul_port):
        c = consul.Consul(port=consul_port)
        index, data = c.kv.get('foo/', recurse=True)
        assert data is None

        c.kv.put('foo/', None)
        index, data = c.kv.get('foo/', recurse=True)
        assert len(data) == 1

        c.kv.put('foo/bar1', '1')
        c.kv.put('foo/bar2', '2')
        c.kv.put('foo/bar3', '3')
        index, data = c.kv.get('foo/', recurse=True)
        assert [x['Key'] for x in data] == [
            'foo/bar1', 'foo/bar2', 'foo/bar3', 'foo/']
        assert [x['Value'] for x in data] == ['1', '2', '3', None]

    def test_kv_delete(self, consul_port):
        c = consul.Consul(port=consul_port)
        c.kv.put('foo1', '1')
        c.kv.put('foo2', '2')
        c.kv.put('foo3', '3')
        index, data = c.kv.get('foo', recurse=True)
        assert [x['Key'] for x in data] == ['foo1', 'foo2', 'foo3']

        assert c.kv.delete('foo2') is True
        index, data = c.kv.get('foo', recurse=True)
        assert [x['Key'] for x in data] == ['foo1', 'foo3']
        assert c.kv.delete('foo', recurse=True) is True
        index, data = c.kv.get('foo', recurse=True)
        assert data is None

    def test_agent_self(self, consul_port):
        c = consul.Consul(port=consul_port)
        assert set(c.agent.self().keys()) == set(['Member', 'Config'])

    def test_agent_services(self, consul_port):
        c = consul.Consul(port=consul_port)
        assert set(c.agent.services().keys()) == set(['consul'])
        assert c.agent.service.register('foo') is True
        assert set(c.agent.services().keys()) == set(['consul', 'foo'])
        assert c.agent.service.deregister('foo') is True
        assert set(c.agent.services().keys()) == set(['consul'])

    def test_catalog(self, consul_port):
        c = consul.Consul(port=consul_port)

        # grab the node our server created, so we can ignore it
        _, nodes = c.catalog.nodes()
        assert len(nodes) == 1
        current = nodes[0]

        # test catalog.datacenters
        assert c.catalog.datacenters() == ['dc1']

        # test catalog.register
        pytest.raises(
            consul.ConsulException,
            c.catalog.register, 'foo', '10.1.10.11', dc='dc2')

        assert c.catalog.register(
            'n1',
            '10.1.10.11',
            service={'service': 's1'},
            check={'name': 'c1'}) is True
        assert c.catalog.register(
            'n1', '10.1.10.11', service={'service': 's2'}) is True
        assert c.catalog.register(
            'n2', '10.1.10.12',
            service={'service': 's1', 'tags': ['master']}) is True

        # test catalog.nodes
        pytest.raises(consul.ConsulException, c.catalog.nodes, dc='dc2')
        _, nodes = c.catalog.nodes()
        nodes.remove(current)
        assert [x['Node'] for x in nodes] == ['n1', 'n2']

        # test catalog.services
        pytest.raises(consul.ConsulException, c.catalog.services, dc='dc2')
        _, services = c.catalog.services()
        assert services == {'s1': [u'master'], 's2': [], 'consul': []}

        # test catalog.node
        pytest.raises(consul.ConsulException, c.catalog.node, 'n1', dc='dc2')
        _, node = c.catalog.node('n1')
        assert set(node['Services'].keys()) == set(['s1', 's2'])
        _, node = c.catalog.node('n3')
        assert node is None

        # test catalog.service
        pytest.raises(
            consul.ConsulException, c.catalog.service, 's1', dc='dc2')
        _, nodes = c.catalog.service('s1')
        assert set([x['Node'] for x in nodes]) == set(['n1', 'n2'])
        _, nodes = c.catalog.service('s1', tag='master')
        assert set([x['Node'] for x in nodes]) == set(['n2'])

        # test catalog.deregister
        pytest.raises(
            consul.ConsulException, c.catalog.deregister, 'n2', dc='dc2')
        assert c.catalog.deregister('n1', check_id='c1') is True
        assert c.catalog.deregister('n2', service_id='s1') is True
        # check the nodes weren't removed
        _, nodes = c.catalog.nodes()
        nodes.remove(current)
        assert [x['Node'] for x in nodes] == ['n1', 'n2']
        # check n2's s1 service was removed though
        _, nodes = c.catalog.service('s1')
        assert set([x['Node'] for x in nodes]) == set(['n1'])

        # cleanup
        assert c.catalog.deregister('n1') is True
        assert c.catalog.deregister('n2') is True
        _, nodes = c.catalog.nodes()
        nodes.remove(current)
        assert [x['Node'] for x in nodes] == []

    def test_health_service(self, consul_port):
        c = consul.Consul(port=consul_port)

        # check there are no nodes for the service 'foo'
        index, nodes = c.health.service('foo')
        assert nodes == []

        # register two nodes, one with a long ttl, the other shorter
        c.agent.service.register('foo', service_id='foo:1', ttl='10s')
        c.agent.service.register('foo', service_id='foo:2', ttl='100ms')

        time.sleep(10/1000.0)

        # check the nodes show for the /health/service endpoint
        index, nodes = c.health.service('foo')
        assert [node['Service']['ID'] for node in nodes] == ['foo:1', 'foo:2']

        # but that they aren't passing their health check
        index, nodes = c.health.service('foo', passing=True)
        assert nodes == []

        # ping the two node's health check
        c.health.check.ttl_pass('service:foo:1')
        c.health.check.ttl_pass('service:foo:2')

        time.sleep(10/1000.0)

        # both nodes are now available
        index, nodes = c.health.service('foo', passing=True)
        assert [node['Service']['ID'] for node in nodes] == ['foo:1', 'foo:2']

        # wait until the short ttl node fails
        time.sleep(120/1000.0)

        # only one node available
        index, nodes = c.health.service('foo', passing=True)
        assert [node['Service']['ID'] for node in nodes] == ['foo:1']

        # ping the failed node's health check
        c.health.check.ttl_pass('service:foo:2')

        time.sleep(10/1000.0)

        # check both nodes are available
        index, nodes = c.health.service('foo', passing=True)
        assert [node['Service']['ID'] for node in nodes] == ['foo:1', 'foo:2']

        # deregister the nodes
        c.agent.service.deregister('foo:1')
        c.agent.service.deregister('foo:2')

        time.sleep(10/1000.0)

        index, nodes = c.health.service('foo')
        assert nodes == []

    def test_acl_disabled(self, consul_port):
        c = consul.Consul(port=consul_port)
        pytest.raises(consul.ACLDisabled, c.acl.list)
        pytest.raises(consul.ACLDisabled, c.acl.info, 'foo')
        pytest.raises(consul.ACLDisabled, c.acl.create)
        pytest.raises(consul.ACLDisabled, c.acl.update, 'foo')
        pytest.raises(consul.ACLDisabled, c.acl.clone, 'foo')
        pytest.raises(consul.ACLDisabled, c.acl.destroy, 'foo')

    def test_acl_permission_denied(self, acl_consul):
        c = consul.Consul(port=acl_consul.port)
        pytest.raises(consul.ACLPermissionDenied, c.acl.list)
        pytest.raises(consul.ACLPermissionDenied, c.acl.create)
        pytest.raises(consul.ACLPermissionDenied, c.acl.update, 'anonymous')
        pytest.raises(consul.ACLPermissionDenied, c.acl.clone, 'anonymous')
        pytest.raises(consul.ACLPermissionDenied, c.acl.destroy, 'anonymous')

    def test_acl_explict_token_use(self, acl_consul):
        c = consul.Consul(port=acl_consul.port)
        master_token = acl_consul.token

        acls = c.acl.list(token=master_token)
        assert set([x['ID'] for x in acls]) == \
            set(['anonymous', master_token])

        assert c.acl.info('foo') is None
        compare = [c.acl.info(master_token), c.acl.info('anonymous')]
        compare.sort(key=operator.itemgetter('ID'))
        assert acls == compare

        rules = """
            key "" {
                policy = "read"
            }
            key "private/" {
                policy = "deny"
            }
        """

        token = c.acl.create(rules=rules, token=master_token)
        assert c.acl.info(token)['Rules'] == rules

        token2 = c.acl.clone(token, token=master_token)
        assert c.acl.info(token2)['Rules'] == rules

        assert c.acl.update(token2, name='Foo', token=master_token) == token2
        assert c.acl.info(token2)['Name'] == 'Foo'

        assert c.acl.destroy(token2, token=master_token) is True
        assert c.acl.info(token2) is None

        c.kv.put('foo', 'bar')
        c.kv.put('private/foo', 'bar')

        assert c.kv.get('foo', token=token)[1]['Value'] == six.b('bar')
        pytest.raises(
            consul.ACLPermissionDenied, c.kv.put, 'foo', 'bar2', token=token)
        pytest.raises(
            consul.ACLPermissionDenied, c.kv.delete, 'foo', token=token)

        assert c.kv.get('private/foo')[1]['Value'] == six.b('bar')
        assert c.kv.get('private/foo', token=token)[1] is None
        pytest.raises(
            consul.ACLPermissionDenied,
            c.kv.put, 'private/foo', 'bar2', token=token)
        pytest.raises(
            consul.ACLPermissionDenied,
            c.kv.delete, 'private/foo', token=token)

        # clean up
        c.acl.destroy(token, token=master_token)
        acls = c.acl.list(token=master_token)
        assert set([x['ID'] for x in acls]) == \
            set(['anonymous', master_token])

    def test_acl_implicit_token_use(self, acl_consul):
        # configure client to use the master token by default
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
        master_token = acl_consul.token

        acls = c.acl.list()
        assert set([x['ID'] for x in acls]) == \
            set(['anonymous', master_token])

        assert c.acl.info('foo') is None
        compare = [c.acl.info(master_token), c.acl.info('anonymous')]
        compare.sort(key=operator.itemgetter('ID'))
        assert acls == compare

        rules = """
            key "" {
                policy = "read"
            }
            key "private/" {
                policy = "deny"
            }
        """
        token = c.acl.create(rules=rules)
        assert c.acl.info(token)['Rules'] == rules

        token2 = c.acl.clone(token)
        assert c.acl.info(token2)['Rules'] == rules

        assert c.acl.update(token2, name='Foo') == token2
        assert c.acl.info(token2)['Name'] == 'Foo'

        assert c.acl.destroy(token2) is True
        assert c.acl.info(token2) is None

        c.kv.put('foo', 'bar')
        c.kv.put('private/foo', 'bar')

        c_limited = consul.Consul(port=acl_consul.port, token=token)
        assert c_limited.kv.get('foo')[1]['Value'] == six.b('bar')
        pytest.raises(
            consul.ACLPermissionDenied, c_limited.kv.put, 'foo', 'bar2')
        pytest.raises(
            consul.ACLPermissionDenied, c_limited.kv.delete, 'foo')

        assert c.kv.get('private/foo')[1]['Value'] == six.b('bar')
        assert c_limited.kv.get('private/foo')[1] is None
        pytest.raises(
            consul.ACLPermissionDenied,
            c_limited.kv.put, 'private/foo', 'bar2')
        pytest.raises(
            consul.ACLPermissionDenied,
            c_limited.kv.delete, 'private/foo')

        # check we can override the client's default token
        assert c.kv.get('private/foo', token=token)[1] is None
        pytest.raises(
            consul.ACLPermissionDenied,
            c.kv.put, 'private/foo', 'bar2', token=token)
        pytest.raises(
            consul.ACLPermissionDenied,
            c.kv.delete, 'private/foo', token=token)

        # clean up
        c.acl.destroy(token)
        acls = c.acl.list()
        assert set([x['ID'] for x in acls]) == \
            set(['anonymous', master_token])
<commit_msg>update test to work with Python 3.4<commit_after>import operator
import time

import pytest
import six

import consul
import consul.std

class TestHTTPClient(object):
    def test_uri(self):
        http = consul.std.HTTPClient()
        assert http.uri('/v1/kv') == 'http://127.0.0.1:8500/v1/kv'
        assert http.uri('/v1/kv', params={'index': 1}) == \
            'http://127.0.0.1:8500/v1/kv?index=1'

class TestConsul(object):
    def test_kv(self, consul_port):
        c = consul.Consul(port=consul_port)
        index, data = c.kv.get('foo')
        assert data is None
        assert c.kv.put('foo', 'bar') is True
        index, data = c.kv.get('foo')
        assert data['Value'] == six.b('bar')

    def test_kv_put_cas(self, consul_port):
        c = consul.Consul(port=consul_port)
        assert c.kv.put('foo', 'bar', cas=50) is False
        assert c.kv.put('foo', 'bar', cas=0) is True
        index, data = c.kv.get('foo')

        assert c.kv.put('foo', 'bar2', cas=data['ModifyIndex']-1) is False
        assert c.kv.put('foo', 'bar2', cas=data['ModifyIndex']) is True
        index, data = c.kv.get('foo')
        assert data['Value'] == six.b('bar2')

    def test_kv_put_flags(self, consul_port):
        c = consul.Consul(port=consul_port)
        c.kv.put('foo', 'bar')
        index, data = c.kv.get('foo')
        assert data['Flags'] == 0

        assert c.kv.put('foo', 'bar', flags=50) is True
        index, data = c.kv.get('foo')
        assert data['Flags'] == 50

    def test_kv_recurse(self, consul_port):
        c = consul.Consul(port=consul_port)
        index, data = c.kv.get('foo/', recurse=True)
        assert data is None

        c.kv.put('foo/', None)
        index, data = c.kv.get('foo/', recurse=True)
        assert len(data) == 1

        c.kv.put('foo/bar1', '1')
        c.kv.put('foo/bar2', '2')
        c.kv.put('foo/bar3', '3')
        index, data = c.kv.get('foo/', recurse=True)
        assert [x['Key'] for x in data] == [
            'foo/bar1', 'foo/bar2', 'foo/bar3', 'foo/']
        assert [x['Value'] for x in data] == [
            six.b('1'), six.b('2'), six.b('3'), None]

    def test_kv_delete(self, consul_port):
        c = consul.Consul(port=consul_port)
        c.kv.put('foo1', '1')
        c.kv.put('foo2', '2')
        c.kv.put('foo3', '3')
        index, data = c.kv.get('foo', recurse=True)
        assert [x['Key'] for x in data] == ['foo1', 'foo2', 'foo3']

        assert c.kv.delete('foo2') is True
        index, data = c.kv.get('foo', recurse=True)
        assert [x['Key'] for x in data] == ['foo1', 'foo3']
        assert c.kv.delete('foo', recurse=True) is True
        index, data = c.kv.get('foo', recurse=True)
        assert data is None

    def test_agent_self(self, consul_port):
        c = consul.Consul(port=consul_port)
        assert set(c.agent.self().keys()) == set(['Member', 'Config'])

    def test_agent_services(self, consul_port):
        c = consul.Consul(port=consul_port)
        assert set(c.agent.services().keys()) == set(['consul'])
        assert c.agent.service.register('foo') is True
        assert set(c.agent.services().keys()) == set(['consul', 'foo'])
        assert c.agent.service.deregister('foo') is True
        assert set(c.agent.services().keys()) == set(['consul'])

    def test_catalog(self, consul_port):
        c = consul.Consul(port=consul_port)

        # grab the node our server created, so we can ignore it
        _, nodes = c.catalog.nodes()
        assert len(nodes) == 1
        current = nodes[0]

        # test catalog.datacenters
        assert c.catalog.datacenters() == ['dc1']

        # test catalog.register
        pytest.raises(
            consul.ConsulException,
            c.catalog.register, 'foo', '10.1.10.11', dc='dc2')

        assert c.catalog.register(
            'n1',
            '10.1.10.11',
            service={'service': 's1'},
            check={'name': 'c1'}) is True
        assert c.catalog.register(
            'n1', '10.1.10.11', service={'service': 's2'}) is True
        assert c.catalog.register(
            'n2', '10.1.10.12',
            service={'service': 's1', 'tags': ['master']}) is True

        # test catalog.nodes
        pytest.raises(consul.ConsulException, c.catalog.nodes, dc='dc2')
        _, nodes = c.catalog.nodes()
        nodes.remove(current)
        assert [x['Node'] for x in nodes] == ['n1', 'n2']

        # test catalog.services
        pytest.raises(consul.ConsulException, c.catalog.services, dc='dc2')
        _, services = c.catalog.services()
        assert services == {'s1': [u'master'], 's2': [], 'consul': []}

        # test catalog.node
        pytest.raises(consul.ConsulException, c.catalog.node, 'n1', dc='dc2')
        _, node = c.catalog.node('n1')
        assert set(node['Services'].keys()) == set(['s1', 's2'])
        _, node = c.catalog.node('n3')
        assert node is None

        # test catalog.service
        pytest.raises(
            consul.ConsulException, c.catalog.service, 's1', dc='dc2')
        _, nodes = c.catalog.service('s1')
        assert set([x['Node'] for x in nodes]) == set(['n1', 'n2'])
        _, nodes = c.catalog.service('s1', tag='master')
        assert set([x['Node'] for x in nodes]) == set(['n2'])

        # test catalog.deregister
        pytest.raises(
            consul.ConsulException, c.catalog.deregister, 'n2', dc='dc2')
        assert c.catalog.deregister('n1', check_id='c1') is True
        assert c.catalog.deregister('n2', service_id='s1') is True
        # check the nodes weren't removed
        _, nodes = c.catalog.nodes()
        nodes.remove(current)
        assert [x['Node'] for x in nodes] == ['n1', 'n2']
        # check n2's s1 service was removed though
        _, nodes = c.catalog.service('s1')
        assert set([x['Node'] for x in nodes]) == set(['n1'])

        # cleanup
        assert c.catalog.deregister('n1') is True
        assert c.catalog.deregister('n2') is True
        _, nodes = c.catalog.nodes()
        nodes.remove(current)
        assert [x['Node'] for x in nodes] == []

    def test_health_service(self, consul_port):
        c = consul.Consul(port=consul_port)

        # check there are no nodes for the service 'foo'
        index, nodes = c.health.service('foo')
        assert nodes == []

        # register two nodes, one with a long ttl, the other shorter
        c.agent.service.register('foo', service_id='foo:1', ttl='10s')
        c.agent.service.register('foo', service_id='foo:2', ttl='100ms')

        time.sleep(10/1000.0)

        # check the nodes show for the /health/service endpoint
        index, nodes = c.health.service('foo')
        assert [node['Service']['ID'] for node in nodes] == ['foo:1', 'foo:2']

        # but that they aren't passing their health check
        index, nodes = c.health.service('foo', passing=True)
        assert nodes == []

        # ping the two node's health check
        c.health.check.ttl_pass('service:foo:1')
        c.health.check.ttl_pass('service:foo:2')

        time.sleep(10/1000.0)

        # both nodes are now available
        index, nodes = c.health.service('foo', passing=True)
        assert [node['Service']['ID'] for node in nodes] == ['foo:1', 'foo:2']

        # wait until the short ttl node fails
        time.sleep(120/1000.0)

        # only one node available
        index, nodes = c.health.service('foo', passing=True)
        assert [node['Service']['ID'] for node in nodes] == ['foo:1']

        # ping the failed node's health check
        c.health.check.ttl_pass('service:foo:2')

        time.sleep(10/1000.0)

        # check both nodes are available
        index, nodes = c.health.service('foo', passing=True)
        assert [node['Service']['ID'] for node in nodes] == ['foo:1', 'foo:2']

        # deregister the nodes
        c.agent.service.deregister('foo:1')
        c.agent.service.deregister('foo:2')

        time.sleep(10/1000.0)

        index, nodes = c.health.service('foo')
        assert nodes == []

    def test_acl_disabled(self, consul_port):
        c = consul.Consul(port=consul_port)
        pytest.raises(consul.ACLDisabled, c.acl.list)
        pytest.raises(consul.ACLDisabled, c.acl.info, 'foo')
        pytest.raises(consul.ACLDisabled, c.acl.create)
        pytest.raises(consul.ACLDisabled, c.acl.update, 'foo')
        pytest.raises(consul.ACLDisabled, c.acl.clone, 'foo')
        pytest.raises(consul.ACLDisabled, c.acl.destroy, 'foo')

    def test_acl_permission_denied(self, acl_consul):
        c = consul.Consul(port=acl_consul.port)
        pytest.raises(consul.ACLPermissionDenied, c.acl.list)
        pytest.raises(consul.ACLPermissionDenied, c.acl.create)
        pytest.raises(consul.ACLPermissionDenied, c.acl.update, 'anonymous')
        pytest.raises(consul.ACLPermissionDenied, c.acl.clone, 'anonymous')
        pytest.raises(consul.ACLPermissionDenied, c.acl.destroy, 'anonymous')

    def test_acl_explict_token_use(self, acl_consul):
        c = consul.Consul(port=acl_consul.port)
        master_token = acl_consul.token

        acls = c.acl.list(token=master_token)
        assert set([x['ID'] for x in acls]) == \
            set(['anonymous', master_token])

        assert c.acl.info('foo') is None
        compare = [c.acl.info(master_token), c.acl.info('anonymous')]
        compare.sort(key=operator.itemgetter('ID'))
        assert acls == compare

        rules = """
            key "" {
                policy = "read"
            }
            key "private/" {
                policy = "deny"
            }
        """

        token = c.acl.create(rules=rules, token=master_token)
        assert c.acl.info(token)['Rules'] == rules

        token2 = c.acl.clone(token, token=master_token)
        assert c.acl.info(token2)['Rules'] == rules

        assert c.acl.update(token2, name='Foo', token=master_token) == token2
        assert c.acl.info(token2)['Name'] == 'Foo'

        assert c.acl.destroy(token2, token=master_token) is True
        assert c.acl.info(token2) is None

        c.kv.put('foo', 'bar')
        c.kv.put('private/foo', 'bar')

        assert c.kv.get('foo', token=token)[1]['Value'] == six.b('bar')
        pytest.raises(
            consul.ACLPermissionDenied, c.kv.put, 'foo', 'bar2', token=token)
        pytest.raises(
            consul.ACLPermissionDenied, c.kv.delete, 'foo', token=token)

        assert c.kv.get('private/foo')[1]['Value'] == six.b('bar')
        assert c.kv.get('private/foo', token=token)[1] is None
        pytest.raises(
            consul.ACLPermissionDenied,
            c.kv.put, 'private/foo', 'bar2', token=token)
        pytest.raises(
            consul.ACLPermissionDenied,
            c.kv.delete, 'private/foo', token=token)

        # clean up
        c.acl.destroy(token, token=master_token)
        acls = c.acl.list(token=master_token)
        assert set([x['ID'] for x in acls]) == \
            set(['anonymous', master_token])

    def test_acl_implicit_token_use(self, acl_consul):
        # configure client to use the master token by default
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
        master_token = acl_consul.token

        acls = c.acl.list()
        assert set([x['ID'] for x in acls]) == \
            set(['anonymous', master_token])

        assert c.acl.info('foo') is None
        compare = [c.acl.info(master_token), c.acl.info('anonymous')]
        compare.sort(key=operator.itemgetter('ID'))
        assert acls == compare

        rules = """
            key "" {
                policy = "read"
            }
            key "private/" {
                policy = "deny"
            }
        """
        token = c.acl.create(rules=rules)
        assert c.acl.info(token)['Rules'] == rules

        token2 = c.acl.clone(token)
        assert c.acl.info(token2)['Rules'] == rules

        assert c.acl.update(token2, name='Foo') == token2
        assert c.acl.info(token2)['Name'] == 'Foo'

        assert c.acl.destroy(token2) is True
        assert c.acl.info(token2) is None

        c.kv.put('foo', 'bar')
        c.kv.put('private/foo', 'bar')

        c_limited = consul.Consul(port=acl_consul.port, token=token)
        assert c_limited.kv.get('foo')[1]['Value'] == six.b('bar')
        pytest.raises(
            consul.ACLPermissionDenied, c_limited.kv.put, 'foo', 'bar2')
        pytest.raises(
            consul.ACLPermissionDenied, c_limited.kv.delete, 'foo')

        assert c.kv.get('private/foo')[1]['Value'] == six.b('bar')
        assert c_limited.kv.get('private/foo')[1] is None
        pytest.raises(
            consul.ACLPermissionDenied,
            c_limited.kv.put, 'private/foo', 'bar2')
        pytest.raises(
            consul.ACLPermissionDenied,
            c_limited.kv.delete, 'private/foo')

        # check we can override the client's default token
        assert c.kv.get('private/foo', token=token)[1] is None
        pytest.raises(
            consul.ACLPermissionDenied,
            c.kv.put, 'private/foo', 'bar2', token=token)
        pytest.raises(
            consul.ACLPermissionDenied,
            c.kv.delete, 'private/foo', token=token)

        # clean up
        c.acl.destroy(token)
        acls = c.acl.list()
        assert set([x['ID'] for x in acls]) == \
            set(['anonymous', master_token])
<|endoftext|>


<commit_before>from eventlet import event, spawn, sleep, patcher
from eventlet.hubs import use_hub, get_hub, _threadlocal
from eventlet.hubs.hub import READ, WRITE
from eventlet.green import zmq
from nose.tools import *
from tests import mock, LimitedTestCase, skip_unless
from unittest import TestCase

from threading import Thread

def using_zmq(_f):
    return 'zeromq' in type(get_hub()).__module__

def skip_unless_zmq(func):
    """ Decorator that skips a test if we're using the pyevent hub."""
    return skip_unless(using_zmq)(func)

class TestUpstreamDownStream(LimitedTestCase):

    def tearDown(self):
        self.clear_up_sockets()
        super(TestUpstreamDownStream, self).tearDown()

    def create_bound_pair(self, type1, type2, interface='tcp://127.0.0.1'):
        """Create a bound socket pair using a random port."""
        self.context = context = get_hub().get_context()
        s1 = context.socket(type1)
        port = s1.bind_to_random_port(interface)
        s2 = context.socket(type2)
        s2.connect('%s:%s' % (interface, port))
        self.sockets = [s1, s2]
        return s1, s2, port

    def clear_up_sockets(self):
        for sock in self.sockets:
            sock.close()

    def assertRaisesErrno(self, errno, func, *args):
        try:
            func(*args)
        except zmq.ZMQError, e:
            self.assertEqual(e.errno, errno, "wrong error raised, expected '%s' \
got '%s'" % (zmq.ZMQError(errno), zmq.ZMQError(e.errno)))
        else:
            self.fail("Function did not raise any error")

    @skip_unless_zmq
    def test_recv_spawned_before_send_is_non_blocking(self):
        req, rep, port = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
#        req.connect(ipc)
#        rep.bind(ipc)
        sleep()
        msg = dict(res=None)
        done = event.Event()
        def rx():
            msg['res'] = rep.recv()
            done.send('done')
        spawn(rx)
        req.send('test')
        done.wait()
        self.assertEqual(msg['res'], 'test')

    @skip_unless_zmq
    def test_close_socket_raises_enotsup(self):
        req, rep, port = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
        rep.close()
        req.close()
        self.assertRaisesErrno(zmq.ENOTSUP, rep.recv)
        self.assertRaisesErrno(zmq.ENOTSUP, req.send, 'test')

    @skip_unless_zmq
    def test_send_1k_req_rep(self):
        req, rep, port = self.create_bound_pair(zmq.REQ, zmq.REP)
        sleep()
        done = event.Event()
        def tx():
            tx_i = 0
            req.send(str(tx_i))
            while req.recv() != 'done':
                tx_i += 1
                req.send(str(tx_i))
        def rx():
            while True:
                rx_i = rep.recv()
                if rx_i == "1000":
                    rep.send('done')
                    sleep()
                    done.send(0)
                    break
                rep.send('i')
        spawn(tx)
        spawn(rx)
        final_i = done.wait()
        self.assertEqual(final_i, 0)

    @skip_unless_zmq
    def test_send_1k_push_pull(self):
        down, up, port = self.create_bound_pair(zmq.PUSH, zmq.PULL)
        sleep()
        done = event.Event()
        def tx():
            tx_i = 0
            while tx_i <= 1000:
                tx_i += 1
                down.send(str(tx_i))
        def rx():
            while True:
                rx_i = up.recv()
                if rx_i == "1000":
                    done.send(0)
                    break
        spawn(tx)
        spawn(rx)
        final_i = done.wait()
        self.assertEqual(final_i, 0)

    @skip_unless_zmq
    def test_send_1k_pub_sub(self):
        pub, sub_all, port = self.create_bound_pair(zmq.PUB, zmq.SUB)
        sub1 = self.context.socket(zmq.SUB)
        sub2 = self.context.socket(zmq.SUB)
        self.sockets.extend([sub1, sub2])
        addr = 'tcp://127.0.0.1:%s' % port
        sub1.connect(addr)
        sub2.connect(addr)
        sub_all.setsockopt(zmq.SUBSCRIBE, '')
        sub1.setsockopt(zmq.SUBSCRIBE, 'sub1')
        sub2.setsockopt(zmq.SUBSCRIBE, 'sub2')
        sub_all_done = event.Event()
        sub1_done = event.Event()
        sub2_done = event.Event()
        sleep(0.2)
        def rx(sock, done_evt, msg_count=10000):
            count = 0
            while count < msg_count:
                msg = sock.recv()
                sleep()
                if 'LAST' in msg:
                    break
                count += 1

            done_evt.send(count)

        def tx(sock):
            for i in range(1, 1001):
                msg = "sub%s %s" % (1 if i % 2 else 2, i)
                sock.send(msg)
                sleep()
            sock.send('sub1 LAST')
            sock.send('sub2 LAST')

        spawn(rx, sub_all, sub_all_done)
        spawn(rx, sub1, sub1_done)
        spawn(rx, sub2, sub2_done)
        spawn(tx, pub)
        sub1_count = sub1_done.wait()
        sub2_count = sub2_done.wait()
        sub_all_count = sub_all_done.wait()
        self.assertEqual(sub1_count, 500)
        self.assertEqual(sub2_count, 500)
        self.assertEqual(sub_all_count, 1000)

    @skip_unless_zmq
    def test_change_subscription(self):
        pub, sub, port = self.create_bound_pair(zmq.PUB, zmq.SUB)
        sub.setsockopt(zmq.SUBSCRIBE, 'test')

        sub_done = event.Event()
        sleep(0.2)
        def rx(sock, done_evt):
            count = 0
            sub = 'test'
            while True:
                msg = sock.recv()
                sleep()
                if 'DONE' in msg:
                    break
                if 'LAST' in msg and sub == 'test':
                    sock.setsockopt(zmq.UNSUBSCRIBE, 'test')
                    sock.setsockopt(zmq.SUBSCRIBE, 'done')
                    sub = 'done'
                    #continue # We don't want to count this message
                count += 1
            done_evt.send(count)

        def tx(sock):
            for i in range(1, 101):
                msg = "test %s" % i
                if i != 50:
                    sock.send(msg)
                else:
                    sock.send('test LAST')
                sleep()
            sock.send('done DONE')

        spawn(rx, sub, sub_done)
        spawn(tx, pub)

        rx_count = sub_done.wait()
        self.assertEqual(rx_count, 50)

class TestThreadedContextAccess(TestCase):
    """zmq's Context must be unique within a hub

    The zeromq API documentation states:
    All zmq sockets passed to the zmq_poll() function must share the same zmq
    context and must belong to the thread calling zmq_poll()

    As zmq_poll is what's eventually being called then we need to insure that
    all sockets that are going to be passed to zmq_poll (via hub.do_poll) are
    in the same context
    """

    @skip_unless_zmq
    def test_threadlocal_context(self):
        hub = get_hub()
        context = hub.get_context()
        self.assertEqual(context, _threadlocal.context)
        next_context = hub.get_context()
        self.assertTrue(context is next_context)

    @skip_unless_zmq
    def test_different_context_in_different_thread(self):
        context = get_hub().get_context()
        test_result = []
        def assert_different(ctx):
            assert not hasattr(_threadlocal, 'hub')
            import os
            os.environ['EVENTLET_HUB'] = 'zeromq'
            hub = get_hub()
            try:
                this_thread_context = hub.get_context()
            except:
                test_result.append('fail')
                raise
            test_result.append(ctx is this_thread_context)
        Thread(target=assert_different, args=(context,)).start()
        count = 0
        while count < 100 and not test_result:
            count += 1
        self.assertFalse(test_result[0])

<commit_msg>Automated merge with https://bitbucket.org/boothead/eventlet<commit_after>from eventlet import event, spawn, sleep, patcher
from eventlet.hubs import use_hub, get_hub, _threadlocal
from eventlet.hubs.hub import READ, WRITE
from eventlet.green import zmq
from nose.tools import *
from tests import mock, LimitedTestCase, skip_unless
from unittest import TestCase

from threading import Thread

def using_zmq(_f):
    return 'zeromq' in type(get_hub()).__module__

def skip_unless_zmq(func):
    """ Decorator that skips a test if we're using the pyevent hub."""
    return skip_unless(using_zmq)(func)

class TestUpstreamDownStream(LimitedTestCase):

    def tearDown(self):
        self.clear_up_sockets()
        super(TestUpstreamDownStream, self).tearDown()

    def create_bound_pair(self, type1, type2, interface='tcp://127.0.0.1'):
        """Create a bound socket pair using a random port."""
        self.context = context = get_hub().get_context()
        s1 = context.socket(type1)
        port = s1.bind_to_random_port(interface)
        s2 = context.socket(type2)
        s2.connect('%s:%s' % (interface, port))
        self.sockets = [s1, s2]
        return s1, s2, port

    def clear_up_sockets(self):
        for sock in self.sockets:
            sock.close()

    def assertRaisesErrno(self, errno, func, *args):
        try:
            func(*args)
        except zmq.ZMQError, e:
            self.assertEqual(e.errno, errno, "wrong error raised, expected '%s' \
got '%s'" % (zmq.ZMQError(errno), zmq.ZMQError(e.errno)))
        else:
            self.fail("Function did not raise any error")

    @skip_unless_zmq
    def test_recv_spawned_before_send_is_non_blocking(self):
        req, rep, port = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
#        req.connect(ipc)
#        rep.bind(ipc)
        sleep()
        msg = dict(res=None)
        done = event.Event()
        def rx():
            msg['res'] = rep.recv()
            done.send('done')
        spawn(rx)
        req.send('test')
        done.wait()
        self.assertEqual(msg['res'], 'test')

    @skip_unless_zmq
    def test_close_socket_raises_enotsup(self):
        req, rep, port = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
        rep.close()
        req.close()
        self.assertRaisesErrno(zmq.ENOTSUP, rep.recv)
        self.assertRaisesErrno(zmq.ENOTSUP, req.send, 'test')

    @skip_unless_zmq
    def test_send_1k_req_rep(self):
        req, rep, port = self.create_bound_pair(zmq.REQ, zmq.REP)
        sleep()
        done = event.Event()
        def tx():
            tx_i = 0
            req.send(str(tx_i))
            while req.recv() != 'done':
                tx_i += 1
                req.send(str(tx_i))
        def rx():
            while True:
                rx_i = rep.recv()
                if rx_i == "1000":
                    rep.send('done')
                    sleep()
                    done.send(0)
                    break
                rep.send('i')
        spawn(tx)
        spawn(rx)
        final_i = done.wait()
        self.assertEqual(final_i, 0)

    @skip_unless_zmq
    def test_send_1k_push_pull(self):
        down, up, port = self.create_bound_pair(zmq.PUSH, zmq.PULL)
        sleep()
        done = event.Event()
        def tx():
            tx_i = 0
            while tx_i <= 1000:
                tx_i += 1
                down.send(str(tx_i))
        def rx():
            while True:
                rx_i = up.recv()
                if rx_i == "1000":
                    done.send(0)
                    break
        spawn(tx)
        spawn(rx)
        final_i = done.wait()
        self.assertEqual(final_i, 0)

    @skip_unless_zmq
    def test_send_1k_pub_sub(self):
        pub, sub_all, port = self.create_bound_pair(zmq.PUB, zmq.SUB)
        sub1 = self.context.socket(zmq.SUB)
        sub2 = self.context.socket(zmq.SUB)
        self.sockets.extend([sub1, sub2])
        addr = 'tcp://127.0.0.1:%s' % port
        sub1.connect(addr)
        sub2.connect(addr)
        sub_all.setsockopt(zmq.SUBSCRIBE, '')
        sub1.setsockopt(zmq.SUBSCRIBE, 'sub1')
        sub2.setsockopt(zmq.SUBSCRIBE, 'sub2')
        sub_all_done = event.Event()
        sub1_done = event.Event()
        sub2_done = event.Event()
        sleep(0.2)
        def rx(sock, done_evt, msg_count=10000):
            count = 0
            while count < msg_count:
                msg = sock.recv()
                sleep()
                if 'LAST' in msg:
                    break
                count += 1

            done_evt.send(count)

        def tx(sock):
            for i in range(1, 1001):
                msg = "sub%s %s" % (1 if i % 2 else 2, i)
                sock.send(msg)
                sleep()
            sock.send('sub1 LAST')
            sock.send('sub2 LAST')

        spawn(rx, sub_all, sub_all_done)
        spawn(rx, sub1, sub1_done)
        spawn(rx, sub2, sub2_done)
        spawn(tx, pub)
        sub1_count = sub1_done.wait()
        sub2_count = sub2_done.wait()
        sub_all_count = sub_all_done.wait()
        self.assertEqual(sub1_count, 500)
        self.assertEqual(sub2_count, 500)
        self.assertEqual(sub_all_count, 1000)

    @skip_unless_zmq
    def test_change_subscription(self):
        pub, sub, port = self.create_bound_pair(zmq.PUB, zmq.SUB)
        sub.setsockopt(zmq.SUBSCRIBE, 'test')

        sub_done = event.Event()
        sleep(0.2)
        def rx(sock, done_evt):
            count = 0
            sub = 'test'
            while True:
                msg = sock.recv()
                sleep()
                if 'DONE' in msg:
                    break
                if 'LAST' in msg and sub == 'test':
                    sock.setsockopt(zmq.UNSUBSCRIBE, 'test')
                    sock.setsockopt(zmq.SUBSCRIBE, 'done')
                    sub = 'done'
                    #continue # We don't want to count this message
                count += 1
            done_evt.send(count)

        def tx(sock):
            for i in range(1, 101):
                msg = "test %s" % i
                if i != 50:
                    sock.send(msg)
                else:
                    sock.send('test LAST')
                sleep()
            sock.send('done DONE')

        spawn(rx, sub, sub_done)
        spawn(tx, pub)

        rx_count = sub_done.wait()
        self.assertEqual(rx_count, 50)

class TestThreadedContextAccess(TestCase):
    """zmq's Context must be unique within a hub

    The zeromq API documentation states:
    All zmq sockets passed to the zmq_poll() function must share the same zmq
    context and must belong to the thread calling zmq_poll()

    As zmq_poll is what's eventually being called then we need to insure that
    all sockets that are going to be passed to zmq_poll (via hub.do_poll) are
    in the same context
    """

    @skip_unless_zmq
    def test_threadlocal_context(self):
        hub = get_hub()
        context = hub.get_context()
        self.assertEqual(context, _threadlocal.context)
        next_context = hub.get_context()
        self.assertTrue(context is next_context)

    @skip_unless_zmq
    def test_different_context_in_different_thread(self):
        context = get_hub().get_context()
        test_result = []
        def assert_different(ctx):
#            assert not hasattr(_threadlocal, 'hub')
#            import os
#            os.environ['EVENTLET_HUB'] = 'zeromq'
            hub = get_hub()
            try:
                this_thread_context = hub.get_context()
            except:
                test_result.append('fail')
                raise
            test_result.append(ctx is this_thread_context)
        Thread(target=assert_different, args=(context,)).start()
        while not test_result:
            sleep(0.1)
        self.assertFalse(test_result[0])

<|endoftext|>


<commit_before># coding=utf-8
from __future__ import unicode_literals

import pytz

from datetime import date, datetime
from django.core import mail
from django.http import HttpRequest
from django.test import override_settings
from enum import Enum
from hypothesis import given
import hypothesis.strategies as st
from uuid import UUID

from casepro.test import BaseCasesTest

from . import safe_max, normalize, match_keywords, truncate, str_to_bool, json_encode, TimelineItem, uuid_to_int
from . import date_to_milliseconds, datetime_to_microseconds, microseconds_to_datetime, month_range, date_range
from . import get_language_name, validate_urn_as_e164, InvalidURN
from .email import send_email
from .middleware import JSONMiddleware

class UtilsTest(BaseCasesTest):

    def test_safe_max(self):
        self.assertEqual(safe_max(1, 2, 3), 3)
        self.assertEqual(safe_max(None, 2, None), 2)
        self.assertEqual(safe_max(None, None), None)
        self.assertEqual(safe_max(date(2012, 3, 6), date(2012, 5, 2), None), date(2012, 5, 2))

    def test_normalize(self):
        self.assertEqual(normalize("Mary  had\ta little lamb"), "mary had a little lamb")  # remove multiple spaces
        self.assertEqual(normalize("Gar\u00e7on"), "garc\u0327on")  # decomposed combined unicode chars (U+E7 = ç)

    def test_match_keywords(self):
        text = "Mary had a little lamb"
        self.assertFalse(match_keywords(text, []))
        self.assertFalse(match_keywords(text, ['sheep']))
        self.assertFalse(match_keywords(text, ['lambburger']))  # complete word matches only

        self.assertTrue(match_keywords(text, ['mary']))  # case-insensitive and start of string
        self.assertTrue(match_keywords(text, ['lamb']))  # end of string
        self.assertTrue(match_keywords(text, ['big', 'little']))  # one match, one mis-match
        self.assertTrue(match_keywords(text, ['little lamb']))  # spaces ok

    def test_truncate(self):
        self.assertEqual(truncate("Hello World", 8), "Hello...")
        self.assertEqual(truncate("Hello World", 8, suffix="_"), "Hello W_")
        self.assertEqual(truncate("Hello World", 98), "Hello World")

    def test_str_to_bool(self):
        self.assertFalse(str_to_bool("0"))
        self.assertFalse(str_to_bool("fALSe"))
        self.assertFalse(str_to_bool("N"))
        self.assertFalse(str_to_bool("No"))
        self.assertFalse(str_to_bool("x"))

        self.assertTrue(str_to_bool("1"))
        self.assertTrue(str_to_bool("TrUE"))
        self.assertTrue(str_to_bool("Y"))
        self.assertTrue(str_to_bool("YeS"))

    def test_date_to_milliseconds(self):
        self.assertEqual(date_to_milliseconds(date(2015, 1, 1)), 1420070400000)
        self.assertEqual(date_to_milliseconds(date(2015, 2, 1)), 1422748800000)

    def test_microseconds_to_datetime(self):
        d1 = datetime(2015, 10, 9, 14, 48, 30, 123456, tzinfo=pytz.utc).astimezone(pytz.timezone("Africa/Kigali"))
        ms = datetime_to_microseconds(d1)
        d2 = microseconds_to_datetime(ms)
        self.assertEqual(d2, datetime(2015, 10, 9, 14, 48, 30, 123456, tzinfo=pytz.utc))

    def test_json_encode(self):
        class MyEnum(Enum):
            bar = 1

        class MyClass(object):
            def to_json(self):
                return dict(bar='X')

        data = [
            "string",
            datetime(2015, 10, 9, 14, 48, 30, 123456, tzinfo=pytz.utc).astimezone(pytz.timezone("Africa/Kigali")),
            MyEnum.bar,
            MyClass()
        ]

        self.assertEqual(json_encode(data), '["string", "2015-10-09T14:48:30.123456", "bar", {"bar": "X"}]')

    def test_month_range(self):
        d1 = datetime(2015, 10, 9, 14, 48, 30, 123456, tzinfo=pytz.utc).astimezone(pytz.timezone("Africa/Kigali"))

        self.assertEqual(month_range(0, now=d1), (datetime(2015, 10, 1, 0, 0, 0, 0, pytz.UTC),
                                                  datetime(2015, 11, 1, 0, 0, 0, 0, pytz.UTC)))
        self.assertEqual(month_range(1, now=d1), (datetime(2015, 11, 1, 0, 0, 0, 0, pytz.UTC),
                                                  datetime(2015, 12, 1, 0, 0, 0, 0, pytz.UTC)))
        self.assertEqual(month_range(-1, now=d1), (datetime(2015, 9, 1, 0, 0, 0, 0, pytz.UTC),
                                                   datetime(2015, 10, 1, 0, 0, 0, 0, pytz.UTC)))

    def test_date_range(self):
        self.assertEqual(list(date_range(date(2015, 1, 29), date(2015, 2, 2))), [
            date(2015, 1, 29),
            date(2015, 1, 30),
            date(2015, 1, 31),
            date(2015, 2, 1)
        ])
        self.assertEqual(list(date_range(date(2015, 1, 29), date(2015, 1, 29))), [])

    def test_timeline_item(self):
        d1 = datetime(2015, 10, 1, 9, 0, 0, 0, pytz.UTC)
        ann = self.create_contact(self.unicef, 'C-101', "Ann")
        msg = self.create_message(self.unicef, 102, ann, "Hello", created_on=d1)
        self.assertEqual(TimelineItem(msg).to_json(), {'time': d1, 'type': 'I', 'item': msg.as_json()})

    def test_uuid_to_int_range(self):
        """
        Ensures that the integer returned will always be in the range [0, 2147483647].
        """
        self.assertEqual(uuid_to_int(UUID(int=(2147483647)).hex), 2147483647)
        self.assertEqual(uuid_to_int(UUID(int=(2147483648)).hex), 0)

    @given(st.uuids())
    def test_uuid_to_int_property(self, uuid):
        """
        Property based testing to ensure that the output of the function is always within the limits.
        """
        self.assertTrue(uuid_to_int(uuid.hex) <= 2147483647)
        self.assertTrue(uuid_to_int(uuid.hex) >= 0)

    def test_get_language_name(self):
        self.assertEqual(get_language_name('fre'), "French")
        self.assertEqual(get_language_name('fre'), "French")  # from cache
        self.assertEqual(get_language_name('cpe'), "Creoles and pidgins, English based")

        # should strip off anything after an open paren or semicolon
        self.assertEqual(get_language_name('arc'), "Official Aramaic")

        self.assertIsNone(get_language_name('xxxxx'))

    def test_validate_urn_as_e164(self):
        self.assertRaises(InvalidURN, validate_urn_as_e164, '0825550011')
        self.assertRaises(InvalidURN, validate_urn_as_e164, '0027825550011')
        self.assertRaises(InvalidURN, validate_urn_as_e164, '027825550011')
        self.assertRaises(InvalidURN, validate_urn_as_e164, '0027005550011')
        self.assertRaises(InvalidURN, validate_urn_as_e164, '0027825550011445566')
        self.assertTrue(validate_urn_as_e164('+27825552233'))

class EmailTest(BaseCasesTest):
    @override_settings(SEND_EMAILS=True)
    def test_send_email(self):
        send_email([self.user1, 'bob@unicef.org'], "Subject", 'utils/email/export',
                   {'download_url': 'http://example.com/export/1/'})

        self.assertEqual(len(mail.outbox), 2)
        self.assertEqual(mail.outbox[0].to, ["evan@unicef.org"])
        self.assertEqual(mail.outbox[0].subject, "Subject")
        self.assertEqual(mail.outbox[1].to, ["bob@unicef.org"])

class MiddlewareTest(BaseCasesTest):
    def test_json(self):
        middleware = JSONMiddleware()

        # test with no content type header
        request = HttpRequest()
        request._body = '{"a":["b"]}'
        middleware.process_request(request)
        self.assertFalse(hasattr(request, 'json'))

        # test with JSON content type header
        request = HttpRequest()
        request._body = '{"a":["b"]}'
        request.META = {'CONTENT_TYPE': "application/json;charset=UTF-8"}
        middleware.process_request(request)
        self.assertEqual(request.json, {'a': ["b"]})

        # test with non-JSON content type header
        request = HttpRequest()
        request._body = ''
        request.META = {'CONTENT_TYPE': "text/html"}
        middleware.process_request(request)
        self.assertFalse(hasattr(request, 'json'))

class ViewsTest(BaseCasesTest):
    def test_partials(self):
        response = self.url_get('unicef', '/partials/modal_confirm.html')
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "[[ title ]]")
<commit_msg>Improved tests for validating urns<commit_after># coding=utf-8
from __future__ import unicode_literals

import pytz

from datetime import date, datetime
from django.core import mail
from django.http import HttpRequest
from django.test import override_settings
from enum import Enum
from hypothesis import given
import hypothesis.strategies as st
from uuid import UUID

from casepro.test import BaseCasesTest

from . import safe_max, normalize, match_keywords, truncate, str_to_bool, json_encode, TimelineItem, uuid_to_int
from . import date_to_milliseconds, datetime_to_microseconds, microseconds_to_datetime, month_range, date_range
from . import get_language_name, validate_urn_as_e164, validate_urn, InvalidURN
from .email import send_email
from .middleware import JSONMiddleware

class UtilsTest(BaseCasesTest):

    def test_safe_max(self):
        self.assertEqual(safe_max(1, 2, 3), 3)
        self.assertEqual(safe_max(None, 2, None), 2)
        self.assertEqual(safe_max(None, None), None)
        self.assertEqual(safe_max(date(2012, 3, 6), date(2012, 5, 2), None), date(2012, 5, 2))

    def test_normalize(self):
        self.assertEqual(normalize("Mary  had\ta little lamb"), "mary had a little lamb")  # remove multiple spaces
        self.assertEqual(normalize("Gar\u00e7on"), "garc\u0327on")  # decomposed combined unicode chars (U+E7 = ç)

    def test_match_keywords(self):
        text = "Mary had a little lamb"
        self.assertFalse(match_keywords(text, []))
        self.assertFalse(match_keywords(text, ['sheep']))
        self.assertFalse(match_keywords(text, ['lambburger']))  # complete word matches only

        self.assertTrue(match_keywords(text, ['mary']))  # case-insensitive and start of string
        self.assertTrue(match_keywords(text, ['lamb']))  # end of string
        self.assertTrue(match_keywords(text, ['big', 'little']))  # one match, one mis-match
        self.assertTrue(match_keywords(text, ['little lamb']))  # spaces ok

    def test_truncate(self):
        self.assertEqual(truncate("Hello World", 8), "Hello...")
        self.assertEqual(truncate("Hello World", 8, suffix="_"), "Hello W_")
        self.assertEqual(truncate("Hello World", 98), "Hello World")

    def test_str_to_bool(self):
        self.assertFalse(str_to_bool("0"))
        self.assertFalse(str_to_bool("fALSe"))
        self.assertFalse(str_to_bool("N"))
        self.assertFalse(str_to_bool("No"))
        self.assertFalse(str_to_bool("x"))

        self.assertTrue(str_to_bool("1"))
        self.assertTrue(str_to_bool("TrUE"))
        self.assertTrue(str_to_bool("Y"))
        self.assertTrue(str_to_bool("YeS"))

    def test_date_to_milliseconds(self):
        self.assertEqual(date_to_milliseconds(date(2015, 1, 1)), 1420070400000)
        self.assertEqual(date_to_milliseconds(date(2015, 2, 1)), 1422748800000)

    def test_microseconds_to_datetime(self):
        d1 = datetime(2015, 10, 9, 14, 48, 30, 123456, tzinfo=pytz.utc).astimezone(pytz.timezone("Africa/Kigali"))
        ms = datetime_to_microseconds(d1)
        d2 = microseconds_to_datetime(ms)
        self.assertEqual(d2, datetime(2015, 10, 9, 14, 48, 30, 123456, tzinfo=pytz.utc))

    def test_json_encode(self):
        class MyEnum(Enum):
            bar = 1

        class MyClass(object):
            def to_json(self):
                return dict(bar='X')

        data = [
            "string",
            datetime(2015, 10, 9, 14, 48, 30, 123456, tzinfo=pytz.utc).astimezone(pytz.timezone("Africa/Kigali")),
            MyEnum.bar,
            MyClass()
        ]

        self.assertEqual(json_encode(data), '["string", "2015-10-09T14:48:30.123456", "bar", {"bar": "X"}]')

    def test_month_range(self):
        d1 = datetime(2015, 10, 9, 14, 48, 30, 123456, tzinfo=pytz.utc).astimezone(pytz.timezone("Africa/Kigali"))

        self.assertEqual(month_range(0, now=d1), (datetime(2015, 10, 1, 0, 0, 0, 0, pytz.UTC),
                                                  datetime(2015, 11, 1, 0, 0, 0, 0, pytz.UTC)))
        self.assertEqual(month_range(1, now=d1), (datetime(2015, 11, 1, 0, 0, 0, 0, pytz.UTC),
                                                  datetime(2015, 12, 1, 0, 0, 0, 0, pytz.UTC)))
        self.assertEqual(month_range(-1, now=d1), (datetime(2015, 9, 1, 0, 0, 0, 0, pytz.UTC),
                                                   datetime(2015, 10, 1, 0, 0, 0, 0, pytz.UTC)))

    def test_date_range(self):
        self.assertEqual(list(date_range(date(2015, 1, 29), date(2015, 2, 2))), [
            date(2015, 1, 29),
            date(2015, 1, 30),
            date(2015, 1, 31),
            date(2015, 2, 1)
        ])
        self.assertEqual(list(date_range(date(2015, 1, 29), date(2015, 1, 29))), [])

    def test_timeline_item(self):
        d1 = datetime(2015, 10, 1, 9, 0, 0, 0, pytz.UTC)
        ann = self.create_contact(self.unicef, 'C-101', "Ann")
        msg = self.create_message(self.unicef, 102, ann, "Hello", created_on=d1)
        self.assertEqual(TimelineItem(msg).to_json(), {'time': d1, 'type': 'I', 'item': msg.as_json()})

    def test_uuid_to_int_range(self):
        """
        Ensures that the integer returned will always be in the range [0, 2147483647].
        """
        self.assertEqual(uuid_to_int(UUID(int=(2147483647)).hex), 2147483647)
        self.assertEqual(uuid_to_int(UUID(int=(2147483648)).hex), 0)

    @given(st.uuids())
    def test_uuid_to_int_property(self, uuid):
        """
        Property based testing to ensure that the output of the function is always within the limits.
        """
        self.assertTrue(uuid_to_int(uuid.hex) <= 2147483647)
        self.assertTrue(uuid_to_int(uuid.hex) >= 0)

    def test_get_language_name(self):
        self.assertEqual(get_language_name('fre'), "French")
        self.assertEqual(get_language_name('fre'), "French")  # from cache
        self.assertEqual(get_language_name('cpe'), "Creoles and pidgins, English based")

        # should strip off anything after an open paren or semicolon
        self.assertEqual(get_language_name('arc'), "Official Aramaic")

        self.assertIsNone(get_language_name('xxxxx'))

    def test_validate_urn_as_e164(self):
        self.assertRaises(InvalidURN, validate_urn_as_e164, '0825550011')  # lacks country code
        self.assertRaises(InvalidURN, validate_urn_as_e164, '(+27)825550011')  # incorrect format (E.123)
        self.assertRaises(InvalidURN, validate_urn_as_e164, '+278255500abc')  # incorrect format
        self.assertRaises(InvalidURN, validate_urn_as_e164, '+278255500115555555')  # too long
        self.assertTrue(validate_urn_as_e164('+27825552233'))

    def test_validate_urn(self):
        self.assertTrue(validate_urn('tel:+27825552233'))
        self.assertRaises(InvalidURN, validate_urn, 'tel:0825550011')
        self.assertTrue(validate_urn('unknown_scheme:address_for_unknown_scheme'))

class EmailTest(BaseCasesTest):
    @override_settings(SEND_EMAILS=True)
    def test_send_email(self):
        send_email([self.user1, 'bob@unicef.org'], "Subject", 'utils/email/export',
                   {'download_url': 'http://example.com/export/1/'})

        self.assertEqual(len(mail.outbox), 2)
        self.assertEqual(mail.outbox[0].to, ["evan@unicef.org"])
        self.assertEqual(mail.outbox[0].subject, "Subject")
        self.assertEqual(mail.outbox[1].to, ["bob@unicef.org"])

class MiddlewareTest(BaseCasesTest):
    def test_json(self):
        middleware = JSONMiddleware()

        # test with no content type header
        request = HttpRequest()
        request._body = '{"a":["b"]}'
        middleware.process_request(request)
        self.assertFalse(hasattr(request, 'json'))

        # test with JSON content type header
        request = HttpRequest()
        request._body = '{"a":["b"]}'
        request.META = {'CONTENT_TYPE': "application/json;charset=UTF-8"}
        middleware.process_request(request)
        self.assertEqual(request.json, {'a': ["b"]})

        # test with non-JSON content type header
        request = HttpRequest()
        request._body = ''
        request.META = {'CONTENT_TYPE': "text/html"}
        middleware.process_request(request)
        self.assertFalse(hasattr(request, 'json'))

class ViewsTest(BaseCasesTest):
    def test_partials(self):
        response = self.url_get('unicef', '/partials/modal_confirm.html')
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "[[ title ]]")
<|endoftext|>