Fix the tests
authorDaniel Roy Greenfeld <pydanny@gmail.com>
Sun, 8 Apr 2018 22:52:07 +0000 (17:52 -0500)
committerDaniel Roy Greenfeld <pydanny@gmail.com>
Sun, 8 Apr 2018 22:52:07 +0000 (17:52 -0500)
.travis.yml
tests/test_async_cached_property.py
tox.ini

index 90bdb254b05e99e4bec8701724d9539e4b1bfe47..0fe543ef72799515317f96d706804805edd26f7a 100644 (file)
@@ -14,4 +14,4 @@ python:
 install: pip install -r requirements.txt
 
 # command to run tests, e.g. python setup.py test
-script: python setup.py test
+script: pytest tests/
index ee428e0bb40d3249ed1ccff28b629449f54078f4..dd9e7bd87e1ea3921de3066c200db919ae62d5ae 100644 (file)
 # -*- coding: utf-8 -*-
+import asyncio
+import time
+import unittest
+from threading import Lock, Thread
+from freezegun import freeze_time
+import cached_property
+
+def unittest_run_loop(f):
+
+    def wrapper(*args, **kwargs):
+        coro = asyncio.coroutine(f)
+        future = coro(*args, **kwargs)
+        loop = asyncio.get_event_loop()
+        loop.run_until_complete(future)
+
+    return wrapper
+
+def CheckFactory(cached_property_decorator, threadsafe=False):
+    """
+    Create dynamically a Check class whose add_cached method is decorated by
+    the cached_property_decorator.
+    """
+
+    class Check(object):
+
+        def __init__(self):
+            self.control_total = 0
+            self.cached_total = 0
+            self.lock = Lock()
+
+        async def add_control(self):
+            self.control_total += 1
+            return self.control_total
+
+        @cached_property_decorator
+        async def add_cached(self):
+            if threadsafe:
+                time.sleep(1)
+                # Need to guard this since += isn't atomic.
+                with self.lock:
+                    self.cached_total += 1
+            else:
+                self.cached_total += 1
+
+            return self.cached_total
+
+        def run_threads(self, num_threads):
+            threads = []
+            for _ in range(num_threads):
 
+                def call_add_cached():
+                    loop = asyncio.new_event_loop()
+                    asyncio.set_event_loop(loop)
+                    loop.run_until_complete(self.add_cached)
 
-try:
-    import asyncio
-    import time
-    import unittest
-    from threading import Lock, Thread
-    from freezegun import freeze_time
-    import cached_property
+                thread = Thread(target=call_add_cached)
+                thread.start()
+                threads.append(thread)
+            for thread in threads:
+                thread.join()
 
-    def unittest_run_loop(f):
+    return Check
 
-        def wrapper(*args, **kwargs):
-            coro = asyncio.coroutine(f)
-            future = coro(*args, **kwargs)
-            loop = asyncio.get_event_loop()
-            loop.run_until_complete(future)
+class TestCachedProperty(unittest.TestCase):
+    """Tests for cached_property"""
 
-        return wrapper
+    cached_property_factory = cached_property.cached_property
 
-    def CheckFactory(cached_property_decorator, threadsafe=False):
+    async def assert_control(self, check, expected):
         """
-        Create dynamically a Check class whose add_cached method is decorated by
-        the cached_property_decorator.
+        Assert that both `add_control` and 'control_total` equal `expected`
         """
+        self.assertEqual(await check.add_control(), expected)
+        self.assertEqual(check.control_total, expected)
 
-        class Check(object):
-
-            def __init__(self):
-                self.control_total = 0
-                self.cached_total = 0
-                self.lock = Lock()
+    async def assert_cached(self, check, expected):
+        """
+        Assert that both `add_cached` and 'cached_total` equal `expected`
+        """
+        print("assert_cached", check.add_cached)
+        self.assertEqual(await check.add_cached, expected)
+        self.assertEqual(check.cached_total, expected)
 
-            async def add_control(self):
-                self.control_total += 1
-                return self.control_total
+    @unittest_run_loop
+    async def test_cached_property(self):
+        Check = CheckFactory(self.cached_property_factory)
+        check = Check()
 
-            @cached_property_decorator
-            async def add_cached(self):
-                if threadsafe:
-                    time.sleep(1)
-                    # Need to guard this since += isn't atomic.
-                    with self.lock:
-                        self.cached_total += 1
-                else:
-                    self.cached_total += 1
+        # The control shows that we can continue to add 1
+        await self.assert_control(check, 1)
+        await self.assert_control(check, 2)
 
