changeset 415:d755f798fe66

fix some tests make item name and contenttype constants unicode give cfg kwarg to RouterBackend delay tokenizer creation to test method call time, so that we're within a test request environment and app is available.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 07 Aug 2011 03:02:46 +0200
parents 598b1731069b
children ef401198ebf9
files MoinMoin/items/__init__.py MoinMoin/items/_tests/test_Item.py MoinMoin/script/maint/xml.py MoinMoin/search/_tests/test_analyzers.py MoinMoin/storage/_tests/test_backends_fs2.py MoinMoin/storage/_tests/test_backends_router.py MoinMoin/storage/backends/__init__.py MoinMoin/storage/backends/indexing.py
diffstat 8 files changed, 52 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/items/__init__.py	Sun Aug 07 00:36:13 2011 +0200
+++ b/MoinMoin/items/__init__.py	Sun Aug 07 03:02:46 2011 +0200
@@ -172,7 +172,7 @@
         if rev_no is None:
             rev_no = -1
         if contenttype is None:
-            contenttype = 'application/x-nonexistent'
+            contenttype = u'application/x-nonexistent'
 
         try:
             if item is None:
@@ -188,7 +188,7 @@
             logging.debug("Got item: %r" % name)
             try:
                 rev = item.get_revision(rev_no)
-                contenttype = 'application/octet-stream' # it exists
+                contenttype = u'application/octet-stream' # it exists
             except NoSuchRevisionError:
                 try:
                     rev = item.get_revision(-1) # fall back to current revision
@@ -430,7 +430,7 @@
                 data = self.data_form_to_internal(data)
                 data = self.data_internal_to_storage(data)
                 # we know it is text and utf-8 - XXX is there a way to get the charset of the form?
-                contenttype_guessed = 'text/plain;charset=utf-8'
+                contenttype_guessed = u'text/plain;charset=utf-8'
         # data might be None here, if we have a form with just the data_file field, no file was uploaded
         # and no data_text field. this can happen if just metadata of a non-text item is edited.
 
--- a/MoinMoin/items/_tests/test_Item.py	Sun Aug 07 00:36:13 2011 +0200
+++ b/MoinMoin/items/_tests/test_Item.py	Sun Aug 07 03:02:46 2011 +0200
@@ -24,18 +24,18 @@
 
     def testClassFinder(self):
         for contenttype, ExpectedClass in [
-                ('application/x-foobar', Binary),
-                ('text/plain', Text),
-                ('text/plain;charset=utf-8', Text),
-                ('image/tiff', Image),
-                ('image/png', TransformableBitmapImage),
+                (u'application/x-foobar', Binary),
+                (u'text/plain', Text),
+                (u'text/plain;charset=utf-8', Text),
+                (u'image/tiff', Image),
+                (u'image/png', TransformableBitmapImage),
             ]:
             item = Item.create('foo', contenttype=contenttype)
             assert isinstance(item, ExpectedClass)
 
     def testCRUD(self):
         name = u'NewItem'
-        contenttype = 'text/plain;charset=utf-8'
+        contenttype = u'text/plain;charset=utf-8'
         data = 'foobar'
         meta = {'foo': 'bar', CONTENTTYPE: contenttype}
         comment = u'saved it'
@@ -84,19 +84,19 @@
         basename = u'Foo'
         for name in ['', '/ab', '/cd/ef', '/gh', '/ij/kl', ]:
             item = Item.create(basename + name)
-            item._save({CONTENTTYPE: 'text/plain;charset=utf-8'}, "foo")
+            item._save({CONTENTTYPE: u'text/plain;charset=utf-8'}, "foo")
 
         # check index
         baseitem = Item.create(basename)
         index = baseitem.get_index()
