[Zope3-checkins] CVS: Zope3/src/zope/products/image/tests - __init__.py:1.1.2.1 test_image.py:1.1.2.1 test_imagedata.py:1.1.2.1 test_imageupload.py:1.1.2.1

Philipp von Weitershausen philikon at philikon.de
Wed Feb 11 11:29:25 EST 2004


Update of /cvs-repository/Zope3/src/zope/products/image/tests
In directory cvs.zope.org:/tmp/cvs-serv21715/image/tests

Added Files:
      Tag: philikon-movecontent-branch
	__init__.py test_image.py test_imagedata.py 
	test_imageupload.py 
Log Message:
Get rid of zope.products.content and zope.products.codecontent and move
content components in their own packages at zope.products.

See the package geddon proposal: http://dev.zope.org/Zope3/PackageGeddon


=== Added File Zope3/src/zope/products/image/tests/__init__.py ===
#
# This file is necessary to make this directory a package.


=== Added File Zope3/src/zope/products/image/tests/test_image.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""

$Id: test_image.py,v 1.1.2.1 2004/02/11 16:29:24 philikon Exp $
"""

import unittest
from zope.interface.verify import verifyClass
from zope.products.image.interfaces import IImage
from zope.products.image.image import Image, FileFactory, ImageSized
from zope.products.file.file import File, FileWriteFile, FileReadFile

zptlogo = (
    'GIF89a\x10\x00\x10\x00\xd5\x00\x00\xff\xff\xff\xff\xff\xfe\xfc\xfd\xfd'
    '\xfa\xfb\xfc\xf7\xf9\xfa\xf5\xf8\xf9\xf3\xf6\xf8\xf2\xf5\xf7\xf0\xf4\xf6'
    '\xeb\xf1\xf3\xe5\xed\xef\xde\xe8\xeb\xdc\xe6\xea\xd9\xe4\xe8\xd7\xe2\xe6'
    '\xd2\xdf\xe3\xd0\xdd\xe3\xcd\xdc\xe1\xcb\xda\xdf\xc9\xd9\xdf\xc8\xd8\xdd'
    '\xc6\xd7\xdc\xc4\xd6\xdc\xc3\xd4\xda\xc2\xd3\xd9\xc1\xd3\xd9\xc0\xd2\xd9'
    '\xbd\xd1\xd8\xbd\xd0\xd7\xbc\xcf\xd7\xbb\xcf\xd6\xbb\xce\xd5\xb9\xcd\xd4'
    '\xb6\xcc\xd4\xb6\xcb\xd3\xb5\xcb\xd2\xb4\xca\xd1\xb2\xc8\xd0\xb1\xc7\xd0'
    '\xb0\xc7\xcf\xaf\xc6\xce\xae\xc4\xce\xad\xc4\xcd\xab\xc3\xcc\xa9\xc2\xcb'
    '\xa8\xc1\xca\xa6\xc0\xc9\xa4\xbe\xc8\xa2\xbd\xc7\xa0\xbb\xc5\x9e\xba\xc4'
    '\x9b\xbf\xcc\x98\xb6\xc1\x8d\xae\xbaFgs\x00\x00\x00\x00\x00\x00\x00\x00'
    '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
    '\x00,\x00\x00\x00\x00\x10\x00\x10\x00\x00\x06z@\x80pH,\x12k\xc8$\xd2f\x04'
    '\xd4\x84\x01\x01\xe1\xf0d\x16\x9f\x80A\x01\x91\xc0ZmL\xb0\xcd\x00V\xd4'
    '\xc4a\x87z\xed\xb0-\x1a\xb3\xb8\x95\xbdf8\x1e\x11\xca,MoC$\x15\x18{'
    '\x006}m\x13\x16\x1a\x1f\x83\x85}6\x17\x1b $\x83\x00\x86\x19\x1d!%)\x8c'
    '\x866#\'+.\x8ca`\x1c`(,/1\x94B5\x19\x1e"&*-024\xacNq\xba\xbb\xb8h\xbeb'
    '\x00A\x00;'
    )

class TestImage(unittest.TestCase):

    def _makeImage(self, *args, **kw):
        return Image(*args, **kw)

    def testEmpty(self):
        file = self._makeImage()
        self.assertEqual(file.getContentType(), '')
        self.assertEqual(file.getData(), '')

    def testConstructor(self):
        file = self._makeImage('Data')
        self.assertEqual(file.getContentType(), '')
        self.assertEqual(file.getData(), 'Data')

    def testMutators(self):
        # XXX What's the point of this test? Does it test that data
        # contents override content-type? Or not? If the former, then
        # real image data should be used.

        file = self._makeImage()

        file.setContentType('text/plain')
        self.assertEqual(file.getContentType(), 'text/plain')

        file.setData('Foobar')
        self.assertEqual(file.getData(), 'Foobar')

        file.edit('Blah', 'text/html')
        self.assertEqual(file.getContentType(), 'text/html')
        self.assertEqual(file.getData(), 'Blah')

    def testInterface(self):
        self.failUnless(IImage.isImplementedByInstancesOf(Image))
        self.failUnless(verifyClass(IImage, Image))

class TestFileAdapters(unittest.TestCase):

    def _makeFile(self, *args, **kw):
        return Image(*args, **kw)

    def test_ReadFile(self):
        file = self._makeFile()
        content = "This is some file\ncontent."
        file.edit(content, 'text/plain')
        self.assertEqual(FileReadFile(file).read(), content)
        self.assertEqual(FileReadFile(file).size(), len(content))

    def test_WriteFile(self):
        file = self._makeFile()
        content = "This is some file\ncontent."
        FileWriteFile(file).write(content)
        self.assertEqual(file.getData(), content)

class DummyImage:

    def __init__(self, width, height, bytes):
        self.width = width
        self.height = height
        self.bytes = bytes

    def getSize(self):
        return self.bytes

    def getImageSize(self):
        return self.width, self.height


class TestFileFactory(unittest.TestCase):

    def test_image(self):
        factory = FileFactory(None)
        f = factory("spam.txt", "image/foo", "hello world")
        self.assert_(isinstance(f, Image), f)
        f = factory("spam.txt", "", zptlogo)
        self.assert_(isinstance(f, Image), f)

    def test_text(self):
        factory = FileFactory(None)
        f = factory("spam.txt", "", "hello world")
        self.assert_(isinstance(f, File), f)
        self.assert_(not isinstance(f, Image), f)
        f = factory("spam.txt", "", "\0\1\2\3\4")
        self.assert_(isinstance(f, File), f)
        self.assert_(not isinstance(f, Image), f)
        f = factory("spam.txt", "text/splat", zptlogo)
        self.assert_(isinstance(f, File), f)
        self.assert_(not isinstance(f, Image), f)
        f = factory("spam.txt", "application/splat", zptlogo)
        self.assert_(isinstance(f, File), f)
        self.assert_(not isinstance(f, Image), f)

class TestSized(unittest.TestCase):

    def testInterface(self):
        from zope.app.interfaces.size import ISized
        self.failUnless(ISized.isImplementedByInstancesOf(ImageSized))
        self.failUnless(verifyClass(ISized, ImageSized))

    def test_zeroSized(self):
        s = ImageSized(DummyImage(0, 0, 0))
        self.assertEqual(s.sizeForSorting(), ('byte', 0))
        self.assertEqual(s.sizeForDisplay(), u'0 KB ${width}x${height}')
        self.assertEqual(s.sizeForDisplay().mapping['width'], '0')
        self.assertEqual(s.sizeForDisplay().mapping['height'], '0')

    def test_arbitrarySize(self):
        s = ImageSized(DummyImage(34, 56, 78))
        self.assertEqual(s.sizeForSorting(), ('byte', 78))
        self.assertEqual(s.sizeForDisplay(), u'1 KB ${width}x${height}')
        self.assertEqual(s.sizeForDisplay().mapping['width'], '34')
        self.assertEqual(s.sizeForDisplay().mapping['height'], '56')

    def test_unknownSize(self):
        s = ImageSized(DummyImage(-1, -1, 23))
        self.assertEqual(s.sizeForSorting(), ('byte', 23))
        self.assertEqual(s.sizeForDisplay(), u'1 KB ${width}x${height}')
        self.assertEqual(s.sizeForDisplay().mapping['width'], '?')
        self.assertEqual(s.sizeForDisplay().mapping['height'], '?')

def test_suite():
    return unittest.TestSuite((
        unittest.makeSuite(TestImage),
        unittest.makeSuite(TestFileAdapters),
        unittest.makeSuite(TestFileFactory),
        unittest.makeSuite(TestSized)
        ))

if __name__=='__main__':
    unittest.TextTestRunner().run(test_suite())


=== Added File Zope3/src/zope/products/image/tests/test_imagedata.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""

$Id: test_imagedata.py,v 1.1.2.1 2004/02/11 16:29:24 philikon Exp $
"""

