Esempio n. 1
0
def test_export():

    # Test using some icons over which I have some control
    B = 'https://raw.githubusercontent.com/pyzo/pyzo/master/pyzo/resources/appicons/'

    for name in ['pyzologo', 'py']:
        icon = Icon(B + name + '.ico')
        assert len(icon.image_sizes()) > 0

        # Export png
        filename = os.path.join(tempdir, name + '.png')
        icon.write(filename)
        for i in icon.image_sizes():
            assert os.path.isfile(os.path.join(tempdir, name + '%i.png' % i))

        # Export bmp
        filename = os.path.join(tempdir, name + '.bmp')
        icon.write(filename)
        for i in icon.image_sizes():
            assert os.path.isfile(os.path.join(tempdir, name + '%i.bmp' % i))

        # Failures ..

        with raises(TypeError):
            icon.write(3)

        with raises(TypeError):
            icon.write([])

        if sys.version_info[0] > 2:
            with raises(TypeError):
                icon.write(filename.encode())

        with raises(ValueError):
            icon.write(os.path.join(tempdir, name + '.foo'))
Esempio n. 2
0
def test_read_wrong():
    with raises(TypeError):
        Icon(4)

    with raises(IOError):
        Icon('file does not exist')

    with raises(IOError):
        Icon('http://url does not exist')

    with raises(TypeError):
        Icon(['no', 'lists'])

    if sys.version_info[0] > 2:
        with raises(TypeError):
            Icon(b'not a filename')
Esempio n. 3
0
def test_option_spec_fail():

    # ok
    Config('aa', foo=(3, int, ''))

    with raises(ValueError):
        Config('aa', _foo=(3, int, ''))

    for spec in [
        (),  # too short
        (3, int),  # still too short
        (3, int, 'docs', None),  # too long
        (3, None, 'docs'),  # type is not a type
        ('', set, 'docs'),  # type is not supported
        ('3,3', [], 'docs'),  # tuple type needs one element
        ('3,3', [int, int], 'docs'),  # not two
        ('3,3', [set], 'docs'),  # and must be supported
    ]:
        with raises(ValueError):
            Config('aa', foo=spec)
Esempio n. 4
0
def test_config_name():

    # Empty config
    c = Config('aa')
    assert len(c) == 0

    # ok
    c = Config('AA')

    with raises(TypeError):
        Config()

    with raises(ValueError):
        Config(3)

    with raises(ValueError):
        Config('0aa')

    with raises(ValueError):
        Config('_aa')
Esempio n. 5
0
def test_access():

    c = Config('testconfig', foo=(1, int, ''), BAR=(1, int, ''))
    assert len(c) == 2

    c.foo = 3
    c.BAR = 4
    assert c['foo'] == 3
    assert c['BAR'] == 4
    c['foO'] = 30
    c['BAr'] = 40
    assert c['FOO'] == 30
    assert c['bar'] == 40

    with raises(AttributeError):
        c.FOO
    with raises(AttributeError):
        c.bar
    with raises(TypeError):
        c[3]
    with raises(IndexError):
        c['optiondoesnotexist']
    with raises(TypeError):
        c[3] = ''
    with raises(IndexError):
        c['optiondoesnotexist'] = ''
Esempio n. 6
0
def test_bool():
    c = Config('testconfig', foo=(True, bool, ''), bar=(False, bool, ''))
    assert c.foo == True
    c.foo = True
    assert c.foo == True
    c.foo = False
    assert c.foo == False

    for name in 'yes on true Yes On TRUE 1'.split(' '):
        c.foo = name
        assert c.foo == True
    for name in 'no off fAlse No Off FALSE 0'.split(' '):
        c.foo = name
        assert c.foo == False

    for name in 'none ok bla asdasdasd cancel'.split(' '):
        with raises(ValueError):
            c.foo = name

    for val in (1, 2, [2], None, 0, 0.0, 1.0, []):
        with raises(ValueError):
            c.foo = val
Esempio n. 7
0
def test_int():
    c = Config('testconfig', foo=(1, int, ''), bar=('1', int, ''))
    assert c.foo == 1
    assert c.bar == 1

    c.foo = 12.1
    assert c.foo == 12
    c.foo = '7'
    assert c.foo == 7
    c.foo = '-23'
    assert c.foo == -23

    for val in ([], None, '1e2', '12.1', 'a'):
        with raises(ValueError):
            c.foo = val
