Title: [commits] (grant) [16044] [APP] Add unit tests for ocap.wxui.image:get_raw_image()
Revision
16044
Author
grant
Date
2007-12-04 17:11:45 -0800 (Tue, 04 Dec 2007)

Log Message

[APP] Add unit tests for ocap.wxui.image:get_raw_image()
- Uses the mocker library (added as the 'tests_require' keyword to setup()).

Modified Paths

Added Paths

Diff

Modified: branches/rearchitecture/Chandler-Platform/setup.py (16043 => 16044)

--- branches/rearchitecture/Chandler-Platform/setup.py	2007-12-04 21:32:21 UTC (rev 16043)
+++ branches/rearchitecture/Chandler-Platform/setup.py	2007-12-05 01:11:45 UTC (rev 16044)
@@ -32,7 +32,8 @@
         'Importing>=1.9.2'
     ],
     dependency_links=['svn://svn.eby-sarna.com/svnroot/Trellis#egg=Trellis-dev'],
-    #test_suite = 'xxx',
+    test_suite = 'test_platform',
+    tests_require = 'mocker>=0.9',
     packages = find_packages(),
     namespace_packages = ['ocap'],
 ),

Added: branches/rearchitecture/Chandler-Platform/test_image.py (16043 => 16044)

--- branches/rearchitecture/Chandler-Platform/test_image.py	2007-12-04 21:32:21 UTC (rev 16043)
+++ branches/rearchitecture/Chandler-Platform/test_image.py	2007-12-05 01:11:45 UTC (rev 16044)
@@ -0,0 +1,132 @@
+from __future__ import with_statement
+import mocker
+from ocap.wxui.image import *
+
+__all__ = ('GetRawImageTestCase',)
+
+# For information on mocker, and MockerTestCase, see:
+#
+# http://labix.org/mocker
+#
+class GetRawImageTestCase(mocker.MockerTestCase):
+    def setUp(self):
+        super(GetRawImageTestCase, self).setUp()
+        # mock the return value of pkg_resources.get_distribution()
+        self.get_distro = self.mocker.replace("pkg_resources.get_distribution")
+        self.mock_distro = self.mocker.mock()
+        
+        # Mock running the code on the new 'creaky' platform
+        self.expect(self.mocker.replace("sys").platform).result("creaky")
+
+        self.expect(self.get_distro("Chandler_App")).result(self.mock_distro)
+
+        self.image_from_stream = self.mocker.replace("wx.ImageFromStream")
+
+
+    def testNonexistent(self):
+        
+        # Make sure we ask for metadata for the platform variant of the
+        # image we want, and then the non-platform version. For the
+        # nonexistent test, mock raising (.throw()) an IOError,
+        # for both of these calls, which is what we expect when
+        # resources aren't present on disk.
+        self.mocker.before(
+            self.expect(
+                self.mock_distro.get_metadata(
+                    "resources/images/nonexistent-creaky.xxx")
+                ).throw(IOError),
+            self.expect(
+                self.mock_distro.get_metadata(
+                    "resources/images/nonexistent.xxx")
+                ).throw(IOError)
+        )
+            
+        self.mocker.replay()
+
+        # After going into replay mode, we make the calls we
+        # want to test.
+        # Make sure get_raw_image() returns None in this case.
+        self.assertIs(get_raw_image("nonexistent.xxx"), None,
+              "get_raw_image() should return None if the image can't be found")
+    
+    class MATCH_STREAM(mocker.SpecialArgument):
+        """
+        This is used as an argument to the (mocked) wx.ImageFromStream; it
+        is used to make sure that the correct data is passed in. The
+        'object' instance variable (passed into the constructor) should be
+        the bytes (str) that's expected.
+        """
+        def matches(self, other):
+            otherBytes = other.read()
+            return (otherBytes == self.object)
+
+    def testWithPlatform(self):
+        # Pretend we have the bytes "flurble" in the platform image path
+        self.expect(
+            self.mock_distro.get_metadata(
+                "resources/images/with-platform-creaky.xxx")
+            ).result("flurble")
+        
+        # Make a mock image to return from wx.ImageFromStream (mocked)
+        image = self.mocker.mock("wx.Image")
+        
+        # Make sure our mocked.ImageFromStream gets passed a stream
+        # containing "flurble"
+        self.expect(
+            self.image_from_stream(self.MATCH_STREAM("flurble"))
+        ).result(image)
+
+        self.mocker.replay()
+
+        self.assertIs(get_raw_image("with-platform.xxx", False), image)
+
+    def testCache(self):
+    
+        self.expect(
+            self.mock_distro.get_metadata(
+                "resources/images/cache-test-creaky.xxx"
+            )
+        ).throw(IOError)
+
+        self.expect(
+            self.mock_distro.get_metadata(
+                "resources/images/cache-test.xxx"
+            )
+        ).result("testdata")
+            
+        image = self.mocker.mock("wx.Image")
+        self.expect(
+            self.image_from_stream(self.MATCH_STREAM("testdata"))
+        ).result(image)
+
+        self.mocker.replay()
+
+        self.assertIs(get_raw_image("cache-test.xxx", False), image)
+        
+        # Make a second call to get_raw_image(), making sure that no calls
+        # to wx.ImageFromStream are made
+        self.assertIs(get_raw_image("cache-test.xxx", False), image)
+
+    def testCopy(self):
+        self.expect(
+            self.mock_distro.get_metadata(
+                "resources/images/cache-copy-test-creaky.xxx"
+            )
+        ).result("testdata")
+            
+        image = self.mocker.mock()
+        copyImage = self.mocker.mock()
+        self.expect(
+            self.image_from_stream(self.MATCH_STREAM("testdata"))
+        ).result(image)
+        self.expect(image.Copy()).result(copyImage)
+
+        self.mocker.replay()
+
+        # Make sure the Copy()'ed image is returned from get_raw_image()
+        self.assertIs(get_raw_image("cache-copy-test.xxx"), copyImage)
+        
+        # Make a second call to get_raw_image(), making sure that no calls
+        # to wx.ImageFromStream are made, and that the un-Copy()'ed image
+        # is returned.
+        self.assertIs(get_raw_image("cache-copy-test.xxx", False), image)
Property changes on: branches/rearchitecture/Chandler-Platform/test_image.py
___________________________________________________________________
Name: svn:eol-style
   + native

Added: branches/rearchitecture/Chandler-Platform/test_platform.py (16043 => 16044)

--- branches/rearchitecture/Chandler-Platform/test_platform.py	2007-12-04 21:32:21 UTC (rev 16043)
+++ branches/rearchitecture/Chandler-Platform/test_platform.py	2007-12-05 01:11:45 UTC (rev 16044)
@@ -0,0 +1 @@
+from test_image import *
Property changes on: branches/rearchitecture/Chandler-Platform/test_platform.py
___________________________________________________________________
Name: svn:eol-style
   + native




_______________________________________________
Commits mailing list
[email protected]
http://lists.osafoundation.org/mailman/listinfo/commits

Reply via email to