import unittest

from zope.products.image.image import Image
from zope.products.image.browser import ImageData

class Test(unittest.TestCase):

    def testData(self):
        """ """
        image = Image('Data')
        id = ImageData(image, None)
        self.assertEqual(id(), 'Data')

    def testTag(self):
        """ """

        # We need that, sinc eabsolute_url is not implemented yet.
        def absolute_url():
            return '/img'

        image = Image()
        fe = ImageData(image, None)
        fe.absolute_url = absolute_url

        self.assertEqual(fe.tag(),
            '<img src="/img" alt="" height="-1" width="-1" border="0" />')
        self.assertEqual(fe.tag(alt="Test Image"),
            '<img src="/img" alt="Test Image" '
            'height="-1" width="-1" border="0" />')
        self.assertEqual(fe.tag(height=100, width=100),
            '<img src="/img" alt="" height="100" width="100" border="0" />')
        self.assertEqual(fe.tag(border=1),
            '<img src="/img" alt="" height="-1" width="-1" border="1" />')
        self.assertEqual(fe.tag(css_class="Image"),
            '<img src="/img" alt="" '
            'height="-1" width="-1" border="0" class="Image" />')
        self.assertEqual(fe.tag(height=100, width="100",
                         border=1, css_class="Image"),
            '<img src="/img" alt="" '
            'height="100" width="100" class="Image" border="1" />')