-        assert index == [(u'Foo/ab', u'ab', 'text/plain;charset=utf-8'),
-                         (u'Foo/cd/ef', u'cd/ef', 'text/plain;charset=utf-8'),
-                         (u'Foo/gh', u'gh', 'text/plain;charset=utf-8'),
-                         (u'Foo/ij/kl', u'ij/kl', 'text/plain;charset=utf-8'),
+        assert index == [(u'Foo/ab', u'ab', u'text/plain;charset=utf-8'),
+                         (u'Foo/cd/ef', u'cd/ef', u'text/plain;charset=utf-8'),
+                         (u'Foo/gh', u'gh', u'text/plain;charset=utf-8'),
+                         (u'Foo/ij/kl', u'ij/kl', u'text/plain;charset=utf-8'),
                         ]
         flat_index = baseitem.flat_index()
-        assert flat_index == [(u'Foo/ab', u'ab', 'text/plain;charset=utf-8'),
-                              (u'Foo/gh', u'gh', 'text/plain;charset=utf-8'),
+        assert flat_index == [(u'Foo/ab', u'ab', u'text/plain;charset=utf-8'),
+                              (u'Foo/gh', u'gh', u'text/plain;charset=utf-8'),
                              ]
 
 
@@ -109,14 +109,14 @@
         creates a container and tests the content saved to the container
         """
         item_name = u'ContainerItem1'
-        item = Item.create(item_name, contenttype='application/x-tar')
+        item = Item.create(item_name, contenttype=u'application/x-tar')
         filecontent = 'abcdefghij'
         content_length = len(filecontent)
         members = set(['example1.txt', 'example2.txt'])
         item.put_member('example1.txt', filecontent, content_length, expected_members=members)
         item.put_member('example2.txt', filecontent, content_length, expected_members=members)
 
-        item = Item.create(item_name, contenttype='application/x-tar')
+        item = Item.create(item_name, contenttype=u'application/x-tar')
         tf_names = set(item.list_members())
         assert tf_names == members
         assert item.get_member('example1.txt').read() == filecontent
@@ -126,7 +126,7 @@
         creates two revisions of a container item
         """
         item_name = u'ContainerItem2'
-        item = Item.create(item_name, contenttype='application/x-tar')
+        item = Item.create(item_name, contenttype=u'application/x-tar')
         filecontent = 'abcdefghij'
         content_length = len(filecontent)
         members = set(['example1.txt'])
@@ -138,7 +138,7 @@
         item = flaskg.storage.get_item(item_name)
         assert item.next_revno == 2
 
-        item = Item.create(item_name, contenttype='application/x-tar')
+        item = Item.create(item_name, contenttype=u'application/x-tar')
         assert item.get_member('example1.txt').read() == filecontent
 
 coverage_modules = ['MoinMoin.items']
--- a/MoinMoin/script/maint/xml.py	Sun Aug 07 00:36:13 2011 +0200
+++ b/MoinMoin/script/maint/xml.py	Sun Aug 07 03:02:46 2011 +0200
@@ -66,7 +66,7 @@
             from MoinMoin.storage.backends import create_simple_mapping, router
             namespace_mapping = create_simple_mapping(backend_uri='fs19:%s' % moin19data)
             storage = router.RouterBackend(
-                    [(ns, be) for ns, be, acls in namespace_mapping], )
+                    [(ns, be) for ns, be, acls in namespace_mapping], cfg=app.cfg)
         else:
             # this deals with the normal storage
             storage = app.unprotected_storage
--- a/MoinMoin/search/_tests/test_analyzers.py	Sun Aug 07 00:36:13 2011 +0200
+++ b/MoinMoin/search/_tests/test_analyzers.py	Sun Aug 07 03:02:46 2011 +0200
@@ -15,8 +15,9 @@
 
     def testTokenizer(self):
         """ analyzers: check what obtained tokens matched given """
+        tokenizer = self.make_tokenizer()
         for value, expected_tokens in self.test_cases_query:
-            tokens = [token.text for token in self.tokenizer(value)]
+            tokens = [token.text for token in tokenizer(value)]
             assert set(expected_tokens) == set(tokens)
 
 
@@ -107,7 +108,8 @@
         )
     ]
 
-    tokenizer = AclTokenizer(app.cfg)
+    def make_tokenizer(self):
+        return AclTokenizer(app.cfg)
 
 
 class TestMimeTokenizer(TokenizerTestBase):
@@ -124,7 +126,8 @@
                   (u'text/html;value1=foo;value1=bar', [u'text', u'html', u'value1=bar'])
                  ]
 
-    tokenizer = MimeTokenizer()
+    def make_tokenizer(self):
+        return MimeTokenizer()
 
 
 class TestItemNameAnalyzer(TokenizerTestBase):
@@ -145,14 +148,15 @@
                         (u'GSOC2011', [u'gsoc', u'2011'])
                        ]
 
-    tokenizer = item_name_analyzer()
+    def make_tokenizer(self):
+        return item_name_analyzer()
 
     def testTokenizer(self):
         """ analyzers: test item name analyzer with "query" and "index" mode """
-
+        tokenizer = self.make_tokenizer()
         for value, expected_tokens in self.test_cases_query:
-            tokens = [token.text for token in self.tokenizer(value, mode="query")]
+            tokens = [token.text for token in tokenizer(value, mode="query")]
             assert set(expected_tokens) == set(tokens)
         for value, expected_tokens in self.test_cases_index:
-            tokens = [token.text for token in self.tokenizer(value, mode="index")]
+            tokens = [token.text for token in tokenizer(value, mode="index")]
             assert set(expected_tokens) == set(tokens)