Esempio n. 8
0
def test_float():
    c = Config('testconfig', foo=(1, float, ''), bar=('1', float, ''))
    assert c.foo == 1.0
    assert c.bar == 1.0

    c.foo = 3
    assert c.foo == 3.0
    c.foo = -3.1
    assert c.foo == -3.1
    c.foo = '2e3'
    assert c.foo == 2000.0
    c.foo = '12.12'
    assert c.foo == 12.12

    for val in ([], None, 'a', '0a'):
        with raises(ValueError):
            c.foo = val
Esempio n. 9
0
def test_reading():

    # # Read using filename (also as unicode)
    # for i in range(5):
    #     filename = os.path.join(tempdir, 'test%i.png' % i)
    #     if sys.version_info[0] == 2 and i > 2:
    #         filename = unicode(filename)
    #     im, shape = read_png(filename)
    #     assert isinstance(im, bytearray)
    #     assert shape[:len(shapes[i])] == shapes[i]
    #     assert im == ims[i]

    # Read using file object
    for i in range(5):
        filename = os.path.join(tempdir, 'test%i.png' % i)
        with open(filename, 'rb') as f:
            im, shape = read_png(f)
        assert isinstance(im, bytearray)
        if len(shapes[i]) == 2:
            assert shape == shapes[i] + (3, )
            assert im[::3] == ims[i]
        else:
            assert shape == shapes[i]
            assert im == ims[i]

    # Read using binary blob
    for i in range(5):
        filename = os.path.join(tempdir, 'test%i.png' % i)
        with open(filename, 'rb') as f:
            blob = f.read()
            im, shape = read_png(blob)
        if len(shapes[i]) == 2:
            assert shape == shapes[i] + (3, )
            assert im[::3] == ims[i]
        else:
            assert shape == shapes[i]
            assert im == ims[i]

    with raises((RuntimeError, TypeError)):  # RuntimeError on legacy py
        read_png('not_a_png_blob.png')
    with raises(TypeError):
        read_png([])
    with raises(RuntimeError):
        read_png(b'xxxxxxxxxxxxxxxxxxxx\x0axx')
    with raises(RuntimeError):
        read_png(b'\x00')

    if sys.version_info > (3, ):
        # seen as filenames on legacy py
        with raises(RuntimeError):
            read_png(b'')
        with raises(RuntimeError):
            read_png(b'xxxxxxxxxxxxxxxxxxxx')
Esempio n. 10
0
def test_with_numpy():

    if np is None:
        skip('Numpy not available')

    im = np.random.normal(100, 50, (100, 100, 1)).astype(np.float32)
    with raises(TypeError):  # need uint8
        write_png(im)

    im = np.ones((100, 100, 1), np.uint8) * (7 * 16 + 7)
    blob = write_png(im)
    assert blob == write_png(im0, shape0)

    im = np.random.normal(100, 50, (100, 100)).astype(np.uint8)
    blob = write_png(im)

    im_bytes, shape = read_png(blob)
    assert isinstance(im_bytes, (bytearray, bytes))
    im_check = np.frombuffer(im_bytes, 'uint8').reshape(shape)
    assert im_check.shape == im.shape + (3, )
    for i in range(3):
        assert (im_check[:, :, i] == im).all()
Esempio n. 11
0
def test_tuple():
    c = Config('testconfig',
               foo=('1,2', [int], ''),
               bar=((1, 2, 3), [str], ''))
    assert c.foo == (1, 2)
    assert c.bar == ('1', '2', '3')

    c.foo = 1.2, 3.3, 5
    assert c.foo == (1, 3, 5)
    c.foo = '(7, 8, 9)'
    assert c.foo == (7, 8, 9)
    c.foo = '1,               2,-3,4'
    assert c.foo == (1, 2, -3, 4)
    c.foo = [1, '2']
    assert c.foo == (1, 2)

    for val in ([[]], [None], ['a'], ['0a'], ['1.2'], 3):
        with raises(ValueError):
            c.foo = val

    c.bar = 'hello,  there,     you '
    assert c.bar == ('hello', 'there', 'you')
    c.bar = [1, '2']
    assert c.bar == ('1', '2')