def test_suite():
    loader = unittest.TestLoader()
    return loader.loadTestsFromTestCase(Test)

if __name__=='__main__':
    unittest.main()


=== Added File Zope3/src/zope/products/image/tests/test_imageupload.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""XXX short summary goes here.

XXX longer description goes here.

$Id: test_imageupload.py,v 1.1.2.1 2004/02/11 16:29:24 philikon Exp $
"""

import os
import unittest

from zope.app.tests import ztapi
from zope.app.browser.form.editview import EditView
from zope.app.browser.form.widget import BytesWidget, BytesAreaWidget
from zope.app.tests.placelesssetup import PlacelessSetup
from zope.publisher.browser import TestRequest
from zope.schema.interfaces import IField, IBytesLine, IBytes

import zope.products.image # for __file__
from zope.products.image.image import Image, IImage
from zope.products.image.browser import ImageUpload


class IU(ImageUpload, EditView):
    schema = IImage


class Test(PlacelessSetup, unittest.TestCase):

    def setUp(self):
        super(Test, self).setUp()

        # Configure the widget views
        ztapi.setDefaultViewName(IField, 'edit')
        ztapi.browserView(IBytesLine, 'edit', BytesWidget)
        ztapi.browserView(IBytes, 'edit', BytesAreaWidget)

        icondir = os.path.split(zope.products.image.__file__)[0]
        data = open(os.path.join(icondir, 'image_icon.gif'), 'rb').read()
        image = Image(data)
        self.__view = IU(image, TestRequest())

    def test_size(self):
        self.assertEqual(self.__view.size(), "16 x 16 pixels, 1 KB")

    def test_apply_update_no_data(self):
        view = self.__view
        ct = view.context.contentType
        data = view.context.data
        d = {}
        self.failUnless(view.apply_update(d))
        self.assertEqual(view.context.contentType, ct)
        self.assertEqual(view.context.data, data)
        d = {'contentType': 'image/gif'}
        self.failUnless(view.apply_update(d))
        self.assertEqual(view.context.contentType, ct)
        self.assertEqual(view.context.data, data)

    def test_apply_update_new_contentType(self):
        view = self.__view
        view.context.contentType = 'foo/bar'
        self.assertEqual(view.context.contentType, 'foo/bar')
        data = view.context.data
        d = {'contentType': 'xxx/yyy'}
        self.failIf(view.apply_update(d))
        self.assertEqual(view.context.contentType, 'xxx/yyy')
        self.assertEqual(view.context.data, data)

    def test_apply_update_new_data(self):
        view = self.__view
        gifdata = view.context.data
        view.context.contentType = 'foo/bar'
        view.context.data = ''
        ct = view.context.contentType
        self.assertEqual(ct, 'foo/bar')
        data = view.context.data
        self.assertEqual(data, '')
        d = {'data': gifdata}
        self.failIf(view.apply_update(d))
        self.assertEqual(view.context.contentType, 'image/gif')
        self.assertEqual(view.context.data, gifdata)

    def test_apply_update_new_data_and_new_ct(self):
        view = self.__view
        gifdata = view.context.data
        view.context.contentType = 'foo/bar'
        view.context.data = ''
        ct = view.context.contentType
        self.assertEqual(ct, 'foo/bar')
        data = view.context.data
        self.assertEqual(data, '')
        d = {'contentType': 'xxx/yyy', 'data': gifdata}
        self.failIf(view.apply_update(d))
        self.assertEqual(view.context.contentType, 'image/gif')
        self.assertEqual(view.context.data, gifdata)


def test_suite():
    return unittest.makeSuite(Test)

if __name__=='__main__':
    unittest.main(defaultTest='test_suite')




More information about the Zope3-Checkins mailing list