changeset 1209:d8bb9c36aae1 modernize tip

Add unit test for accounts task.
author Brian Neal <bgneal@gmail.com>
date Thu, 30 Jan 2025 18:46:03 -0600
parents 5c8a38122e24
children
files accounts/fixtures/accounts.json accounts/tests/test_tasks.py
diffstat 2 files changed, 60 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/accounts/fixtures/accounts.json	Tue Jan 28 19:02:18 2025 -0600
+++ b/accounts/fixtures/accounts.json	Thu Jan 30 18:46:03 2025 -0600
@@ -1,30 +1,48 @@
 [
     {
-        "pk": 1, 
-        "model": "accounts.illegalusername", 
+        "pk": 1,
+        "model": "auth.user",
+        "fields": {
+            "username": "Gremmie",
+            "first_name": "",
+            "last_name": "",
+            "is_active": true,
+            "is_superuser": false,
+            "is_staff": false,
+            "last_login": "1969-12-31 18:00:00",
+            "groups": [],
+            "user_permissions": [],
+            "password": "!",
+            "email": "",
+            "date_joined": "2000-11-10 00:00:00"
+        }
+    },
+    {
+        "pk": 1,
+        "model": "accounts.illegalusername",
         "fields": {
             "username": "root"
         }
-    }, 
+    },
     {
-        "pk": 2, 
-        "model": "accounts.illegalusername", 
+        "pk": 2,
+        "model": "accounts.illegalusername",
         "fields": {
             "username": "sg101"
         }
-    }, 
+    },
     {
-        "pk": 3, 
-        "model": "accounts.illegalusername", 
+        "pk": 3,
+        "model": "accounts.illegalusername",
         "fields": {
             "username": "surfguitar101"
         }
-    }, 
+    },
     {
-        "pk": 4, 
-        "model": "accounts.illegalusername", 
+        "pk": 4,
+        "model": "accounts.illegalusername",
         "fields": {
             "username": "webmaster"
         }
     }
-]
\ No newline at end of file
+]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/accounts/tests/test_tasks.py	Thu Jan 30 18:46:03 2025 -0600
@@ -0,0 +1,30 @@
+"""
+Tests for the account application tasks.
+
+"""
+from django.test import TestCase
+from mock import call, patch, Mock
+
+import accounts.stats
+from accounts.tasks import user_stats_task
+
+
+class UserStatsTaskTestCase(TestCase):
+    fixtures = ['accounts.json']
+
+    @patch('accounts.stats.get_redis_connection')
+    def test_user_stats_task(self, connection_mock):
+        redis_mock = Mock()
+        redis_mock.incr.return_value = 1
+        connection_mock.return_value = redis_mock
+
+        task = user_stats_task.s(1).apply()
+
+        self.assertEquals(redis_mock.mock_calls, [
+            call.incr('accounts:user_count'),
+            call.set('accounts:user_count', 1),
+            call.pipeline(),
+            call.pipeline().lpush('accounts:new_users', u'Gremmie'),
+            call.pipeline().ltrim('accounts:new_users', 0, 9),
+            call.pipeline().execute(),
+        ])