Esempio n. 12
0
def test_writing_failures():

    with raises(ValueError):
        write_png([1, 2, 3, 4], (2, 2))

    with raises(ValueError):
        write_png(b'x' * 10)

    with raises(ValueError):
        write_png(b'x' * 10, (3, 3))

    write_png(b'x' * 12, (2, 2, 3))

    with raises(ValueError):
        write_png(b'x' * 8, (2, 2, 2))

    with raises(ValueError):
        write_png(b'x' * 20, (2, 2, 5))

    with raises(ValueError):
        write_png(b'x' * 13, (2, 2, 3))
Esempio n. 13
0
def test_read_file():

    # Prepare config files
    filename1 = os.path.join(tempfile.gettempdir(),
                             'webruntime_config_test1.cfg')
    with open(filename1, 'wb') as f:
        f.write(SAMPLE1.encode())
    filename2 = os.path.join(tempfile.gettempdir(),
                             'webruntime_config_test2.cfg')
    with open(filename2, 'wb') as f:
        f.write(SAMPLE2.encode())
    filename3 = os.path.join(tempfile.gettempdir(),
                             'webruntime_config_test3.cfg')
    with open(filename3, 'wb') as f:
        f.write(SAMPLE3.encode())
    filename4 = os.path.join(tempfile.gettempdir(),
                             'webruntime_config_test4.cfg')
    with open(filename4, 'wb') as f:
        f.write(b'\x00\xff')

    # Config without sources
    c = Config('testconfig',
               foo=(False, bool, ''),
               bar=(1, int, ''),
               spam=(0.0, float, ''),
               eggs=('', str, ''))
    assert c.foo == False
    assert c.bar == 1

    # Config with filename, implicit section
    c = Config('testconfig',
               filename1,
               foo=(False, bool, ''),
               bar=(1, int, ''),
               spam=(0.0, float, ''),
               eggs=('', str, ''))
    assert c.foo == True
    assert c.bar == 3
    assert c.eggs == 'bla bla'

    # Config with filename, explicit section
    c = Config('testconfig',
               filename2,
               foo=(False, bool, ''),
               bar=(1, int, ''),
               spam=(0.0, float, ''),
               eggs=('', str, ''))
    assert c.foo == True
    assert c.bar == 4
    assert c.eggs == 'bla bla bla'

    # Config with string, implicit section
    c = Config('testconfig',
               SAMPLE1,
               foo=(False, bool, ''),
               bar=(1, int, ''),
               spam=(0.0, float, ''),
               eggs=('', str, ''))
    assert c.foo == True
    assert c.bar == 3
    assert c.eggs == 'bla bla'

    # Config with string, explicit section
    c = Config('testconfig',
               SAMPLE2,
               foo=(False, bool, ''),
               bar=(1, int, ''),
               spam=(0.0, float, ''),
               eggs=('', str, ''))
    assert c.foo == True
    assert c.bar == 4
    assert c.eggs == 'bla bla bla'

    # Config with string, implicit section, different name
    c = Config('aaaa',
               SAMPLE1,
               foo=(False, bool, ''),
               bar=(1, int, ''),
               spam=(0.0, float, ''),
               eggs=('', str, ''))
    assert c.foo == True
    assert c.bar == 3

    # Config with string, explicit section, different name (no section match)
    c = Config('aaaa',
               SAMPLE2,
               foo=(False, bool, ''),
               bar=(1, int, ''),
               spam=(0.0, float, ''),
               eggs=('', str, ''))
    assert c.foo == False
    assert c.bar == 1

    # Config with both, and filenames can be nonexistent
    c = Config('testconfig',
               SAMPLE1,
               filename2,
               filename1 + '.cfg',
               foo=(False, bool, ''),
               bar=(1, int, ''),
               spam=(0.0, float, ''),
               eggs=('', str, ''))
    assert c.bar == 4
    #
    c = Config('testconfig',
               filename2,
               filename1 + '.cfg',
               SAMPLE1,
               foo=(False, bool, ''),
               bar=(1, int, ''),
               spam=(0.0, float, ''),
               eggs=('', str, ''))
    assert c.bar == 3

    # Config from invalid string is ignored (logged)
    c = Config('testconfig', SAMPLE3, bar=(1, int, ''))
    assert c.bar == 1

    # Config from invalid file is ignored (logged)
    c = Config('testconfig', filename3, bar=(1, int, ''))
    assert c.bar == 1

    # Config from invalid unidocde file is ignored (logged)
    c = Config('testconfig', filename4, bar=(1, int, ''))
    assert c.bar == 1

    # Fails
    with raises(ValueError):
        c = Config('testconfig', [])
    with raises(ValueError):
        c = Config('testconfig', 3)