--- a/MoinMoin/storage/_tests/test_backends_fs2.py	Sun Aug 07 00:36:13 2011 +0200
+++ b/MoinMoin/storage/_tests/test_backends_fs2.py	Sun Aug 07 03:02:46 2011 +0200
@@ -5,8 +5,9 @@
     MoinMoin - Test - FS2Backend
 """
 
+import py, os, tempfile, shutil
 
-import py, os, tempfile, shutil
+from flask import current_app as app
 
 from MoinMoin.storage._tests.test_backends import BackendTest
 from MoinMoin.storage.backends.fs2 import FS2Backend
@@ -16,7 +17,7 @@
 
     def create_backend(self):
         self.tempdir = tempfile.mkdtemp('', 'moin-')
-        return RouterBackend([('/', FS2Backend(self.tempdir))])
+        return RouterBackend([('/', FS2Backend(self.tempdir))], cfg=app.cfg)
 
     def kill_backend(self):
         try:
--- a/MoinMoin/storage/_tests/test_backends_router.py	Sun Aug 07 00:36:13 2011 +0200
+++ b/MoinMoin/storage/_tests/test_backends_router.py	Sun Aug 07 03:02:46 2011 +0200
@@ -29,19 +29,19 @@
         self.child = MemoryBackend()
         self.other = MemoryBackend()
         self.mapping = [('child', self.child), ('other/', self.other), (self.ns_user_profile, self.users), ('/', self.root)]
-        return RouterBackend(self.mapping)
+        return RouterBackend(self.mapping, cfg=app.cfg)
 
     def kill_backend(self):
         pass
 
 
     def test_correct_backend(self):
-        mymap = {'rootitem': self.root,         # == /rootitem
-                 'child/joe': self.child,       # Direct child of namespace.
-                 'other/jane': self.other,      # Direct child of namespace.
-                 'child/': self.child,          # Root of namespace itself (!= root)
-                 'other/': self.other,          # Root of namespace
-                 '': self.root,                 # Due to lack of any namespace info
+        mymap = {u'rootitem': self.root,         # == /rootitem
+                 u'child/joe': self.child,       # Direct child of namespace.
+                 u'other/jane': self.other,      # Direct child of namespace.
+                 u'child/': self.child,          # Root of namespace itself (!= root)
+                 u'other/': self.other,          # Root of namespace
+                 u'': self.root,                 # Due to lack of any namespace info
                 }
 
         assert not (self.root is self.child is self.other)
@@ -63,12 +63,12 @@
         assert item.name == itemname
 
     def test_traversal(self):
-        mymap = {'rootitem': self.root,         # == /rootitem
-                 'child/joe': self.child,       # Direct child of namespace.
-                 'other/jane': self.other,      # Direct child of namespace.
-                 'child/': self.child,          # Root of namespace itself (!= root)
-                 'other/': self.other,          # Root of namespace
-                 '': self.root,                 # Due to lack of any namespace info
+        mymap = {u'rootitem': self.root,         # == /rootitem
+                 u'child/joe': self.child,       # Direct child of namespace.
+                 u'other/jane': self.other,      # Direct child of namespace.
+                 u'child/': self.child,          # Root of namespace itself (!= root)
+                 u'other/': self.other,          # Root of namespace
+                 u'': self.root,                 # Due to lack of any namespace info
                 }
 
         items_in = []
--- a/MoinMoin/storage/backends/__init__.py	Sun Aug 07 00:36:13 2011 +0200
+++ b/MoinMoin/storage/backends/__init__.py	Sun Aug 07 03:02:46 2011 +0200
@@ -11,6 +11,7 @@
 """
 
 
+from flask import current_app as app
 from flask import g as flaskg
 
 from MoinMoin.storage.serialization import unserialize
@@ -122,7 +123,7 @@
     """
     Upgrade the wiki's system pages from an XML file.
     """
-    tmp_backend = router.RouterBackend([('/', memory.MemoryBackend())])
+    tmp_backend = router.RouterBackend([('/', memory.MemoryBackend())], cfg=app.cfg)
     unserialize(tmp_backend, xmlfile)
 
     # clone to real backend from config WITHOUT checking ACLs!
--- a/MoinMoin/storage/backends/indexing.py	Sun Aug 07 00:36:13 2011 +0200
+++ b/MoinMoin/storage/backends/indexing.py	Sun Aug 07 03:02:46 2011 +0200
@@ -36,7 +36,7 @@
     Backend indexing support
     """
     def __init__(self, *args, **kw):
-        cfg = kw.pop('cfg', None)
+        cfg = kw.pop('cfg')
         super(IndexingBackendMixin, self).__init__(*args, **kw)
         self._index = ItemIndex(cfg)
 
@@ -176,7 +176,7 @@
         if UUID not in self:
             self[UUID] = uuid # do we want the item's uuid in the rev's metadata?
         if CONTENTTYPE not in self:
-            self[CONTENTTYPE] = 'application/octet-stream'
+            self[CONTENTTYPE] = u'application/octet-stream'
         metas = self
         logging.debug("item %r revno %d update index:" % (name, revno))
         for k, v in metas.items():