-                return self.cached_total
+        # The cached version demonstrates how nothing is added after the first
+        await self.assert_cached(check, 1)
+        await self.assert_cached(check, 1)
 
-            def run_threads(self, num_threads):
-                threads = []
-                for _ in range(num_threads):
-
-                    def call_add_cached():
-                        loop = asyncio.new_event_loop()
-                        asyncio.set_event_loop(loop)
-                        loop.run_until_complete(self.add_cached)
-
-                    thread = Thread(target=call_add_cached)
-                    thread.start()
-                    threads.append(thread)
-                for thread in threads:
-                    thread.join()
-
-        return Check
-
-    class TestCachedProperty(unittest.TestCase):
-        """Tests for cached_property"""
-
-        cached_property_factory = cached_property.cached_property
-
-        async def assert_control(self, check, expected):
-            """
-            Assert that both `add_control` and 'control_total` equal `expected`
-            """
-            self.assertEqual(await check.add_control(), expected)
-            self.assertEqual(check.control_total, expected)
-
-        async def assert_cached(self, check, expected):
-            """
-            Assert that both `add_cached` and 'cached_total` equal `expected`
-            """
-            print("assert_cached", check.add_cached)
-            self.assertEqual(await check.add_cached, expected)
-            self.assertEqual(check.cached_total, expected)
-
-        @unittest_run_loop
-        async def test_cached_property(self):
-            Check = CheckFactory(self.cached_property_factory)
-            check = Check()
-
-            # The control shows that we can continue to add 1
-            await self.assert_control(check, 1)
-            await self.assert_control(check, 2)
-
-            # The cached version demonstrates how nothing is added after the first
+        # The cache does not expire
+        with freeze_time("9999-01-01"):
             await self.assert_cached(check, 1)
-            await self.assert_cached(check, 1)
-
-            # The cache does not expire
-            with freeze_time("9999-01-01"):
-                await self.assert_cached(check, 1)
-
-            # Typically descriptors return themselves if accessed though the class
-            # rather than through an instance.
-            self.assertTrue(isinstance(Check.add_cached, self.cached_property_factory))
 
-        @unittest_run_loop
-        async def test_reset_cached_property(self):
-            Check = CheckFactory(self.cached_property_factory)
-            check = Check()
-
-            # Run standard cache assertion
-            await self.assert_cached(check, 1)
-            await self.assert_cached(check, 1)
+        # Typically descriptors return themselves if accessed though the class
+        # rather than through an instance.
+        self.assertTrue(isinstance(Check.add_cached, self.cached_property_factory))
 
-            # Clear the cache
-            del check.add_cached
+    @unittest_run_loop
+    async def test_reset_cached_property(self):
+        Check = CheckFactory(self.cached_property_factory)
+        check = Check()
 
-            # Value is cached again after the next access
-            await self.assert_cached(check, 2)
-            await self.assert_cached(check, 2)
+        # Run standard cache assertion
+        await self.assert_cached(check, 1)
+        await self.assert_cached(check, 1)
 
-        @unittest_run_loop
-        async def test_none_cached_property(self):
+        # Clear the cache
+        del check.add_cached
 
-            class Check(object):
+        # Value is cached again after the next access
+        await self.assert_cached(check, 2)
+        await self.assert_cached(check, 2)
 
-                def __init__(self):
-                    self.cached_total = None
+    @unittest_run_loop
+    async def test_none_cached_property(self):
 
-                @self.cached_property_factory
-                async def add_cached(self):
-                    return self.cached_total
+        class Check(object):
 
-            await self.assert_cached(Check(), None)
+            def __init__(self):
+                self.cached_total = None
 
+            @self.cached_property_factory
+            async def add_cached(self):
+                return self.cached_total
 
-except ImportError:
-    pass  # Running older version of Python that doesn't support asyncio
+        await self.assert_cached(Check(), None)  
\ No newline at end of file
diff --git a/tox.ini b/tox.ini
index be4b74cc4e6746280ad981212e76e9ad88791859..4f94c9bdc001952ca073b2e2258ac775ff2b9af1 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -4,7 +4,7 @@ envlist = py27, py33, py34, py35, py36
 [testenv]
 setenv =
     PYTHONPATH = {toxinidir}:{toxinidir}/cached-property
-commands = python setup.py test
+commands = pytest tests/
 deps =
     pytest
     freezegun